kramdown-syntax_tree_sitter 0.2.0 → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 4ba9a8e090021116214ebf5938b8de67856e7aa0054aeb49de5a67041b0683a5
4
- data.tar.gz: b9d7a9110a29f9abd175ea82c3f54eccb15ca334b69181299f291f43c8c6c35b
3
+ metadata.gz: '09858b648cc381cb603b21a3ed091b1d96a25b55264f4dd2c4e8fe7436d68288'
4
+ data.tar.gz: 9ceeb8b78ac8cea098cbb329abbdc9e650202e676b042b647b3f0ca55665c3ed
5
5
  SHA512:
6
- metadata.gz: ff14f9dff79b8681771b4a0b078e8a1a820489cb2e338f91f5f2ca30d758495a19763f9449585429872c00943c38dca10e88a9465eb7a97cd1c64c298935d528
7
- data.tar.gz: fc629b06c06b8df3614084dd40ccf8d38747ddc5ab9e096cb85f5976feb0a8f0729615e303a88f256b0bdd9be0c96309ec2cd461bedf472bf7e2f7aa5d4605a5
6
+ metadata.gz: a3a7cf735cab71d863433b3be34e8b1dd537cca412bfddb611b56d4ff8292b74f0f45283da701510d928d432ad4aa39224316a012eb07cbd524cc1977bfb3a2e
7
+ data.tar.gz: 23197c154c12ca2e97534829179dd2785c9907760f89205a7a7be3e80f3cca7f8f6dd2c6314897a887e1c45b55eaddbd05a2937321017378caa0d03e33e450a5
data/README.md CHANGED
@@ -1,6 +1,6 @@
1
1
  # Kramdown Tree-sitter Highlighter
2
2
 
