mirror of
https://github.com/lise-henry/crowbook
synced 2024-05-23 21:36:15 +02:00
Fix latex template
This commit is contained in:
parent
de2d9fd8fa
commit
1596c2b4f4
|
@ -1281,7 +1281,7 @@ impl<'a> Book<'a> {
|
||||||
m.insert("crowbook_version".into(), env!("CARGO_PKG_VERSION").into());
|
m.insert("crowbook_version".into(), env!("CARGO_PKG_VERSION").into());
|
||||||
m.insert(format!("lang_{}", self.options.get_str("lang").unwrap()), true.into());
|
m.insert(format!("lang_{}", self.options.get_str("lang").unwrap()), true.into());
|
||||||
|
|
||||||
// Add metadata to mapbuilder
|
// Add metadata to map
|
||||||
for key in self.options.get_metadata() {
|
for key in self.options.get_metadata() {
|
||||||
if let Ok(s) = self.options.get_str(key) {
|
if let Ok(s) = self.options.get_str(key) {
|
||||||
let key = key.replace('.', "_");
|
let key = key.replace('.', "_");
|
||||||
|
@ -1300,6 +1300,7 @@ impl<'a> Book<'a> {
|
||||||
|
|
||||||
m.insert(format!("has_{key}"), true.into());
|
m.insert(format!("has_{key}"), true.into());
|
||||||
} else {
|
} else {
|
||||||
|
m.insert(key.clone(), "".into());
|
||||||
m.insert(format!("has_{key}"), false.into());
|
m.insert(format!("has_{key}"), false.into());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1316,6 +1317,7 @@ impl<'a> Book<'a> {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
|
m.insert(key.clone(), "".into());
|
||||||
m.insert(format!("has_{key}"), false.into());
|
m.insert(format!("has_{key}"), false.into());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -211,11 +211,16 @@ impl<'a> LatexRenderer<'a> {
|
||||||
});
|
});
|
||||||
|
|
||||||
let template_src = self.book.get_template("tex.template")?;
|
let template_src = self.book.get_template("tex.template")?;
|
||||||
let template = self.book.compile_str(
|
|
||||||
template_src.as_ref(),
|
// We have to use a different template engine because we need different syntax
|
||||||
&self.book.source,
|
let syntax = upon::Syntax::builder()
|
||||||
"tex.template",
|
.expr("<<", ">>")
|
||||||
)?;
|
.block("<#", "#>")
|
||||||
|
.comment("<%", "%>")
|
||||||
|
.build();
|
||||||
|
let mut engine = upon::Engine::with_syntax(syntax);
|
||||||
|
engine.add_template("tex.template", template_src)?;
|
||||||
|
let template = engine.get_template("tex.template").unwrap();
|
||||||
let mut data = self
|
let mut data = self
|
||||||
.book
|
.book
|
||||||
.get_metadata(|s| self.render_vec(&Parser::new().parse_inline(s)?))?;
|
.get_metadata(|s| self.render_vec(&Parser::new().parse_inline(s)?))?;
|
||||||
|
@ -231,20 +236,22 @@ impl<'a> LatexRenderer<'a> {
|
||||||
data.insert("use_images".into(), self.book.features.image.into());
|
data.insert("use_images".into(), self.book.features.image.into());
|
||||||
data.insert("use_strikethrough".into(), self.book.features.strikethrough.into());
|
data.insert("use_strikethrough".into(), self.book.features.strikethrough.into());
|
||||||
data.insert("tex_lang".into(), tex_lang.into());
|
data.insert("tex_lang".into(), tex_lang.into());
|
||||||
if let Ok(tex_tmpl_add) = self.book.options.get_str("tex.template.add") {
|
let tex_tmpl_add = self.book.options.get_str("tex.template.add").unwrap_or("".into());
|
||||||
data.insert("additional_code".into(), tex_tmpl_add.into());
|
data.insert("additional_code".into(), tex_tmpl_add.into());
|
||||||
}
|
|
||||||
if let Ok(tex_font_size) = self.book.options.get_i32("tex.font.size") {
|
if let Ok(tex_font_size) = self.book.options.get_i32("tex.font.size") {
|
||||||
data.insert("has_tex_size".into(), true.into());
|
data.insert("has_tex_size".into(), true.into());
|
||||||
data.insert("tex_size".into(), format!("{tex_font_size}").into());
|
data.insert("tex_size".into(), format!("{tex_font_size}").into());
|
||||||
|
} else {
|
||||||
|
data.insert("has_tex_size".into(), false.into());
|
||||||
}
|
}
|
||||||
|
|
||||||
// If class isn't book, set open_any to true, so margins are symetric.
|
// If class isn't book, set open_any to true, so margins are symetric.
|
||||||
let mut book = false;
|
let mut book = false;
|
||||||
if self.book.options.get_str("tex.class").unwrap() == "book" {
|
if self.book.options.get_str("tex.class").unwrap() == "book" {
|
||||||
data.insert("book".into(), true.into());
|
|
||||||
book = true;
|
book = true;
|
||||||
}
|
}
|
||||||
|
data.insert("book".into(), book.into());
|
||||||
data.insert(
|
data.insert(
|
||||||
"margin_left".into(),
|
"margin_left".into(),
|
||||||
self.book
|
self.book
|
||||||
|
@ -268,20 +275,20 @@ impl<'a> LatexRenderer<'a> {
|
||||||
self.book.options.get_str("tex.margin.top").unwrap().into(),
|
self.book.options.get_str("tex.margin.top").unwrap().into(),
|
||||||
);
|
);
|
||||||
|
|
||||||
if let Ok(chapter_name) = self.book.options.get_str("rendering.chapter") {
|
let chapter_name = self.book.options.get_str("rendering.chapter").unwrap_or("".into());
|
||||||
data.insert("chapter_name".into(), chapter_name.into());
|
data.insert("chapter_name".into(), chapter_name.into());
|
||||||
}
|
|
||||||
if let Ok(part_name) = self.book.options.get_str("rendering.part") {
|
let part_name = self.book.options.get_str("rendering.part").unwrap_or("".into());
|
||||||
data.insert("part_name".into(), part_name.into());
|
data.insert("part_name".into(), part_name.into());
|
||||||
}
|
data.insert("initials".into(), self.book.options.get_bool("rendering.initials").unwrap().into());
|
||||||
if self.book.options.get_bool("rendering.initials") == Ok(true) {
|
|
||||||
data.insert("initials".into(), true.into());
|
|
||||||
}
|
|
||||||
// Insert xelatex if tex.command is set to xelatex or tectonic
|
// Insert xelatex if tex.command is set to xelatex or tectonic
|
||||||
if (self.book.options.get_str("tex.command") == Ok("xelatex"))
|
if (self.book.options.get_str("tex.command") == Ok("xelatex"))
|
||||||
| (self.book.options.get_str("tex.command") == Ok("tectonic"))
|
| (self.book.options.get_str("tex.command") == Ok("tectonic"))
|
||||||
{
|
{
|
||||||
data.insert("xelatex".into(), true.into());
|
data.insert("xelatex".into(), true.into());
|
||||||
|
} else
|
||||||
|
{
|
||||||
|
data.insert("xelatex".into(), false.into());
|
||||||
}
|
}
|
||||||
Ok(template.render(&data).to_string()?)
|
Ok(template.render(&data).to_string()?)
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,7 +1,6 @@
|
||||||
{{=<< >>=}} % Use <<foo>> to include (non HTML-escape) variable foo instead of {{{foo}}}
|
|
||||||
\documentclass<# if has_tex_size #>[<<tex_size>>pt]<# endif #>{<<class>>}
|
\documentclass<# if has_tex_size #>[<<tex_size>>pt]<# endif #>{<<class>>}
|
||||||
|
|
||||||
%% Pacake inclusion
|
%% Package inclusion
|
||||||
<# if xelatex #>
|
<# if xelatex #>
|
||||||
% Unicode support if xelatex is used
|
% Unicode support if xelatex is used
|
||||||
\usepackage{fontspec}
|
\usepackage{fontspec}
|
||||||
|
@ -117,8 +116,8 @@
|
||||||
\null\vfill
|
\null\vfill
|
||||||
\noindent
|
\noindent
|
||||||
\begin{center}
|
\begin{center}
|
||||||
{\emph{\@title}<#if has_version>>{, <<version>>}<# endif #><# if has_author #>, © \@author<# endif #>.\\[5mm]}
|
{\emph{\@title}<#if has_version #>{, <<version>>}<# endif #><# if has_author #>, © \@author<# endif #>.\\[5mm]}
|
||||||
<# if has_license>>{<<license>>\\[5mm]}<# endif #>
|
<# if has_license #>{<<license>>\\[5mm]}<# endif #>
|
||||||
\end{center}
|
\end{center}
|
||||||
\pagebreak
|
\pagebreak
|
||||||
\newpage
|
\newpage
|
||||||
|
|
Loading…
Reference in New Issue