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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
use anyhow::Result;
use std::{
    fs,
    fs::{File, OpenOptions},
    io::{Read, Write},
};

use crate::{
    assets::{CSS, HIGHLIGHT_CSS, HIGHLIGHT_JS, JS, LANDING, LIVERELOAD_JS},
    config::UnveilConfig,
    helper,
};

use crate::{
    assets::{
        CLIPBOARD_JS,
        FONT_AWESOME,
        FONT_AWESOME_EOT,
        FONT_AWESOME_EOT_900,
        FONT_AWESOME_EOT_BRANDS,
        FONT_AWESOME_SVG,
        FONT_AWESOME_SVG_900,
        FONT_AWESOME_SVG_BRANDS,
        FONT_AWESOME_TTF,
        FONT_AWESOME_TTF_900,
        FONT_AWESOME_TTF_BRANDS,
        FONT_AWESOME_WOFF,
        FONT_AWESOME_WOFF2,
        FONT_AWESOME_WOFF2_900,
        FONT_AWESOME_WOFF2_BRANDS,
        FONT_AWESOME_WOFF_900,
        FONT_AWESOME_WOFF_BRANDS,
    },
    html::HtmlBuilder,
    server::Server,
};
use std::path::PathBuf;

pub struct UnveilProject {
    pub root: PathBuf,
    pub markdown: Vec<String>,
    pub livereload: bool,
}

impl Default for UnveilProject {
    fn default() -> Self {
        UnveilProject {
            // TODO: handle this correctly and use it!
            root: PathBuf::from("."),
            markdown: vec![],
            livereload: true,
        }
    }
}

impl UnveilProject {
    /// get markdowns slides as strings
    fn get_markdown_from_file() -> Result<Vec<String>> {
        let mut markdown_contents = vec![];
        let config = UnveilConfig::from_disk("unveil.toml")?;

        // Read slide names from config and lookup the corresponding slide in the
        // slides directory, this allow to order slides rendering
        for slide_name in config.slides.iter() {
            let path = PathBuf::from(&format!("slides/{}", slide_name));
            let mut file = File::open(path)?;
            let mut contents = String::new();
            file.read_to_string(&mut contents)?;
            markdown_contents.push(contents);
        }
        Ok(markdown_contents)
    }

