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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
use std::path::PathBuf;

use ahash::AHashSet;
use clap::Args;
use color_eyre::eyre::{ensure, Result};
use fluent_templates::Loader;
use novel_api::Timing;
use parking_lot::RwLock;
use pulldown_cmark::{Event, HeadingLevel, Options, Parser, Tag};
use rayon::prelude::*;
use tracing::info;

use crate::{
    utils::{self, CurrentDir},
    LANG_ID, LOCALES,
};

#[must_use]
#[derive(Args)]
#[command(arg_required_else_help = true,
    about = LOCALES.lookup(&LANG_ID, "check_command").unwrap())]
pub struct Check {
    #[arg(help = LOCALES.lookup(&LANG_ID, "markdown_path").unwrap())]
    pub markdown_path: PathBuf,
}

pub fn execute(config: Check) -> Result<()> {
    let mut timing = Timing::new();

    let (meta_data, markdown) = utils::read_markdown(&config.markdown_path)?;

    let markdown_path = dunce::canonicalize(&config.markdown_path)?;
    let current_dir = CurrentDir::new(markdown_path.parent().unwrap())?;

    ensure!(
        meta_data.lang_is_ok(),
        "The lang field must be zh-Hant or zh-Hans: `{}`",
        meta_data.lang
    );
    ensure!(
        meta_data.cover_image_is_ok(),
        "Cover image does not exist: `{}`",
        meta_data.cover_image.unwrap().display()
    );

    let parser = Parser::new_ext(&markdown, Options::empty());
    let events = parser.into_offset_iter().collect::<Vec<(_, _)>>();
    let char_set = RwLock::new(AHashSet::new());

    // TODO i18n output
    events
        .into_par_iter()
        .for_each(|(event, range)| match event {
            Event::Start(tag) => match tag {
                Tag::Heading(heading_level, _, _) => {
                    let title = markdown[range].trim_start_matches('#').trim();

                    if heading_level == HeadingLevel::H1 && !check_volume_title(title) {
                        println(format!("Irregular volume title format: `{title}`"));
                    } else if heading_level == HeadingLevel::H2 && !check_chapter_title(title) {
                        println(format!("Irregular chapter title format: `{title}`"));
                    }
                }
                Tag::Image(_, path, _) => {
                    let image_path = PathBuf::from(&path.to_string());

                    if !image_path.is_file() {
                        println(format!("Image `{}` does not exist", image_path.display()));
                    }
                }
                Tag::Paragraph => (),
                Tag::BlockQuote
                | Tag::CodeBlock(_)
                | Tag::List(_)
                | Tag::Item
                | Tag::FootnoteDefinition(_)
                | Tag::Table(_)
                | Tag::TableHead
                | Tag::TableRow
                | Tag::TableCell
                | Tag::Emphasis
                | Tag::Strong
                | Tag::Strikethrough
                | Tag::Link(_, _, _) => {
                    let content = &markdown[range].trim();

                    println(format!(
                        "Markdown tag that should not appear: `{tag:?}`, content: `{content}`"
                    ));
                }
            },
            Event::Text(text) => {
                for c in text.chars() {
                    if !utils::is_cjk(c)
                        && !utils::is_punctuation(c)
                        && !c.is_ascii_alphanumeric()
                        && c != ' '
                    {
                        if char_set.read().contains(&c) {
                            continue;
                        } else {
                            char_set.write().insert(c);

                            println(format!(
                                "Irregular char: `{}`, at `{}`",
                                c,
                                markdown[range.clone()].trim()
                            ));
                        }
                    }
                }
            }
            Event::End(_) => (),
            Event::HardBreak => (),
            Event::Code(_)
            | Event::Html(_)
            | Event::FootnoteReference(_)
            | Event::SoftBreak
            | Event::Rule
            | Event::TaskListMarker(_) => {
                let content = &markdown[range].trim();

                println(format!(
                    "Markdown event that should not appear: `{event:?}`, content: `{content}`"
                ));
            }
        });

    current_dir.restore()?;

    info!("Time spent on `check`: {}", timing.elapsed()?);

    Ok(())
}

#[inline]
fn println(msg: String) {
    println!("{} {}", utils::emoji("⚠️"), msg);
}

macro_rules! regex {
    ($re:literal $(,)?) => {{
        static RE: once_cell::sync::OnceCell<regex::Regex> = once_cell::sync::OnceCell::new();
        RE.get_or_init(|| regex::Regex::new($re).unwrap())
    }};
}

#[must_use]
#[inline]
fn check_chapter_title<T>(title: T) -> bool
where
    T: AsRef<str>,
{
    let regex = regex!(r"第([零一二三四五六七八九十百千]|[0-9]){1,7}[章话] .+");
    regex.is_match(title.as_ref())
}

#[must_use]
#[inline]
fn check_volume_title<T>(title: T) -> bool
where
    T: AsRef<str>,
{
    let regex = regex!(r"第([一二三四五六七八九十]|[0-9]){1,3}卷 .+");
    regex.is_match(title.as_ref())
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn check_chapter_title_test() {
        assert!(check_chapter_title("第一章 被俘虏的开始"));
        assert!(check_chapter_title("第一百三十二章 标标标标标标标标标"));
        assert!(check_chapter_title("第123章 标题标标标标"));
        assert!(!check_chapter_title("第一章 "));
        assert!(!check_chapter_title("第1二3话"));
        assert!(!check_chapter_title("第123话标题"));
        assert!(!check_chapter_title("123话 标题"));
    }

    #[test]
    fn check_volume_title_test() {
        assert!(check_volume_title("第三十二卷 标标标标标标标标标"));
        assert!(!check_volume_title("第123话 标题标标标标"));
        assert!(!check_volume_title("第1卷 "));
    }
}