aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorThomas Letan <lthms@soap.coffee>2020-11-17 12:55:31 +0100
committerThomas Letan <lthms@soap.coffee>2020-11-17 12:55:31 +0100
commit7e71da7c6585c256ced929cbe13b99c854b83b37 (patch)
treec20cef2d70769f3216a9364c4cb3fbce91b6b0e2
parentFix install rule (diff)
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.
-rw-r--r--app/src/filesystem.rs8
-rw-r--r--lib/src/epub.rs5
-rw-r--r--lib/src/lib.rs1
-rw-r--r--lib/src/project.rs25
-rw-r--r--lib/src/render.rs1
-rw-r--r--lib/src/wstatic.rs7
-rw-r--r--templates/epub/chapter.xhtml4
-rw-r--r--templates/static/chapter.html4
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<PathBuf, Error> {
}
}
-fn canonicalize_chapter(chapter : &Chapter<Vec<PathBuf>>) -> Result<Chapter<Vec<PathBuf>>, Error> {
+fn canonicalize_chapter(chapter : &Chapter<PathBuf>) -> Result<Chapter<PathBuf>, Error> {
let title = chapter.title.clone();
Ok(Chapter {
title : title,
@@ -46,8 +46,8 @@ fn canonicalize_chapter(chapter : &Chapter<Vec<PathBuf>>) -> Result<Chapter<Vec<
}
fn canonicalize_project(
- project : Project<PathBuf, Vec<PathBuf>>,
-) -> Result<Project<PathBuf, Vec<PathBuf>>, Error> {
+ project : Project<PathBuf, PathBuf>,
+) -> Result<Project<PathBuf, PathBuf>, 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<Project<PathBuf, Vec<PathBuf>>, Error> {
+ fn load_project(&self, id : &PathBuf) -> Result<Project<PathBuf, PathBuf>, 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<Chapter<String>>,
+ chapters : &Vec<Chapter<Html>>,
numbering : bool,
lang : &Language,
) -> Result<(), Error> {
@@ -83,7 +84,7 @@ pub trait EpubWriter: BookWriter {
fn generate_epub(
&mut self,
- project : &Project<Cover, String>,
+ project : &Project<Cover, Html>,
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<String, Error>;
- fn load_project(
- &self,
- id : &Self::ProjId,
- ) -> Result<Project<Self::CovId, Vec<Self::DocId>>, Error>;
+ fn load_project(&self, id : &Self::ProjId) -> Result<Project<Self::CovId, Self::DocId>, Error>;
}
#[derive(Debug, Serialize, Deserialize)]
pub struct Chapter<I> {
pub title : Option<String>,
- pub content : I,
+ pub content : Vec<I>,
}
-impl<I> Chapter<Vec<I>> {
- fn load_and_render<T, L>(&self, loader : &L, typo : &T) -> Result<Chapter<String>, Error>
+impl<I> Chapter<I> {
+ fn load_and_render<T, L, O>(&self, loader : &L, typo : &T) -> Result<Chapter<O>, Error>
where
T : Typography + ?Sized,
L : Loader<DocId = I>,
+ O : Output,
{
let title = &self.title;
let content = &self.content;
@@ -61,10 +59,8 @@ impl<I> Chapter<Vec<I>> {
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::<Result<Vec<String>, Error>>()?
- .join("");
+ .collect::<Result<Vec<O>, Error>>()?;
Ok(Chapter {
title : title.clone(),
@@ -84,13 +80,14 @@ pub struct Project<C, I> {
pub language : Language,
}
-impl Project<Cover, String> {
+impl<O> Project<Cover, O> {
pub fn load_and_render<'input, L>(
id : &L::ProjId,
loader : &L,
- ) -> Result<Project<Cover, String>, Error>
+ ) -> Result<Project<Cover, O>, Error>
where
L : Loader,
+ O : Output,
{
let project = loader.load_project(id)?;
@@ -109,7 +106,7 @@ impl Project<Cover, String> {
.chapters
.into_iter()
.map(|chapter| chapter.load_and_render(loader, typo))
- .collect::<Result<Vec<Chapter<String>>, Error>>()
+ .collect::<Result<Vec<Chapter<O>>, 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<Cover, String>,
+ project : &Project<Cover, Html>,
tera : &Tera,
) -> Result<(), Error> {
let chaps : Vec<_> = project
@@ -88,7 +89,7 @@ impl Static {
fn generate_chapters(
&mut self,
tera : &Tera,
- chapters : &Vec<Chapter<String>>,
+ chapters : &Vec<Chapter<Html>>,
numbering : bool,
lang : &Language,
) -> Result<(), Error> {
@@ -119,7 +120,7 @@ impl Static {
pub fn generate_static_website(
&mut self,
- project : &Project<Cover, String>,
+ project : &Project<Cover, Html>,
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 @@
</h1>
{% endif %}
<div class="ogmarkup">
- {{ chapter.content }}
+ {% for content in chapter.content %}
+ {{ content | safe }}
+ {% endfor %}
</div>
</body>
</html>
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 @@
</h1>
<article class="ogmarkup">
- {{ chapter.content | safe }}
+ {% for content in chapter.content %}
+ {{ content | safe }}
+ {% endfor %}
</article>
{% if not body_only %}
</body>