novel_cli/cmd/
template.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
use std::path::PathBuf;

use clap::Args;
use color_eyre::eyre::Result;
use fluent_templates::Loader;
use image::ImageReader;

use super::Convert;
use crate::{
    renderer,
    utils::{self, Chapter, Content, Novel, Volume},
    LANG_ID, LOCALES,
};

#[must_use]
#[derive(Args)]
#[command(arg_required_else_help = true,
    about = LOCALES.lookup(&LANG_ID, "template_command"))]
pub struct Template {
    #[arg(help = LOCALES.lookup(&LANG_ID, "novel_name"))]
    pub novel_name: String,

    #[arg(long, help = LOCALES.lookup(&LANG_ID, "cover_image"))]
    pub cover_image: Option<PathBuf>,

    #[arg(short, long, value_enum, value_delimiter = ',',
        help = LOCALES.lookup(&LANG_ID, "converts"))]
    pub converts: Vec<Convert>,
}

pub fn execute(config: Template) -> Result<()> {
    let chapter = Chapter {
        id: 0,
        title: String::from("Chapter 1"),
        contents: Some(vec![
            Content::Text(String::from("◇◇◇")),
            Content::Text(String::from("![](001.webp)")),
        ]),
    };

    let volume = Volume {
        title: String::from("Volume 1"),
        chapters: vec![chapter],
    };

    let cover_image = if config.cover_image.is_some() {
        let image = ImageReader::open(config.cover_image.unwrap())?
            .with_guessed_format()?
            .decode()?;
        Some(image)
    } else {
        None
    };

    let mut novel = Novel {
        name: config.novel_name,
        author_name: String::from("TODO"),
        introduction: Some(vec![String::from("line 1"), String::from("line 2")]),
        cover_image,
        volumes: vec![volume],
    };

    utils::convert(&mut novel, &config.converts)?;

    renderer::generate_pandoc_markdown(novel, config.converts)?;

    Ok(())
}