2017-03-20 08:30:50 +00:00
|
|
|
//! This program is mainly intended for generating the dumps that are compiled in to
|
|
|
|
//! syntect, not as a helpful example for beginners.
|
|
|
|
//! Although it is a valid example for serializing syntaxes, you probably won't need
|
|
|
|
//! to do this yourself unless you want to cache your own compiled grammars.
|
2019-11-19 21:50:40 +00:00
|
|
|
|
|
|
|
use std::collections::HashMap;
|
|
|
|
use std::collections::HashSet;
|
2017-03-20 08:30:50 +00:00
|
|
|
use std::env;
|
2019-11-19 21:50:40 +00:00
|
|
|
use std::iter::FromIterator;
|
2021-01-02 08:29:28 +00:00
|
|
|
use std::path::Path;
|
2018-10-31 07:18:57 +00:00
|
|
|
use syntect::dumps::*;
|
|
|
|
use syntect::highlighting::ThemeSet;
|
|
|
|
use syntect::parsing::SyntaxSetBuilder;
|
2017-03-20 08:30:50 +00:00
|
|
|
|
|
|
|
fn usage_and_exit() -> ! {
|
2017-04-21 07:21:44 +00:00
|
|
|
println!("USAGE: cargo run --example generate_sublime synpack source-dir newlines.packdump nonewlines.packdump\n
|
|
|
|
cargo run --example generate_sublime themepack source-dir themepack.themedump");
|
2017-03-20 08:30:50 +00:00
|
|
|
::std::process::exit(2);
|
|
|
|
}
|
|
|
|
|
2018-10-18 20:50:06 +00:00
|
|
|
// Not an example of zola but is used to generate the theme and syntax dump
|
2017-04-21 07:21:44 +00:00
|
|
|
// used for syntax highlighting.
|
|
|
|
// Check README for more details
|
2017-03-20 08:30:50 +00:00
|
|
|
fn main() {
|
2017-04-21 07:21:44 +00:00
|
|
|
let mut args = env::args().skip(1);
|
2018-08-15 13:42:43 +00:00
|
|
|
match (args.next(), args.next(), args.next()) {
|
|
|
|
(Some(ref cmd), Some(ref package_dir), Some(ref packpath_newlines)) if cmd == "synpack" => {
|
2018-10-09 12:33:43 +00:00
|
|
|
let mut builder = SyntaxSetBuilder::new();
|
|
|
|
builder.add_plain_text_syntax();
|
2020-12-28 16:39:43 +00:00
|
|
|
let base_path = Path::new(&package_dir).to_path_buf();
|
|
|
|
|
|
|
|
// First the official Sublime packages
|
|
|
|
let mut default = base_path.clone();
|
|
|
|
default.push("Packages");
|
|
|
|
match builder.add_from_folder(&default, true) {
|
2020-05-12 18:07:02 +00:00
|
|
|
Ok(_) => (),
|
2020-06-18 19:11:22 +00:00
|
|
|
Err(e) => println!("Loading error: {:?}", e),
|
2020-04-29 20:35:28 +00:00
|
|
|
};
|
2020-12-28 16:39:43 +00:00
|
|
|
|
|
|
|
// and then the ones we add
|
|
|
|
let mut extra = base_path.clone();
|
|
|
|
extra.push("extra");
|
|
|
|
match builder.add_from_folder(&extra, true) {
|
|
|
|
Ok(_) => (),
|
|
|
|
Err(e) => println!("Loading error: {:?}", e),
|
|
|
|
};
|
|
|
|
|
2018-10-09 12:33:43 +00:00
|
|
|
let ss = builder.build();
|
|
|
|
dump_to_file(&ss, packpath_newlines).unwrap();
|
2019-11-19 21:50:40 +00:00
|
|
|
let mut syntaxes: HashMap<String, HashSet<String>> = HashMap::new();
|
2017-03-20 08:30:50 +00:00
|
|
|
|
2019-11-19 21:50:40 +00:00
|
|
|
for s in ss.syntaxes().iter() {
|
|
|
|
syntaxes
|
|
|
|
.entry(s.name.clone())
|
|
|
|
.and_modify(|e| {
|
|
|
|
for ext in &s.file_extensions {
|
|
|
|
e.insert(ext.clone());
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.or_insert_with(|| HashSet::from_iter(s.file_extensions.iter().cloned()));
|
|
|
|
}
|
|
|
|
let mut keys = syntaxes.keys().collect::<Vec<_>>();
|
|
|
|
keys.sort_by(|a, b| a.to_lowercase().cmp(&b.to_lowercase()));
|
|
|
|
for k in keys {
|
|
|
|
if !syntaxes[k].is_empty() {
|
|
|
|
let mut extensions_sorted = syntaxes[k].iter().cloned().collect::<Vec<_>>();
|
|
|
|
extensions_sorted.sort();
|
|
|
|
println!("- {} -> {:?}", k, extensions_sorted);
|
2017-10-19 11:48:50 +00:00
|
|
|
}
|
2017-04-22 01:46:38 +00:00
|
|
|
}
|
2018-10-31 07:18:57 +00:00
|
|
|
}
|
2018-08-15 13:42:43 +00:00
|
|
|
(Some(ref cmd), Some(ref theme_dir), Some(ref packpath)) if cmd == "themepack" => {
|
2017-03-20 08:30:50 +00:00
|
|
|
let ts = ThemeSet::load_from_folder(theme_dir).unwrap();
|
2017-03-22 11:59:49 +00:00
|
|
|
for path in ts.themes.keys() {
|
2017-03-20 08:30:50 +00:00
|
|
|
println!("{:?}", path);
|
|
|
|
}
|
|
|
|
dump_to_file(&ts, packpath).unwrap();
|
|
|
|
}
|
|
|
|
_ => usage_and_exit(),
|
|
|
|
}
|
|
|
|
}
|