aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorThomas Letan <contact@thomasletan.fr>2019-08-15 10:19:38 +0200
committerThomas Letan <contact@thomasletan.fr>2019-08-15 10:19:38 +0200
commit672c210db34ce94d01cc5282d09208afeb7c76ca (patch)
tree55e1d2a02eda04ba1c75565a2458843c677d8d91
parentfeature: Generate the epub archive (diff)
feature: Make chapters numbering optional
There is now an optional option `numbering' one can set in the Book.toml file. If set to true, numbers will appear consistently in the book (even if no title is set). If unset, only the book title, when it exists, will be set.
-rw-r--r--src/epub.rs10
-rw-r--r--src/project.rs3
-rw-r--r--templates/chapter.xhtml8
3 files changed, 19 insertions, 2 deletions
diff --git a/src/epub.rs b/src/epub.rs
index 785f497..a6f09e5 100644
--- a/src/epub.rs
+++ b/src/epub.rs
@@ -52,12 +52,18 @@ pub trait EpubWriter {
)
}
- fn create_chapters(&mut self, tera : &Tera, chapters : &Vec<Chapter<String>>) -> Result<(), Error> {
+ fn create_chapters(
+ &mut self,
+ tera : &Tera,
+ chapters : &Vec<Chapter<String>>,
+ numbering : bool
+ ) -> Result<(), Error> {
chapters.iter().enumerate()
.map(|(idx, c)| {
let mut ctx = Context::new();
ctx.insert("number", &(idx + 1));
ctx.insert("chapter", &c);
+ ctx.insert("numbering", &numbering);
let path : String = format!("{}.xhtml", idx);
@@ -106,7 +112,7 @@ pub trait EpubWriter {
self.create_mimetype()?;
self.create_container(&tera)?;
- self.create_chapters(&tera, &project.chapters)?;
+ self.create_chapters(&tera, &project.chapters, project.numbering.unwrap_or(false))?;
self.write_template(
&PathBuf::from("OEBPS/Style/main.css"),
diff --git a/src/project.rs b/src/project.rs
index c411587..da8cd35 100644
--- a/src/project.rs
+++ b/src/project.rs
@@ -57,6 +57,7 @@ pub struct Project<A> {
pub title: String,
pub chapters: Vec<Chapter<A>>,
pub cover: Option<PathBuf>,
+ pub numbering: Option<bool>,
}
impl Project<Vec<PathBuf>> {
@@ -105,6 +106,7 @@ impl Project<Vec<PathBuf>> {
where
T : Typography
{
+ let numbering = self.numbering;
let author = self.author;
let title = self.title;
let cover = self.cover
@@ -121,6 +123,7 @@ impl Project<Vec<PathBuf>> {
title: title,
chapters: x,
cover: cover,
+ numbering: numbering,
})
}
}
diff --git a/templates/chapter.xhtml b/templates/chapter.xhtml
index b9a77d0..18a33d6 100644
--- a/templates/chapter.xhtml
+++ b/templates/chapter.xhtml
@@ -10,10 +10,18 @@
<body>
{% if chapter.title %}
<h1>
+ {% if numbering %}
– {{ number }} –
<span class="title">
{% filter upper %}{{ chapter.title }}{% endfilter %}
</span>
+ {% else %}
+ {% filter upper %}{{ chapter.title }}{% endfilter %}
+ {% endif %}
+ </h1>
+ {% elif numbering %}
+ <h1>
+ – {{ number }} –
</h1>
{% endif %}
<div class="ogmarkup">