Include syntaxes and themes in repository

This changes a few things:

- All syntaxes and themes are now stored (as submodules) under
  assets/syntaxes and assets/themes

- The default directories for syntaxes and themes are "syntaxes"
  and "themes" (used to be "syntax" and "themes")

- The "bat cache" command can now take a `--source <dir>` and
  `--target <dir>` option.

- The cached files have been renamed to "themes.bin" and "syntaxes.bin"
This commit is contained in:
sharkdp 2018-05-16 21:22:16 +02:00 committed by David Peter
parent 9af1d2b891
commit 145b99f01c
17 changed files with 78 additions and 53 deletions

18
.gitmodules vendored Normal file
View File

@ -0,0 +1,18 @@
[submodule "assets/syntaxes/Elixir"]
path = assets/syntaxes/Elixir
url = https://github.com/princemaple/elixir-sublime-syntax/
[submodule "assets/syntaxes/Packages"]
path = assets/syntaxes/Packages
url = https://github.com/sublimehq/Packages/
[submodule "assets/syntaxes/TOML"]
path = assets/syntaxes/TOML
url = https://github.com/sharkdp/sublime_toml_highlighting
[submodule "assets/syntaxes/Julia"]
path = assets/syntaxes/Julia
url = https://github.com/JuliaEditorSupport/Julia-sublime
[submodule "assets/themes/sublime-monokai-extended"]
path = assets/themes/sublime-monokai-extended
url = https://github.com/jonschlinkert/sublime-monokai-extended
[submodule "assets/syntaxes/Markdown-Extended"]
path = assets/syntaxes/Markdown-Extended
url = https://github.com/jonschlinkert/sublime-markdown-extended

View File

