use std::{
fmt::{self, Display, Formatter},
path::Path,
str::FromStr,
};
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub enum Theme {
#[default]
Auto,
Light,
Dark,
}
impl Theme {
#[inline]
pub const fn attribute_value(self) -> Option<&'static str> {
match self {
Self::Auto => None,
Self::Light => Some("light"),
Self::Dark => Some("dark"),
}
}
}
impl Display for Theme {
#[inline]
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
f.write_str(match self {
Self::Auto => "auto",
Self::Light => "light",
Self::Dark => "dark",
})
}
}
impl FromStr for Theme {
type Err = UnknownTheme;
#[inline]
fn from_str(s: &str) -> Result<Self, Self::Err> {
match s {
"auto" => Ok(Self::Auto),
"light" => Ok(Self::Light),
"dark" => Ok(Self::Dark),
_ => Err(UnknownTheme),
}
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct UnknownTheme;
impl Display for UnknownTheme {
#[inline]
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
f.write_str("the theme should be one of `auto`, `light` and `dark`")
}
}
impl std::error::Error for UnknownTheme {}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub enum MathMode {
MathJaxEmbedded,
MathJaxClient,
KatexEmbedded,
#[default]
KatexClient,
}
impl MathMode {
#[inline]
pub const fn is_katex(self) -> bool {
matches!(self, Self::KatexEmbedded | Self::KatexClient)
}
}
impl Display for MathMode {
#[inline]
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
f.write_str(match self {
Self::MathJaxEmbedded => "mathjax-embedded",
Self::MathJaxClient => "mathjax-client",
Self::KatexEmbedded => "katex-embedded",
Self::KatexClient => "katex-client",
})
}
}
impl FromStr for MathMode {
type Err = UnknownMathMode;
#[inline]
fn from_str(s: &str) -> Result<Self, Self::Err> {
match s {
"mathjax-embedded" => Ok(Self::MathJaxEmbedded),
"mathjax-client" => Ok(Self::MathJaxClient),
"katex-embedded" => Ok(Self::KatexEmbedded),
"katex-client" => Ok(Self::KatexClient),
_ => Err(UnknownMathMode),
}
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct UnknownMathMode;
impl Display for UnknownMathMode {
#[inline]
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
f.write_str(
"the math mode should be one of `mathjax-embedded`, `mathjax-client`, \
`katex-embedded` and `katex-client`",
)
}
}
impl std::error::Error for UnknownMathMode {}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub enum MermaidMode {
Embedded,
#[default]
Client,
}
impl Display for MermaidMode {
#[inline]
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
f.write_str(match self {
Self::Embedded => "embedded",
Self::Client => "client",
})
}
}
impl FromStr for MermaidMode {
type Err = UnknownMermaidMode;
#[inline]
fn from_str(s: &str) -> Result<Self, Self::Err> {
match s {
"embedded" => Ok(Self::Embedded),
"client" => Ok(Self::Client),
_ => Err(UnknownMermaidMode),
}
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct UnknownMermaidMode;
impl Display for UnknownMermaidMode {
#[inline]
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
f.write_str("the Mermaid mode should be either `embedded` or `client`")
}
}
impl std::error::Error for UnknownMermaidMode {}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub enum HighlightLanguages<'a> {
#[default]
BuiltIn,
Any,
Only(&'a [&'a str]),
}
#[derive(Debug, Clone)]
pub struct ConvertOptions<'a> {
pub title: Option<&'a str>,
pub default_title: Option<&'a str>,
pub lang: &'a str,
pub theme: Theme,
pub allow_unsafe: bool,
pub hardbreaks: bool,
pub highlight: bool,
pub highlight_languages: HighlightLanguages<'a>,
pub math: Option<MathMode>,
pub mermaid: Option<MermaidMode>,
pub cjk_fonts: bool,
pub minify: bool,
pub embed_images: bool,
pub base_path: Option<&'a Path>,
pub css: Option<&'a str>,
pub extra_css: Option<&'a str>,
pub highlight_js: Option<&'a str>,
pub highlight_css: Option<&'a str>,
pub mathjax_js: Option<&'a str>,
pub katex_js: Option<&'a str>,
pub katex_css: Option<&'a str>,
pub mermaid_js: Option<&'a str>,
}
impl Default for ConvertOptions<'_> {
#[inline]
fn default() -> Self {
Self {
title: None,
default_title: None,
lang: "en",
theme: Theme::default(),
allow_unsafe: false,
hardbreaks: true,
highlight: true,
highlight_languages: HighlightLanguages::default(),
math: Some(MathMode::default()),
mermaid: Some(MermaidMode::default()),
cjk_fonts: true,
minify: true,
embed_images: false,
base_path: None,
css: None,
extra_css: None,
highlight_js: None,
highlight_css: None,
mathjax_js: None,
katex_js: None,
katex_css: None,
mermaid_js: None,
}
}
}