1
1
use crate :: book:: { Book , BookItem } ;
2
- use crate :: config:: { Config , HtmlConfig , Playpen } ;
2
+ use crate :: config:: { Config , HtmlConfig , Playpen , RustEdition } ;
3
3
use crate :: errors:: * ;
4
4
use crate :: renderer:: html_handlebars:: helpers;
5
5
use crate :: renderer:: { RenderContext , Renderer } ;
@@ -85,7 +85,7 @@ impl HtmlHandlebars {
85
85
debug ! ( "Render template" ) ;
86
86
let rendered = ctx. handlebars . render ( "index" , & ctx. data ) ?;
87
87
88
- let rendered = self . post_process ( rendered, & ctx. html_config . playpen ) ;
88
+ let rendered = self . post_process ( rendered, & ctx. html_config . playpen , ctx . edition ) ;
89
89
90
90
// Write to file
91
91
debug ! ( "Creating {}" , filepath. display( ) ) ;
@@ -96,7 +96,8 @@ impl HtmlHandlebars {
96
96
ctx. data . insert ( "path_to_root" . to_owned ( ) , json ! ( "" ) ) ;
97
97
ctx. data . insert ( "is_index" . to_owned ( ) , json ! ( "true" ) ) ;
98
98
let rendered_index = ctx. handlebars . render ( "index" , & ctx. data ) ?;
99
- let rendered_index = self . post_process ( rendered_index, & ctx. html_config . playpen ) ;
99
+ let rendered_index =
100
+ self . post_process ( rendered_index, & ctx. html_config . playpen , ctx. edition ) ;
100
101
debug ! ( "Creating index.html from {}" , path) ;
101
102
utils:: fs:: write_file ( & ctx. destination , "index.html" , rendered_index. as_bytes ( ) ) ?;
102
103
}
@@ -106,10 +107,15 @@ impl HtmlHandlebars {
106
107
}
107
108
108
109
#[ cfg_attr( feature = "cargo-clippy" , allow( clippy:: let_and_return) ) ]
109
- fn post_process ( & self , rendered : String , playpen_config : & Playpen ) -> String {
110
+ fn post_process (
111
+ & self ,
112
+ rendered : String ,
113
+ playpen_config : & Playpen ,
114
+ edition : Option < RustEdition > ,
115
+ ) -> String {
110
116
let rendered = build_header_links ( & rendered) ;
111
117
let rendered = fix_code_blocks ( & rendered) ;
112
- let rendered = add_playpen_pre ( & rendered, playpen_config) ;
118
+ let rendered = add_playpen_pre ( & rendered, playpen_config, edition ) ;
113
119
114
120
rendered
115
121
}
@@ -343,6 +349,7 @@ impl Renderer for HtmlHandlebars {
343
349
data : data. clone ( ) ,
344
350
is_index,
345
351
html_config : html_config. clone ( ) ,
352
+ edition : ctx. config . rust . edition ,
346
353
} ;
347
354
self . render_item ( item, ctx, & mut print_content) ?;
348
355
is_index = false ;
@@ -358,7 +365,7 @@ impl Renderer for HtmlHandlebars {
358
365
debug ! ( "Render template" ) ;
359
366
let rendered = handlebars. render ( "index" , & data) ?;
360
367
361
- let rendered = self . post_process ( rendered, & html_config. playpen ) ;
368
+ let rendered = self . post_process ( rendered, & html_config. playpen , ctx . config . rust . edition ) ;
362
369
363
370
utils:: fs:: write_file ( & destination, "print.html" , rendered. as_bytes ( ) ) ?;
364
371
debug ! ( "Creating print.html ✓" ) ;
@@ -605,7 +612,7 @@ fn fix_code_blocks(html: &str) -> String {
605
612
. into_owned ( )
606
613
}
607
614
608
- fn add_playpen_pre ( html : & str , playpen_config : & Playpen ) -> String {
615
+ fn add_playpen_pre ( html : & str , playpen_config : & Playpen , edition : Option < RustEdition > ) -> String {
609
616
let regex = Regex :: new ( r##"((?s)<code[^>]?class="([^"]+)".*?>(.*?)</code>)"## ) . unwrap ( ) ;
610
617
regex
611
618
. replace_all ( html, |caps : & Captures < ' _ > | {
@@ -617,10 +624,24 @@ fn add_playpen_pre(html: &str, playpen_config: &Playpen) -> String {
617
624
if ( !classes. contains ( "ignore" ) && !classes. contains ( "noplaypen" ) )
618
625
|| classes. contains ( "mdbook-runnable" )
619
626
{
627
+ let contains_e2015 = classes. contains ( "edition2015" ) ;
628
+ let contains_e2018 = classes. contains ( "edition2018" ) ;
629
+ let edition_class = if contains_e2015 || contains_e2018 {
630
+ // the user forced edition, we should not overwrite it
631
+ ""
632
+ } else {
633
+ match edition {
634
+ Some ( RustEdition :: E2015 ) => " edition2015" ,
635
+ Some ( RustEdition :: E2018 ) => " edition2018" ,
636
+ None => "" ,
637
+ }
638
+ } ;
639
+
620
640
// wrap the contents in an external pre block
621
641
format ! (
622
- "<pre class=\" playpen\" ><code class=\" {}\" >{}</code></pre>" ,
642
+ "<pre class=\" playpen\" ><code class=\" {}{} \" >{}</code></pre>" ,
623
643
classes,
644
+ edition_class,
624
645
{
625
646
let content: Cow <' _, str > = if playpen_config. editable
626
647
&& classes. contains( "editable" )
@@ -711,6 +732,7 @@ struct RenderItemContext<'a> {
711
732
data : serde_json:: Map < String , serde_json:: Value > ,
712
733
is_index : bool ,
713
734
html_config : HtmlConfig ,
735
+ edition : Option < RustEdition > ,
714
736
}
715
737
716
738
#[ cfg( test) ]
@@ -777,6 +799,55 @@ mod tests {
777
799
editable : true ,
778
800
..Playpen :: default ( )
779
801
} ,
802
+ None ,
803
+ ) ;
804
+ assert_eq ! ( & * got, * should_be) ;
805
+ }
806
+ }
807
+ #[ test]
808
+ fn add_playpen_edition2015 ( ) {
809
+ let inputs = [
810
+ ( "<code class=\" language-rust\" >x()</code>" ,
811
+ "<pre class=\" playpen\" ><code class=\" language-rust edition2015\" >\n <span class=\" boring\" >#![allow(unused)]\n </span><span class=\" boring\" >fn main() {\n </span>x()\n <span class=\" boring\" >}\n </span></code></pre>" ) ,
812
+ ( "<code class=\" language-rust\" >fn main() {}</code>" ,
813
+ "<pre class=\" playpen\" ><code class=\" language-rust edition2015\" >fn main() {}\n </code></pre>" ) ,
814
+ ( "<code class=\" language-rust edition2015\" >fn main() {}</code>" ,
815
+ "<pre class=\" playpen\" ><code class=\" language-rust edition2015\" >fn main() {}\n </code></pre>" ) ,
816
+ ( "<code class=\" language-rust edition2018\" >fn main() {}</code>" ,
817
+ "<pre class=\" playpen\" ><code class=\" language-rust edition2018\" >fn main() {}\n </code></pre>" ) ,
818
+ ] ;
819
+ for ( src, should_be) in & inputs {
820
+ let got = add_playpen_pre (
821
+ src,
822
+ & Playpen {
823
+ editable : true ,
824
+ ..Playpen :: default ( )
825
+ } ,
826
+ Some ( RustEdition :: E2015 ) ,
827
+ ) ;
828
+ assert_eq ! ( & * got, * should_be) ;
829
+ }
830
+ }
831
+ #[ test]
832
+ fn add_playpen_edition2018 ( ) {
833
+ let inputs = [
834
+ ( "<code class=\" language-rust\" >x()</code>" ,
835
+ "<pre class=\" playpen\" ><code class=\" language-rust edition2018\" >\n <span class=\" boring\" >#![allow(unused)]\n </span><span class=\" boring\" >fn main() {\n </span>x()\n <span class=\" boring\" >}\n </span></code></pre>" ) ,
836
+ ( "<code class=\" language-rust\" >fn main() {}</code>" ,
837
+ "<pre class=\" playpen\" ><code class=\" language-rust edition2018\" >fn main() {}\n </code></pre>" ) ,
838
+ ( "<code class=\" language-rust edition2015\" >fn main() {}</code>" ,
839
+ "<pre class=\" playpen\" ><code class=\" language-rust edition2015\" >fn main() {}\n </code></pre>" ) ,
840
+ ( "<code class=\" language-rust edition2018\" >fn main() {}</code>" ,
841
+ "<pre class=\" playpen\" ><code class=\" language-rust edition2018\" >fn main() {}\n </code></pre>" ) ,
842
+ ] ;
843
+ for ( src, should_be) in & inputs {
844
+ let got = add_playpen_pre (
845
+ src,
846
+ & Playpen {
847
+ editable : true ,
848
+ ..Playpen :: default ( )
849
+ } ,
850
+ Some ( RustEdition :: E2018 ) ,
780
851
) ;
781
852
assert_eq ! ( & * got, * should_be) ;
782
853
}
0 commit comments