1
0
Fork 0
mirror of https://github.com/lise-henry/crowbook synced 2024-06-03 03:26:19 +02:00

Add BookRenderer trait

This commit is contained in:
Elisabeth Henry 2016-12-20 12:36:07 +01:00
parent 290c9b0b2e
commit f41331010a
6 changed files with 112 additions and 12 deletions

View File

@ -1,3 +1,20 @@
// Copyright (C) 2016 Élisabeth HENRY.
//
// 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 error::{Error, Result, Source};
use cleaner::{Cleaner, CleanerParams, French, Off, Default};
use bookoptions::BookOptions;
@ -429,9 +446,10 @@ impl Book {
#[cfg(not(feature = "proofread"))]
fn init_checker(&mut self) {}
fn render_one(&self, s: &str) -> () {
if self.options.get(s).is_ok() {
let (result, name) = match s {
/// Renders the book to the given format
pub fn render_format(&self, format: &str) -> () {
if self.options.get(format).is_ok() {
let (result, name) = match format {
"output.pdf" => (self.render_pdf(), "PDF"),
"output.epub" => (self.render_epub(), "EPUB"),
"output.html_dir" => (self.render_html_dir(), "HTML directory"),
@ -476,16 +494,16 @@ impl Book {
let mut handles = vec![];
crossbeam::scope(|scope| {
if self.options.get("output.pdf").is_ok() {
handles.push(scope.spawn(|| self.render_one("output.pdf")));
handles.push(scope.spawn(|| self.render_format("output.pdf")));
}
if self.options.get("output.epub").is_ok() {
handles.push(scope.spawn(|| self.render_one("output.epub")));
handles.push(scope.spawn(|| self.render_format("output.epub")));
}
if self.options.get("output.html_dir").is_ok() {
handles.push(scope.spawn(|| self.render_one("output.html_dir")));
handles.push(scope.spawn(|| self.render_format("output.html_dir")));
}
if self.options.get("output.odt").is_ok() {
handles.push(scope.spawn(|| self.render_one("output.odt")));
handles.push(scope.spawn(|| self.render_format("output.odt")));
}
if self.options.get_path("output.html").is_ok() {
handles.push(scope.spawn(|| self.render_one_file("output.html")));
@ -495,10 +513,10 @@ impl Book {
}
if self.is_proofread() {
if self.options.get("output.proofread.pdf").is_ok() {
handles.push(scope.spawn(|| self.render_one("output.proofread.pdf")));
handles.push(scope.spawn(|| self.render_format("output.proofread.pdf")));
}
if self.options.get("output.proofread.html_dir").is_ok() {
handles.push(scope.spawn(|| self.render_one("output.proofread.html_dir")));
handles.push(scope.spawn(|| self.render_format("output.proofread.html_dir")));
}
if self.options.get_path("output.proofread.html").is_ok() {
handles.push(scope.spawn(|| self.render_one_file("output.proofread.html")));

43
src/lib/book_renderer.rs Normal file
View File

@ -0,0 +1,43 @@
// Copyright (C) 2016 Élisabeth HENRY.
//
// 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 error::{Error, Result, Source};
use book::Book;
use std::io::Write;
use std::fs::File;
use std::path::Path;
/// Thait that must be implemented by the various renderers to render a whole book.
pub trait BookRenderer {
/// Render the book and write the result to the specified writer
fn render(&mut self, book: &Book, to: &mut Write) -> Result<()>;
/// Render the book to a given file.
///
/// The default implementation creates a file and calls `render` to write to it,
/// but in some cases it might be useful to override it.
fn render_to_file(&mut self, book: &Book, path: &AsRef<Path>) -> Result<()> {
let mut file = File::create(path)
.map_err(|err| Error::default(Source::empty(),
lformat!("could not create file '{file}': {err}",
file = path.as_ref().display(),
err = err)))?;
self.render(book, &mut file)?;
Ok(())
}
}

View File

@ -411,3 +411,5 @@ fn filenamer(i: usize) -> String {
}
derive_html!{HtmlDirRenderer<'a>, HtmlRenderer::static_render_token}

View File

@ -15,18 +15,19 @@
// You should have received ba copy of the GNU Lesser General Public License
// along with Crowbook. If not, see <http://www.gnu.org/licenses/>.
use error::{Result, Source};
use error::{Error, Result, Source};
use html::HtmlRenderer;
use book::{Book, compile_str};
use token::Token;
use templates::img;
use renderer::Renderer;
use book_renderer::BookRenderer;
use parser::Parser;
use rustc_serialize::base64::{self, ToBase64};
use std::convert::{AsMut, AsRef};
use std::io::Write;
/// Single file HTML renderer
///
@ -253,3 +254,37 @@ impl<'a> HtmlSingleRenderer<'a> {
}
derive_html!{HtmlSingleRenderer<'a>, HtmlSingleRenderer::static_render_token}
struct HtmlSingle {}
struct ProofHtmlSingle {}
impl BookRenderer for HtmlSingle {
fn render(&mut self, book: &Book, to: &mut Write) -> Result<()> {
let mut html = HtmlSingleRenderer::new(book);
book.logger.debug(lformat!("Attempting to generate HTML..."));
let result = html.render_book()?;
to.write_all(&result.as_bytes())
.map_err(|e| {
Error::render(&book.source,
lformat!("problem when writing HTML: {error}", error = e))
})?;
Ok(())
}
}
impl BookRenderer for ProofHtmlSingle {
fn render(&mut self, book: &Book, to: &mut Write) -> Result<()> {
let mut html = HtmlSingleRenderer::new(book)
.proofread();
book.logger.debug(lformat!("Attempting to generate HTML..."));
let result = html.render_book()?;
to.write_all(&result.as_bytes())
.map_err(|e| {
Error::render(&book.source,
lformat!("problem when writing HTML: {error}", error = e))
})?;
Ok(())
}
}

View File

@ -126,6 +126,7 @@ extern crate url;
extern crate caribon;
pub use parser::Parser;
pub use book::Book;
pub use bookoption::BookOption;
pub use bookoptions::BookOptions;
@ -160,6 +161,7 @@ mod logger;
mod bookoptions;
mod lang;
mod renderer;
mod book_renderer;
mod html_single;
mod toc;

View File

@ -18,7 +18,7 @@
use token::Token;
use error::Result;
/// Renderer trait.
/// Renderer trait, implemented by various renderer to render a list of `Token`s.
pub trait Renderer {
/// Render an individual token
fn render_token(&mut self, token: &Token) -> Result<String>;