crowbook 0.17.1

Render a Markdown book in HTML, PDF or Epub
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
// Copyright (C) 2016-2026 Lizzie Crowdagger
//
// This file is part of Crowbook.
//
// Crowbook is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published
// by the Free Software Foundation, either version 2.1 of the License, or
// (at your option) any later version.
//
// Crowbook is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with Crowbook.  If not, see <http://www.gnu.org/licenses/>.

use crate::book::Book;
use crate::book_renderer::BookRenderer;
use crate::error::{Error, Result, Source};
use crate::html::Highlight;
use crate::html::HtmlRenderer;
use crate::parser::Parser;
use crate::renderer::Renderer;
use crate::resource_handler;
use crate::templates::img;
use crate::text_view::view_as_text;
use crate::token::Token;

use std::borrow::Cow;
use std::convert::{AsMut, AsRef};
use std::fmt::Write;
use std::fs;
use std::fs::File;
use std::io;
use std::io::Read;
use std::path::Path;
use std::path::PathBuf;
use rust_i18n::t;

/// Multiple files HTML renderer
///
/// Renders HTML in a given directory.
pub struct HtmlDirRenderer<'a> {
    html: HtmlRenderer<'a>,
}

impl<'a> HtmlDirRenderer<'a> {
    /// Creates a new HtmlDirRenderer
    pub fn new(book: &'a Book) -> Result<HtmlDirRenderer<'a>> {
        let mut html = HtmlRenderer::new(
            book,
            book.options
                .get_str("html.highlight.theme")
                .unwrap_or_else(|_| book.options.get_str("rendering.highlight.theme").unwrap()),
        )?;
        html.handler.set_images_mapping(true);
        html.handler.set_base64(false);
        Ok(HtmlDirRenderer { html })
    }

    /// Render a book
    pub fn render_book(&mut self, dest_path: &Path) -> Result<()> {
        // Add internal files to resource handler
        for (i, chapter) in self.html.book.chapters.iter().enumerate() {
            self.html
                .handler
                .add_link(chapter.filename.as_str(), filenamer(i));
        }

        if let Ok(metadata) = fs::metadata(dest_path) {
            if metadata.is_file() {
                return Err(Error::render(
                    &self.html.book.source,
                    t!("html.exist_not_dir",
                        path = dest_path.display()
                    ),
                ));
            } else if metadata.is_dir() {
                debug!(
                    "{}",
                    t!("html.delete_dir",
                        path = dest_path.display()
                    )
                );
                fs::remove_dir_all(dest_path).map_err(|e| {
                    Error::render(
                        &self.html.book.source,
                        t!("html.delete_dir_error",
                            path = dest_path.display(),
                            error = e
                        ),
                    )
                })?;
            }
        }

        fs::DirBuilder::new()
            .recursive(true)
            .create(dest_path)
            .map_err(|e| {
                Error::render(
                    &self.html.book.source,
                    t!("html.create_dir_error",
                       path = dest_path.display(),
                       error = e
                    ),
                )
            })?;

        // Write CSS
        self.write_css()?;
        // Write print.css
        self.write_file(
            "print.css",
            self.html
                .book
                .get_template("html.css.print")
                .unwrap()
                .as_bytes(),
        )?;
        // Write index.html and chapter_xxx.html
        self.write_html()?;
        // Write menu.svg
        self.write_file("menu.svg", img::MENU_SVG)?;

        // Write highlight files if they are needed
        if self.html.highlight == Highlight::Js {
            self.write_file(
                "highlight.js",
                self.html
                    .book
                    .get_template("html.highlight.js")
                    .unwrap()
                    .as_bytes(),
            )?;
            self.write_file(
                "highlight.css",
                self.html
                    .book
                    .get_template("html.highlight.css")
                    .unwrap()
                    .as_bytes(),
            )?;
        }

        // Write all images (including cover)
        for (source, dest) in self.html.handler.images_mapping() {
            let mut f = fs::canonicalize(source).and_then(File::open).map_err(|_| {
                Error::file_not_found(
                    &self.html.book.source,
                    t!("epub.image_or_cover"),
                    source.clone(),
                )
            })?;
            let mut content = vec![];
            f.read_to_end(&mut content).map_err(|e| {
                Error::render(
                    &self.html.book.source,
                    t!("html.reading_image_error",
                        file = source,
                        error = e
                    ),
                )
            })?;
            self.write_file(dest, &content)?;
        }

        // Write additional files
        if let Ok(list) = self.html.book.options.get_str_vec("resources.files") {
            let files_path = self
                .html
                .book
                .options
                .get_path("resources.base_path.files")
                .unwrap();
            let data_path = Path::new(
                self.html
                    .book
                    .options
                    .get_relative_path("resources.out_path")
                    .unwrap(),
            );
            let list = resource_handler::get_files(list, &files_path)?;
            for path in list {
                let abs_path = Path::new(&files_path).join(&path);
                let mut f = fs::canonicalize(&abs_path)
                    .and_then(File::open)
                    .map_err(|_| {
                        Error::file_not_found(
                            &self.html.book.source,
                            t!("epub.resources"),
                            abs_path.to_string_lossy().into_owned(),
                        )
                    })?;
                let mut content = vec![];
                f.read_to_end(&mut content).map_err(|e| {
                    Error::render(
                        &self.html.book.source,
                        t!("html.resource_error", error = e),
                    )
                })?;
                self.write_file(data_path.join(&path).to_str().unwrap(), &content)?;
            }
        }

        Ok(())
    }

    // Render each chapter and write them, and index.html too
    fn write_html(&mut self) -> Result<()> {
        let mut chapters = vec![];

        let mut titles = vec![];
        let mut titles_raw = vec![];
        for (i, chapter) in self.html.book.chapters.iter().enumerate() {
            let n = chapter.number;
            let v = &chapter.content;
            self.html.chapter_config(i, n, filenamer(i));
            let mut title = String::new();
            let mut title_raw = String::new();
            for token in v {
                match *token {
                    Token::Header(1, ref vec) => {
                        if self.html.current_hide || self.html.current_numbering == 0 {
                            title = self.html.render_vec(vec)?;
                            title_raw = view_as_text(vec);
                        } else {
                            title = self
                                .html
                                .book
                                .get_chapter_header(
                                    self.html.current_chapter[1] + 1,
                                    self.html.render_vec(vec)?,
                                    |s| self.render_vec(&Parser::new().parse_inline(s)?),
                                )?
                                .text;
                            title_raw = self
                                .html
                                .book
                                .get_chapter_header(
                                    self.html.current_chapter[1] + 1,
                                    view_as_text(vec),
                                    |s| Ok(view_as_text(&Parser::new().parse_inline(s)?)),
                                )?
                                .text;
                        }
                        break;
                    }
                    _ => {
                        continue;
                    }
                }
            }
            titles.push(title);
            titles_raw.push(title_raw);

            let chapter = HtmlRenderer::render_html(self, v, true);
            chapters.push(chapter);
        }
        self.html.source = Source::empty();
        let toc = self.html.toc.render(false, false);

        // render all chapters
        let template_src = self.html.book.get_template("html.dir.template")?;
        let template = self.html.book.compile_str(
            template_src.as_ref(),
            &self.html.book.source,
            "html.dir.template",
        )?;
        for (i, content) in chapters.into_iter().enumerate() {
            let prev_chapter = if i > 0 {
                format!(
                    "<p class = \"prev_chapter\">
  <a href = \"{}\">
    « {}
  </a>
</p>",
                    filenamer(i - 1),
                    titles[i - 1]
                )
            } else {
                String::new()
            };

            let next_chapter = if i < titles.len() - 1 {
                format!(
                    "<p class = \"next_chapter\">
  <a href = \"{}\">
    {} »
  </a>
</p>",
                    filenamer(i + 1),
                    titles[i + 1]
                )
            } else {
                String::new()
            };

            // Render each HTML document
            let mut data = self
                .html
                .get_metadata()?;
            data.insert("content".into(), content?.into());
            data.insert("chapter_title".into(), titles[i].clone().into());
            data.insert("chapter_title_raw".into(), titles_raw[i].clone().into());
            data.insert("toc".into(), toc.clone().into());
            data.insert("prev_chapter".into(), prev_chapter.into());
            data.insert("next_chapter".into(), next_chapter.into());
            data.insert("is_chapter".into(), true.into());
            
            if let Ok(favicon) = self.html.book.options.get_path("html.icon") {
                let favicon = self
                    .html
                    .handler
                    .map_image(&self.html.book.source, favicon)?;
                data.insert(
                    "favicon".into(),
                    format!("<link rel = \"icon\" href = \"{favicon}\">").into(),
                );
            } else {
                data.insert("favicon".into(), "".into());
            }


            let res = template.render(&self.html.book.registry, &data).to_string()?;
            self.write_file(&filenamer(i), res.as_bytes())?;
        }

        let mut content = if let Ok(cover) = self.html.book.options.get_path("cover") {
            // checks first that cover exists
            if fs::metadata(&cover).is_err() {
                return Err(Error::file_not_found(
                    &self.html.book.source,
                    t!("epub.cover"),
                    cover,
                ));
            }
            format!(
                "<div id = \"cover\">
  <img class = \"cover\" alt = \"{}\" src = \"{}\" />
</div>",
                self.html.book.options.get_str("title").unwrap(),
                self.html
                    .handler
                    .map_image(&self.html.book.source, Cow::Owned(cover))?
                    .as_ref()
            )
        } else {
            String::new()
        };

        content = {
            let mut f = |key| {
                self.render_vec(&Parser::new().parse_inline(self.html.book.options.get_str(key)?)?)
            };

            format!(
                "<h2 class = 'author'>{author}</h2>
<h1 class = 'title'>{title}</h1>
<h2 class = 'subtitle'>{subtitle}</h2>
<div class = \"autograph\">
{autograph}
</div>
{content}",
                author = f("author")?,
                title = f("title")?,
                autograph = f("autograph").unwrap_or_else(|_| String::new()),
                content = content,
                subtitle = f("subtitle").unwrap_or_else(|_| String::new())
            )
        };

        // Insert toc inline if option is set
        if self
            .html
            .book
            .options
            .get_bool("rendering.inline_toc")
            .unwrap()
        {
            write!(
                content,
                "<h1>{}</h1>
<div id = \"toc\">
{}
</div>
",
                self.html.get_toc_name()?,
                &toc
            )?;
        }

        if titles.len() > 1 {
            write!(
                content,
                "<p class = \"next_chapter\">
  <a href = \"{}\">
    {} »
  </a>
</p>",
                filenamer(0),
                titles[0]
            )?;
        }
        // Render index.html and write it too
        let mut data = self
            .html
            .get_metadata()?;
        data.insert("content".into(), content.into());
        data.insert("toc".into(), toc.into());
        data.insert("is_chapter".into(), false.into());
        if let Ok(favicon) = self.html.book.options.get_path("html.icon") {
            let favicon = self
                .html
                .handler
                .map_image(&self.html.book.source, favicon)?;
            data.insert(
                "favicon".into(),
                format!("<link rel = \"icon\" href = \"{favicon}\">").into(),
            );
        }
        let template_src = self.html.book.get_template("html.dir.template")?;
        let template = self.html.book.compile_str(
            template_src.as_ref(),
            &self.html.book.source,
            "html.dir.template",
        )?;
        let res = template.render(&self.html.book.registry, &data).to_string()?;
        self.write_file("index.html", res.as_bytes())?;

        Ok(())
    }

    // Render the CSS file and write it
    fn write_css(&self) -> Result<()> {
        // Render the CSS
        let template_css_src = self.html.book.get_template("html.css")?;
        let template_css = self.html.book.compile_str(
            template_css_src.as_ref(),
            &self.html.book.source,
            "html.css",
        )?;
        let mut data = self.html.book.get_metadata(|s| Ok(s.to_owned()))?;
        data.insert("colors".into(), self.html.book.get_template("html.css.colors")?.into());
        let html_css_add = self.html.book.options.get_str("html.css.add").unwrap_or("".into());
        data.insert("additional_code".into(), html_css_add.into());
        
        let css = template_css.render(&self.html.book.registry, &data).to_string()?;

        // Write it
        self.write_file("stylesheet.css", css.as_bytes())
    }

    // Write content to a file
    fn write_file(&self, file: &str, content: &[u8]) -> Result<()> {
        let dir_name = self.html.book.options.get_path("output.html.dir").unwrap();
        let dest_path = PathBuf::from(&dir_name);
        assert!(dest_path.starts_with(dir_name),
                "multifile HTML renderer is asked to create a file ({dest_path}) outside of its directory, no way!",
                dest_path = dest_path.display());
        let dest_file = dest_path.join(file);
        let dest_dir = dest_file.parent().unwrap();
        if fs::metadata(dest_dir).is_err() {
            // dir does not exist, create it
            fs::DirBuilder::new()
                .recursive(true)
                .create(dest_dir)
                .map_err(|e| {
                    Error::render(
                        &self.html.book.source,
                        t!("html.create_dir_error",
                            path = dest_dir.display(),
                            error = e
                        ),
                    )
                })?;
        }
        let mut f = File::create(&dest_file).map_err(|e| {
            Error::render(
                &self.html.book.source,
                t!("html.create_file_error",
                   file = dest_file.display(),
                   error = e
                ),
            )
        })?;
        io::Write::write_all(&mut f, content).map_err(|e| {
            Error::render(
                &self.html.book.source,
                t!("html.write_file_error",
                   file = dest_file.display(),
                   error = e
                ),
            )
        })
    }
}

/// Generate a file name given an int
fn filenamer(i: usize) -> String {
    format!("chapter_{i:03}.html")
}

derive_html! {HtmlDirRenderer<'a>, HtmlRenderer::static_render_token}

pub struct HtmlDir {}

impl BookRenderer for HtmlDir {
    fn auto_path(&self, _: &str) -> Result<String> {
        Ok(String::from("output_html"))
    }

    fn render(&self, _: &Book, _: &mut dyn io::Write) -> Result<()> {
        Err(Error::render(
            Source::empty(),
            t!("html.dir_to_stream_error"),
        ))
    }

    fn render_to_file(&self, book: &Book, path: &Path) -> Result<()> {
        HtmlDirRenderer::new(book)?.render_book(path)?;
        Ok(())
    }
}