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.

65 lines
2.7KB

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