@ -136,8 +136,8 @@ ln -s "sublime-monokai-extended/Monokai Extended.tmTheme" Default.tmTheme
Create a folder with language definition files:
``` bash
mkdir -p "$BAT_CONFIG_DIR/syntax"
cd "$BAT_CONFIG_DIR/syntax"
mkdir -p "$BAT_CONFIG_DIR/syntaxes"
cd "$BAT_CONFIG_DIR/syntaxes"
# Download some language definition files, for example:
git clone https://github.com/sublimehq/Packages/

View File

@ -1,37 +1,11 @@
set -e
#!/bin/bash
THEME_FOLDER="$HOME/.config/bat/themes"
SYNTAX_FOLDER="$HOME/.config/bat/syntax"
ASSET_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
if [ ! -e "$THEME_FOLDER" ]; then
mkdir -p "$THEME_FOLDER"
(
cd "$THEME_FOLDER"
git clone https://github.com/jonschlinkert/sublime-monokai-extended
ln -s "sublime-monokai-extended/Monokai Extended.tmTheme" Default.tmTheme
)
fi
DEFAULT_MARKDOWN_SYNTAX="$ASSET_DIR/syntaxes/Packages/Markdown"
if [ ! -e "$SYNTAX_FOLDER" ]; then
mkdir -p "$SYNTAX_FOLDER"
(
cd "$SYNTAX_FOLDER"
git clone https://github.com/sublimehq/Packages/
rm -rf "$DEFAULT_MARKDOWN_SYNTAX"
# Patch JavaScript syntax
sed -i -e 's/{{identifier_break}}+/{{identifier_break}}/' Packages/JavaScript/JavaScript.sublime-syntax
bat cache --init --source="$ASSET_DIR" --target="$ASSET_DIR"
# Use extended Markdown syntax
rm -rf Packages/Markdown
git clone https://github.com/jonschlinkert/sublime-markdown-extended
# Add additional sxntax definitions
git clone https://github.com/princemaple/elixir-sublime-syntax/
git clone https://github.com/sharkdp/sublime_toml_highlighting
git clone https://github.com/JuliaEditorSupport/Julia-sublime
)
fi
bat cache --init
cp "$HOME/.cache/bat"/* .
git -C "$ASSET_DIR/syntaxes/Packages" checkout "$DEFAULT_MARKDOWN_SYNTAX"

Binary file not shown.

BIN
assets/syntaxes.bin Normal file

Binary file not shown.

@ -0,0 +1 @@
Subproject commit 044d9af8a1cc5c4da234e60f4a22ce8603b2d772

1
assets/syntaxes/Julia Submodule

@ -0,0 +1 @@
Subproject commit 581805e47c7af5ab0a880aaef5b27f8c1ccc29aa

@ -0,0 +1 @@
Subproject commit 7b7a83aeeaf3afc752f7d921b10051330557aa0e

@ -0,0 +1 @@
Subproject commit 1cb4c3ec368c751d6f7ecfa16fe02ceff23a1f6b

1
assets/syntaxes/TOML Submodule

@ -0,0 +1 @@
Subproject commit b5e3064e9950dd36a2debed2ea95ab90712ab06a

Binary file not shown.

BIN
assets/themes.bin Normal file

Binary file not shown.

View File

@ -0,0 +1 @@
sublime-monokai-extended/Monokai Extended.tmTheme

@ -0,0 +1 @@
Subproject commit 1658f51c15400978b3f6ba7cf5ed6b171e71058c

View File

@ -117,7 +117,8 @@ impl App {
Arg::with_name("init")
.long("init")
.short("i")
.help("Initialize the cache by loading from the config dir"),
.help("Initialize the syntax/theme cache")
.long_help("Initialize the syntax/theme cache by loading from the source directory (default: the configuration directory)"),
)
.arg(
Arg::with_name("clear")
@ -135,7 +136,23 @@ impl App {
ArgGroup::with_name("cache-actions")
.args(&["init", "clear", "config-dir"])
.required(true),
),
)
.arg(
Arg::with_name("source")
.long("source")
.requires("init")
.takes_value(true)
.value_name("dir")
.help("Use a different source for loading syntaxes and themes from"),
)
.arg(
Arg::with_name("target")
.long("target")
.requires("init")
.takes_value(true)
.value_name("dir")
.help("Use a different source to store the cached syntax and theme set"),
)
)
.help_message("Print this help message.")
.version_message("Show version information.")

View File

@ -3,7 +3,7 @@ use errors::*;
use std::borrow::Cow;
use std::fs::{self, File};
use std::io;
use std::path::PathBuf;
use std::path::{Path, PathBuf};
use syntect::dumps::{dump_to_file, from_binary, from_reader};
use syntect::highlighting::{Theme, ThemeSet};
use syntect::parsing::SyntaxSet;
@ -22,11 +22,10 @@ impl HighlightingAssets {
Self::from_cache().unwrap_or_else(|_| Self::from_binary())
}
pub fn from_files() -> Result<Self> {
let config_dir = PROJECT_DIRS.config_dir();
let theme_dir = config_dir.join("themes");
pub fn from_files(dir: Option<&Path>) -> Result<Self> {
let source_dir = dir.unwrap_or_else(|| PROJECT_DIRS.config_dir());
let theme_dir = source_dir.join("themes");
let theme_set = ThemeSet::load_from_folder(&theme_dir).map_err(|_| {
io::Error::new(
io::ErrorKind::Other,
@ -38,7 +37,13 @@ impl HighlightingAssets {
})?;
let mut syntax_set = SyntaxSet::new();
let syntax_dir = config_dir.join("syntax");
let syntax_dir = source_dir.join("syntaxes");
if !syntax_dir.exists() {
return Err(format!(
"Could not load syntaxes from '{}'",
syntax_dir.to_string_lossy()
).into());
}
let _ = syntax_set.load_syntaxes(syntax_dir, true);
syntax_set.load_plain_text_syntax();
@ -77,9 +82,9 @@ impl HighlightingAssets {
}
fn from_binary() -> Self {
let mut syntax_set: SyntaxSet = from_binary(include_bytes!("../assets/syntax_set"));
let mut syntax_set: SyntaxSet = from_binary(include_bytes!("../assets/syntaxes.bin"));
syntax_set.link_syntaxes();
let theme_set: ThemeSet = from_binary(include_bytes!("../assets/theme_set"));
let theme_set: ThemeSet = from_binary(include_bytes!("../assets/themes.bin"));
HighlightingAssets {
syntax_set,
@ -87,11 +92,11 @@ impl HighlightingAssets {
}
}
pub fn save(&self) -> Result<()> {
let cache_dir = PROJECT_DIRS.cache_dir();
let _ = fs::create_dir(cache_dir);
let theme_set_path = theme_set_path();
let syntax_set_path = syntax_set_path();
pub fn save(&self, dir: Option<&Path>) -> Result<()> {
let target_dir = dir.unwrap_or_else(|| PROJECT_DIRS.cache_dir());
let _ = fs::create_dir(target_dir);
let theme_set_path = target_dir.join("themes.bin");
let syntax_set_path = target_dir.join("syntaxes.bin");
print!(
"Writing theme set to {} ... ",
@ -137,11 +142,11 @@ impl HighlightingAssets {
}
pub fn theme_set_path() -> PathBuf {
PROJECT_DIRS.cache_dir().join("theme_set")
PROJECT_DIRS.cache_dir().join("themes.bin")
}
pub fn syntax_set_path() -> PathBuf {
PROJECT_DIRS.cache_dir().join("syntax_set")
PROJECT_DIRS.cache_dir().join("syntaxes.bin")
}
pub fn config_dir() -> Cow<'static, str> {

View File

@ -27,6 +27,7 @@ mod terminal;
use std::fs::{self, File};
use std::io::{self, BufRead, BufReader, Write};
use std::path::Path;
use std::process::{self, Child, Command, Stdio};
#[cfg(unix)]
@ -206,8 +207,11 @@ fn run() -> Result<()> {
match app.matches.subcommand() {
("cache", Some(cache_matches)) => {
if cache_matches.is_present("init") {
let assets = HighlightingAssets::from_files()?;
assets.save()?;
let source_dir = cache_matches.value_of("source").map(Path::new);
let target_dir = cache_matches.value_of("target").map(Path::new);
let assets = HighlightingAssets::from_files(source_dir)?;
assets.save(target_dir)?;
} else if cache_matches.is_present("clear") {
print!("Clearing theme set cache ... ");
fs::remove_file(theme_set_path()).ok();