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
/*
 * Snekdown - Custom Markdown flavour and parser
 * Copyright (C) 2021  Trivernis
 * See LICENSE for more information.
 */

use crate::settings::style_settings::Theme;
use std::time::Instant;
use syntect::highlighting::ThemeSet;
use syntect::parsing::SyntaxSet;

/// Returns the css of a theme compiled from sass
pub fn get_css_for_theme(theme: Theme) -> String {
    let start = Instant::now();
    let vars = match theme {
        Theme::GitHub => include_str!("assets/light-github.scss"),
        Theme::SolarizedDark => include_str!("assets/dark-solarized.scss"),
        Theme::SolarizedLight => include_str!("assets/light-solarized.scss"),
        Theme::OceanDark => include_str!("assets/dark-ocean.scss"),
        Theme::OceanLight => include_str!("assets/light-ocean.scss"),
        Theme::MagicDark => include_str!("assets/dark-magic.scss"),
    };
    let style = format!("{}\n{}", vars, include_str!("assets/base.scss"));

    let css = compile_sass(&*style);

    log::debug!("Compiled style in {} ms", start.elapsed().as_millis());

    css
}

/// Returns the syntax theme for a given theme
pub fn get_code_theme_for_theme(theme: Theme) -> (syntect::highlighting::Theme, SyntaxSet) {
    lazy_static::lazy_static! { static ref PS: SyntaxSet = SyntaxSet::load_defaults_nonewlines(); }
    lazy_static::lazy_static! { static ref TS: ThemeSet = ThemeSet::load_defaults(); }

    let theme = match theme {
        Theme::GitHub => "InspiredGitHub",
        Theme::SolarizedDark => "Solarized (dark)",
        Theme::SolarizedLight => "Solarized (light)",
        Theme::OceanDark => "base16-ocean.dark",
        Theme::OceanLight => "base16-ocean.light",
        Theme::MagicDark => "base16-ocean.dark",
    };

    return (TS.themes[theme].clone(), PS.clone());
}

fn compile_sass(sass: &str) -> String {
    String::from_utf8(
        rsass::compile_scss(
            sass.as_bytes(),
            rsass::output::Format {
                style: rsass::output::Style::Compressed,
                precision: 5,
            },
        )
        .unwrap(),
    )
    .unwrap()
}