@@ -59,10 +59,11 @@ impl HtmlHandlebars {
59
59
60
60
let content = utils:: render_markdown ( & ch. content , ctx. html_config . smart_punctuation ( ) ) ;
61
61
62
- let fixed_content = utils:: render_markdown_with_path (
62
+ let printed_item = utils:: render_markdown_with_path_and_redirects (
63
63
& ch. content ,
64
64
ctx. html_config . smart_punctuation ( ) ,
65
65
Some ( path) ,
66
+ & ctx. html_config . redirect ,
66
67
) ;
67
68
if !ctx. is_index && ctx. html_config . print . page_break {
68
69
// Add page break between chapters
@@ -71,7 +72,25 @@ impl HtmlHandlebars {
71
72
print_content
72
73
. push_str ( r#"<div style="break-before: page; page-break-before: always;"></div>"# ) ;
73
74
}
74
- print_content. push_str ( & fixed_content) ;
75
+ let print_page_id = {
76
+ let mut base = path. display ( ) . to_string ( ) ;
77
+ if base. ends_with ( ".md" ) {
78
+ base. truncate ( base. len ( ) - 3 ) ;
79
+ }
80
+ & base
81
+ . replace ( "/" , "-" )
82
+ . replace ( "\\ " , "-" )
83
+ . to_ascii_lowercase ( )
84
+ } ;
85
+
86
+ // We have to build header links in advance so that we can know the ranges
87
+ // for the headers in one page.
88
+ // Insert a dummy div to make sure that we can locate the specific page.
89
+ print_content. push_str ( & ( format ! ( r#"<div id="{print_page_id}"></div>"# ) ) ) ;
90
+ print_content. push_str ( & build_header_links (
91
+ & build_print_element_id ( & printed_item, & print_page_id) ,
92
+ Some ( print_page_id) ,
93
+ ) ) ;
75
94
76
95
// Update the context with data for this file
77
96
let ctx_path = path
@@ -216,7 +235,23 @@ impl HtmlHandlebars {
216
235
code_config : & Code ,
217
236
edition : Option < RustEdition > ,
218
237
) -> String {
219
- let rendered = build_header_links ( & rendered) ;
238
+ let rendered = build_header_links ( & rendered, None ) ;
239
+ let rendered = self . post_process_common ( rendered, & playground_config, code_config, edition) ;
240
+
241
+ rendered
242
+ }
243
+
244
+ /// Applies some post-processing to the HTML to apply some adjustments.
245
+ ///
246
+ /// This common function is used for both normal chapters (via
247
+ /// `post_process`) and the combined print page.
248
+ fn post_process_common (
249
+ & self ,
250
+ rendered : String ,
251
+ playground_config : & Playground ,
252
+ code_config : & Code ,
253
+ edition : Option < RustEdition > ,
254
+ ) -> String {
220
255
let rendered = fix_code_blocks ( & rendered) ;
221
256
let rendered = add_playground_pre ( & rendered, playground_config, edition) ;
222
257
let rendered = hide_lines ( & rendered, code_config) ;
@@ -465,7 +500,7 @@ impl Renderer for HtmlHandlebars {
465
500
debug ! ( "Render template" ) ;
466
501
let rendered = handlebars. render ( "index" , & data) ?;
467
502
468
- let rendered = self . post_process (
503
+ let rendered = self . post_process_common (
469
504
rendered,
470
505
& html_config. playground ,
471
506
& html_config. code ,
@@ -661,9 +696,34 @@ fn make_data(
661
696
Ok ( data)
662
697
}
663
698
699
+ /// Go through the rendered print page HTML,
700
+ /// add path id prefix to all the elements id as well as footnote links.
701
+ fn build_print_element_id ( html : & str , print_page_id : & str ) -> String {
702
+ static ALL_ID : Lazy < Regex > = Lazy :: new ( || Regex :: new ( r#"(<[^>]*?id=")([^"]+?)""# ) . unwrap ( ) ) ;
703
+ static FOOTNOTE_ID : Lazy < Regex > = Lazy :: new ( || {
704
+ Regex :: new (
705
+ r##"(<sup [^>]*?class="footnote-reference"[^>]*?>[^<]*?<a [^>]*?href="#)([^"]+?)""## ,
706
+ )
707
+ . unwrap ( )
708
+ } ) ;
709
+
710
+ let temp_html = ALL_ID . replace_all ( html, |caps : & Captures < ' _ > | {
711
+ format ! ( "{}{}-{}\" " , & caps[ 1 ] , print_page_id, & caps[ 2 ] )
712
+ } ) ;
713
+
714
+ FOOTNOTE_ID
715
+ . replace_all ( & temp_html, |caps : & Captures < ' _ > | {
716
+ format ! ( "{}{}-{}\" " , & caps[ 1 ] , print_page_id, & caps[ 2 ] )
717
+ } )
718
+ . into_owned ( )
719
+ }
720
+
664
721
/// Goes through the rendered HTML, making sure all header tags have
665
722
/// an anchor respectively so people can link to sections directly.
666
- fn build_header_links ( html : & str ) -> String {
723
+ ///
724
+ /// `print_page_id` should be set to the print page ID prefix when adjusting the
725
+ /// print page.
726
+ fn build_header_links ( html : & str , print_page_id : Option < & str > ) -> String {
667
727
static BUILD_HEADER_LINKS : Lazy < Regex > = Lazy :: new ( || {
668
728
Regex :: new ( r#"<h(\d)(?: id="([^"]+)")?(?: class="([^"]+)")?>(.*?)</h\d>"# ) . unwrap ( )
669
729
} ) ;
@@ -692,21 +752,34 @@ fn build_header_links(html: &str) -> String {
692
752
caps. get ( 2 ) . map ( |x| x. as_str ( ) . to_string ( ) ) ,
693
753
caps. get ( 3 ) . map ( |x| x. as_str ( ) . to_string ( ) ) ,
694
754
& mut id_counter,
755
+ print_page_id,
695
756
)
696
757
} )
697
758
. into_owned ( )
698
759
}
699
760
700
761
/// Insert a single link into a header, making sure each link gets its own
701
762
/// unique ID by appending an auto-incremented number (if necessary).
763
+ ///
764
+ /// For `print.html`, we will add a path id prefix.
702
765
fn insert_link_into_header (
703
766
level : usize ,
704
767
content : & str ,
705
768
id : Option < String > ,
706
769
classes : Option < String > ,
707
770
id_counter : & mut HashMap < String , usize > ,
771
+ print_page_id : Option < & str > ,
708
772
) -> String {
709
- let id = id. unwrap_or_else ( || utils:: unique_id_from_content ( content, id_counter) ) ;
773
+ let id = if let Some ( print_page_id) = print_page_id {
774
+ let content_id = {
775
+ #[ allow( deprecated) ]
776
+ utils:: id_from_content ( content)
777
+ } ;
778
+ let with_prefix = format ! ( "{} {}" , print_page_id, content_id) ;
779
+ id. unwrap_or_else ( || utils:: unique_id_from_content ( & with_prefix, id_counter) )
780
+ } else {
781
+ id. unwrap_or_else ( || utils:: unique_id_from_content ( content, id_counter) )
782
+ } ;
710
783
let classes = classes
711
784
. map ( |s| format ! ( " class=\" {s}\" " ) )
712
785
. unwrap_or_default ( ) ;
@@ -983,7 +1056,7 @@ mod tests {
983
1056
] ;
984
1057
985
1058
for ( src, should_be) in inputs {
986
- let got = build_header_links ( src) ;
1059
+ let got = build_header_links ( src, None ) ;
987
1060
assert_eq ! ( got, should_be) ;
988
1061
}
989
1062
}
0 commit comments