diff options
author | Thomas Letan <contact@thomasletan.fr> | 2019-08-04 17:48:17 +0200 |
---|---|---|
committer | Thomas Letan <contact@thomasletan.fr> | 2019-08-04 17:48:17 +0200 |
commit | 22c4e3081e67eaf841099760093467692a9e9c04 (patch) | |
tree | 22d82b794e3d6e6b5e43d5d4d6112cbb4479c4cd | |
parent | feature: Add fonts support and make assets relocatable (diff) |
feature: Generate a table of contents
-rw-r--r-- | Cargo.lock | 1 | ||||
-rw-r--r-- | Cargo.toml | 1 | ||||
-rw-r--r-- | src/epub.rs | 18 | ||||
-rw-r--r-- | src/main.rs | 1 | ||||
-rw-r--r-- | templates/chapter.xhtml | 7 | ||||
-rw-r--r-- | templates/content.opf | 4 | ||||
-rw-r--r-- | templates/main.css | 4 | ||||
-rw-r--r-- | templates/toc.ncx | 21 |
8 files changed, 54 insertions, 3 deletions
@@ -98,6 +98,7 @@ dependencies = [ "ogmarkup 0.1.0 (git+https://git.sr.ht/~lthms/ogmarkup/?rev=feaf8d0cbc7aa34ac521020914df91489ef24595)", "serde 1.0.92 (registry+https://github.com/rust-lang/crates.io-index)", "serde_derive 1.0.92 (registry+https://github.com/rust-lang/crates.io-index)", + "serde_json 1.0.40 (registry+https://github.com/rust-lang/crates.io-index)", "tera 0.11.20 (registry+https://github.com/rust-lang/crates.io-index)", "toml 0.5.1 (registry+https://github.com/rust-lang/crates.io-index)", ] @@ -8,6 +8,7 @@ toml = "0.5" clap = "2.33" serde = "1.0" serde_derive = "1.0" +serde_json = "1.0" tera = "0.11" [dependencies.ogmarkup] diff --git a/src/epub.rs b/src/epub.rs index 33afa09..10e5937 100644 --- a/src/epub.rs +++ b/src/epub.rs @@ -2,6 +2,7 @@ use crate::project::{Error, Project, Chapter}; use std::fs::{create_dir_all}; use std::path::{Path, PathBuf}; use tera::{Tera, Context}; +use serde_json::json; const EPUB_MIMETYPE: &'static str = "application/epub+zip"; @@ -46,6 +47,7 @@ fn create_chapters(tera : &Tera, chapters : &Vec<Chapter<String>>) -> Result<Vec chapters.iter().enumerate() .map(|(idx, c)| { let mut ctx = Context::new(); + ctx.insert("number", &(idx + 1)); ctx.insert("chapter", &c); let path : String = format!("{}.xhtml", idx); @@ -134,5 +136,21 @@ pub fn generate(project : &Project<String>, assets : &PathBuf) -> Result<(), Err &PathBuf::from("OEBPS/content.opf") )?; + let chaps: Vec<_> = project.chapters.iter().enumerate() + .map(|(idx, chapter)| json!({ + "index": idx, + "title": chapter.title, + })) + .collect(); + + let mut ctx = Context::new(); + ctx.insert("chapters", &chaps); + write_template_to( + &tera, + "toc.ncx", + &ctx, + &PathBuf::from("OEBPS/toc.ncx") + )?; + Ok(()) } diff --git a/src/main.rs b/src/main.rs index 06410d4..b3d0251 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,6 +1,7 @@ extern crate clap; extern crate ogmarkup; extern crate serde_derive; +extern crate serde_json; extern crate toml; #[macro_use] extern crate tera; diff --git a/templates/chapter.xhtml b/templates/chapter.xhtml index 8ced676..a0508ed 100644 --- a/templates/chapter.xhtml +++ b/templates/chapter.xhtml @@ -4,7 +4,12 @@ </head> <body> {% if chapter.title %} - <h1>{% filter upper %}{{ chapter.title }}{% endfilter %}</h1> + <h1> + – {{ number }} – + <div class="title"> + {% filter upper %}{{ chapter.title }}{% endfilter %} + </div> + </h1> {% endif %} <div class="ogmarkup"> {{ chapter.content }} diff --git a/templates/content.opf b/templates/content.opf index 4dcd262..823f9d6 100644 --- a/templates/content.opf +++ b/templates/content.opf @@ -17,9 +17,9 @@ <item href="Text/{{ f }}" id="{{ f }}" media-type="application/xhtml+xml" /> {% endfor %} </manifest> - <spine toc="ncxtoc"> + <spine toc="ncx"> {% for f in files %} <itemref idref="{{ f }}" /> {% endfor %} </spine> -</package>
\ No newline at end of file +</package> diff --git a/templates/main.css b/templates/main.css index eae7896..56c52d5 100644 --- a/templates/main.css +++ b/templates/main.css @@ -32,6 +32,10 @@ h1 { text-align: center; } +h1 .title { + font-size: 120%; +} + p { text-indent: .9rem; padding: 0; diff --git a/templates/toc.ncx b/templates/toc.ncx new file mode 100644 index 0000000..26a88f3 --- /dev/null +++ b/templates/toc.ncx @@ -0,0 +1,21 @@ +<?xml version="1.0" encoding="UTF-8"?> +<ncx version="2005-1" xmlns="http://www.daisy.org/z3986/2005/ncx/"> + <head> + <meta name="dtb:depth" content="1" /> + <meta name="dtb:totalPageCount" content="0" /> + <meta name="dtb:maxPageNumber" content="0" /> + </head> + <docTitle> + <text>Table des matières</text> + </docTitle> + <navMap> + {% for info in chapters %} + <navPoint class="h1" id="ch1"> + <navLabel> + <text>{{ info.index + 1 }}. {{ info.title }}</text> + </navLabel> + <content src="Text/{{ info.index }}.xhtml" /> + </navPoint> + {% endfor %} + </navMap> +</ncx> |