@@ -30,84 +30,82 @@ impl HtmlHandlebars {
30
30
print_content : & mut String ,
31
31
) -> Result < ( ) > {
32
32
// FIXME: This should be made DRY-er and rely less on mutable state
33
- if let BookItem :: Chapter ( ref ch) = * item {
34
- if let Some ( ref path) = ch. path {
35
- let content = ch. content . clone ( ) ;
36
- let content = utils:: render_markdown ( & content, ctx. html_config . curly_quotes ) ;
37
-
38
- let fixed_content = utils:: render_markdown_with_path (
39
- & ch. content ,
40
- ctx. html_config . curly_quotes ,
41
- Some ( & path) ,
42
- ) ;
43
- print_content. push_str ( & fixed_content) ;
44
33
45
- // Update the context with data for this file
46
- let ctx_path = path
47
- . to_str ( )
48
- . chain_err ( || "Could not convert path to str" ) ?;
49
- let filepath = Path :: new ( & ctx_path) . with_extension ( "html" ) ;
34
+ let ( ch, path) = match item {
35
+ BookItem :: Chapter ( ch) if !ch. is_draft_chapter ( ) => ( ch, ch. path . as_ref ( ) . unwrap ( ) ) ,
36
+ _ => return Ok ( ( ) ) ,
37
+ } ;
50
38
51
- // "print.html" is used for the print page.
52
- if path == Path :: new ( "print.md" ) {
53
- bail ! ( ErrorKind :: ReservedFilenameError ( path. clone( ) ) ) ;
54
- } ;
39
+ let content = ch. content . clone ( ) ;
40
+ let content = utils:: render_markdown ( & content, ctx. html_config . curly_quotes ) ;
55
41
56
- // Non-lexical lifetimes needed :'(
57
- let title: String ;
58
- {
59
- let book_title = ctx
60
- . data
61
- . get ( "book_title" )
62
- . and_then ( serde_json:: Value :: as_str)
63
- . unwrap_or ( "" ) ;
64
-
65
- title = match book_title {
66
- "" => ch. name . clone ( ) ,
67
- _ => ch. name . clone ( ) + " - " + book_title,
68
- }
69
- }
42
+ let fixed_content = utils:: render_markdown_with_path (
43
+ & ch. content ,
44
+ ctx. html_config . curly_quotes ,
45
+ Some ( & path) ,
46
+ ) ;
47
+ print_content. push_str ( & fixed_content) ;
70
48
71
- ctx. data . insert ( "path" . to_owned ( ) , json ! ( path) ) ;
72
- ctx. data . insert ( "content" . to_owned ( ) , json ! ( content) ) ;
73
- ctx. data . insert ( "chapter_title" . to_owned ( ) , json ! ( ch. name) ) ;
74
- ctx. data . insert ( "title" . to_owned ( ) , json ! ( title) ) ;
75
- ctx. data . insert (
76
- "path_to_root" . to_owned ( ) ,
77
- json ! ( utils:: fs:: path_to_root( & path) ) ,
78
- ) ;
79
- if let Some ( ref section) = ch. number {
80
- ctx. data
81
- . insert ( "section" . to_owned ( ) , json ! ( section. to_string( ) ) ) ;
82
- }
49
+ // Update the context with data for this file
50
+ let ctx_path = path
51
+ . to_str ( )
52
+ . chain_err ( || "Could not convert path to str" ) ?;
53
+ let filepath = Path :: new ( & ctx_path) . with_extension ( "html" ) ;
83
54
84
- // Render the handlebars template with the data
85
- debug ! ( "Render template" ) ;
86
- let rendered = ctx. handlebars . render ( "index" , & ctx. data ) ?;
87
-
88
- let rendered = self . post_process ( rendered, & ctx. html_config . playpen , ctx. edition ) ;
89
-
90
- // Write to file
91
- debug ! ( "Creating {}" , filepath. display( ) ) ;
92
- utils:: fs:: write_file ( & ctx. destination , & filepath, rendered. as_bytes ( ) ) ?;
93
-
94
- if ctx. is_index {
95
- ctx. data . insert ( "path" . to_owned ( ) , json ! ( "index.md" ) ) ;
96
- ctx. data . insert ( "path_to_root" . to_owned ( ) , json ! ( "" ) ) ;
97
- ctx. data . insert ( "is_index" . to_owned ( ) , json ! ( "true" ) ) ;
98
- let rendered_index = ctx. handlebars . render ( "index" , & ctx. data ) ?;
99
- let rendered_index =
100
- self . post_process ( rendered_index, & ctx. html_config . playpen , ctx. edition ) ;
101
- debug ! ( "Creating index.html from {}" , ctx_path) ;
102
- utils:: fs:: write_file (
103
- & ctx. destination ,
104
- "index.html" ,
105
- rendered_index. as_bytes ( ) ,
106
- ) ?;
107
- }
55
+ // "print.html" is used for the print page.
56
+ if path == Path :: new ( "print.md" ) {
57
+ bail ! ( ErrorKind :: ReservedFilenameError ( path. clone( ) ) ) ;
58
+ } ;
59
+
60
+ // Non-lexical lifetimes needed :'(
61
+ let title: String ;
62
+ {
63
+ let book_title = ctx
64
+ . data
65
+ . get ( "book_title" )
66
+ . and_then ( serde_json:: Value :: as_str)
67
+ . unwrap_or ( "" ) ;
68
+
69
+ title = match book_title {
70
+ "" => ch. name . clone ( ) ,
71
+ _ => ch. name . clone ( ) + " - " + book_title,
108
72
}
109
73
}
110
74
75
+ ctx. data . insert ( "path" . to_owned ( ) , json ! ( path) ) ;
76
+ ctx. data . insert ( "content" . to_owned ( ) , json ! ( content) ) ;
77
+ ctx. data . insert ( "chapter_title" . to_owned ( ) , json ! ( ch. name) ) ;
78
+ ctx. data . insert ( "title" . to_owned ( ) , json ! ( title) ) ;
79
+ ctx. data . insert (
80
+ "path_to_root" . to_owned ( ) ,
81
+ json ! ( utils:: fs:: path_to_root( & path) ) ,
82
+ ) ;
83
+ if let Some ( ref section) = ch. number {
84
+ ctx. data
85
+ . insert ( "section" . to_owned ( ) , json ! ( section. to_string( ) ) ) ;
86
+ }
87
+
88
+ // Render the handlebars template with the data
89
+ debug ! ( "Render template" ) ;
90
+ let rendered = ctx. handlebars . render ( "index" , & ctx. data ) ?;
91
+
92
+ let rendered = self . post_process ( rendered, & ctx. html_config . playpen , ctx. edition ) ;
93
+
94
+ // Write to file
95
+ debug ! ( "Creating {}" , filepath. display( ) ) ;
96
+ utils:: fs:: write_file ( & ctx. destination , & filepath, rendered. as_bytes ( ) ) ?;
97
+
98
+ if ctx. is_index {
99
+ ctx. data . insert ( "path" . to_owned ( ) , json ! ( "index.md" ) ) ;
100
+ ctx. data . insert ( "path_to_root" . to_owned ( ) , json ! ( "" ) ) ;
101
+ ctx. data . insert ( "is_index" . to_owned ( ) , json ! ( "true" ) ) ;
102
+ let rendered_index = ctx. handlebars . render ( "index" , & ctx. data ) ?;
103
+ let rendered_index =
104
+ self . post_process ( rendered_index, & ctx. html_config . playpen , ctx. edition ) ;
105
+ debug ! ( "Creating index.html from {}" , ctx_path) ;
106
+ utils:: fs:: write_file ( & ctx. destination , "index.html" , rendered_index. as_bytes ( ) ) ?;
107
+ }
108
+
111
109
Ok ( ( ) )
112
110
}
113
111
0 commit comments