3
- [![Build status](https://img.shields.io/github/workflow/status/andrewtbiehl/kramdown-syntax_tree_sitter/Quality%20Control?style=flat-square&logo=github)](https://github.com/andrewtbiehl/kramdown-syntax_tree_sitter/actions/workflows/quality-control.yml)
3
+ [![Build status](https://img.shields.io/github/actions/workflow/status/andrewtbiehl/kramdown-syntax_tree_sitter/quality-control.yml?branch=main&style=flat-square&logo=github)](https://github.com/andrewtbiehl/kramdown-syntax_tree_sitter/actions/workflows/quality-control.yml)
4
4
  [![RubyGems page](https://img.shields.io/gem/v/kramdown-syntax_tree_sitter?style=flat-square&color=blue&logo=rubygems&logoColor=white)](https://rubygems.org/gems/kramdown-syntax_tree_sitter)
5
5
 
6
6
  ***Syntax highlight code with [Tree-sitter](https://tree-sitter.github.io/tree-sitter)
@@ -22,8 +22,8 @@ essentially an adapter for the
22
22
  hence also requires a compatible [Rust](https://www.rust-lang.org) installation to
23
23
  function. It is officially compatible with the following environments:
24
24
 
25
- - **Ruby**: 2.7, 3.0, 3.1
26
- - **Rust**: 1.61, 1.62, 1.63, 1.64, 1.65
25
+ - **Ruby**: 2.7, 3.0, 3.1, 3.2
26
+ - **Rust**: 1.62, 1.63, 1.64, 1.65, 1.66
27
27
  - **Platforms**: MacOS, Linux
28
28
 
29
29
  ### Installation
@@ -56,7 +56,7 @@ require 'kramdown'
56
56
  require 'kramdown/syntax_tree_sitter'
57
57
 
58
58
  text = <<~MARKDOWN
59
- ~~~source.python
59
+ ~~~python
60
60
  print('Hello, World!')
61
61
  ~~~
62
62
  MARKDOWN
@@ -64,6 +64,13 @@ MARKDOWN
64
64
  Kramdown::Document.new(text, syntax_highlighter: :'tree-sitter').to_html
65
65
  ```
66
66
 
67
+ ### General usage
68
+
69
+ To successfully highlight input text via Kramdown with this plugin enabled, make sure
70
+ that every language referenced has a corresponding Tree-sitter parser library installed.
71
+ See the subsequent ['Language identifiers'](#language-identifiers) and
72
+ ['Tree-sitter parsers'](#tree-sitter-parsers) sections for more information.
73
+
67
74
  ### Usage with Jekyll
68
75
 
69
76
  This plugin can be used with the popular static site generator
@@ -82,11 +89,9 @@ kramdown:
82
89
  # Other Kramdown options...
83
90
  ```
84
91
 
85
- To highlight every code block in a Jekyll project via the plugin, make sure that every
86
- language identifier is expressed with the correct Tree-sitter scope and that every
87
- language referenced has a corresponding Tree-sitter parser library installed. See the
88
- subsequent ['Language identifiers'](#language-identifiers) and
89
- ['Tree-sitter parsers'](#tree-sitter-parsers) sections for more information.
92
+ Note that this plugin's general usage prerequisites (described in the previous
93
+ ['General usage'](#general-usage) section) still apply when using this plugin with
94
+ Jekyll.
90
95
 
91
96
  Also, there are multiple ways to render highlighted code blocks with Jekyll, as
92
97
  illustrated in the following table:
@@ -103,13 +108,13 @@ illustrated in the following table:
103
108
 
104
109
  ---
105
110
  ````
106
- ```source.python
111
+ ```python
107
112
  print('Hello, World!')
108
113
  ```
109
114
 
110
115
  or
111
116
 
112
- ~~~source.python
117
+ ~~~python
113
118
  print('Hello, World!')
114
119
  ~~~
115
120
  ````
@@ -125,7 +130,7 @@ print('Hello, World!')
125
130
  ---
126
131
  ```
127
132
  print('Hello, World!')
128
- {: class="language-source.python" }
133
+ {: class="language-python" }
129
134
  ```
130
135
  ---
131
136
 
@@ -138,7 +143,7 @@ print('Hello, World!')
138
143
 
139
144
  ---
140
145
  ```
141
- {% highlight source.python %}
146
+ {% highlight python %}
142
147
  print('Hello, World!')
143
148
  {% endhighlight %}
144
149
  ```
@@ -171,12 +176,18 @@ can be found on
171
176
 
172
177
  ### Language identifiers
173
178
 
174
- Tree-sitter uses a string-based identifier called a 'scope' to identify each language.
175
- For example, the scope string for Python is 'source.python', whereas for HTML it is
176
- 'text.html.basic'. Currently, this plugin follows this same convention, so a given code
177
- block will only be correctly highlighted if the language identifier provided for that
178
- code block is its language's corresponding Tree-sitter scope string. This is illustrated
179
- by the code block used in the [Quickstart](#quickstart) example.
179
+ For most popular languages, this plugin recognizes the same language identifiers as used
180
+ by the popular highlighter [Rouge](https://github.com/rouge-ruby/rouge). For example,
181
+ both 'python' and 'py' may be used to identify a code block written in Python. For the
182
+ complete list of Rouge language identifiers recognized by this plugin, see
183
+ [`lib/kramdown/syntax_tree_sitter/languages.rb`](https://github.com/andrewtbiehl/kramdown-syntax_tree_sitter/blob/main/lib/kramdown/syntax_tree_sitter/languages.rb).
184
+
185
+ This plugin also automatically supports the language identifier format used by
186
+ Tree-sitter, known as as the 'scope' name. For example, the scope name for Python is
187
+ 'source.python', whereas for HTML it is 'text.html.basic'.
188
+
189
+ For any identifiers currently supported by Rouge but not by this plugin, feel free to
190
+ open an issue or pull request on GitHub to have them included.
180
191
 
181
192
  ### CSS styling
182
193
 
@@ -31,9 +31,9 @@ dependencies = [
31
31
 
32
32
  [[package]]
33
33
  name = "anyhow"
34
- version = "1.0.66"
34
+ version = "1.0.68"
35
35
  source = "registry+https://github.com/rust-lang/crates.io-index"
36
- checksum = "216261ddc8289130e551ddcd5ce8a064710c0d064a4d2895c67151c92b5443f6"
36
+ checksum = "2cb2f989d18dd141ab8ae82f64d1a8cdd37e0840f73a406896cf5e99502fab61"
37
37
 
38
38
  [[package]]
39
39
  name = "ascii"
@@ -3,6 +3,8 @@
3
3
  require 'kramdown'
4
4
  require 'tree_sitter_adapter'
5
5
 
6
+ require_relative '../../syntax_tree_sitter/languages'
7
+
6
8
  module Kramdown
7
9
  module Converter # rubocop:disable Style/Documentation
8
10
  module SyntaxHighlighter
@@ -13,13 +15,14 @@ module Kramdown
13
15
  module TreeSitter
14
16
  DEFAULT_PARSERS_DIR = '~/tree_sitter_parsers'
15
17
 
16
- def self.call(converter, raw_text, language, type, _)
17
- return nil unless language
18
+ def self.call(converter, raw_text, language_alias, type, _)
19
+ return nil unless language_alias
18
20
 
21
+ language_scope = LANGUAGE_SCOPES.fetch(language_alias, language_alias)
19
22
  rendered_text = TreeSitterAdapter.highlight(
20
23
  raw_text,
21
24
  get_parsers_dir(converter),
22
- language,
25
+ language_scope,
23
26
  get_use_css_classes(converter)
24
27
  )
25
28
  # Code blocks are additionally wrapped in HTML code tags
@@ -0,0 +1,58 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Kramdown
4
+ module Converter
5
+ module SyntaxHighlighter
6
+ module TreeSitter
7
+ # Maps each recognized language alias to its corresponding Tree-sitter scope
8
+ LANGUAGE_SCOPES =
9
+ {
10
+ 'source.R' => %w[R S r s],
11
+ 'source.bash' => %w[bash ksh sh shell zsh],
12
+ 'source.c' => %w[c],
13
+ 'source.cpp' => %w[c++ cpp],
14
+ 'source.cs' => %w[c# cs csharp],
15
+ 'source.css' => %w[css],
16
+ 'source.d' => %w[d dlang],
17
+ 'source.dot' => %w[dot],
18
+ 'source.elixir' => %w[elixir exs],
19
+ 'source.elm' => %w[elm],
20
+ 'source.emacs.lisp' => %w[elisp emacs-lisp],
21
+ 'source.go' => %w[go golang],
22
+ 'source.hack' => %w[hack hh],
23
+ 'source.haskell' => %w[haskell hs],
24
+ 'source.hcl' => %w[hcl],
25
+ 'source.java' => %w[java],
26
+ 'source.js' => %w[javascript js],
27
+ 'source.json' => %w[json],
28
+ 'source.julia' => %w[jl julia],
29
+ 'source.lua' => %w[lua],
30
+ 'source.mk' => %w[bsdmake gnumake make makefile mf],
31
+ 'source.nix' => %w[nix nixos],
32
+ 'source.objc' => %w[obj-c obj_c objc objective_c objectivec],
33
+ 'source.ocaml' => %w[ocaml],
34
+ 'source.perl' => %w[perl pl],
35
+ 'source.php' => %w[php php3 php4 php5],
36
+ 'source.proto' => %w[proto protobuf],
37
+ 'source.python' => %w[py python],
38
+ 'source.racket' => %w[racket],
39
+ 'source.ruby' => %w[rb ruby],
40
+ 'source.rust' => %w[rs rust],
41
+ 'source.scala' => %w[scala],
42
+ 'source.sparql' => %w[sparql],
43
+ 'source.sql' => %w[sql],
44
+ 'source.swift' => %w[swift],
45
+ 'source.toml' => %w[toml],
46
+ 'source.ts' => %w[ts typescript],
47
+ 'source.vhd' => %w[vhdl],
48
+ 'text.html.basic' => %w[html],
49
+ 'text.html.erb' => %w[erb eruby rhtml]
50
+ }
51
+ .map { |scope, aliases| aliases.map { |alias_| [alias_, scope] } }
52
+ .flatten(1)
53
+ .to_h
54
+ .freeze
55
+ end
56
+ end
57
+ end
58
+ end
@@ -5,7 +5,7 @@ module Kramdown
5
5
  module SyntaxHighlighter
6
6
  module TreeSitter
7
7
  # Version of kramdown-syntax_tree_sitter gem
8
- VERSION = '0.2.0'
8
+ VERSION = '0.3.0'
9
9
  end
10
10
  end
11
11
  end
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.2.0
4
+ version: 0.3.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: 2022-12-08 00:00:00.000000000 Z
11
+ date: 2023-01-11 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: kramdown
@@ -139,6 +139,7 @@ files:
139
139
  - ext/tree_sitter_adapter/src/tree_sitter_adapter.rs
140
140
  - lib/kramdown/converter/syntax_highlighter/tree_sitter.rb
141
141
  - lib/kramdown/syntax_tree_sitter.rb
142
+ - lib/kramdown/syntax_tree_sitter/languages.rb
142
143
  - lib/kramdown/syntax_tree_sitter/version.rb
143
144
  - lib/tree_sitter_adapter.rb
144
145
  homepage: https://github.com/andrewtbiehl/kramdown-syntax_tree_sitter
@@ -161,7 +162,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
161
162
  - !ruby/object:Gem::Version
162
163
  version: '0'
163
164
  requirements: []
164
- rubygems_version: 3.3.7
165
+ rubygems_version: 3.4.1
165
166
  signing_key:
166
167
  specification_version: 4
167
168
  summary: Syntax highlight code with Tree-sitter via Kramdown.