kramdown-syntax_tree_sitter 0.4.0 → 0.6.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/README.md +2 -2
- data/ext/tree_sitter_adapter/Cargo.lock +760 -235
- data/ext/tree_sitter_adapter/Cargo.toml +1 -1
- data/ext/tree_sitter_adapter/src/lib.rs +13 -63
- data/ext/tree_sitter_adapter/src/tree_sitter_adapter.rs +24 -6
- data/lib/kramdown/syntax_tree_sitter/languages.rb +1 -1
- data/lib/kramdown/syntax_tree_sitter/version.rb +1 -1
- data/lib/tree_sitter_adapter.rb +3 -0
- metadata +19 -76
|
@@ -1,69 +1,19 @@
|
|
|
1
|
-
|
|
2
|
-
extern crate rutie;
|
|
3
|
-
|
|
4
|
-
use rutie::{AnyException, Boolean, Class, Exception, Object, RString, VM};
|
|
1
|
+
use magnus::{exception, function, Error, Ruby};
|
|
5
2
|
|
|
6
3
|
mod tree_sitter_adapter;
|
|
7
4
|
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
raw_parsers_dir: RString,
|
|
17
|
-
raw_scope: RString,
|
|
18
|
-
css_classes: Boolean
|
|
19
|
-
) -> RString {
|
|
20
|
-
VM::unwrap_or_raise_ex(
|
|
21
|
-
tree_sitter_adapter::highlight(
|
|
22
|
-
&raw_code.unwrap().to_string(),
|
|
23
|
-
&raw_parsers_dir.unwrap().to_string(),
|
|
24
|
-
&raw_scope.unwrap().to_string(),
|
|
25
|
-
css_classes.unwrap().to_bool(),
|
|
26
|
-
)
|
|
27
|
-
.as_ref()
|
|
28
|
-
.map(String::as_str)
|
|
29
|
-
.map(RString::new_utf8)
|
|
30
|
-
.map_err(String::as_str)
|
|
31
|
-
.map_err(AnyException::new_runtime_error),
|
|
32
|
-
)
|
|
33
|
-
}
|
|
34
|
-
);
|
|
35
|
-
|
|
36
|
-
#[no_mangle]
|
|
37
|
-
pub extern "C" fn Init_tree_sitter_adapter() {
|
|
38
|
-
Class::new("TreeSitterAdapter", None).define(|class_| {
|
|
39
|
-
class_.def_self("highlight", pub_highlight);
|
|
40
|
-
});
|
|
41
|
-
}
|
|
42
|
-
|
|
43
|
-
pub trait VMExt {
|
|
44
|
-
fn unwrap_or_raise_ex<T, E>(x: Result<T, E>) -> T
|
|
45
|
-
where
|
|
46
|
-
E: Into<AnyException>;
|
|
47
|
-
}
|
|
48
|
-
|
|
49
|
-
impl VMExt for VM {
|
|
50
|
-
fn unwrap_or_raise_ex<T, E>(result: Result<T, E>) -> T
|
|
51
|
-
where
|
|
52
|
-
E: Into<AnyException>,
|
|
53
|
-
{
|
|
54
|
-
result.unwrap_or_else(|e| {
|
|
55
|
-
VM::raise_ex(e);
|
|
56
|
-
unreachable!();
|
|
57
|
-
})
|
|
58
|
-
}
|
|
59
|
-
}
|
|
60
|
-
|
|
61
|
-
pub trait AnyExceptionExt {
|
|
62
|
-
fn new_runtime_error(message: &str) -> Self;
|
|
5
|
+
fn pub_highlight(
|
|
6
|
+
code: String,
|
|
7
|
+
parsers_dir: String,
|
|
8
|
+
scope: String,
|
|
9
|
+
css_classes: bool,
|
|
10
|
+
) -> Result<String, Error> {
|
|
11
|
+
tree_sitter_adapter::highlight(&code, &parsers_dir, &scope, css_classes)
|
|
12
|
+
.map_err(|message| Error::new(exception::runtime_error(), message))
|
|
63
13
|
}
|
|
64
14
|
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
15
|
+
#[magnus::init]
|
|
16
|
+
fn init(ruby: &Ruby) -> Result<(), Error> {
|
|
17
|
+
ruby.define_module("TreeSitterAdapter")?
|
|
18
|
+
.define_module_function("highlight", function!(pub_highlight, 4))
|
|
69
19
|
}
|
|
@@ -9,6 +9,8 @@ use tree_sitter_highlight::{Error as TSError, HighlightEvent};
|
|
|
9
9
|
use tree_sitter_highlight::{Highlight, HighlightConfiguration, Highlighter, HtmlRenderer};
|
|
10
10
|
use tree_sitter_loader::{Config, LanguageConfiguration, Loader};
|
|
11
11
|
|
|
12
|
+
use std::collections::HashSet;
|
|
13
|
+
|
|
12
14
|
const LOADER_ERROR_MSG: &str = "Error loading Tree-sitter parsers from directory";
|
|
13
15
|
const NO_LANGUAGE_ERROR_MSG: &str = "Error retrieving language configuration for scope";
|
|
14
16
|
const NO_HIGHLIGHT_ERROR_MSG: &str = "Error retrieving highlight configuration for scope";
|
|
@@ -58,7 +60,7 @@ fn highlight_configuration<'a>(
|
|
|
58
60
|
scope: &'a str,
|
|
59
61
|
) -> Result<&'a HighlightConfiguration> {
|
|
60
62
|
config
|
|
61
|
-
.highlight_config(language)
|
|
63
|
+
.highlight_config(language, None)
|
|
62
64
|
.transpose()
|
|
63
65
|
.with_context(|| format!("{NO_HIGHLIGHT_ERROR_MSG} '{scope}'"))
|
|
64
66
|
.flatten_()
|
|
@@ -105,10 +107,26 @@ fn render_html(
|
|
|
105
107
|
Ok(renderer.lines().collect())
|
|
106
108
|
}
|
|
107
109
|
|
|
108
|
-
fn
|
|
110
|
+
fn highlight_names_for_language(scope: &str, loader: &Loader) -> Result<Vec<String>> {
|
|
109
111
|
let (language, config) = language_and_configuration(loader, scope)?;
|
|
110
112
|
let highlight_config = highlight_configuration(language, config, scope)?;
|
|
111
|
-
Ok(highlight_config
|
|
113
|
+
Ok(highlight_config
|
|
114
|
+
.names()
|
|
115
|
+
.iter()
|
|
116
|
+
.map(ToString::to_string)
|
|
117
|
+
.collect())
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
fn highlight_names(parser_directory: PathBuf) -> Result<Vec<String>> {
|
|
121
|
+
let loader = loader(parser_directory)?;
|
|
122
|
+
let highlight_names = loader
|
|
123
|
+
.get_all_language_configurations()
|
|
124
|
+
.into_iter()
|
|
125
|
+
.flat_map(|(config, _)| config.scope.clone())
|
|
126
|
+
.flat_map(|scope| highlight_names_for_language(&scope, &loader))
|
|
127
|
+
.flat_map(Vec::into_iter)
|
|
128
|
+
.collect::<HashSet<_>>();
|
|
129
|
+
Ok(highlight_names.into_iter().collect())
|
|
112
130
|
}
|
|
113
131
|
|
|
114
132
|
fn highlight_name_styles() -> HashMap<String, Style> {
|
|
@@ -116,7 +134,7 @@ fn highlight_name_styles() -> HashMap<String, Style> {
|
|
|
116
134
|
theme
|
|
117
135
|
.highlight_names
|
|
118
136
|
.into_iter()
|
|
119
|
-
.zip(theme.styles
|
|
137
|
+
.zip(theme.styles)
|
|
120
138
|
.collect()
|
|
121
139
|
}
|
|
122
140
|
|
|
@@ -146,8 +164,8 @@ fn highlight_adapter(
|
|
|
146
164
|
css_classes: bool,
|
|
147
165
|
) -> Result<String> {
|
|
148
166
|
let parsers_dir = PathBuf::from(parsers_dir);
|
|
149
|
-
let mut loader = loader(parsers_dir)?;
|
|
150
|
-
let highlight_names = highlight_names(
|
|
167
|
+
let mut loader = loader(parsers_dir.clone())?;
|
|
168
|
+
let highlight_names = highlight_names(parsers_dir)?;
|
|
151
169
|
loader.configure_highlights(&highlight_names);
|
|
152
170
|
let (language, config) = language_and_configuration(&loader, scope)?;
|
|
153
171
|
let highlight_config = highlight_configuration(language, config, scope)?;
|
|
@@ -45,7 +45,7 @@ module Kramdown
|
|
|
45
45
|
'source.toml' => %w[toml],
|
|
46
46
|
'source.ts' => %w[ts typescript],
|
|
47
47
|
'source.vhd' => %w[vhdl],
|
|
48
|
-
'
|
|
48
|
+
'source.html' => %w[html],
|
|
49
49
|
'text.html.erb' => %w[erb eruby rhtml]
|
|
50
50
|
}
|
|
51
51
|
.map { |scope, aliases| aliases.map { |alias_| [alias_, scope] } }
|
data/lib/tree_sitter_adapter.rb
CHANGED
|
@@ -1,5 +1,8 @@
|
|
|
1
1
|
# frozen_string_literal: true
|
|
2
2
|
|
|
3
|
+
# Even though this project uses Magnus to handle interoperabilty between Ruby and Rust,
|
|
4
|
+
# the Ruby support library for Rutie still happens to work very well for initializing
|
|
5
|
+
# the internal Rust extension for use by this Ruby library
|
|
3
6
|
require 'rutie'
|
|
4
7
|
|
|
5
8
|
Rutie.new(:tree_sitter_adapter).init(
|
metadata
CHANGED
|
@@ -1,14 +1,14 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: kramdown-syntax_tree_sitter
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 0.
|
|
4
|
+
version: 0.6.0
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- Andrew T. Biehl
|
|
8
8
|
autorequire:
|
|
9
9
|
bindir: bin
|
|
10
10
|
cert_chain: []
|
|
11
|
-
date:
|
|
11
|
+
date: 2024-05-31 00:00:00.000000000 Z
|
|
12
12
|
dependencies:
|
|
13
13
|
- !ruby/object:Gem::Dependency
|
|
14
14
|
name: kramdown
|
|
@@ -52,77 +52,10 @@ dependencies:
|
|
|
52
52
|
- - "~>"
|
|
53
53
|
- !ruby/object:Gem::Version
|
|
54
54
|
version: 0.0.4
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
- - '='
|
|
60
|
-
- !ruby/object:Gem::Version
|
|
61
|
-
version: 5.18.0
|
|
62
|
-
type: :development
|
|
63
|
-
prerelease: false
|
|
64
|
-
version_requirements: !ruby/object:Gem::Requirement
|
|
65
|
-
requirements:
|
|
66
|
-
- - '='
|
|
67
|
-
- !ruby/object:Gem::Version
|
|
68
|
-
version: 5.18.0
|
|
69
|
-
- !ruby/object:Gem::Dependency
|
|
70
|
-
name: rouge
|
|
71
|
-
requirement: !ruby/object:Gem::Requirement
|
|
72
|
-
requirements:
|
|
73
|
-
- - '='
|
|
74
|
-
- !ruby/object:Gem::Version
|
|
75
|
-
version: 4.1.0
|
|
76
|
-
type: :development
|
|
77
|
-
prerelease: false
|
|
78
|
-
version_requirements: !ruby/object:Gem::Requirement
|
|
79
|
-
requirements:
|
|
80
|
-
- - '='
|
|
81
|
-
- !ruby/object:Gem::Version
|
|
82
|
-
version: 4.1.0
|
|
83
|
-
- !ruby/object:Gem::Dependency
|
|
84
|
-
name: rubocop
|
|
85
|
-
requirement: !ruby/object:Gem::Requirement
|
|
86
|
-
requirements:
|
|
87
|
-
- - '='
|
|
88
|
-
- !ruby/object:Gem::Version
|
|
89
|
-
version: 1.50.0
|
|
90
|
-
type: :development
|
|
91
|
-
prerelease: false
|
|
92
|
-
version_requirements: !ruby/object:Gem::Requirement
|
|
93
|
-
requirements:
|
|
94
|
-
- - '='
|
|
95
|
-
- !ruby/object:Gem::Version
|
|
96
|
-
version: 1.50.0
|
|
97
|
-
- !ruby/object:Gem::Dependency
|
|
98
|
-
name: rubocop-minitest
|
|
99
|
-
requirement: !ruby/object:Gem::Requirement
|
|
100
|
-
requirements:
|
|
101
|
-
- - '='
|
|
102
|
-
- !ruby/object:Gem::Version
|
|
103
|
-
version: 0.30.0
|
|
104
|
-
type: :development
|
|
105
|
-
prerelease: false
|
|
106
|
-
version_requirements: !ruby/object:Gem::Requirement
|
|
107
|
-
requirements:
|
|
108
|
-
- - '='
|
|
109
|
-
- !ruby/object:Gem::Version
|
|
110
|
-
version: 0.30.0
|
|
111
|
-
- !ruby/object:Gem::Dependency
|
|
112
|
-
name: rubocop-rake
|
|
113
|
-
requirement: !ruby/object:Gem::Requirement
|
|
114
|
-
requirements:
|
|
115
|
-
- - '='
|
|
116
|
-
- !ruby/object:Gem::Version
|
|
117
|
-
version: 0.6.0
|
|
118
|
-
type: :development
|
|
119
|
-
prerelease: false
|
|
120
|
-
version_requirements: !ruby/object:Gem::Requirement
|
|
121
|
-
requirements:
|
|
122
|
-
- - '='
|
|
123
|
-
- !ruby/object:Gem::Version
|
|
124
|
-
version: 0.6.0
|
|
125
|
-
description:
|
|
55
|
+
description: |-
|
|
56
|
+
This is a syntax highlighter plugin for Kramdown that leverages Tree-sitter's native syntax highlighter to highlight code blocks (and spans) when rendering HTML.
|
|
57
|
+
|
|
58
|
+
Tree-sitter is a modern, general-purpose parsing library that outclasses many existing tools at the task of syntax highlighting. This plugin adapts Tree-sitter's native highlighter for Kramdown, so that Tree-sitter's superior highlighting capabilities can be easily leveraged in the context of rendering Markdown.
|
|
126
59
|
email:
|
|
127
60
|
executables: []
|
|
128
61
|
extensions:
|
|
@@ -147,6 +80,11 @@ licenses:
|
|
|
147
80
|
- MIT
|
|
148
81
|
metadata:
|
|
149
82
|
rubygems_mfa_required: 'true'
|
|
83
|
+
homepage_uri: https://github.com/andrewtbiehl/kramdown-syntax_tree_sitter
|
|
84
|
+
source_code_uri: https://github.com/andrewtbiehl/kramdown-syntax_tree_sitter
|
|
85
|
+
documentation_uri: https://github.com/andrewtbiehl/kramdown-syntax_tree_sitter/blob/main/README.md
|
|
86
|
+
changelog_uri: https://github.com/andrewtbiehl/kramdown-syntax_tree_sitter/blob/main/CHANGELOG.md
|
|
87
|
+
bug_tracker_uri: https://github.com/andrewtbiehl/kramdown-syntax_tree_sitter/issues
|
|
150
88
|
post_install_message:
|
|
151
89
|
rdoc_options: []
|
|
152
90
|
require_paths:
|
|
@@ -155,14 +93,19 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
|
155
93
|
requirements:
|
|
156
94
|
- - ">="
|
|
157
95
|
- !ruby/object:Gem::Version
|
|
158
|
-
version: '
|
|
96
|
+
version: '3.0'
|
|
159
97
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
160
98
|
requirements:
|
|
161
99
|
- - ">="
|
|
162
100
|
- !ruby/object:Gem::Version
|
|
163
101
|
version: '0'
|
|
164
|
-
requirements:
|
|
165
|
-
|
|
102
|
+
requirements:
|
|
103
|
+
- |-
|
|
104
|
+
This plugin is essentially an adapter for the Tree-sitter highlight library and hence requires a compatible Rust installation to function. It is officially compatible with the following environments:
|
|
105
|
+
|
|
106
|
+
- Rust: 1.75, 1.76, 1.77, 1.78
|
|
107
|
+
- Platforms: MacOS, Linux
|
|
108
|
+
rubygems_version: 3.5.9
|
|
166
109
|
signing_key:
|
|
167
110
|
specification_version: 4
|
|
168
111
|
summary: Syntax highlight code with Tree-sitter via Kramdown.
|