File tree Expand file tree Collapse file tree 6 files changed +77
-0
lines changed
Expand file tree Collapse file tree 6 files changed +77
-0
lines changed Original file line number Diff line number Diff line change @@ -38,6 +38,7 @@ git-repository-url = "https://github.com/rust-lang/book"
3838[preprocessor .trpl-note ]
3939
4040[preprocessor .trpl-listing ]
41+ [preprocessor .trpl-listing-link ]
4142output-mode = " default"
4243
4344[rust ]
Original file line number Diff line number Diff line change @@ -11,6 +11,10 @@ path = "src/bin/note.rs"
1111name = " mdbook-trpl-listing"
1212path = " src/bin/listing.rs"
1313
14+ [[bin ]]
15+ name = " mdbook-trpl-listing-link"
16+ path = " src/bin/listing_link.rs"
17+
1418[[bin ]]
1519name = " mdbook-trpl-heading"
1620path = " src/bin/heading.rs"
@@ -29,6 +33,7 @@ pulldown-cmark-to-cmark = "19"
2933serde_json = " 1"
3034thiserror = " 1.0.60"
3135toml = " 0.8.12"
36+ regex = " 1.5"
3237
3338[dev-dependencies ]
3439assert_cmd = " 2"
Original file line number Diff line number Diff line change 1+ use std:: io;
2+
3+ use clap:: { self , Parser , Subcommand } ;
4+ use mdbook:: preprocess:: { CmdPreprocessor , Preprocessor } ;
5+
6+ use mdbook_trpl:: listing_link:: ListingLinkPreprocessor ;
7+
8+ fn main ( ) -> Result < ( ) , String > {
9+ let cli = Cli :: parse ( ) ;
10+ if let Some ( Command :: Supports { renderer } ) = cli. command {
11+ return if ListingLinkPreprocessor . supports_renderer ( & renderer) {
12+ Ok ( ( ) )
13+ } else {
14+ Err ( format ! ( "Renderer '{renderer}' is unsupported" ) )
15+ } ;
16+ }
17+
18+ let ( ctx, book) = CmdPreprocessor :: parse_input ( io:: stdin ( ) )
19+ . map_err ( |e| format ! ( "{e}" ) ) ?;
20+ let processed = ListingLinkPreprocessor
21+ . run ( & ctx, book)
22+ . map_err ( |e| format ! ( "{e}" ) ) ?;
23+ serde_json:: to_writer ( io:: stdout ( ) , & processed) . map_err ( |e| format ! ( "{e}" ) )
24+ }
25+
26+ /// A simple preprocessor for handling listing links in _The Rust Programming Language_.
27+ #[ derive( Parser , Debug ) ]
28+ struct Cli {
29+ #[ command( subcommand) ]
30+ command : Option < Command > ,
31+ }
32+
33+ #[ derive( Subcommand , Debug ) ]
34+ enum Command {
35+ /// Is the renderer supported?
36+ ///
37+ /// All renderers are supported! This is the contract for mdBook.
38+ Supports { renderer : String } ,
39+ }
Original file line number Diff line number Diff line change @@ -2,6 +2,7 @@ mod config;
22mod figure;
33mod heading;
44mod listing;
5+ pub mod listing_link;
56mod note;
67
78pub use config:: Mode ;
Original file line number Diff line number Diff line change 1+ use mdbook:: {
2+ book:: Book ,
3+ errors:: Result ,
4+ preprocess:: { Preprocessor , PreprocessorContext } ,
5+ BookItem ,
6+ } ;
7+ use regex:: Regex ;
8+
9+ pub struct ListingLinkPreprocessor ;
10+
11+ impl Preprocessor for ListingLinkPreprocessor {
12+ fn name ( & self ) -> & str {
13+ "trpl-listing-link"
14+ }
15+
16+ fn run ( & self , _ctx : & PreprocessorContext , mut book : Book ) -> Result < Book > {
17+ let re = Regex :: new ( r"Listing\s(\d+-\d+)" ) . unwrap ( ) ;
18+
19+ book. for_each_mut ( |item| {
20+ if let BookItem :: Chapter ( ref mut chapter) = item {
21+ chapter. content = re. replace_all ( & chapter. content , r##"<a href="#listing-$1" class="listing-link">Listing $1</a>"## ) . to_string ( ) ;
22+ }
23+ } ) ;
24+
25+ Ok ( book)
26+ }
27+
28+ fn supports_renderer ( & self , renderer : & str ) -> bool {
29+ renderer == "html" || renderer == "markdown" || renderer == "test"
30+ }
31+ }
You can’t perform that action at this time.
0 commit comments