Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use bat's syntect syntax set #21

Merged
merged 3 commits into from
Oct 24, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ version = "0.2.1"
edition = "2021"

[dependencies]
bincode = "1.3"
clap = { version = "4.4", features = ["derive", "string"] }
comrak = { version = "0.19", default-features = false }
crossterm = { version = "0.27", features = ["serde"] }
Expand Down
1 change: 1 addition & 0 deletions flake.nix
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
".cargo"
"src"
"themes"
"syntaxes"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thought: Oh, you've already added it? Then it's done. :D

];

buildSrc = flakeboxLib.filterSubPaths {
Expand Down
16 changes: 16 additions & 0 deletions src/markdown/elements.rs
Original file line number Diff line number Diff line change
Expand Up @@ -160,39 +160,55 @@ pub(crate) struct Code {
/// The language of a piece of code.
#[derive(Clone, Debug, PartialEq, Eq, EnumIter)]
pub(crate) enum CodeLanguage {
Ada,
Asp,
Awk,
Bash,
BatchFile,
C,
CMake,
Crontab,
CSharp,
Clojure,
Cpp,
Css,
DLang,
Docker,
Dotenv,
Elixir,
Elm,
Erlang,
Go,
Haskell,
Html,
Java,
JavaScript,
Json,
Kotlin,
Latex,
Lua,
Makefile,
Markdown,
OCaml,
Perl,
Php,
Protobuf,
Puppet,
Python,
R,
Rust,
Scala,
Shell(String),
Sql,
Swift,
Svelte,
Terraform,
TypeScript,
Unknown,
Xml,
Yaml,
Vue,
Zig,
}

impl CodeLanguage {
Expand Down
16 changes: 16 additions & 0 deletions src/markdown/parse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -127,37 +127,53 @@ impl<'a> MarkdownParser<'a> {
let info = block.info.as_str();
let mut tokens = info.split(' ');
let language = match tokens.next().unwrap_or("") {
"ada" => Ada,
"asp" => Asp,
"awk" => Awk,
"c" => C,
"cmake" => CMake,
"crontab" => Crontab,
"csharp" => CSharp,
"clojure" => Clojure,
"cpp" | "c++" => Cpp,
"css" => Css,
"d" => DLang,
"docker" => Docker,
"dotenv" => Dotenv,
"elixir" => Elixir,
"elm" => Elm,
"erlang" => Erlang,
"go" => Go,
"haskell" => Haskell,
"html" => Html,
"java" => Java,
"javascript" | "js" => JavaScript,
"json" => Json,
"kotlin" => Kotlin,
"latex" => Latex,
"lua" => Lua,
"make" => Makefile,
"markdown" => Markdown,
"ocaml" => OCaml,
"perl" => Perl,
"php" => Php,
"protobuf" => Protobuf,
"puppet" => Puppet,
"python" => Python,
"r" => R,
"rust" => Rust,
"scala" => Scala,
"shell" => Shell("sh".into()),
interpreter @ ("bash" | "sh" | "zsh" | "fish") => Shell(interpreter.into()),
"sql" => Sql,
"svelte" => Svelte,
"swift" => Swift,
"terraform" => Terraform,
"typescript" | "ts" => TypeScript,
"xml" => Xml,
"yaml" => Yaml,
"vue" => Vue,
"zig" => Zig,
_ => Unknown,
};
let flags = CodeFlags { execute: tokens.any(|token| token == "+exec") };
Expand Down
27 changes: 23 additions & 4 deletions src/render/highlighting.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,10 @@ use syntect::{
util::{as_24_bit_terminal_escaped, LinesWithEndings},
};

static SYNTAX_SET: Lazy<SyntaxSet> = Lazy::new(SyntaxSet::load_defaults_newlines);
static SYNTAX_SET: Lazy<SyntaxSet> = Lazy::new(|| {
let contents = include_bytes!("../../syntaxes/syntaxes.bin");
bincode::deserialize(contents).expect("syntaxes are broken")
});
static THEMES: Lazy<ThemeSet> = Lazy::new(ThemeSet::load_defaults);

/// A code highlighter.
Expand Down Expand Up @@ -43,40 +46,56 @@ impl CodeHighlighter {
fn language_extension(language: &CodeLanguage) -> &'static str {
use CodeLanguage::*;
match language {
Ada => "adb",
Asp => "asa",
Awk => "awk",
Bash => "bash",
BatchFile => "bat",
C => "c",
CMake => "cmake",
CSharp => "cs",
Clojure => "clj",
Cpp => "cpp",
Crontab => "crontab",
Css => "css",
DLang => "d",
Docker => "Dockerfile",
Dotenv => "env",
Elixir => "ex",
Elm => "elm",
Erlang => "erl",
Go => "go",
Haskell => "hs",
Html => "html",
Java => "java",
JavaScript => "js",
Json => "json",
Kotlin => "kt",
Latex => "tex",
Lua => "lua",
Makefile => "make",
Markdown => "md",
OCaml => "ml",
Perl => "pl",
Php => "php",
Protobuf => "proto",
Puppet => "pp",
Python => "py",
R => "r",
Rust => "rs",
Scala => "scala",
Shell(_) => "sh",
Sql => "sql",
TypeScript => "js",
Xml => "xml",
Yaml => "yaml",
Swift => "swift",
Svelte => "svelte",
Terraform => "tf",
TypeScript => "ts",
// default to plain text so we get the same look&feel
Unknown => "txt",
Vue => "vue",
Xml => "xml",
Yaml => "yaml",
Zig => "zig",
}
}
}
Expand Down
Binary file added syntaxes/syntaxes.bin
Binary file not shown.
1 change: 1 addition & 0 deletions syntaxes/syntaxes.git-hash
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
3d87b25b190e0990e0e75a2ab8f994d6c277d263
27 changes: 27 additions & 0 deletions syntaxes/update.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#!/bin/bash

set -e

if [ $# -ne 1 ]
then
echo "Usage: $0 <bat-git-hash>"
exit 1
fi

script_path=$(realpath "$0")
script_dir=$(dirname "$script_path")
git_hash=$1
clone_path=$(mktemp -d)
output_file="$script_dir/syntaxes.bin"
output_tag="$script_dir/syntaxes.git-hash"

echo "Cloning repo @ ${git_hash} into '$clone_path'"
git clone https://github.com/sharkdp/bat.git $clone_path
cd $clone_path
git reset --hard $git_hash

cp assets/syntaxes.bin $output_file
echo $git_hash > $output_tag

echo "syntaxes file copied to '$output_file'"