jekyll-highlight-param 0.0.1
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 +7 -0
- data/.gitignore +15 -0
- data/README.md +1 -0
- data/jekyll-highlight-param.gemspec +23 -0
- data/lib/jekyll-highlight-param.rb +122 -0
- metadata +76 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: b73acabebbdec05a36634b8804d60f07a2cd5f82961e371b7a8b691b0254e794
|
4
|
+
data.tar.gz: c562dbad209384989a885701b814d4d9d9bed98289039dafd40340cd36fe55d4
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 19d3c9f2c8412d9f06ee559029626326059a64fd561a79600d4323bd5387c79e8b13ad27d845f8b72f8eaf2ca4417eca83028cb2f5589c5d2b38fe08786be2f2
|
7
|
+
data.tar.gz: f3a8d2aff0127d44f5225f181ec891771fe830619a8014b38a3158d64a38a8b6551eae11274377d1b4f0848a900191d95ed9b76f3b5d1b3590aeecca64927eed
|
data/.gitignore
ADDED
data/README.md
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
# jekyll-highlight-param
|
@@ -0,0 +1,23 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
# frozen_string_literal: true
|
3
|
+
lib = File.expand_path("../lib", __FILE__)
|
4
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "jekyll-highlight-param"
|
8
|
+
spec.version = "0.0.1"
|
9
|
+
spec.date = "2020-09-23"
|
10
|
+
spec.authors = ["Uri Shani"]
|
11
|
+
spec.email = ["usdogi@gmail.com"]
|
12
|
+
spec.summary = "Jekyll syntax highlighter that accepts parameter for the language"
|
13
|
+
spec.description = "A Liquid tag plugin for Jekyll that replaces the built in highlight tag, and allows passing the language to highlight in as a parameter."
|
14
|
+
spec.homepage = "https://github.com/UriShX/jekyll-highlight-param"
|
15
|
+
spec.license = "MIT"
|
16
|
+
spec.required_ruby_version = ">= 2.0.0"
|
17
|
+
|
18
|
+
spec.files = `git ls-files -z`.split("\x0")
|
19
|
+
spec.require_paths = ["lib"]
|
20
|
+
|
21
|
+
spec.add_development_dependency "jekyll", ">= 3.6.3"
|
22
|
+
spec.add_development_dependency "bundler", "~> 1.6"
|
23
|
+
end
|
@@ -0,0 +1,122 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
require "rouge"
|
3
|
+
|
4
|
+
module Jekyll
|
5
|
+
module Tags
|
6
|
+
class HighlightBlockParam < Liquid::Block
|
7
|
+
include Liquid::StandardFilters
|
8
|
+
|
9
|
+
# The regular expression syntax checker. Start with the language specifier.
|
10
|
+
# Follow that by zero or more space separated options that take one of three
|
11
|
+
# forms: name, name=value, or name="<quoted list>"
|
12
|
+
#
|
13
|
+
# <quoted list> is a space-separated list of numbers
|
14
|
+
SYNTAX = %r!^([a-zA-Z0-9.+#_-]+)((\s+\w+(=(\w+|"([0-9]+\s)*[0-9]+"))?)*)$!.freeze
|
15
|
+
|
16
|
+
def initialize(tag_name, markup, tokens)
|
17
|
+
super
|
18
|
+
if markup.strip =~ SYNTAX
|
19
|
+
@lang = Regexp.last_match(1)
|
20
|
+
@highlight_options = parse_options(Regexp.last_match(2))
|
21
|
+
else
|
22
|
+
raise SyntaxError, <<~MSG
|
23
|
+
Syntax Error in tag 'highlight' while parsing the following markup:
|
24
|
+
|
25
|
+
#{markup}
|
26
|
+
|
27
|
+
Valid syntax: highlight <lang> [linenos]
|
28
|
+
MSG
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
LEADING_OR_TRAILING_LINE_TERMINATORS = %r!\A(\n|\r)+|(\n|\r)+\z!.freeze
|
33
|
+
|
34
|
+
def render(context)
|
35
|
+
prefix = context["highlighter_prefix"] || ""
|
36
|
+
suffix = context["highlighter_suffix"] || ""
|
37
|
+
code = super.to_s.gsub(LEADING_OR_TRAILING_LINE_TERMINATORS, "")
|
38
|
+
|
39
|
+
|
40
|
+
# if ::Rouge::Lexer.find(@lang.downcase) != nil
|
41
|
+
# @lang = @lang.downcase
|
42
|
+
# else
|
43
|
+
begin
|
44
|
+
::Rouge::Lexer.find((context[@lang]).downcase) != nil
|
45
|
+
@lang = (context[@lang]).downcase
|
46
|
+
rescue
|
47
|
+
@lang = @lang.downcase
|
48
|
+
end
|
49
|
+
# end
|
50
|
+
|
51
|
+
output =
|
52
|
+
case context.registers[:site].highlighter
|
53
|
+
when "rouge"
|
54
|
+
render_rouge(code)
|
55
|
+
when "pygments"
|
56
|
+
render_pygments(code, context)
|
57
|
+
else
|
58
|
+
render_codehighlighter(code)
|
59
|
+
end
|
60
|
+
|
61
|
+
rendered_output = add_code_tag(output)
|
62
|
+
prefix + rendered_output + suffix
|
63
|
+
end
|
64
|
+
|
65
|
+
private
|
66
|
+
|
67
|
+
OPTIONS_REGEX = %r!(?:\w="[^"]*"|\w=\w|\w)+!.freeze
|
68
|
+
|
69
|
+
def parse_options(input)
|
70
|
+
options = {}
|
71
|
+
return options if input.empty?
|
72
|
+
|
73
|
+
# Split along 3 possible forms -- key="<quoted list>", key=value, or key
|
74
|
+
input.scan(OPTIONS_REGEX) do |opt|
|
75
|
+
key, value = opt.split("=")
|
76
|
+
# If a quoted list, convert to array
|
77
|
+
if value&.include?('"')
|
78
|
+
value.delete!('"')
|
79
|
+
value = value.split
|
80
|
+
end
|
81
|
+
options[key.to_sym] = value || true
|
82
|
+
end
|
83
|
+
|
84
|
+
options[:linenos] = "inline" if options[:linenos] == true
|
85
|
+
options
|
86
|
+
end
|
87
|
+
|
88
|
+
def render_pygments(code, _context)
|
89
|
+
Jekyll.logger.warn "Warning:", "Highlight Tag no longer supports rendering with Pygments."
|
90
|
+
Jekyll.logger.warn "", "Using the default highlighter, Rouge, instead."
|
91
|
+
render_rouge(code)
|
92
|
+
end
|
93
|
+
|
94
|
+
def render_rouge(code)
|
95
|
+
formatter = ::Rouge::Formatters::HTMLLegacy.new(
|
96
|
+
:line_numbers => @highlight_options[:linenos],
|
97
|
+
:wrap => false,
|
98
|
+
:css_class => "highlight",
|
99
|
+
:gutter_class => "gutter",
|
100
|
+
:code_class => "code"
|
101
|
+
)
|
102
|
+
lexer = ::Rouge::Lexer.find_fancy(@lang, code) || Rouge::Lexers::PlainText
|
103
|
+
formatter.format(lexer.lex(code))
|
104
|
+
end
|
105
|
+
|
106
|
+
def render_codehighlighter(code)
|
107
|
+
h(code).strip
|
108
|
+
end
|
109
|
+
|
110
|
+
def add_code_tag(code)
|
111
|
+
code_attributes = [
|
112
|
+
"class=\"language-#{@lang.to_s.tr("+", "-")}\"",
|
113
|
+
"data-lang=\"#{@lang}\"",
|
114
|
+
].join(" ")
|
115
|
+
"<figure class=\"highlight\"><pre><code #{code_attributes}>"\
|
116
|
+
"#{code.chomp}</code></pre></figure>"
|
117
|
+
end
|
118
|
+
end
|
119
|
+
end
|
120
|
+
end
|
121
|
+
|
122
|
+
Liquid::Template.register_tag("highlight_param", Jekyll::Tags::HighlightBlockParam)
|
metadata
ADDED
@@ -0,0 +1,76 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: jekyll-highlight-param
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Uri Shani
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2020-09-23 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: jekyll
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ">="
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: 3.6.3
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ">="
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: 3.6.3
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: bundler
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '1.6'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '1.6'
|
41
|
+
description: A Liquid tag plugin for Jekyll that replaces the built in highlight tag,
|
42
|
+
and allows passing the language to highlight in as a parameter.
|
43
|
+
email:
|
44
|
+
- usdogi@gmail.com
|
45
|
+
executables: []
|
46
|
+
extensions: []
|
47
|
+
extra_rdoc_files: []
|
48
|
+
files:
|
49
|
+
- ".gitignore"
|
50
|
+
- README.md
|
51
|
+
- jekyll-highlight-param.gemspec
|
52
|
+
- lib/jekyll-highlight-param.rb
|
53
|
+
homepage: https://github.com/UriShX/jekyll-highlight-param
|
54
|
+
licenses:
|
55
|
+
- MIT
|
56
|
+
metadata: {}
|
57
|
+
post_install_message:
|
58
|
+
rdoc_options: []
|
59
|
+
require_paths:
|
60
|
+
- lib
|
61
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
62
|
+
requirements:
|
63
|
+
- - ">="
|
64
|
+
- !ruby/object:Gem::Version
|
65
|
+
version: 2.0.0
|
66
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
67
|
+
requirements:
|
68
|
+
- - ">="
|
69
|
+
- !ruby/object:Gem::Version
|
70
|
+
version: '0'
|
71
|
+
requirements: []
|
72
|
+
rubygems_version: 3.0.3
|
73
|
+
signing_key:
|
74
|
+
specification_version: 4
|
75
|
+
summary: Jekyll syntax highlighter that accepts parameter for the language
|
76
|
+
test_files: []
|