This commit is contained in:
LordMZTE 2021-11-17 01:11:36 +01:00
commit f496f1fab6
6 changed files with 104 additions and 0 deletions

2
.gitignore vendored Normal file
View file

@ -0,0 +1,2 @@
/target
Cargo.lock

3
.gitmodules vendored Normal file
View file

@ -0,0 +1,3 @@
[submodule "assets/dracula_theme"]
path = assets/dracula_theme
url = https://github.com/dracula/sublime.git

11
Cargo.toml Normal file
View file

@ -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"

1
assets/dracula_theme Submodule

@ -0,0 +1 @@
Subproject commit 09faa29057c3c39e9a45f3a51a5e262375e3bf9f

12
rustfmt.toml Normal file
View file

@ -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

75
src/main.rs Normal file
View file

@ -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!("<pre>");
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##"<font color="#{:02x}{:02x}{:02x}">{}</font>"##,
r,
g,
b,
html_escape::encode_text(txt)
);
}
println!();
}
println!("</pre>");
}