Module: Kramdown::Converter::SyntaxHighlighter::TreeSitter

Defined in:
lib/kramdown/converter/syntax_highlighter/tree_sitter.rb,
lib/kramdown/syntax_tree_sitter/version.rb,
lib/kramdown/syntax_tree_sitter/languages.rb

Overview

This highlighter is not yet fully configured to highlight code.

Currently it merely escapes the code so that it can be safely inserted into HTML text.

Constant Summary collapse

VERSION =

Version of kramdown-syntax_tree_sitter gem

'0.4.0'
LANGUAGE_SCOPES =

Maps each recognized language alias to its corresponding Tree-sitter scope

{
  'source.R' => %w[R S r s],
  'source.bash' => %w[bash ksh sh shell zsh],
  'source.c' => %w[c],
  'source.cpp' => %w[c++ cpp],
  'source.cs' => %w[c# cs csharp],
  'source.css' => %w[css],
  'source.d' => %w[d dlang],
  'source.dot' => %w[dot],
  'source.elixir' => %w[elixir exs],
  'source.elm' => %w[elm],
  'source.emacs.lisp' => %w[elisp emacs-lisp],
  'source.go' => %w[go golang],
  'source.hack' => %w[hack hh],
  'source.haskell' => %w[haskell hs],
  'source.hcl' => %w[hcl],
  'source.java' => %w[java],
  'source.js' => %w[javascript js],
  'source.json' => %w[json],
  'source.julia' => %w[jl julia],
  'source.lua' => %w[lua],
  'source.mk' => %w[bsdmake gnumake make makefile mf],
  'source.nix' => %w[nix nixos],
  'source.objc' => %w[obj-c obj_c objc objective_c objectivec],
  'source.ocaml' => %w[ocaml],
  'source.perl' => %w[perl pl],
  'source.php' => %w[php php3 php4 php5],
  'source.proto' => %w[proto protobuf],
  'source.python' => %w[py python],
  'source.racket' => %w[racket],
  'source.ruby' => %w[rb ruby],
  'source.rust' => %w[rs rust],
  'source.scala' => %w[scala],
  'source.sparql' => %w[sparql],
  'source.sql' => %w[sql],
  'source.swift' => %w[swift],
  'source.toml' => %w[toml],
  'source.ts' => %w[ts typescript],
  'source.vhd' => %w[vhdl],
  'text.html.basic' => %w[html],
  'text.html.erb' => %w[erb eruby rhtml]
}
.map { |scope, aliases| aliases.map { |alias_| [alias_, scope] } }
.flatten(1)
.to_h
.freeze
DEFAULT_PARSERS_DIR =
'~/tree_sitter_parsers'

Class Method Summary collapse

Class Method Details

.call(converter, raw_text, language_alias, type, _) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/kramdown/converter/syntax_highlighter/tree_sitter.rb', line 18

def self.call(converter, raw_text, language_alias, type, _)
  return nil unless language_alias

  language_scope = LANGUAGE_SCOPES.fetch(language_alias, language_alias)
  rendered_text = TreeSitterAdapter.highlight(
    raw_text,
    get_parsers_dir(converter),
    language_scope,
    get_use_css_classes(converter)
  )
  # Code blocks are additionally wrapped in HTML code tags
  type == :block ? "<pre><code>#{rendered_text}</code></pre>" : rendered_text
end

.get_option(converter, name) ⇒ Object



42
43
44
# File 'lib/kramdown/converter/syntax_highlighter/tree_sitter.rb', line 42

def self.get_option(converter, name)
  converter.options[:syntax_highlighter_opts][name]
end

.get_parsers_dir(converter) ⇒ Object



32
33
34
35
36
# File 'lib/kramdown/converter/syntax_highlighter/tree_sitter.rb', line 32

def self.get_parsers_dir(converter)
  File.expand_path(
    get_option(converter, :tree_sitter_parsers_dir) || DEFAULT_PARSERS_DIR
  )
end

.get_use_css_classes(converter) ⇒ Object



38
39
40
# File 'lib/kramdown/converter/syntax_highlighter/tree_sitter.rb', line 38

def self.get_use_css_classes(converter)
  get_option(converter, :css_classes) || false
end