aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorThomas Letan <contact@thomasletan.fr>2020-02-08 14:20:00 +0100
committerThomas Letan <contact@thomasletan.fr>2020-02-08 14:20:00 +0100
commit12c20c643beda76d561e7c4258d26e358097bb30 (patch)
tree6dfe8ab1053c294b9dec82059fe550bfc4a52ecc
parentfeature: Make the generated static website more responsive (diff)
feature: Add options `output` and `body-only` to celtchar-static
The main idea behind these options is to make it possible to integrate a celtchar book inside a larger static website generated by other means (like Hakyll or soupault, for instance).
-rw-r--r--app/src/main.rs44
-rw-r--r--lib/src/wstatic.rs17
-rw-r--r--templates/static/chapter.html4
-rw-r--r--templates/static/index.html4
4 files changed, 53 insertions, 16 deletions
diff --git a/app/src/main.rs b/app/src/main.rs
index ba38fcd..1ae254d 100644
--- a/app/src/main.rs
+++ b/app/src/main.rs
@@ -7,9 +7,9 @@ extern crate toml;
use std::path::PathBuf;
-use clap::{App, SubCommand};
+use clap::{App, Arg, SubCommand};
-use libceltchar::{EpubWriter, Error, Loader, Project, Zip, Static};
+use libceltchar::{EpubWriter, Error, Loader, Project, Static, Zip};
#[cfg(debug_assertions)]
use libceltchar::Raise;
@@ -49,13 +49,13 @@ fn build_epub(assets : &PathBuf) -> Result<(), Error> {
Ok(())
}
-fn build_static(assets : &PathBuf) -> Result<(), Error> {
+fn build_static(assets : &PathBuf, body_only : bool, out : &PathBuf) -> Result<(), Error> {
let root = find_root()?;
let loader = Fs;
let project = Project::load_and_render(&root, &loader)?;
- let mut static_website = Static::init(&PathBuf::from("out"))?;
+ let mut static_website = Static::init(out, body_only)?;
static_website.generate_static_website(&project, assets)?;
Ok(())
@@ -78,19 +78,41 @@ fn main() -> Result<(), Error> {
.about("A tool to generate novels")
.subcommand(SubCommand::with_name("new").about("Create a new celtchar document"))
.subcommand(SubCommand::with_name("epub").about("Build a epub"))
- .subcommand(SubCommand::with_name("static").about("Build a static website"))
+ .subcommand(
+ SubCommand::with_name("static")
+ .about("Build a static website")
+ .arg(
+ Arg::with_name("body-only")
+ .help("Only output the bodies of the documents.")
+ .takes_value(false)
+ .short("b")
+ .long("body-only"),
+ )
+ .arg(
+ Arg::with_name("output")
+ .value_name("DIRECTORY")
+ .help("Output directory where the generated documents are saved")
+ .takes_value(true)
+ .short("o")
+ .long("output"),
+ ),
+ )
.subcommand(SubCommand::with_name("build").about("Build a celtchar document"))
.subcommand(SubCommand::with_name("deps").about("List dependencies of a celtchar document"))
.get_matches();
- let (subcommand, _args) = matches.subcommand();
-
let assets : PathBuf = get_assets()?;
- match subcommand {
- "epub" => build_epub(&assets)?,
- "static" => build_static(&assets)?,
- "deps" => deps()?,
+ match matches.subcommand() {
+ ("epub", _) => build_epub(&assets)?,
+ ("static",Some(args)) => {
+ let body_only = args.is_present("body-only");
+ let output_dir = PathBuf::from(
+ args.value_of("output").unwrap_or("out")
+ );
+ build_static(&assets, body_only, &output_dir)?
+ },
+ ("deps", _) => deps()?,
_ => unimplemented!(),
}
diff --git a/lib/src/wstatic.rs b/lib/src/wstatic.rs
index da0e8ac..a38e8f5 100644
--- a/lib/src/wstatic.rs
+++ b/lib/src/wstatic.rs
@@ -10,6 +10,7 @@ use crate::assets::template_dir;
pub struct Static {
base : PathBuf,
+ body_only : bool,
}
impl BookWriter for Static {
@@ -32,7 +33,7 @@ impl BookWriter for Static {
}
impl Static {
- pub fn init(base : &PathBuf) -> Result<Static, Error> {
+ pub fn init(base : &PathBuf, body_only : bool) -> Result<Static, Error> {
if !base.exists() {
create_dir(base).or_raise("Could not create output directory.")?;
}
@@ -40,6 +41,8 @@ impl Static {
if base.is_dir() {
Ok(Static {
base : base.to_owned()
+ base : base.to_owned(),
+ body_only : body_only,
})
} else {
Err(Error::new(&format!("{:?} already exists and is not a directory", base)))
@@ -67,6 +70,7 @@ impl Static {
ctx.insert("chapters", &chaps);
ctx.insert("language", &project.language);
ctx.insert("title", &project.title);
+ ctx.insert("body_only", &self.body_only);
self.write_template(
&PathBuf::from("index.html"),
@@ -96,6 +100,7 @@ impl Static {
ctx.insert("chapter", &c);
ctx.insert("numbering", &numbering);
ctx.insert("language", &lang);
+ ctx.insert("body_only", &self.body_only);
ctx.insert("chapters_number", &max);
let path : PathBuf = PathBuf::from(format!("{}.html", idx));
@@ -132,10 +137,12 @@ impl Static {
&project.language,
)?;
- self.write_file(
- &PathBuf::from("style.css"),
- &assets.join(PathBuf::from("templates/static/style.css")),
- )?;
+ if !self.body_only {
+ self.write_file(
+ &PathBuf::from("style.css"),
+ &assets.join(PathBuf::from("templates/static/style.css")),
+ )?;
+ }
Ok(())
}
diff --git a/templates/static/chapter.html b/templates/static/chapter.html
index 60d452c..2c1832f 100644
--- a/templates/static/chapter.html
+++ b/templates/static/chapter.html
@@ -1,3 +1,4 @@
+{% if not body_only %}
<!DOCTYPE html>
<html lang="{{ language | lower }}">
<head>
@@ -8,6 +9,7 @@
<link rel="stylesheet" href="style.css">
</head>
<body>
+{% endif %}
<nav>
{% if 1 < number %}
<a href="{{ number - 2 }}.html" class="nav_item">
@@ -62,5 +64,7 @@
<div class="nav_item"></div>
{% endif %}
</nav>
+{% if not body_only %}
</body>
</html>
+{% endif %}
diff --git a/templates/static/index.html b/templates/static/index.html
index 0542291..9c88832 100644
--- a/templates/static/index.html
+++ b/templates/static/index.html
@@ -1,3 +1,4 @@
+{% if not body_only %}
<!DOCTYPE html>
<html lang="{{ language | lower }}">
<head>
@@ -7,11 +8,14 @@
<link rel="stylesheet" href="style.css">
</head>
<body>
+{% endif %}
<h1>{{ title | upper }}</h1>
<ul>
{% for info in chapters %}
<li><a href="{{info.index}}.html">{{ info.index + 1 }}. {{ info.title }}</a></li>
{% endfor %}
</ul>
+{% if not body_only %}
</body>
</html>
+{% endif %}