You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

generate_sublime.rs 2.6KB

6 years ago
6 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. //! This program is mainly intended for generating the dumps that are compiled in to
  2. //! syntect, not as a helpful example for beginners.
  3. //! Although it is a valid example for serializing syntaxes, you probably won't need
  4. //! to do this yourself unless you want to cache your own compiled grammars.
  5. use std::collections::HashMap;
  6. use std::collections::HashSet;
  7. use std::env;
  8. use std::iter::FromIterator;
  9. use syntect::dumps::*;
  10. use syntect::highlighting::ThemeSet;
  11. use syntect::parsing::SyntaxSetBuilder;
  12. fn usage_and_exit() -> ! {
  13. println!("USAGE: cargo run --example generate_sublime synpack source-dir newlines.packdump nonewlines.packdump\n
  14. cargo run --example generate_sublime themepack source-dir themepack.themedump");
  15. ::std::process::exit(2);
  16. }
  17. // Not an example of zola but is used to generate the theme and syntax dump
  18. // used for syntax highlighting.
  19. // Check README for more details
  20. fn main() {
  21. let mut args = env::args().skip(1);
  22. match (args.next(), args.next(), args.next()) {
  23. (Some(ref cmd), Some(ref package_dir), Some(ref packpath_newlines)) if cmd == "synpack" => {
  24. let mut builder = SyntaxSetBuilder::new();
  25. builder.add_plain_text_syntax();
  26. builder.add_from_folder(package_dir, true).unwrap();
  27. let ss = builder.build();
  28. dump_to_file(&ss, packpath_newlines).unwrap();
  29. let mut syntaxes: HashMap<String, HashSet<String>> = HashMap::new();
  30. for s in ss.syntaxes().iter() {
  31. syntaxes
  32. .entry(s.name.clone())
  33. .and_modify(|e| {
  34. for ext in &s.file_extensions {
  35. e.insert(ext.clone());
  36. }
  37. })
  38. .or_insert_with(|| HashSet::from_iter(s.file_extensions.iter().cloned()));
  39. }
  40. let mut keys = syntaxes.keys().collect::<Vec<_>>();
  41. keys.sort_by(|a, b| a.to_lowercase().cmp(&b.to_lowercase()));
  42. for k in keys {
  43. if !syntaxes[k].is_empty() {
  44. let mut extensions_sorted = syntaxes[k].iter().cloned().collect::<Vec<_>>();
  45. extensions_sorted.sort();
  46. println!("- {} -> {:?}", k, extensions_sorted);
  47. }
  48. }
  49. }
  50. (Some(ref cmd), Some(ref theme_dir), Some(ref packpath)) if cmd == "themepack" => {
  51. let ts = ThemeSet::load_from_folder(theme_dir).unwrap();
  52. for path in ts.themes.keys() {
  53. println!("{:?}", path);
  54. }
  55. dump_to_file(&ts, packpath).unwrap();
  56. }
  57. _ => usage_and_exit(),
  58. }
  59. }