commit f496f1fab621e8a860f7c1d3273520079a3f2fa8 Author: LordMZTE Date: Wed Nov 17 01:11:36 2021 +0100 init diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..96ef6c0 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +/target +Cargo.lock diff --git a/.gitmodules b/.gitmodules new file mode 100644 index 0000000..3204586 --- /dev/null +++ b/.gitmodules @@ -0,0 +1,3 @@ +[submodule "assets/dracula_theme"] + path = assets/dracula_theme + url = https://github.com/dracula/sublime.git diff --git a/Cargo.toml b/Cargo.toml new file mode 100644 index 0000000..0b15726 --- /dev/null +++ b/Cargo.toml @@ -0,0 +1,11 @@ +[package] +name = "synmtx" +version = "0.1.0" +edition = "2021" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] +clap = { version = "3.0.0-beta.5", features = ["derive"] } +html-escape = "0.2.9" +syntect = "4.6.0" diff --git a/assets/dracula_theme b/assets/dracula_theme new file mode 160000 index 0000000..09faa29 --- /dev/null +++ b/assets/dracula_theme @@ -0,0 +1 @@ +Subproject commit 09faa29057c3c39e9a45f3a51a5e262375e3bf9f diff --git a/rustfmt.toml b/rustfmt.toml new file mode 100644 index 0000000..1059111 --- /dev/null +++ b/rustfmt.toml @@ -0,0 +1,12 @@ +unstable_features = true +binop_separator = "Back" +format_code_in_doc_comments = true +format_macro_matchers = true +format_strings = true +imports_layout = "HorizontalVertical" +match_block_trailing_comma = true +merge_imports = true +normalize_comments = true +use_field_init_shorthand = true +use_try_shorthand = true +wrap_comments = true diff --git a/src/main.rs b/src/main.rs new file mode 100644 index 0000000..46d3478 --- /dev/null +++ b/src/main.rs @@ -0,0 +1,75 @@ +use std::{ + io::{BufRead, Cursor}, + path::PathBuf, +}; + +use clap::Parser; +use syntect::{ + easy::HighlightFile, + highlighting::{Color, Style, ThemeSet}, + parsing::SyntaxSet, +}; + +#[derive(Parser)] +struct Opt { + #[clap(about = "The file to highlight")] + file: PathBuf, + + #[clap( + long, + short, + default_value = "dracula", + about = "The color scheme to use" + )] + theme: String, +} + +fn main() { + let opt = Opt::parse(); + + let ss = SyntaxSet::load_defaults_newlines(); + let mut ts = ThemeSet::load_defaults(); + ts.themes.insert( + "dracula".to_string(), + ThemeSet::load_from_reader(&mut Cursor::new(include_bytes!( + "../assets/dracula_theme/Dracula.tmTheme" + ))) + .expect("Failed to load static dracula theme"), + ); + + let theme = if let Some(t) = ts.themes.get(&opt.theme) { + t + } else { + eprintln!("No such theme!"); + // TODO dont exit successfully + return; + }; + + let mut h = HighlightFile::new(opt.file, &ss, theme).unwrap(); + + print!("
");
+    for line in h.reader.lines() {
+        let line = line.unwrap();
+        let regions = h.highlight_lines.highlight(&line, &ss);
+
+        for (
+            Style {
+                foreground: Color { r, g, b, .. },
+                ..
+            },
+            txt,
+        ) in regions
+        {
+            print!(
+                r##"{}"##,
+                r,
+                g,
+                b,
+                html_escape::encode_text(txt)
+            );
+        }
+
+        println!();
+    }
+    println!("
"); +}