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
#![recursion_limit = "1000"]
extern crate clap;
extern crate genco;
#[macro_use]
extern crate log;
extern crate pulldown_cmark;
extern crate reproto_backend as backend;
extern crate reproto_core as core;
extern crate reproto_manifest as manifest;
extern crate reproto_trans as trans;
extern crate syntect;

#[macro_use]
mod macros;
mod doc_builder;
mod doc_compiler;
mod enum_processor;
mod escape;
mod index_processor;
mod interface_processor;
mod package_processor;
mod processor;
mod rendering;
mod service_processor;
mod tuple_processor;
mod type_processor;

pub const NORMALIZE_CSS_NAME: &str = "normalize.css";
pub const DOC_CSS_NAME: &str = "doc.css";
pub const EXT: &str = "html";
pub const INDEX: &str = "index";
pub const DEFAULT_THEME: &str = "light";
pub const DEFAULT_SYNTAX_THEME: &str = "ayu-mirage";

use clap::{App, Arg, ArgMatches};
use core::CoreFlavor;
use core::errors::*;
use doc_compiler::DocCompiler;
use manifest::Manifest;
use std::collections::HashMap;
use syntect::dumps::from_binary;
use syntect::highlighting::{Theme, ThemeSet};
use syntect::parsing::SyntaxSet;
use trans::Environment;

include!(concat!(env!("OUT_DIR"), "/themes.rs"));

fn build_themes() -> HashMap<&'static str, &'static [u8]> {
    let mut m = HashMap::new();

    for (key, value) in build_themes_vec() {
        m.insert(key, value);
    }

    m
}

static SYNTAX_DUMP: &'static [u8] =
    include_bytes!(concat!(env!("CARGO_MANIFEST_DIR"), "/dumps/syntaxdump"));
static THEME_DUMP: &'static [u8] =
    include_bytes!(concat!(env!("CARGO_MANIFEST_DIR"), "/dumps/themedump"));

fn load_syntax_set() -> SyntaxSet {
    let mut ss: SyntaxSet = from_binary(SYNTAX_DUMP);
    ss.link_syntaxes();
    ss
}

fn load_theme_set() -> ThemeSet {
    from_binary(THEME_DUMP)
}

pub fn shared_options<'a, 'b>(out: App<'a, 'b>) -> App<'a, 'b> {
    let out = out.arg(
        Arg::with_name("theme")
            .long("theme")
            .takes_value(true)
            .help("Theme to use (use `--list-themes` for available)"),
    );

    let out = out.arg(
        Arg::with_name("list-themes")
            .long("list-themes")
            .help("List available themes"),
    );

    let out = out.arg(
        Arg::with_name("syntax-theme")
            .long("syntax-theme")
            .takes_value(true)
            .help("Syntax theme to use (use `--list-syntax-themes` for available)"),
    );

    let out = out.arg(
        Arg::with_name("list-syntax-themes")
            .long("list-syntax-themes")
            .help("List available syntax themes"),
    );

    let out = out.arg(
        Arg::with_name("skip-static")
            .long("skip-static")
            .help("Skip building with static files"),
    );

    out
}

pub fn compile_options<'a, 'b>(out: App<'a, 'b>) -> App<'a, 'b> {
    shared_options(out).about("Compile Documentation")
}

/// Load and execute the provided clojure with a syntax theme.
fn with_initialized<F>(
    matches: &ArgMatches,
    manifest: Manifest,
    themes: &HashMap<&'static str, &'static [u8]>,
    f: F,
) -> Result<()>
where
    F: FnOnce(&Theme, &SyntaxSet, &[u8]) -> Result<()>,
{
    let syntax_theme = matches
        .value_of("syntax-theme")
        .or_else(|| manifest.doc.syntax_theme.as_ref().map(String::as_str))
        .unwrap_or(DEFAULT_SYNTAX_THEME);

    let default_theme: Theme = Default::default();
    let theme_set = load_theme_set();
    let syntax_set = load_syntax_set();

    let syntax_theme = if let Some(syntax_theme) = theme_set.themes.get(syntax_theme) {
        syntax_theme
    } else {
        warn!(
            "No syntax theme named `{}`, falling back to default",
            syntax_theme
        );

        &default_theme
    };

    let theme = matches.value_of("theme").unwrap_or(DEFAULT_THEME);

    let theme_css = if let Some(theme_css) = themes.get(theme) {
        theme_css
    } else {
        warn!("No syntax theme named `{}`, falling back to default", theme);

        themes
            .get(DEFAULT_THEME)
            .ok_or_else(|| format!("no such default theme: {}", DEFAULT_THEME))?
    };

    f(syntax_theme, &syntax_set, theme_css)
}

fn list_themes(themes: &HashMap<&'static str, &'static [u8]>) -> Result<()> {
    let mut names = themes.keys().collect::<Vec<_>>();
    names.sort();

    println!("Available Themes:");

    for id in names {
        println!("{}", id);
    }

    Ok(())
}

fn list_syntax_themes() -> Result<()> {
    let theme_set = load_theme_set();

    let mut names: Vec<(&str, &Theme)> = theme_set
        .themes
        .iter()
        .map(|e| (e.0.as_str(), e.1))
        .collect::<Vec<_>>();

    names.sort_by(|a, b| a.0.cmp(b.0));

    println!("Available Syntax Themes:");

    for (id, theme) in names {
        let name = theme
            .name
            .as_ref()
            .map(String::as_str)
            .unwrap_or("*no name*");

        let author = theme
            .author
            .as_ref()
            .map(String::as_str)
            .unwrap_or("*unknown*");

        println!("{} - {} by {}", id, name, author);
    }

    Ok(())
}

pub fn compile(
    env: Environment<CoreFlavor>,
    matches: &ArgMatches,
    manifest: Manifest,
) -> Result<()> {
    let env = env.translate_default()?;

    let themes = build_themes();

    let mut done = false;

    if matches.is_present("list-themes") {
        list_themes(&themes)?;
        done = true;
    }

    if matches.is_present("list-syntax-themes") {
        list_syntax_themes()?;
        done = true;
    }

    // other task performed (e.g. listing themes).
    if done {
        return Ok(());
    }

    let skip_static = matches.is_present("skip-static");
    let out = manifest
        .output
        .as_ref()
        .ok_or("Missing `--out` or `output=`")?
        .clone();

    with_initialized(
        matches,
        manifest,
        &themes,
        |syntax_theme, syntax_set, theme_css| {
            let compiler = DocCompiler {
                env: env,
                out_path: out.clone(),
                skip_static: skip_static,
                theme_css: theme_css,
                syntax_theme: syntax_theme,
                syntax_set: syntax_set,
            };

            compiler.compile()
        },
    )?;

    println!("Wrote documentation in: {}", out.display());

    Ok(())
}