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
use std::fmt::{Display, Formatter};
use std::hash::Hash;

use anyhow::anyhow;
use clap::ValueEnum;
use serde::{Deserialize, Serialize};

use crate::loader::{Loader, MarkdownLoader, NotebookLoader};
use crate::parser::{Parser, ParserSettings};
use crate::processors::exercises::ExercisesConfig;
use crate::processors::shortcodes::ShortcodesConfig;
use crate::renderers::html::HtmlRenderer;
use crate::renderers::latex::LatexRenderer;
use crate::renderers::markdown::MarkdownRenderer;
use crate::renderers::notebook::NotebookRenderer;
use crate::renderers::Renderer;

#[derive(Hash, Clone, Copy, Eq, PartialEq, Serialize, Deserialize, Debug, ValueEnum)]
#[serde(rename_all = "lowercase")]
pub enum InputFormat {
    Markdown,
    Notebook,
}

#[derive(Hash, Clone, Copy, Eq, PartialEq, Serialize, Deserialize, Debug)]
#[serde(rename_all = "lowercase")]
pub enum OutputFormat {
    Notebook,
    Html,
    Info,
    Markdown,
    LaTeX,
}

impl InputFormat {
    pub fn loader(&self) -> Box<dyn Loader> {
        match self {
            InputFormat::Markdown => Box::new(MarkdownLoader),
            InputFormat::Notebook => Box::new(NotebookLoader),
        }
    }

    pub fn extension(&self) -> &str {
        match self {
            InputFormat::Markdown => "md",
            InputFormat::Notebook => "ipynb",
        }
    }

    pub fn name(&self) -> &str {
        match self {
            InputFormat::Markdown => "markdown",
            InputFormat::Notebook => "notebook",
        }
    }

    pub fn from_extension(ext: &str) -> Result<Self, anyhow::Error> {
        match ext {
            "md" => Ok(InputFormat::Markdown),
            "ipynb" => Ok(InputFormat::Notebook),
            _ => Err(anyhow!("Invalid extension for input")),
        }
    }

    pub fn from_name(name: &str) -> Result<Self, anyhow::Error> {
        match name {
            "markdown" => Ok(InputFormat::Markdown),
            "notebook" => Ok(InputFormat::Notebook),
            _ => Err(anyhow!("Invalid format name for input")),
        }
    }
}

impl OutputFormat {
    pub fn no_parse(&self) -> bool {
        match self {
            OutputFormat::Notebook => false,
            OutputFormat::Html => false,
            OutputFormat::Info => true,
            OutputFormat::LaTeX => false,
            OutputFormat::Markdown => false,
        }
    }

    // pub fn from_extension(ext: &str) -> Result<Self, anyhow::Error> {
    //     match ext {
    //         "ipynb" => Ok(OutputFormat::Notebook),
    //         "html" => Ok(OutputFormat::Html),
    //         _ => Err(anyhow!("Invalid extension for output")),
    //     }
    // }

    // pub fn from_name(name: &str) -> Result<Self, anyhow::Error> {
    //     match name {
    //         "notebook" => Ok(OutputFormat::Notebook),
    //         "html" => Ok(OutputFormat::Html),
    //         "info" => Ok(OutputFormat::Info),
    //         _ => Err(anyhow!("Invalid format name for output")),
    //     }
    // }

    pub fn extension(&self) -> &str {
        match self {
            OutputFormat::Notebook => "ipynb",
            OutputFormat::Html => "html",
            OutputFormat::Info => "yml",
            OutputFormat::LaTeX => "tex",
            OutputFormat::Markdown => "md",
        }
    }

    pub fn template_extension(&self) -> &str {
        match self {
            OutputFormat::Notebook => "md",
            OutputFormat::Html => "html",
            OutputFormat::Info => "yml",
            OutputFormat::LaTeX => "tex",
            OutputFormat::Markdown => "md",
        }
    }

    pub fn name(&self) -> &str {
        match self {
            OutputFormat::Notebook => "notebook",
            OutputFormat::Html => "html",
            OutputFormat::Info => "info",
            OutputFormat::LaTeX => "latex",
            OutputFormat::Markdown => "markdown",
        }
    }

    pub fn renderer(&self) -> Option<Box<dyn Renderer>> {
        match self {
            OutputFormat::Notebook => Some(Box::new(NotebookRenderer)),
            OutputFormat::Html => Some(Box::new(HtmlRenderer {
                interactive_cells: true,
            })),
            OutputFormat::Info => None,
            OutputFormat::LaTeX => Some(Box::new(LatexRenderer)),
            OutputFormat::Markdown => Some(Box::new(MarkdownRenderer)),
        }
    }
}

impl Display for InputFormat {
    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
        write!(f, "{}", self.name())
    }
}

impl Display for OutputFormat {
    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
        write!(f, "{}", self.name())
    }
}

#[allow(unused)]
fn get_default_parser(_format: OutputFormat) -> Parser {
    Parser {
        md_processors: vec![Box::new(ShortcodesConfig)],
        ast_processors: vec![Box::new(ExercisesConfig)],
        settings: ParserSettings {
            solutions: false,
            notebook_outputs: false,
        },
    }
}