mirror of
https://github.com/lise-henry/crowbook
synced 2024-05-28 05:46:23 +02:00
added support for chapter whose title is hidden
This commit is contained in:
parent
3446801baa
commit
c6455dd98c
|
@ -16,9 +16,10 @@ use std::borrow::Cow;
|
|||
use mustache;
|
||||
use mustache::MapBuilder;
|
||||
|
||||
// Numbering for a given chapter
|
||||
/// Numbering for a given chapter
|
||||
#[derive(Debug, PartialEq, Clone, Copy)]
|
||||
pub enum Number {
|
||||
Hidden, // chapter's title is hidden
|
||||
Unnumbered, // chapter is not numbered
|
||||
Default, // chapter follows books numbering, number is given automatically
|
||||
Specified(i32), //chapter number set to specified number
|
||||
|
@ -202,6 +203,10 @@ impl Book {
|
|||
//nunmbered chapter
|
||||
let file = try!(get_filename(line));
|
||||
try!(self.add_chapter(Number::Default, file));
|
||||
} else if line.starts_with('!') {
|
||||
// hidden chapter
|
||||
let file = try!(get_filename(line));
|
||||
try!(self.add_chapter(Number::Hidden, file));
|
||||
} else if line.starts_with(|c: char| c.is_digit(10)) {
|
||||
// chapter with specific number
|
||||
let parts:Vec<_> = line.splitn(2, |c: char| c == '.' || c == ':' || c == '+').collect();
|
||||
|
|
|
@ -37,6 +37,7 @@ use std::fs::File;
|
|||
pub struct EpubRenderer<'a> {
|
||||
book: &'a Book,
|
||||
current_numbering: bool,
|
||||
current_hide: bool,
|
||||
current_chapter: i32,
|
||||
toc: Vec<String>,
|
||||
html: HtmlRenderer<'a>,
|
||||
|
@ -45,12 +46,13 @@ pub struct EpubRenderer<'a> {
|
|||
impl<'a> EpubRenderer<'a> {
|
||||
/// Creates a new Epub renderer
|
||||
pub fn new(book: &'a Book) -> EpubRenderer<'a> {
|
||||
EpubRenderer {
|
||||
EpubRenderer {
|
||||
book: book,
|
||||
html: HtmlRenderer::new(book),
|
||||
current_numbering: book.numbering,
|
||||
current_chapter: 1,
|
||||
toc: vec!(),
|
||||
current_hide: false,
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -63,12 +65,17 @@ impl<'a> EpubRenderer<'a> {
|
|||
|
||||
// Write chapters
|
||||
for (i, &(n, ref v)) in self.book.chapters.iter().enumerate() {
|
||||
self.current_hide = false;
|
||||
match n {
|
||||
Number::Unnumbered => self.current_numbering = false,
|
||||
Number::Default => self.current_numbering = self.book.numbering,
|
||||
Number::Specified(n) => {
|
||||
self.current_numbering = self.book.numbering;
|
||||
self.current_chapter = n;
|
||||
},
|
||||
Number::Hidden => {
|
||||
self.current_numbering = false;
|
||||
self.current_hide = true;
|
||||
}
|
||||
}
|
||||
let chapter = try!(self.render_chapter(v));
|
||||
|
@ -314,6 +321,15 @@ impl<'a> EpubRenderer<'a> {
|
|||
fn parse_token(&mut self, token: &Token, title: &mut String) -> String {
|
||||
match *token {
|
||||
Token::Header(n, ref vec) => {
|
||||
if n == 1 && self.current_hide {
|
||||
if title.is_empty() {
|
||||
*title = self.html.render_vec(vec);
|
||||
} else {
|
||||
println!("Warning: detected two chapter titles inside the same markdown file...");
|
||||
println!("...in a file where chapter titles are not even rendered.");
|
||||
}
|
||||
return String::new();
|
||||
}
|
||||
let s = if n == 1 && self.current_numbering {
|
||||
let chapter = self.current_chapter;
|
||||
self.current_chapter += 1;
|
||||
|
|
|
@ -28,7 +28,8 @@ use mustache;
|
|||
pub struct HtmlRenderer<'a> {
|
||||
current_chapter: i32,
|
||||
book: &'a Book,
|
||||
current_numbering: bool
|
||||
current_numbering: bool,
|
||||
current_hide: bool,
|
||||
}
|
||||
|
||||
impl<'a> HtmlRenderer<'a> {
|
||||
|
@ -38,6 +39,7 @@ impl<'a> HtmlRenderer<'a> {
|
|||
book: book,
|
||||
current_chapter: 1,
|
||||
current_numbering: book.numbering,
|
||||
current_hide: false
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -46,13 +48,18 @@ impl<'a> HtmlRenderer<'a> {
|
|||
let mut content = String::new();
|
||||
|
||||
for &(n, ref v) in &self.book.chapters {
|
||||
self.current_hide = false;
|
||||
match n {
|
||||
Number::Unnumbered => self.current_numbering = false,
|
||||
Number::Default => self.current_numbering = self.book.numbering,
|
||||
Number::Specified(n) => {
|
||||
self.current_numbering = self.book.numbering;
|
||||
self.current_chapter = n;
|
||||
}
|
||||
},
|
||||
Number::Hidden => {
|
||||
self.current_numbering = false;
|
||||
self.current_hide = true;
|
||||
},
|
||||
}
|
||||
for token in v {
|
||||
content.push_str(&self.parse_token(token));
|
||||
|
@ -91,6 +98,9 @@ impl<'a> HtmlRenderer<'a> {
|
|||
Token::Str(ref text) => escape_html(&*text),
|
||||
Token::Paragraph(ref vec) => format!("<p>{}</p>\n", self.render_vec(vec)),
|
||||
Token::Header(n, ref vec) => {
|
||||
if n == 1 && self.current_hide {
|
||||
return String::new();
|
||||
}
|
||||
let s = if n == 1 && self.current_numbering {
|
||||
let chapter = self.current_chapter;
|
||||
self.current_chapter += 1;
|
||||
|
|
|
@ -104,6 +104,9 @@ impl<'a> LatexRenderer<'a> {
|
|||
self.render_vec(vec, escape)),
|
||||
Token::Header(n, ref vec) => {
|
||||
let mut content = String::new();
|
||||
if n == 1 && self.current_chapter == Number::Hidden {
|
||||
return String::new();
|
||||
}
|
||||
if n == 1 {
|
||||
if let Number::Specified(n) = self.current_chapter {
|
||||
content.push_str(r"\setcounter{chapter}{");
|
||||
|
|
|
@ -41,5 +41,5 @@ pub mod epub3 {
|
|||
pub static COVER:&'static str = include_str!("../../templates/epub3/cover.xhtml");
|
||||
pub static NAV:&'static str = include_str!("../../templates/epub3/nav.xhtml");
|
||||
pub static OPF:&'static str = include_str!("../../templates/epub3/content.opf");
|
||||
pub static TITLE:&'static str = include_str!("../../templates/epub3</titlepage.xhtml");
|
||||
pub static TITLE:&'static str = include_str!("../../templates/epub3/titlepage.xhtml");
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue