From 7e71da7c6585c256ced929cbe13b99c854b83b37 Mon Sep 17 00:00:00 2001 From: Thomas Letan Date: Tue, 17 Nov 2020 12:55:31 +0100 Subject: Generalize the `load_and_render' methods Prior to this patch, these methods were always constructing a `String'-based result, which was fine when all we wanted to do with said result is use it with a template engine, but not that fine when we aim to provide a tool to count words. --- app/src/filesystem.rs | 8 ++++---- lib/src/epub.rs | 5 +++-- lib/src/lib.rs | 1 + lib/src/project.rs | 25 +++++++++++-------------- lib/src/render.rs | 1 + lib/src/wstatic.rs | 7 ++++--- templates/epub/chapter.xhtml | 4 +++- templates/static/chapter.html | 4 +++- 8 files changed, 30 insertions(+), 25 deletions(-) diff --git a/app/src/filesystem.rs b/app/src/filesystem.rs index 758282a..ade9a79 100644 --- a/app/src/filesystem.rs +++ b/app/src/filesystem.rs @@ -33,7 +33,7 @@ pub fn find_root() -> Result { } } -fn canonicalize_chapter(chapter : &Chapter>) -> Result>, Error> { +fn canonicalize_chapter(chapter : &Chapter) -> Result, Error> { let title = chapter.title.clone(); Ok(Chapter { title : title, @@ -46,8 +46,8 @@ fn canonicalize_chapter(chapter : &Chapter>) -> Result>, -) -> Result>, Error> { + project : Project, +) -> Result, Error> { Ok(Project { author : project.author, title : project.title, @@ -72,7 +72,7 @@ impl Loader for Fs { type CovId = PathBuf; type DocId = PathBuf; - fn load_project(&self, id : &PathBuf) -> Result>, Error> { + fn load_project(&self, id : &PathBuf) -> Result, Error> { let cwd = current_dir().or_raise("could not get current dir")?; let input = diff --git a/lib/src/epub.rs b/lib/src/epub.rs index 8ef5363..98765da 100644 --- a/lib/src/epub.rs +++ b/lib/src/epub.rs @@ -14,6 +14,7 @@ use zip::write::FileOptions; use zip::ZipWriter; use crate::assets::{fonts_dir, template_dir}; +use crate::render::Html; use crate::writer::BookWriter; const EPUB_MIMETYPE : &'static str = "application/epub+zip"; @@ -35,7 +36,7 @@ pub trait EpubWriter: BookWriter { fn create_chapters( &mut self, tera : &Tera, - chapters : &Vec>, + chapters : &Vec>, numbering : bool, lang : &Language, ) -> Result<(), Error> { @@ -83,7 +84,7 @@ pub trait EpubWriter: BookWriter { fn generate_epub( &mut self, - project : &Project, + project : &Project, assets : &PathBuf, ) -> Result<(), Error> { let tera = diff --git a/lib/src/lib.rs b/lib/src/lib.rs index 6d313d6..d3cf956 100644 --- a/lib/src/lib.rs +++ b/lib/src/lib.rs @@ -1,4 +1,5 @@ extern crate ogmarkup; +#[macro_use] extern crate serde_derive; extern crate serde_json; extern crate tera; diff --git a/lib/src/project.rs b/lib/src/project.rs index 1ba9498..e4cdda7 100644 --- a/lib/src/project.rs +++ b/lib/src/project.rs @@ -1,8 +1,8 @@ +use ogmarkup::generator::Output; use ogmarkup::typography::{Typography, ENGLISH, FRENCH}; use serde_derive::{Deserialize, Serialize}; use crate::error::{Error, Raise}; -use crate::render::Html; #[derive(Debug, Serialize, Deserialize)] pub enum Language { @@ -34,23 +34,21 @@ pub trait Loader { fn load_document(&self, id : &Self::DocId) -> Result; - fn load_project( - &self, - id : &Self::ProjId, - ) -> Result>, Error>; + fn load_project(&self, id : &Self::ProjId) -> Result, Error>; } #[derive(Debug, Serialize, Deserialize)] pub struct Chapter { pub title : Option, - pub content : I, + pub content : Vec, } -impl Chapter> { - fn load_and_render(&self, loader : &L, typo : &T) -> Result, Error> +impl Chapter { + fn load_and_render(&self, loader : &L, typo : &T) -> Result, Error> where T : Typography + ?Sized, L : Loader, + O : Output, { let title = &self.title; let content = &self.content; @@ -61,10 +59,8 @@ impl Chapter> { let input = loader.load_document(x)?; ogmarkup::compile(&input, typo) .or_raise("Cannot parse an ogmarkup document for some reason") - .map(Html::to_string) }) - .collect::, Error>>()? - .join(""); + .collect::, Error>>()?; Ok(Chapter { title : title.clone(), @@ -84,13 +80,14 @@ pub struct Project { pub language : Language, } -impl Project { +impl Project { pub fn load_and_render<'input, L>( id : &L::ProjId, loader : &L, - ) -> Result, Error> + ) -> Result, Error> where L : Loader, + O : Output, { let project = loader.load_project(id)?; @@ -109,7 +106,7 @@ impl Project { .chapters .into_iter() .map(|chapter| chapter.load_and_render(loader, typo)) - .collect::>, Error>>() + .collect::>, Error>>() .map(|x| Project { author : author, title : title, diff --git a/lib/src/render.rs b/lib/src/render.rs index 5548d2d..9df29a2 100644 --- a/lib/src/render.rs +++ b/lib/src/render.rs @@ -1,6 +1,7 @@ use ogmarkup::generator::Output; use ogmarkup::typography::Space; +#[derive(Serialize)] pub struct Html(String); impl Html { diff --git a/lib/src/wstatic.rs b/lib/src/wstatic.rs index 3c2ab0c..f9cc8f4 100644 --- a/lib/src/wstatic.rs +++ b/lib/src/wstatic.rs @@ -6,6 +6,7 @@ use tera::{Context, Tera}; use crate::assets::template_dir; use crate::error::{Error, Raise}; use crate::project::{Chapter, Cover, Language, Project}; +use crate::render::Html; use crate::BookWriter; pub struct Static { @@ -52,7 +53,7 @@ impl Static { fn generate_index( &mut self, - project : &Project, + project : &Project, tera : &Tera, ) -> Result<(), Error> { let chaps : Vec<_> = project @@ -88,7 +89,7 @@ impl Static { fn generate_chapters( &mut self, tera : &Tera, - chapters : &Vec>, + chapters : &Vec>, numbering : bool, lang : &Language, ) -> Result<(), Error> { @@ -119,7 +120,7 @@ impl Static { pub fn generate_static_website( &mut self, - project : &Project, + project : &Project, assets : &PathBuf, ) -> Result<(), Error> { let tera = diff --git a/templates/epub/chapter.xhtml b/templates/epub/chapter.xhtml index 095ec3d..9da84f7 100644 --- a/templates/epub/chapter.xhtml +++ b/templates/epub/chapter.xhtml @@ -25,7 +25,9 @@ {% endif %}
- {{ chapter.content }} + {% for content in chapter.content %} + {{ content | safe }} + {% endfor %}
diff --git a/templates/static/chapter.html b/templates/static/chapter.html index edbc040..842761f 100644 --- a/templates/static/chapter.html +++ b/templates/static/chapter.html @@ -41,7 +41,9 @@
- {{ chapter.content | safe }} + {% for content in chapter.content %} + {{ content | safe }} + {% endfor %}
{% if not body_only %} -- cgit v1.2.3