    /// Build a assets file from the markdown content located in `slides/`
    pub fn build(&mut self) -> Result<()> {
        // Generate html from markdown files in
        let markdowns = UnveilProject::get_markdown_from_file()?;
        let mut processor = HtmlBuilder::new(markdowns, self.livereload);

        let (user_css, html) = processor.build()?;
        let public = PathBuf::from("public");

        // Double check we are actually in an unveil project
        let config = UnveilConfig::from_disk("unveil.toml")?;

        // User has remove gitignore and we now need to recreate it
        if config.gitignore {
            helper::fs::write_file(".gitignore", b"public")?;
        }

        if !public.exists() {
            std::fs::create_dir("public")?;
        }

        helper::fs::replace("public/index.html", html.as_bytes())?;
        helper::fs::write_file("public/unveil.js", JS)?;

        if let Some(css) = user_css {
            helper::fs::replace("public/user_css.css", css.as_bytes())?;
        }

        helper::fs::write_file("public/highlight.css", HIGHLIGHT_CSS)?;
        helper::fs::write_file("public/livereload.js", LIVERELOAD_JS)?;
        helper::fs::write_file("public/clipboard.js", CLIPBOARD_JS)?;
        helper::fs::write_file("public/highlight.js", HIGHLIGHT_JS)?;

        helper::fs::create_dir("public/fontawesome");
        helper::fs::create_dir("public/fontawesome/webfonts");
        helper::fs::create_dir("public/fontawesome/css");

        helper::fs::write_file(
            "public/fontawesome/webfonts/fa-regular-400.eot",
            FONT_AWESOME_EOT,
        )?;
        helper::fs::write_file(
            "public/fontawesome/webfonts/fa-regular-400.svg",
            FONT_AWESOME_SVG,
        )?;
        helper::fs::write_file(
            "public/fontawesome/webfonts/fa-regular-400.ttf",
            FONT_AWESOME_TTF,
        )?;
        helper::fs::write_file(
            "public/fontawesome/webfonts/fa-regular-400.woff",
            FONT_AWESOME_WOFF,
        )?;
        helper::fs::write_file(
            "public/fontawesome/webfonts/fa-regular-400.woff2",
            FONT_AWESOME_WOFF2,
        )?;

        helper::fs::write_file(
            "public/fontawesome/webfonts/fa-brands-400.eot",
            FONT_AWESOME_EOT_BRANDS,
        )?;
        helper::fs::write_file(
            "public/fontawesome/webfonts/fa-brands-400.svg",
            FONT_AWESOME_SVG_BRANDS,
        )?;
        helper::fs::write_file(
            "public/fontawesome/webfonts/fa-brands-400.ttf",
            FONT_AWESOME_TTF_BRANDS,
        )?;
        helper::fs::write_file(
            "public/fontawesome/webfonts/fa-brands-400.woff",
            FONT_AWESOME_WOFF_BRANDS,
        )?;
        helper::fs::write_file(
            "public/fontawesome/webfonts/fa-brands-400.woff2",
            FONT_AWESOME_WOFF2_BRANDS,
        )?;

        helper::fs::write_file(
            "public/fontawesome/webfonts/fa-solid-900.eot",
            FONT_AWESOME_EOT_900,
        )?;
        helper::fs::write_file(
            "public/fontawesome/webfonts/fa-solid-900.svg",
            FONT_AWESOME_SVG_900,
        )?;
        helper::fs::write_file(
            "public/fontawesome/webfonts/fa-solid-900.ttf",
            FONT_AWESOME_TTF_900,
        )?;
        helper::fs::write_file(
            "public/fontawesome/webfonts/fa-solid-900.woff",
            FONT_AWESOME_WOFF_900,
        )?;
        helper::fs::write_file(
            "public/fontawesome/webfonts/fa-solid-900.woff2",
            FONT_AWESOME_WOFF2_900,
        )?;
        helper::fs::write_file("public/fontawesome/css/fontawesome.css", FONT_AWESOME)?;

        // We don't overwrite CSS by default
        if !PathBuf::from("public/unveil.css").exists() {
            let mut css = File::create("public/unveil.css")?;
            css.write_all(CSS)?;
        }

        Ok(())
    }

    /// Initialize a template project
    pub fn init(
        &mut self,
        project_name: Option<&str>,
    ) -> Result<()> {
        let project_name = if let Some(project_name) = project_name {
            project_name
        } else {
            "unveil"
        };

        // Create slides dir
        std::fs::create_dir(project_name)?;
        std::fs::create_dir(&format!("{}/slides", project_name))?;

        // Add default gitignore
        let mut gitignore = File::create(&format!("{}/.gitignore", project_name))?;
        gitignore.write_all(b"pubic")?;

        // Add a default example slides
        let mut landing = File::create(&format!("{}/slides/landing.md", project_name))?;
        landing.write_all(LANDING)?;

        // Generate default config
        let mut config_file = File::create(&format!("{}/unveil.toml", project_name))?;
        let default_config = toml::to_string(&UnveilConfig::default())?;
        config_file.write_all(default_config.as_bytes())?;

        Ok(())
    }

    pub fn clean() -> Result<()> {
        fs::remove_dir_all("public")
            .map_err(|err| anyhow!("Unable to remove public directory : {}", err))
    }

    pub fn new_slide(
        &mut self,
        name: &str,
    ) -> Result<()> {
        let filename = if name.ends_with(".md") {
            name.into()
        } else {
            format!("{}.md", name)
        };

        let path = PathBuf::from("slides").join(&filename);

        let mut config = UnveilConfig::from_disk("unveil.toml")?;

        File::create(path).map(|_| ())?;
        config.slides.push(filename);

        let mut file = OpenOptions::new()
            .write(true)
            .create(true)
            .open("unveil.toml")?;

        file.write_all(toml::to_string(&config)?.as_bytes())
            .map_err(|err| anyhow!("Error writing to unveil.toml : {}", err))
    }

    pub fn serve(
        &mut self,
        port: Option<i32>,
    ) -> Result<()> {
        let mut server = Server::default();

        if let Some(port) = port {
            server.with_port(port)
        }

        self.build()?;

        server.serve()
    }
}