@@ -86,9 +86,6 @@ impl HtmlHandlebars {
86
86
let filepath = Path :: new ( & ch. path ) . with_extension ( "html" ) ;
87
87
let rendered = self . post_process (
88
88
rendered,
89
- & normalize_path ( filepath. to_str ( ) . ok_or_else ( || {
90
- Error :: from ( format ! ( "Bad file name: {}" , filepath. display( ) ) )
91
- } ) ?) ,
92
89
& ctx. html_config . playpen ,
93
90
) ;
94
91
@@ -115,14 +112,6 @@ impl HtmlHandlebars {
115
112
File :: open ( destination. join ( & ch. path . with_extension ( "html" ) ) ) ?
116
113
. read_to_string ( & mut content) ?;
117
114
118
- // This could cause a problem when someone displays
119
- // code containing <base href=...>
120
- // on the front page, however this case should be very very rare...
121
- content = content. lines ( )
122
- . filter ( |line| !line. contains ( "<base href=" ) )
123
- . collect :: < Vec < & str > > ( )
124
- . join ( "\n " ) ;
125
-
126
115
self . write_file ( destination, "index.html" , content. as_bytes ( ) ) ?;
127
116
128
117
debug ! (
@@ -136,11 +125,9 @@ impl HtmlHandlebars {
136
125
#[ cfg_attr( feature = "cargo-clippy" , allow( let_and_return) ) ]
137
126
fn post_process ( & self ,
138
127
rendered : String ,
139
- filepath : & str ,
140
128
playpen_config : & Playpen )
141
129
-> String {
142
- let rendered = build_header_links ( & rendered, filepath) ;
143
- let rendered = fix_anchor_links ( & rendered, filepath) ;
130
+ let rendered = build_header_links ( & rendered) ;
144
131
let rendered = fix_code_blocks ( & rendered) ;
145
132
let rendered = add_playpen_pre ( & rendered, playpen_config) ;
146
133
@@ -330,7 +317,6 @@ impl Renderer for HtmlHandlebars {
330
317
let rendered = handlebars. render ( "index" , & data) ?;
331
318
332
319
let rendered = self . post_process ( rendered,
333
- "print.html" ,
334
320
& html_config. playpen ) ;
335
321
336
322
self . write_file ( & destination, "print.html" , & rendered. into_bytes ( ) ) ?;
@@ -449,15 +435,15 @@ fn make_data(root: &Path, book: &Book, config: &Config, html_config: &HtmlConfig
449
435
450
436
/// Goes through the rendered HTML, making sure all header tags are wrapped in
451
437
/// an anchor so people can link to sections directly.
452
- fn build_header_links ( html : & str , filepath : & str ) -> String {
438
+ fn build_header_links ( html : & str ) -> String {
453
439
let regex = Regex :: new ( r"<h(\d)>(.*?)</h\d>" ) . unwrap ( ) ;
454
440
let mut id_counter = HashMap :: new ( ) ;
455
441
456
442
regex. replace_all ( html, |caps : & Captures | {
457
443
let level = caps[ 1 ] . parse ( )
458
444
. expect ( "Regex should ensure we only ever get numbers here" ) ;
459
445
460
- wrap_header_with_link ( level, & caps[ 2 ] , & mut id_counter, filepath )
446
+ wrap_header_with_link ( level, & caps[ 2 ] , & mut id_counter)
461
447
} )
462
448
. into_owned ( )
463
449
}
@@ -466,8 +452,7 @@ fn build_header_links(html: &str, filepath: &str) -> String {
466
452
/// unique ID by appending an auto-incremented number (if necessary).
467
453
fn wrap_header_with_link ( level : usize ,
468
454
content : & str ,
469
- id_counter : & mut HashMap < String , usize > ,
470
- filepath : & str )
455
+ id_counter : & mut HashMap < String , usize > )
471
456
-> String {
472
457
let raw_id = id_from_content ( content) ;
473
458
@@ -481,11 +466,10 @@ fn wrap_header_with_link(level: usize,
481
466
* id_count += 1 ;
482
467
483
468
format ! (
484
- r##"<a class="header" href="{filepath} #{id}" id="{id}"><h{level}>{text}</h{level}></a>"## ,
469
+ r##"<a class="header" href="#{id}" id="{id}"><h{level}>{text}</h{level}></a>"## ,
485
470
level = level,
486
471
id = id,
487
- text = content,
488
- filepath = filepath
472
+ text = content
489
473
)
490
474
}
491
475
@@ -516,25 +500,6 @@ fn id_from_content(content: &str) -> String {
516
500
normalize_id ( trimmed)
517
501
}
518
502
519
- // anchors to the same page (href="#anchor") do not work because of
520
- // <base href="../"> pointing to the root folder. This function *fixes*
521
- // that in a very inelegant way
522
- fn fix_anchor_links ( html : & str , filepath : & str ) -> String {
523
- let regex = Regex :: new ( r##"<a([^>]+)href="#([^"]+)"([^>]*)>"## ) . unwrap ( ) ;
524
- regex. replace_all ( html, |caps : & Captures | {
525
- let before = & caps[ 1 ] ;
526
- let anchor = & caps[ 2 ] ;
527
- let after = & caps[ 3 ] ;
528
-
529
- format ! ( "<a{before}href=\" {filepath}#{anchor}\" {after}>" ,
530
- before = before,
531
- filepath = filepath,
532
- anchor = anchor,
533
- after = after)
534
- } )
535
- . into_owned ( )
536
- }
537
-
538
503
539
504
// The rust book uses annotations for rustdoc to test code snippets,
540
505
// like the following:
@@ -624,12 +589,6 @@ struct RenderItemContext<'a> {
624
589
html_config : HtmlConfig ,
625
590
}
626
591
627
- pub fn normalize_path ( path : & str ) -> String {
628
- use std:: path:: is_separator;
629
- path. chars ( )
630
- . map ( |ch| if is_separator ( ch) { '/' } else { ch } )
631
- . collect :: < String > ( )
632
- }
633
592
634
593
pub fn normalize_id ( content : & str ) -> String {
635
594
content. chars ( )
@@ -653,37 +612,32 @@ mod tests {
653
612
let inputs = vec ! [
654
613
(
655
614
"blah blah <h1>Foo</h1>" ,
656
- r##"blah blah <a class="header" href="./some_chapter/some_section.html #foo" id="foo"><h1>Foo</h1></a>"## ,
615
+ r##"blah blah <a class="header" href="#foo" id="foo"><h1>Foo</h1></a>"## ,
657
616
) ,
658
617
(
659
618
"<h1>Foo</h1>" ,
660
- r##"<a class="header" href="./some_chapter/some_section.html #foo" id="foo"><h1>Foo</h1></a>"## ,
619
+ r##"<a class="header" href="#foo" id="foo"><h1>Foo</h1></a>"## ,
661
620
) ,
662
621
(
663
622
"<h3>Foo^bar</h3>" ,
664
- r##"<a class="header" href="./some_chapter/some_section.html #foobar" id="foobar"><h3>Foo^bar</h3></a>"## ,
623
+ r##"<a class="header" href="#foobar" id="foobar"><h3>Foo^bar</h3></a>"## ,
665
624
) ,
666
625
(
667
626
"<h4></h4>" ,
668
- r##"<a class="header" href="./some_chapter/some_section.html #" id=""><h4></h4></a>"## ,
627
+ r##"<a class="header" href="#" id=""><h4></h4></a>"## ,
669
628
) ,
670
629
(
671
630
"<h4><em>Hï</em></h4>" ,
672
- r##"<a class="header" href="./some_chapter/some_section.html #hï" id="hï"><h4><em>Hï</em></h4></a>"## ,
631
+ r##"<a class="header" href="#hï" id="hï"><h4><em>Hï</em></h4></a>"## ,
673
632
) ,
674
633
(
675
634
"<h1>Foo</h1><h3>Foo</h3>" ,
676
- r##"<a class="header" href="./some_chapter/some_section.html #foo" id="foo"><h1>Foo</h1></a><a class="header" href="./some_chapter/some_section.html #foo-1" id="foo-1"><h3>Foo</h3></a>"## ,
635
+ r##"<a class="header" href="#foo" id="foo"><h1>Foo</h1></a><a class="header" href="#foo-1" id="foo-1"><h3>Foo</h3></a>"## ,
677
636
) ,
678
637
] ;
679
638
680
639
for ( src, should_be) in inputs {
681
- let filepath = "./some_chapter/some_section.html" ;
682
- let got = build_header_links ( & src, filepath) ;
683
- assert_eq ! ( got, should_be) ;
684
-
685
- // This is redundant for most cases
686
- let got = fix_anchor_links ( & got, filepath) ;
640
+ let got = build_header_links ( & src) ;
687
641
assert_eq ! ( got, should_be) ;
688
642
}
689
643
}
0 commit comments