@@ -42,10 +42,6 @@ impl HtmlHandlebars {
42
42
. to_str ( )
43
43
. chain_err ( || "Could not convert path to str" ) ?;
44
44
let filepath = Path :: new ( & ch. path ) . with_extension ( "html" ) ;
45
- let filepathstr = filepath
46
- . to_str ( )
47
- . chain_err ( || "Could not convert HTML path to str" ) ?;
48
- let filepathstr = utils:: fs:: normalize_path ( filepathstr) ;
49
45
50
46
// "print.html" is used for the print page.
51
47
if ch. path == Path :: new ( "print.md" ) {
@@ -75,10 +71,10 @@ impl HtmlHandlebars {
75
71
debug ! ( "Render template" ) ;
76
72
let rendered = ctx. handlebars . render ( "index" , & ctx. data ) ?;
77
73
78
- let rendered = self . post_process ( rendered, & filepathstr , & ctx. html_config . playpen ) ;
74
+ let rendered = self . post_process ( rendered, & ctx. html_config . playpen ) ;
79
75
80
76
// Write to file
81
- debug ! ( "Creating {} ✓" , filepathstr ) ;
77
+ debug ! ( "Creating {} ✓" , filepath . display ( ) ) ;
82
78
utils:: fs:: write_file ( & ctx. destination , & filepath, & rendered. into_bytes ( ) ) ?;
83
79
84
80
if ctx. is_index {
@@ -120,9 +116,8 @@ impl HtmlHandlebars {
120
116
}
121
117
122
118
#[ cfg_attr( feature = "cargo-clippy" , allow( let_and_return) ) ]
123
- fn post_process ( & self , rendered : String , filepath : & str , playpen_config : & Playpen ) -> String {
124
- let rendered = build_header_links ( & rendered, filepath) ;
125
- let rendered = fix_anchor_links ( & rendered, filepath) ;
119
+ fn post_process ( & self , rendered : String , playpen_config : & Playpen ) -> String {
120
+ let rendered = build_header_links ( & rendered) ;
126
121
let rendered = fix_code_blocks ( & rendered) ;
127
122
let rendered = add_playpen_pre ( & rendered, playpen_config) ;
128
123
@@ -360,7 +355,7 @@ impl Renderer for HtmlHandlebars {
360
355
debug ! ( "Render template" ) ;
361
356
let rendered = handlebars. render ( "index" , & data) ?;
362
357
363
- let rendered = self . post_process ( rendered, "print.html" , & html_config. playpen ) ;
358
+ let rendered = self . post_process ( rendered, & html_config. playpen ) ;
364
359
365
360
utils:: fs:: write_file ( & destination, "print.html" , & rendered. into_bytes ( ) ) ?;
366
361
debug ! ( "Creating print.html ✓" ) ;
@@ -497,7 +492,7 @@ fn make_data(
497
492
498
493
/// Goes through the rendered HTML, making sure all header tags are wrapped in
499
494
/// an anchor so people can link to sections directly.
500
- fn build_header_links ( html : & str , filepath : & str ) -> String {
495
+ fn build_header_links ( html : & str ) -> String {
501
496
let regex = Regex :: new ( r"<h(\d)>(.*?)</h\d>" ) . unwrap ( ) ;
502
497
let mut id_counter = HashMap :: new ( ) ;
503
498
@@ -507,7 +502,7 @@ fn build_header_links(html: &str, filepath: &str) -> String {
507
502
. parse ( )
508
503
. expect ( "Regex should ensure we only ever get numbers here" ) ;
509
504
510
- wrap_header_with_link ( level, & caps[ 2 ] , & mut id_counter, filepath )
505
+ wrap_header_with_link ( level, & caps[ 2 ] , & mut id_counter)
511
506
} )
512
507
. into_owned ( )
513
508
}
@@ -517,8 +512,7 @@ fn build_header_links(html: &str, filepath: &str) -> String {
517
512
fn wrap_header_with_link (
518
513
level : usize ,
519
514
content : & str ,
520
- id_counter : & mut HashMap < String , usize > ,
521
- filepath : & str ,
515
+ id_counter : & mut HashMap < String , usize >
522
516
) -> String {
523
517
let raw_id = utils:: id_from_content ( content) ;
524
518
@@ -532,35 +526,13 @@ fn wrap_header_with_link(
532
526
* id_count += 1 ;
533
527
534
528
format ! (
535
- r##"<a class="header" href="{filepath} #{id}" id="{id}"><h{level}>{text}</h{level}></a>"## ,
529
+ r##"<a class="header" href="#{id}" id="{id}"><h{level}>{text}</h{level}></a>"## ,
536
530
level = level,
537
531
id = id,
538
- text = content,
539
- filepath = filepath
532
+ text = content
540
533
)
541
534
}
542
535
543
- // anchors to the same page (href="#anchor") do not work because of
544
- // <base href="../"> pointing to the root folder. This function *fixes*
545
- // that in a very inelegant way
546
- fn fix_anchor_links ( html : & str , filepath : & str ) -> String {
547
- let regex = Regex :: new ( r##"<a([^>]+)href="#([^"]+)"([^>]*)>"## ) . unwrap ( ) ;
548
- regex
549
- . replace_all ( html, |caps : & Captures | {
550
- let before = & caps[ 1 ] ;
551
- let anchor = & caps[ 2 ] ;
552
- let after = & caps[ 3 ] ;
553
-
554
- format ! (
555
- "<a{before}href=\" {filepath}#{anchor}\" {after}>" ,
556
- before = before,
557
- filepath = filepath,
558
- anchor = anchor,
559
- after = after
560
- )
561
- } )
562
- . into_owned ( )
563
- }
564
536
565
537
// The rust book uses annotations for rustdoc to test code snippets,
566
538
// like the following:
@@ -660,37 +632,32 @@ mod tests {
660
632
let inputs = vec ! [
661
633
(
662
634
"blah blah <h1>Foo</h1>" ,
663
- r##"blah blah <a class="header" href="./some_chapter/some_section.html #foo" id="foo"><h1>Foo</h1></a>"## ,
635
+ r##"blah blah <a class="header" href="#foo" id="foo"><h1>Foo</h1></a>"## ,
664
636
) ,
665
637
(
666
638
"<h1>Foo</h1>" ,
667
- r##"<a class="header" href="./some_chapter/some_section.html #foo" id="foo"><h1>Foo</h1></a>"## ,
639
+ r##"<a class="header" href="#foo" id="foo"><h1>Foo</h1></a>"## ,
668
640
) ,
669
641
(
670
642
"<h3>Foo^bar</h3>" ,
671
- r##"<a class="header" href="./some_chapter/some_section.html #foobar" id="foobar"><h3>Foo^bar</h3></a>"## ,
643
+ r##"<a class="header" href="#foobar" id="foobar"><h3>Foo^bar</h3></a>"## ,
672
644
) ,
673
645
(
674
646
"<h4></h4>" ,
675
- r##"<a class="header" href="./some_chapter/some_section.html #" id=""><h4></h4></a>"## ,
647
+ r##"<a class="header" href="#" id=""><h4></h4></a>"## ,
676
648
) ,
677
649
(
678
650
"<h4><em>Hï</em></h4>" ,
679
- r##"<a class="header" href="./some_chapter/some_section.html #hï" id="hï"><h4><em>Hï</em></h4></a>"## ,
651
+ r##"<a class="header" href="#hï" id="hï"><h4><em>Hï</em></h4></a>"## ,
680
652
) ,
681
653
(
682
654
"<h1>Foo</h1><h3>Foo</h3>" ,
683
- 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>"## ,
655
+ 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>"## ,
684
656
) ,
685
657
] ;
686
658
687
659
for ( src, should_be) in inputs {
688
- let filepath = "./some_chapter/some_section.html" ;
689
- let got = build_header_links ( & src, filepath) ;
690
- assert_eq ! ( got, should_be) ;
691
-
692
- // This is redundant for most cases
693
- let got = fix_anchor_links ( & got, filepath) ;
660
+ let got = build_header_links ( & src) ;
694
661
assert_eq ! ( got, should_be) ;
695
662
}
696
663
}
0 commit comments