kramdown-syntax-coderay 1.0.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 +7 -0
- data/CONTRIBUTERS +3 -0
- data/COPYING +21 -0
- data/VERSION +1 -0
- data/lib/kramdown-syntax-coderay.rb +10 -0
- data/lib/kramdown/converter/syntax_highlighter/coderay.rb +169 -0
- data/test/test_files.rb +33 -0
- data/test/testcases/highlighting-opts.html +6 -0
- data/test/testcases/highlighting-opts.options +8 -0
- data/test/testcases/highlighting-opts.text +4 -0
- data/test/testcases/highlighting-span.html +1 -0
- data/test/testcases/highlighting-span.options +1 -0
- data/test/testcases/highlighting-span.text +1 -0
- data/test/testcases/highlighting.html +6 -0
- data/test/testcases/highlighting.options +6 -0
- data/test/testcases/highlighting.text +4 -0
- metadata +86 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: b9ff914162352505548ed4030c8459dc662f703bac13db5c018d4509a8f3abab
|
4
|
+
data.tar.gz: fca3f23ba27c5fc0ed3e8312c771997bcc8fd6f2cd648ff161c20dc164c32026
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 8adb57e22b44fba6df4702d9bb30b714ab7b4e2a89a6c42d83f7708e22ecce634dd7263a63e1389691f60fb76e0c618ac96b92865d2b1b1c4cd9ca54bed2f3ba
|
7
|
+
data.tar.gz: 1963926f4d89f97a39b99f98b208ac4b09007ab944710e443d211c10c0019da9cbdc9233a318f8ed61c0ada9d4583195fdcb215aabeb46520b654789692a6de5
|
data/CONTRIBUTERS
ADDED
data/COPYING
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
kramdown-syntax-coderay
|
2
|
+
Copyright (C) 2019 Thomas Leitner <t_leitner@gmx.at>
|
3
|
+
|
4
|
+
Permission is hereby granted, free of charge, to any person obtaining a
|
5
|
+
copy of this software and associated documentation files (the
|
6
|
+
"Software"), to deal in the Software without restriction, including
|
7
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
8
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
9
|
+
permit persons to whom the Software is furnished to do so, subject to
|
10
|
+
the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included
|
13
|
+
in all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
|
16
|
+
OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
17
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
|
18
|
+
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
|
19
|
+
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
|
20
|
+
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
|
21
|
+
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/VERSION
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
1.0.0
|
@@ -0,0 +1,10 @@
|
|
1
|
+
# -*- coding: utf-8; frozen_string_literal: true -*-
|
2
|
+
#
|
3
|
+
#--
|
4
|
+
# Copyright (C) 2019 Thomas Leitner <t_leitner@gmx.at>
|
5
|
+
#
|
6
|
+
# This file is part of kramdown-syntax-coderay which is licensed under the MIT.
|
7
|
+
#++
|
8
|
+
#
|
9
|
+
|
10
|
+
require 'kramdown/converter/syntax_highlighter/coderay'
|
@@ -0,0 +1,169 @@
|
|
1
|
+
# -*- coding: utf-8; frozen_string_literal: true -*-
|
2
|
+
#
|
3
|
+
#--
|
4
|
+
# Copyright (C) 2019 Thomas Leitner <t_leitner@gmx.at>
|
5
|
+
#
|
6
|
+
# This file is part of kramdown-syntax-coderay which is licensed under the MIT.
|
7
|
+
#++
|
8
|
+
#
|
9
|
+
|
10
|
+
require 'kramdown/options'
|
11
|
+
require 'kramdown/converter'
|
12
|
+
require 'coderay'
|
13
|
+
|
14
|
+
module Kramdown #:nodoc:
|
15
|
+
|
16
|
+
module Options #:nodoc:
|
17
|
+
|
18
|
+
define(:enable_coderay, Boolean, true, <<EOF)
|
19
|
+
Use coderay for syntax highlighting
|
20
|
+
|
21
|
+
If this option is `true`, coderay is used by the HTML converter for
|
22
|
+
syntax highlighting the content of code spans and code blocks.
|
23
|
+
|
24
|
+
Default: true
|
25
|
+
Used by: HTML converter
|
26
|
+
EOF
|
27
|
+
|
28
|
+
define(:coderay_wrap, Symbol, :div, <<EOF)
|
29
|
+
Defines how the highlighted code should be wrapped
|
30
|
+
|
31
|
+
The possible values are :span, :div or nil.
|
32
|
+
|
33
|
+
Default: :div
|
34
|
+
Used by: HTML converter
|
35
|
+
EOF
|
36
|
+
|
37
|
+
define(:coderay_line_numbers, Symbol, :inline, <<EOF)
|
38
|
+
Defines how and if line numbers should be shown
|
39
|
+
|
40
|
+
The possible values are :table, :inline or nil. If this option is
|
41
|
+
nil, no line numbers are shown.
|
42
|
+
|
43
|
+
Default: :inline
|
44
|
+
Used by: HTML converter
|
45
|
+
EOF
|
46
|
+
|
47
|
+
define(:coderay_line_number_start, Integer, 1, <<EOF)
|
48
|
+
The start value for the line numbers
|
49
|
+
|
50
|
+
Default: 1
|
51
|
+
Used by: HTML converter
|
52
|
+
EOF
|
53
|
+
|
54
|
+
define(:coderay_tab_width, Integer, 8, <<EOF)
|
55
|
+
The tab width used in highlighted code
|
56
|
+
|
57
|
+
Used by: HTML converter
|
58
|
+
EOF
|
59
|
+
|
60
|
+
define(:coderay_bold_every, Object, 10, <<EOF) do |val|
|
61
|
+
Defines how often a line number should be made bold
|
62
|
+
|
63
|
+
Can either be an integer or false (to turn off bold line numbers
|
64
|
+
completely).
|
65
|
+
|
66
|
+
Default: 10
|
67
|
+
Used by: HTML converter
|
68
|
+
EOF
|
69
|
+
if val == false || val.to_s == 'false'
|
70
|
+
false
|
71
|
+
else
|
72
|
+
Integer(val.to_s) rescue raise Kramdown::Error, "Invalid value for option 'coderay_bold_every'"
|
73
|
+
end
|
74
|
+
end
|
75
|
+
|
76
|
+
define(:coderay_css, Symbol, :style, <<EOF)
|
77
|
+
Defines how the highlighted code gets styled
|
78
|
+
|
79
|
+
Possible values are :class (CSS classes are applied to the code
|
80
|
+
elements, one must supply the needed CSS file) or :style (default CSS
|
81
|
+
styles are directly applied to the code elements).
|
82
|
+
|
83
|
+
Default: style
|
84
|
+
Used by: HTML converter
|
85
|
+
EOF
|
86
|
+
|
87
|
+
define(:coderay_default_lang, Symbol, nil, <<EOF)
|
88
|
+
Sets the default language for highlighting code blocks
|
89
|
+
|
90
|
+
If no language is set for a code block, the default language is used
|
91
|
+
instead. The value has to be one of the languages supported by coderay
|
92
|
+
or nil if no default language should be used.
|
93
|
+
|
94
|
+
Default: nil
|
95
|
+
Used by: HTML converter
|
96
|
+
EOF
|
97
|
+
|
98
|
+
end
|
99
|
+
|
100
|
+
module Converter #:nodoc:
|
101
|
+
module SyntaxHighlighter #:nodoc:
|
102
|
+
|
103
|
+
# Uses Coderay to highlight code blocks and code spans.
|
104
|
+
module Coderay
|
105
|
+
|
106
|
+
VERSION = '1.0.0'
|
107
|
+
|
108
|
+
def self.call(converter, text, lang, type, call_opts)
|
109
|
+
return nil unless converter.options[:enable_coderay]
|
110
|
+
if type == :span && lang
|
111
|
+
::CodeRay.scan(text, lang.to_sym).html(options(converter, :span)).chomp
|
112
|
+
elsif type == :block && (lang || options(converter, :default_lang))
|
113
|
+
lang ||= call_opts[:default_lang] = options(converter, :default_lang)
|
114
|
+
::CodeRay.scan(text, lang.to_s.tr('-', '_').to_sym).html(options(converter, :block)).chomp << "\n"
|
115
|
+
else
|
116
|
+
nil
|
117
|
+
end
|
118
|
+
rescue StandardError
|
119
|
+
converter.warning("There was an error using CodeRay: #{$!.message}")
|
120
|
+
nil
|
121
|
+
end
|
122
|
+
|
123
|
+
def self.options(converter, type)
|
124
|
+
prepare_options(converter)
|
125
|
+
converter.data[:syntax_highlighter_coderay][type]
|
126
|
+
end
|
127
|
+
|
128
|
+
def self.prepare_options(converter)
|
129
|
+
return if converter.data.key?(:syntax_highlighter_coderay)
|
130
|
+
|
131
|
+
cache = converter.data[:syntax_highlighter_coderay] = {}
|
132
|
+
|
133
|
+
opts = converter.options[:syntax_highlighter_opts].dup
|
134
|
+
span_opts = (opts.delete(:span) || {}).dup
|
135
|
+
block_opts = (opts.delete(:block) || {}).dup
|
136
|
+
[span_opts, block_opts].each do |hash|
|
137
|
+
hash.keys.each do |k|
|
138
|
+
hash[k.kind_of?(String) ? ::Kramdown::Options.str_to_sym(k) : k] = hash.delete(k)
|
139
|
+
end
|
140
|
+
end
|
141
|
+
|
142
|
+
cache[:default_lang] = converter.options[:coderay_default_lang] || opts.delete(:default_lang)
|
143
|
+
cache[:span] = {
|
144
|
+
css: converter.options[:coderay_css],
|
145
|
+
}.update(opts).update(span_opts).update(wrap: :span)
|
146
|
+
cache[:block] = {
|
147
|
+
wrap: converter.options[:coderay_wrap],
|
148
|
+
line_numbers: converter.options[:coderay_line_numbers],
|
149
|
+
line_number_start: converter.options[:coderay_line_number_start],
|
150
|
+
tab_width: converter.options[:coderay_tab_width],
|
151
|
+
bold_every: converter.options[:coderay_bold_every],
|
152
|
+
css: converter.options[:coderay_css],
|
153
|
+
}.update(opts).update(block_opts)
|
154
|
+
|
155
|
+
[:css, :wrap, :line_numbers].each do |key|
|
156
|
+
[:block, :span].each do |type|
|
157
|
+
cache[type][key] = cache[type][key].to_sym if cache[type][key].kind_of?(String)
|
158
|
+
end
|
159
|
+
end
|
160
|
+
end
|
161
|
+
|
162
|
+
end
|
163
|
+
end
|
164
|
+
|
165
|
+
add_syntax_highlighter(:coderay, SyntaxHighlighter::Coderay)
|
166
|
+
|
167
|
+
end
|
168
|
+
|
169
|
+
end
|
data/test/test_files.rb
ADDED
@@ -0,0 +1,33 @@
|
|
1
|
+
# -*- coding: utf-8 -*-
|
2
|
+
#
|
3
|
+
#--
|
4
|
+
# Copyright (C) 2019 Thomas Leitner <t_leitner@gmx.at>
|
5
|
+
#
|
6
|
+
# This file is part of kramdown-syntax-coderay which is licensed under the MIT.
|
7
|
+
#++
|
8
|
+
#
|
9
|
+
|
10
|
+
require 'minitest/autorun'
|
11
|
+
require 'kramdown'
|
12
|
+
require 'kramdown-syntax-coderay'
|
13
|
+
require 'yaml'
|
14
|
+
require 'tmpdir'
|
15
|
+
|
16
|
+
Encoding.default_external = 'utf-8'
|
17
|
+
|
18
|
+
class TestFiles < Minitest::Test
|
19
|
+
|
20
|
+
Dir[File.dirname(__FILE__) + '/testcases/**/*.text'].each do |text_file|
|
21
|
+
basename = text_file.sub(/\.text$/, '')
|
22
|
+
|
23
|
+
html_file = basename + '.html'
|
24
|
+
define_method('test_' + text_file.tr('.', '_') + "_to_html") do
|
25
|
+
opts_file = basename + '.options'
|
26
|
+
opts_file = File.join(File.dirname(html_file), 'options') if !File.exist?(opts_file)
|
27
|
+
options = File.exist?(opts_file) ? YAML::load(File.read(opts_file)) : {auto_ids: false, footnote_nr: 1}
|
28
|
+
doc = Kramdown::Document.new(File.read(text_file), options)
|
29
|
+
assert_equal(File.read(html_file), doc.to_html)
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
end
|
@@ -0,0 +1,6 @@
|
|
1
|
+
<div class="language-ruby highlighter-coderay"><span class="CodeRay">x = <span class="constant">Class</span>.new
|
2
|
+
</span>
|
3
|
+
</div>
|
4
|
+
<div class="language-html highlighter-coderay"><span class="CodeRay"><span class="tag"><a></span>href<span class="tag"></a></span>
|
5
|
+
</span>
|
6
|
+
</div>
|
@@ -0,0 +1 @@
|
|
1
|
+
<p>You can say <code class="language-ruby highlighter-coderay"><span class="CodeRay">x = <span style="color:#036;font-weight:bold">Class</span>.new</span></code>, for example.</p>
|
@@ -0,0 +1 @@
|
|
1
|
+
:syntax_highlighter: :coderay
|
@@ -0,0 +1 @@
|
|
1
|
+
You can say `x = Class.new`{:.language-ruby}, for example.
|
@@ -0,0 +1,6 @@
|
|
1
|
+
<div class="language-ruby highlighter-coderay"><span class="CodeRay">x = <span class="constant">Class</span>.new
|
2
|
+
</span>
|
3
|
+
</div>
|
4
|
+
<div class="language-html highlighter-coderay"><span class="CodeRay"><span class="tag"><a></span>href<span class="tag"></a></span>
|
5
|
+
</span>
|
6
|
+
</div>
|
metadata
ADDED
@@ -0,0 +1,86 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: kramdown-syntax-coderay
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.0.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Thomas Leitner
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2019-01-12 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: kramdown
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '2.0'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '2.0'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: coderay
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: 1.0.0
|
34
|
+
type: :runtime
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: 1.0.0
|
41
|
+
description:
|
42
|
+
email: t_leitner@gmx.at
|
43
|
+
executables: []
|
44
|
+
extensions: []
|
45
|
+
extra_rdoc_files: []
|
46
|
+
files:
|
47
|
+
- CONTRIBUTERS
|
48
|
+
- COPYING
|
49
|
+
- VERSION
|
50
|
+
- lib/kramdown-syntax-coderay.rb
|
51
|
+
- lib/kramdown/converter/syntax_highlighter/coderay.rb
|
52
|
+
- test/test_files.rb
|
53
|
+
- test/testcases/highlighting-opts.html
|
54
|
+
- test/testcases/highlighting-opts.options
|
55
|
+
- test/testcases/highlighting-opts.text
|
56
|
+
- test/testcases/highlighting-span.html
|
57
|
+
- test/testcases/highlighting-span.options
|
58
|
+
- test/testcases/highlighting-span.text
|
59
|
+
- test/testcases/highlighting.html
|
60
|
+
- test/testcases/highlighting.options
|
61
|
+
- test/testcases/highlighting.text
|
62
|
+
homepage: https://github.com/kramdown/syntax-coderay
|
63
|
+
licenses:
|
64
|
+
- MIT
|
65
|
+
metadata: {}
|
66
|
+
post_install_message:
|
67
|
+
rdoc_options: []
|
68
|
+
require_paths:
|
69
|
+
- lib
|
70
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
71
|
+
requirements:
|
72
|
+
- - ">="
|
73
|
+
- !ruby/object:Gem::Version
|
74
|
+
version: '2.3'
|
75
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
76
|
+
requirements:
|
77
|
+
- - ">="
|
78
|
+
- !ruby/object:Gem::Version
|
79
|
+
version: '0'
|
80
|
+
requirements: []
|
81
|
+
rubyforge_project:
|
82
|
+
rubygems_version: 2.7.3
|
83
|
+
signing_key:
|
84
|
+
specification_version: 4
|
85
|
+
summary: kramdown-syntax-coderay uses coderay to highlight code blocks/spans
|
86
|
+
test_files: []
|