|
| 1 | +use core::fmt; |
| 2 | +use std::path::{Path, PathBuf}; |
| 3 | + |
| 4 | +use clap::{Parser, Subcommand, ValueEnum}; |
| 5 | +use reflexo_typst::error_once; |
| 6 | + |
| 7 | +use crate::{utils::make_absolute, version::VersionFormat}; |
| 8 | + |
| 9 | +#[derive(Debug, Parser)] |
| 10 | +#[clap(name = "shiroa", version = "0.3.1-rc4")] |
| 11 | +pub struct Opts { |
| 12 | + /// Print Version |
| 13 | + #[arg(short = 'V', long, group = "version-dump")] |
| 14 | + pub version: bool, |
| 15 | + |
| 16 | + /// Print Version in format |
| 17 | + #[arg(long = "VV", alias = "version-fmt", group = "version-dump", default_value_t = VersionFormat::None)] |
| 18 | + pub vv: VersionFormat, |
| 19 | + |
| 20 | + /// Print Verbose Log |
| 21 | + #[arg(short = 'v', long, global = true)] |
| 22 | + pub verbose: bool, |
| 23 | + |
| 24 | + #[clap(subcommand)] |
| 25 | + pub sub: Option<Subcommands>, |
| 26 | +} |
| 27 | + |
| 28 | +#[derive(Debug, Subcommand)] |
| 29 | +#[clap( |
| 30 | + about = "The cli for shiroa.", |
| 31 | + after_help = "", |
| 32 | + next_display_order = None |
| 33 | +)] |
| 34 | +#[allow(clippy::large_enum_variant)] |
| 35 | +pub enum Subcommands { |
| 36 | + #[clap(about = "init book.")] |
| 37 | + Init(InitArgs), |
| 38 | + #[clap(about = "build book.")] |
| 39 | + Build(BuildArgs), |
| 40 | + #[clap(about = "serve book.")] |
| 41 | + Serve(ServeArgs), |
| 42 | +} |
| 43 | + |
| 44 | +/// Determine the approach to retrieving metadata of a book project. |
| 45 | +#[derive(ValueEnum, Debug, Clone, Copy, Eq, PartialEq, Default)] |
| 46 | +#[value(rename_all = "kebab-case")] |
| 47 | +pub enum MetaSource { |
| 48 | + /// Strictly retrieve the project's meta by label queries. |
| 49 | + /// - retrieve the book meta from `<shiroa-book-meta>` |
| 50 | + /// - retrieve the build meta from `<shiroa-build-meta>` |
| 51 | + Strict, |
| 52 | + /// Infer the project's meta from the outline of main file. |
| 53 | + /// Note: if the main file also contains `<shiroa-book-meta>` or |
| 54 | + /// `<shiroa-build-meta>`, the manual-set meta will be used first. |
| 55 | + #[default] |
| 56 | + Outline, |
| 57 | +} |
| 58 | + |
| 59 | +impl fmt::Display for MetaSource { |
| 60 | + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
| 61 | + f.write_str(self.to_possible_value().unwrap().get_name()) |
| 62 | + } |
| 63 | +} |
| 64 | + |
| 65 | +#[derive(ValueEnum, Debug, Clone, Copy, Eq, PartialEq, Default)] |
| 66 | +#[value(rename_all = "kebab-case")] |
| 67 | +pub enum RenderMode { |
| 68 | + /// Dynamically render as paged document. |
| 69 | + #[default] |
| 70 | + DynPaged, |
| 71 | + /// Statically render html parts as much as possible, and leave frames |
| 72 | + /// rendered dynamically. |
| 73 | + StaticHtmlDynPaged, |
| 74 | + /// Statically render the whole document, the embedded frames are not |
| 75 | + /// resizable. |
| 76 | + StaticHtml, |
| 77 | +} |
| 78 | + |
| 79 | +const ENV_PATH_SEP: char = if cfg!(windows) { ';' } else { ':' }; |
| 80 | + |
| 81 | +#[derive(Default, Debug, Clone, Parser)] |
| 82 | +#[clap(next_help_heading = "Compile options")] |
| 83 | +pub struct CompileArgs { |
| 84 | + /// The directory storing the `book.typ` file. |
| 85 | + /// (Defaults to the current directory when omitted) |
| 86 | + #[clap(default_value = "")] |
| 87 | + pub dir: String, |
| 88 | + |
| 89 | + /// Determine the approach to retrieving metadata of the book project. |
| 90 | + #[clap(long, default_value = "strict")] |
| 91 | + pub meta_source: MetaSource, |
| 92 | + |
| 93 | + /// The mode to render typst document. The dynamically rendering means that |
| 94 | + /// some elements will be rendered by a wasm module in the browser. |
| 95 | + #[clap(long, default_value = "dyn-paged")] |
| 96 | + pub mode: RenderMode, |
| 97 | + |
| 98 | + /// Deprecated: use `--root` instead. |
| 99 | + /// |
| 100 | + /// Root directory for the typst workspace, which is same as the |
| 101 | + /// `typst-cli`'s `--root` flag. (Defaults to the root directory for the |
| 102 | + /// book when omitted) |
| 103 | + #[clap(long, short, default_value = "")] |
| 104 | + pub workspace: String, |
| 105 | + |
| 106 | + /// Configure the project root (for absolute paths in typst source files). |
| 107 | + #[clap(long = "root", value_name = "DIR")] |
| 108 | + pub root: Option<String>, |
| 109 | + |
| 110 | + /// Add additional directories that are recursively searched for fonts. |
| 111 | + /// |
| 112 | + /// If multiple paths are specified, they are separated by the system's path |
| 113 | + /// separator (`:` on Unix-like systems and `;` on Windows). |
| 114 | + #[clap( |
| 115 | + long = "font-path", |
| 116 | + value_name = "DIR", |
| 117 | + action = clap::ArgAction::Append, |
| 118 | + env = "TYPST_FONT_PATHS", |
| 119 | + value_delimiter = ENV_PATH_SEP |
| 120 | + )] |
| 121 | + pub font_paths: Vec<PathBuf>, |
| 122 | + |
| 123 | + /// Output to directory, default in the same directory as the entry file. |
| 124 | + /// Relative paths are interpreted relative to the book's root directory. |
| 125 | + /// If omitted, shiroa uses #build-meta.build-dir from book.typ or defaults |
| 126 | + /// to `./dist`. |
| 127 | + #[clap(long, short, default_value = "")] |
| 128 | + pub dest_dir: String, |
| 129 | + |
| 130 | + /// Reset path to root in html files. |
| 131 | + #[clap(long, default_value = "/")] |
| 132 | + pub path_to_root: String, |
| 133 | + |
| 134 | + /// Specify a filter to only load files with a specific extension. |
| 135 | + #[clap(long, default_value = "^(player.bilibili.com)$")] |
| 136 | + pub allowed_url_source: Option<String>, |
| 137 | +} |
| 138 | + |
| 139 | +impl CompileArgs { |
| 140 | + pub fn compat(&mut self) { |
| 141 | + if !self.workspace.is_empty() { |
| 142 | + eprintln!("warning: the --workspace flag is deprecated, use --root instead"); |
| 143 | + } |
| 144 | + if let Some(root) = self.root.take() { |
| 145 | + self.workspace = root; |
| 146 | + } |
| 147 | + } |
| 148 | + |
| 149 | + pub fn canonicalize(&mut self) -> crate::error::Result<()> { |
| 150 | + if !self.path_to_root.starts_with('/') { |
| 151 | + self.path_to_root.insert(0, '/'); |
| 152 | + } |
| 153 | + |
| 154 | + if !self.path_to_root.ends_with('/') { |
| 155 | + self.path_to_root.push('/'); |
| 156 | + } |
| 157 | + |
| 158 | + make_absolute(Path::new(&self.dir)) |
| 159 | + .to_str() |
| 160 | + .unwrap() |
| 161 | + .clone_into(&mut self.dir); |
| 162 | + |
| 163 | + let dir = Path::new(&self.dir); |
| 164 | + if dir.is_file() { |
| 165 | + if self.meta_source == MetaSource::Strict { |
| 166 | + return Err(error_once!("project dir is a file", dir: dir.display())); |
| 167 | + } |
| 168 | + let w = dir.parent().unwrap().to_str().unwrap().to_owned(); |
| 169 | + self.dir = w; |
| 170 | + } |
| 171 | + |
| 172 | + if self.workspace.is_empty() { |
| 173 | + self.workspace.clone_from(&self.dir); |
| 174 | + } |
| 175 | + |
| 176 | + Ok(()) |
| 177 | + } |
| 178 | +} |
| 179 | + |
| 180 | +#[derive(Default, Debug, Clone, Parser)] |
| 181 | +#[clap(next_help_heading = "Init options")] |
| 182 | +pub struct InitArgs { |
| 183 | + /// arguments for compile setting. |
| 184 | + #[clap(flatten)] |
| 185 | + pub compile: CompileArgs, |
| 186 | +} |
| 187 | + |
| 188 | +#[derive(Default, Debug, Clone, Parser)] |
| 189 | +#[clap(next_help_heading = "Build options")] |
| 190 | +pub struct BuildArgs { |
| 191 | + /// arguments for compile setting. |
| 192 | + #[clap(flatten)] |
| 193 | + pub compile: CompileArgs, |
| 194 | +} |
| 195 | + |
| 196 | +#[derive(Default, Debug, Clone, Parser)] |
| 197 | +#[clap(next_help_heading = "Compile options")] |
| 198 | +pub struct ServeArgs { |
| 199 | + /// arguments for compile setting. |
| 200 | + #[clap(flatten)] |
| 201 | + pub compile: CompileArgs, |
| 202 | + |
| 203 | + /// Do not build the book before serving. |
| 204 | + #[clap(long)] |
| 205 | + pub no_build: bool, |
| 206 | + |
| 207 | + /// Listen address. |
| 208 | + #[clap(long, default_value = "127.0.0.1:25520")] |
| 209 | + pub addr: String, |
| 210 | +} |
0 commit comments