asciidoctor-rouge 0.1.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/LICENSE +21 -0
- data/README.adoc +50 -0
- data/asciidoctor-rouge.gemspec +27 -0
- data/lib/asciidoctor-rouge.rb +2 -0
- data/lib/asciidoctor/rouge.rb +10 -0
- data/lib/asciidoctor/rouge/callouts_substitutor.rb +88 -0
- data/lib/asciidoctor/rouge/docinfo_processor.rb +22 -0
- data/lib/asciidoctor/rouge/html_formatter.rb +77 -0
- data/lib/asciidoctor/rouge/passthroughs_substitutor.rb +53 -0
- data/lib/asciidoctor/rouge/treeprocessor.rb +110 -0
- data/lib/asciidoctor/rouge/version.rb +7 -0
- metadata +166 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 514ab17fada9d5d6be0a3fc05ae1f8faa5df71ff
|
4
|
+
data.tar.gz: d9fc422a4bd70c1df7096c5762fb87eaa00d54a2
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: b875cadbe7eb749630918d4849cd0858b413b287b1524cc53653d66878f492db990c69e3ba8602196eaaa186d715d64ba7da7b6fda4065c616ff673040ca8075
|
7
|
+
data.tar.gz: 5f490dde43ac3ef448bcb0f250f7d13fe1ed85dac38e68f445dbcf5714b3dea9fea9947837a3b093efb644241423a420eb4bb0cd23ee1d3d0c7642779df91f2d
|
data/LICENSE
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License
|
2
|
+
|
3
|
+
Copyright 2017 Jakub Jirutka <jakub@jirutka.cz>.
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
data/README.adoc
ADDED
@@ -0,0 +1,50 @@
|
|
1
|
+
= Asciidoctor Rouge
|
2
|
+
:source-language: ruby
|
3
|
+
// custom
|
4
|
+
:gem-name: asciidoctor-rouge
|
5
|
+
:gem-version: 0.1.0
|
6
|
+
:gh-name: jirutka/{gem-name}
|
7
|
+
:gh-branch: master
|
8
|
+
:codacy-id: d2ed58f5f3f949a19bab7637fe7d0bdb
|
9
|
+
|
10
|
+
ifdef::env-github[]
|
11
|
+
image:https://travis-ci.org/{gh-name}.svg?branch={gh-branch}[Build Status, link="https://travis-ci.org/{gh-name}"]
|
12
|
+
image:https://api.codacy.com/project/badge/Coverage/{codacy-id}["Test Coverage", link="https://www.codacy.com/app/{gh-name}"]
|
13
|
+
image:https://api.codacy.com/project/badge/Grade/{codacy-id}["Codacy Code quality", link="https://www.codacy.com/app/{gh-name}"]
|
14
|
+
image:https://img.shields.io/gem/v/{gem-name}.svg?style=flat[Gem Version, link="https://rubygems.org/gems/{gem-name}"]
|
15
|
+
image:https://img.shields.io/badge/yard-docs-blue.svg[Yard Docs, link="http://www.rubydoc.info/github/{gh-name}/{gh-branch}"]
|
16
|
+
endif::env-github[]
|
17
|
+
|
18
|
+
|
19
|
+
This project provides an http://asciidoctor.org/[Asciidoctor] extension for highlighting source listing blocks using http://rouge.jneen.net/[Rouge] – a pure-ruby code highlighter that is compatible with pygments.
|
20
|
+
|
21
|
+
It supports all Asciidoctor features such as http://asciidoctor.org/docs/user-manual/#callouts[callouts], highlighting of specified lines (attribute `highlight`), http://asciidoctor.org/docs/user-manual/#passthroughs[passthroughs] inside a code and all other substitutions.
|
22
|
+
|
23
|
+
This extension is highly customizable and modular.
|
24
|
+
|
25
|
+
|
26
|
+
== Installation
|
27
|
+
|
28
|
+
To install (or update to the latest version):
|
29
|
+
|
30
|
+
[source, sh, subs="+attributes"]
|
31
|
+
gem install {gem-name}
|
32
|
+
|
33
|
+
or to install the latest development version:
|
34
|
+
|
35
|
+
[source, sh, subs="+attributes"]
|
36
|
+
gem install {gem-name} --pre
|
37
|
+
|
38
|
+
|
39
|
+
== Usage
|
40
|
+
|
41
|
+
Set attribute `source-highlighter` to `rouge`; globally or in the document.
|
42
|
+
|
43
|
+
[source, sh, subs="+attributes"]
|
44
|
+
asciidoctor -r {gem-name} -a source-highlighter=rouge Example.adoc
|
45
|
+
|
46
|
+
|
47
|
+
== License
|
48
|
+
|
49
|
+
This project is licensed under http://opensource.org/licenses/MIT/[MIT License].
|
50
|
+
For the full text of the license, see the link:LICENSE[LICENSE] file.
|
@@ -0,0 +1,27 @@
|
|
1
|
+
require File.expand_path('../lib/asciidoctor/rouge/version', __FILE__)
|
2
|
+
|
3
|
+
Gem::Specification.new do |s|
|
4
|
+
s.name = 'asciidoctor-rouge'
|
5
|
+
s.version = Asciidoctor::Rouge::VERSION
|
6
|
+
s.author = 'Jakub Jirutka'
|
7
|
+
s.email = 'jakub@jirutka.cz'
|
8
|
+
s.homepage = 'https://github.com/jirutka/asciidoctor-rouge'
|
9
|
+
s.license = 'MIT'
|
10
|
+
|
11
|
+
s.summary = 'Rouge code highlighter support for Asciidoctor'
|
12
|
+
|
13
|
+
s.files = Dir['lib/**/*', '*.gemspec', 'LICENSE*', 'README*']
|
14
|
+
s.has_rdoc = 'yard'
|
15
|
+
|
16
|
+
s.required_ruby_version = '>= 2.1'
|
17
|
+
|
18
|
+
s.add_runtime_dependency 'asciidoctor', '~> 1.5.6'
|
19
|
+
s.add_runtime_dependency 'rouge', '~> 2.2'
|
20
|
+
|
21
|
+
s.add_development_dependency 'corefines', '~> 1.11'
|
22
|
+
s.add_development_dependency 'rake', '~> 12.0'
|
23
|
+
s.add_development_dependency 'rspec', '~> 3.6'
|
24
|
+
s.add_development_dependency 'rubocop', '~> 0.49.0'
|
25
|
+
s.add_development_dependency 'simplecov', '~> 0.14'
|
26
|
+
s.add_development_dependency 'yard', '~> 0.9'
|
27
|
+
end
|
@@ -0,0 +1,10 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
require 'asciidoctor/extensions'
|
3
|
+
require 'asciidoctor/rouge/version'
|
4
|
+
require 'asciidoctor/rouge/docinfo_processor'
|
5
|
+
require 'asciidoctor/rouge/treeprocessor'
|
6
|
+
|
7
|
+
Asciidoctor::Extensions.register do
|
8
|
+
treeprocessor Asciidoctor::Rouge::Treeprocessor
|
9
|
+
docinfo_processor Asciidoctor::Rouge::DocinfoProcessor
|
10
|
+
end
|
@@ -0,0 +1,88 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
require 'English'
|
3
|
+
require 'asciidoctor'
|
4
|
+
require 'asciidoctor/inline'
|
5
|
+
require 'asciidoctor/substitutors'
|
6
|
+
require 'asciidoctor/rouge/version'
|
7
|
+
|
8
|
+
module Asciidoctor::Rouge
|
9
|
+
# A substitutor for processing callouts inside a source listing block.
|
10
|
+
class CalloutsSubstitutor
|
11
|
+
|
12
|
+
# @return [Hash<Integer, Array<Integer>>]
|
13
|
+
attr_reader :callouts
|
14
|
+
|
15
|
+
# @param node [Asciidoctor::AbstractNode]
|
16
|
+
# @return [CalloutsSubstitutor] a callouts substitutor for the given _node_.
|
17
|
+
def self.create(node)
|
18
|
+
new(node)
|
19
|
+
end
|
20
|
+
|
21
|
+
# Extracts and stashes callout markers from the given _text_ for
|
22
|
+
# reinsertion after processing.
|
23
|
+
#
|
24
|
+
# This should be used prior passing the source to a code highlighter.
|
25
|
+
#
|
26
|
+
# @param text [#each_line] source of the listing block.
|
27
|
+
# @return [String] a copy of the _text_ with callout marks removed.
|
28
|
+
def extract(text)
|
29
|
+
escape_char = ::Asciidoctor::Substitutors::RS
|
30
|
+
@callouts.clear
|
31
|
+
|
32
|
+
text.each_line.with_index.map { |line, ln|
|
33
|
+
line.gsub(@callout_rx) do
|
34
|
+
match = $LAST_MATCH_INFO
|
35
|
+
if match[1] == escape_char
|
36
|
+
# We have to use sub since we aren't sure it's the first char.
|
37
|
+
match[0].sub(escape_char, '')
|
38
|
+
else
|
39
|
+
(@callouts[ln] ||= []) << match[3].to_i
|
40
|
+
nil
|
41
|
+
end
|
42
|
+
end
|
43
|
+
}.join
|
44
|
+
end
|
45
|
+
|
46
|
+
# Converts and restores the extracted callouts for the given _text_.
|
47
|
+
#
|
48
|
+
# @param text [#each_line]
|
49
|
+
# @return [String] a copy of the _text_ with inserted callouts.
|
50
|
+
def restore(text)
|
51
|
+
return text if @callouts.empty?
|
52
|
+
|
53
|
+
text.each_line.with_index.map { |line, ln|
|
54
|
+
if (conums = @callouts.delete(ln))
|
55
|
+
line.chomp + conums.map { |num| convert_callout(num) }.join(' ') + "\n"
|
56
|
+
else
|
57
|
+
line
|
58
|
+
end
|
59
|
+
}.join
|
60
|
+
end
|
61
|
+
|
62
|
+
protected
|
63
|
+
|
64
|
+
# (see .create)
|
65
|
+
def initialize(node)
|
66
|
+
@node = node
|
67
|
+
@callouts = {}
|
68
|
+
|
69
|
+
@callout_rx = if node.attr? 'line-comment'
|
70
|
+
comment_rx = ::Regexp.escape(node.attr('line-comment'))
|
71
|
+
/(?:#{comment_rx} )?#{::Asciidoctor::CalloutExtractRxt}/
|
72
|
+
else
|
73
|
+
::Asciidoctor::CalloutExtractRx
|
74
|
+
end
|
75
|
+
end
|
76
|
+
|
77
|
+
# @param number [Integer] callout number.
|
78
|
+
# @return [String] an HTML markup of callout with the given _number_.
|
79
|
+
def convert_callout(number)
|
80
|
+
::Asciidoctor::Inline.new(@node, :callout, number, id: next_callout_id).convert
|
81
|
+
end
|
82
|
+
|
83
|
+
# @return [Integer] an unique ID for callout.
|
84
|
+
def next_callout_id
|
85
|
+
(@doc_callouts ||= @node.document.callouts).read_next_id
|
86
|
+
end
|
87
|
+
end
|
88
|
+
end
|
@@ -0,0 +1,22 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
require 'asciidoctor/rouge/version'
|
3
|
+
require 'asciidoctor/extensions'
|
4
|
+
require 'rouge'
|
5
|
+
|
6
|
+
module Asciidoctor::Rouge
|
7
|
+
# A docinfo processor that embeds CSS for Rouge into the document's header.
|
8
|
+
class DocinfoProcessor < ::Asciidoctor::Extensions::DocinfoProcessor
|
9
|
+
|
10
|
+
# @param document [Asciidoctor::Document] the document to process.
|
11
|
+
# @return [String, nil]
|
12
|
+
def process(document)
|
13
|
+
return unless document.attr?('source-highlighter', 'rouge')
|
14
|
+
return unless document.attr('rouge-css', 'class') == 'class'
|
15
|
+
|
16
|
+
if (theme = ::Rouge::Theme.find(document.attr('rouge-theme', 'github')))
|
17
|
+
css = theme.render(scope: '.highlight')
|
18
|
+
['<style>', css, '</style>'].join("\n")
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
@@ -0,0 +1,77 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
require 'asciidoctor/rouge/version'
|
3
|
+
require 'rouge'
|
4
|
+
|
5
|
+
module Asciidoctor::Rouge
|
6
|
+
# An HTML Rouge formatter with support for lines ID and highlighted lines.
|
7
|
+
class HtmlFormatter < ::Rouge::Formatter
|
8
|
+
|
9
|
+
tag 'asciidoctor_html'
|
10
|
+
|
11
|
+
# @param parent [Rouge::Formatter] the parent formatter; it must respond
|
12
|
+
# to method +span(token, value)+. Defaults to +Rouge::Formatters::HTML+.
|
13
|
+
#
|
14
|
+
# @param highlight_lines [Array<Integer>] a list of line numbers (1-based)
|
15
|
+
# to be highlighted (i.e. wrapped in +<strong></strong>+). Defaults to
|
16
|
+
# empty array.
|
17
|
+
#
|
18
|
+
# @param highlight_class [String] CSS class to use on an element wrapping
|
19
|
+
# highlighted lines (see above). Defaults to "highlighted".
|
20
|
+
#
|
21
|
+
# @param line_id [String] format string specifying +id+ for each line.
|
22
|
+
# Defaults to "L%i".
|
23
|
+
#
|
24
|
+
# @param line_class [String] CSS class to use on a line wrapper element.
|
25
|
+
# Defaults to "line".
|
26
|
+
#
|
27
|
+
def initialize(parent: ::Rouge::Formatters::HTML.new,
|
28
|
+
highlight_lines: [],
|
29
|
+
highlight_class: 'highlighted',
|
30
|
+
line_id: 'L%i',
|
31
|
+
line_class: 'line', **)
|
32
|
+
@parent = parent
|
33
|
+
@highlight_lines = highlight_lines
|
34
|
+
@highlight_class = highlight_class
|
35
|
+
@line_id = line_id
|
36
|
+
@line_class = line_class
|
37
|
+
end
|
38
|
+
|
39
|
+
def stream(tokens)
|
40
|
+
lno = 1
|
41
|
+
token_lines(tokens) do |line|
|
42
|
+
highlighted = @highlight_lines.include?(lno)
|
43
|
+
|
44
|
+
yield "\n" if lno > 1
|
45
|
+
yield line_open(lno)
|
46
|
+
yield highlight_open if highlighted
|
47
|
+
|
48
|
+
line.each do |token, value|
|
49
|
+
yield @parent.span(token, value)
|
50
|
+
end
|
51
|
+
|
52
|
+
yield highlight_close if highlighted
|
53
|
+
yield line_close
|
54
|
+
|
55
|
+
lno += 1
|
56
|
+
end
|
57
|
+
end
|
58
|
+
|
59
|
+
protected
|
60
|
+
|
61
|
+
def line_open(lno)
|
62
|
+
%(<span id=#{sprintf(@line_id, lno).inspect} class=#{@line_class.inspect}>)
|
63
|
+
end
|
64
|
+
|
65
|
+
def line_close
|
66
|
+
%(</span>)
|
67
|
+
end
|
68
|
+
|
69
|
+
def highlight_open
|
70
|
+
%(<strong class=#{@highlight_class.inspect}>)
|
71
|
+
end
|
72
|
+
|
73
|
+
def highlight_close
|
74
|
+
%(</strong>)
|
75
|
+
end
|
76
|
+
end
|
77
|
+
end
|
@@ -0,0 +1,53 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
require 'asciidoctor/substitutors'
|
3
|
+
require 'asciidoctor/rouge/version'
|
4
|
+
|
5
|
+
module Asciidoctor::Rouge
|
6
|
+
# A substitutor for processing passthroughs inside listing blocks.
|
7
|
+
# It's basically just a facade for Asciidoctor's internal methods.
|
8
|
+
class PassthroughsSubstitutor
|
9
|
+
|
10
|
+
PASS_START_MARK = ::Asciidoctor::Substitutors::PASS_START
|
11
|
+
PASS_END_MARK = ::Asciidoctor::Substitutors::PASS_END
|
12
|
+
PASS_SLOT_RX = ::Asciidoctor::Substitutors::HighlightedPassSlotRx
|
13
|
+
|
14
|
+
# @param node [Asciidoctor::AbstractNode]
|
15
|
+
# @return [PassthroughsSubstitutor] a passthroughs substitutor for
|
16
|
+
# the given _node_.
|
17
|
+
def self.create(node)
|
18
|
+
new(node)
|
19
|
+
end
|
20
|
+
|
21
|
+
# Extracts passthrough regions from the given text for reinsertion
|
22
|
+
# after processing.
|
23
|
+
#
|
24
|
+
# @param text [String] the source of the node.
|
25
|
+
# @return [String] a copy of the _text_ with passthrough regions
|
26
|
+
# substituted with placeholders.
|
27
|
+
def extract(text)
|
28
|
+
@node.extract_passthroughs(text)
|
29
|
+
end
|
30
|
+
|
31
|
+
# Restores the extracted passthroughs by reinserting them into the
|
32
|
+
# placeholder positions.
|
33
|
+
#
|
34
|
+
# @param text [String] the text into which to restore the passthroughs.
|
35
|
+
# @return [String] a copy of the _text_ with restored passthroughs.
|
36
|
+
def restore(text)
|
37
|
+
return text if @node.passthroughs.empty?
|
38
|
+
|
39
|
+
# Fix passthrough placeholders that got caught up in syntax highlighting.
|
40
|
+
text = text.gsub(PASS_SLOT_RX, "#{PASS_START_MARK}\\1#{PASS_END_MARK}")
|
41
|
+
|
42
|
+
# Restore converted passthroughs.
|
43
|
+
@node.restore_passthroughs(text)
|
44
|
+
end
|
45
|
+
|
46
|
+
protected
|
47
|
+
|
48
|
+
# (see .create)
|
49
|
+
def initialize(node)
|
50
|
+
@node = node
|
51
|
+
end
|
52
|
+
end
|
53
|
+
end
|
@@ -0,0 +1,110 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
require 'asciidoctor/rouge/version'
|
3
|
+
require 'asciidoctor/rouge/callouts_substitutor'
|
4
|
+
require 'asciidoctor/rouge/html_formatter'
|
5
|
+
require 'asciidoctor/rouge/passthroughs_substitutor'
|
6
|
+
require 'asciidoctor/extensions'
|
7
|
+
require 'rouge'
|
8
|
+
|
9
|
+
module Asciidoctor::Rouge
|
10
|
+
# An Asciidoctor extension that highlights source listings using Rouge.
|
11
|
+
class Treeprocessor < ::Asciidoctor::Extensions::Treeprocessor
|
12
|
+
|
13
|
+
# @param formatter [Class<Rouge::Formatter>] the Rouge formatter to use for
|
14
|
+
# formatting a token stream from a Rouge lexer. It must respond to method
|
15
|
+
# +format+ accepting a token stream and (optionally) a hash of options,
|
16
|
+
# producing +String+. Defaults to {HtmlFormatter}.
|
17
|
+
#
|
18
|
+
# @param formatter_opts [Hash] options to pass to the _formatter_.
|
19
|
+
# It's used only if _formatter's_ +format+ method has arity > 1.
|
20
|
+
# Defaults to empty hash.
|
21
|
+
#
|
22
|
+
# @param callouts_sub [#create] the callouts substitutor class to use for
|
23
|
+
# processing callouts. Defaults to {CalloutsSubstitutor}.
|
24
|
+
#
|
25
|
+
# @param passthroughs_sub [#create] the passthroughs substitutor class to
|
26
|
+
# use for processing passthroughs.
|
27
|
+
# Defaults to {PassthroughsSubstitutor}.
|
28
|
+
#
|
29
|
+
def initialize(formatter: HtmlFormatter,
|
30
|
+
formatter_opts: {},
|
31
|
+
callouts_sub: CalloutsSubstitutor,
|
32
|
+
passthroughs_sub: PassthroughsSubstitutor, **)
|
33
|
+
super
|
34
|
+
|
35
|
+
@formatter = formatter
|
36
|
+
@formatter_opts = formatter_opts
|
37
|
+
@callouts_sub = callouts_sub
|
38
|
+
@passthroughs_sub = passthroughs_sub
|
39
|
+
end
|
40
|
+
|
41
|
+
# @param document [Asciidoctor::Document] the document to process.
|
42
|
+
def process(document)
|
43
|
+
return unless document.attr? 'source-highlighter', 'rouge'
|
44
|
+
|
45
|
+
document.find_by(context: :listing, style: 'source') do |block|
|
46
|
+
process_listing(block)
|
47
|
+
end
|
48
|
+
end
|
49
|
+
|
50
|
+
protected
|
51
|
+
|
52
|
+
# @param block [Asciidoctor::Block] the listing block to highlight.
|
53
|
+
def process_listing(block)
|
54
|
+
source = block.source # String
|
55
|
+
subs = block.subs # Array<Symbol>
|
56
|
+
|
57
|
+
# Don't escape special characters, Rouge will take care of it.
|
58
|
+
subs.delete(:specialcharacters)
|
59
|
+
|
60
|
+
if subs.delete(:macros)
|
61
|
+
passthroughs = @passthroughs_sub.create(block)
|
62
|
+
source = passthroughs.extract(source)
|
63
|
+
end
|
64
|
+
|
65
|
+
if subs.delete(:callouts)
|
66
|
+
callouts = @callouts_sub.create(block)
|
67
|
+
source = callouts.extract(source)
|
68
|
+
end
|
69
|
+
|
70
|
+
source = block.apply_subs(source, subs)
|
71
|
+
subs.clear
|
72
|
+
|
73
|
+
lang = block.attr('language', 'plaintext', false)
|
74
|
+
lexer = find_lexer(lang)
|
75
|
+
block.set_attr('language', lexer.tag)
|
76
|
+
|
77
|
+
if block.attr?('highlight', nil, false)
|
78
|
+
highlight_lines = block.resolve_highlight_lines(block.attr('highlight', '', false))
|
79
|
+
end
|
80
|
+
|
81
|
+
result = highlight(lexer, source, highlight_lines)
|
82
|
+
result = callouts.restore(result) if callouts
|
83
|
+
result = passthroughs.restore(result) if passthroughs
|
84
|
+
|
85
|
+
block.lines.replace(result.split("\n"))
|
86
|
+
end
|
87
|
+
|
88
|
+
# @param language [String]
|
89
|
+
# @return [Rouge::Lexer] a lexer for the specified _language_.
|
90
|
+
def find_lexer(language)
|
91
|
+
(::Rouge::Lexer.find(language) || ::Rouge::Lexers::PlainText).new
|
92
|
+
end
|
93
|
+
|
94
|
+
# @param lexer [Rouge::Lexer] the lexer to use.
|
95
|
+
# @param source [String] the code to highlight.
|
96
|
+
# @param highlight_lines [Array<Integer>] a list of line numbers (1-based)
|
97
|
+
# to be highlighted.
|
98
|
+
# @return [String] a highlighted and formatted _source_.
|
99
|
+
def highlight(lexer, source, highlight_lines = [])
|
100
|
+
tokens = lexer.lex(source)
|
101
|
+
|
102
|
+
if @formatter.method(:format).arity.abs > 1
|
103
|
+
opts = @formatter_opts.merge(highlight_lines: highlight_lines || [])
|
104
|
+
@formatter.format(tokens, opts)
|
105
|
+
else
|
106
|
+
@Formatter.format(tokens)
|
107
|
+
end
|
108
|
+
end
|
109
|
+
end
|
110
|
+
end
|
metadata
ADDED
@@ -0,0 +1,166 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: asciidoctor-rouge
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Jakub Jirutka
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2017-09-18 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: asciidoctor
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: 1.5.6
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: 1.5.6
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rouge
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '2.2'
|
34
|
+
type: :runtime
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '2.2'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: corefines
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '1.11'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '1.11'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: rake
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - "~>"
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '12.0'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - "~>"
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '12.0'
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: rspec
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - "~>"
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '3.6'
|
76
|
+
type: :development
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - "~>"
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '3.6'
|
83
|
+
- !ruby/object:Gem::Dependency
|
84
|
+
name: rubocop
|
85
|
+
requirement: !ruby/object:Gem::Requirement
|
86
|
+
requirements:
|
87
|
+
- - "~>"
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
version: 0.49.0
|
90
|
+
type: :development
|
91
|
+
prerelease: false
|
92
|
+
version_requirements: !ruby/object:Gem::Requirement
|
93
|
+
requirements:
|
94
|
+
- - "~>"
|
95
|
+
- !ruby/object:Gem::Version
|
96
|
+
version: 0.49.0
|
97
|
+
- !ruby/object:Gem::Dependency
|
98
|
+
name: simplecov
|
99
|
+
requirement: !ruby/object:Gem::Requirement
|
100
|
+
requirements:
|
101
|
+
- - "~>"
|
102
|
+
- !ruby/object:Gem::Version
|
103
|
+
version: '0.14'
|
104
|
+
type: :development
|
105
|
+
prerelease: false
|
106
|
+
version_requirements: !ruby/object:Gem::Requirement
|
107
|
+
requirements:
|
108
|
+
- - "~>"
|
109
|
+
- !ruby/object:Gem::Version
|
110
|
+
version: '0.14'
|
111
|
+
- !ruby/object:Gem::Dependency
|
112
|
+
name: yard
|
113
|
+
requirement: !ruby/object:Gem::Requirement
|
114
|
+
requirements:
|
115
|
+
- - "~>"
|
116
|
+
- !ruby/object:Gem::Version
|
117
|
+
version: '0.9'
|
118
|
+
type: :development
|
119
|
+
prerelease: false
|
120
|
+
version_requirements: !ruby/object:Gem::Requirement
|
121
|
+
requirements:
|
122
|
+
- - "~>"
|
123
|
+
- !ruby/object:Gem::Version
|
124
|
+
version: '0.9'
|
125
|
+
description:
|
126
|
+
email: jakub@jirutka.cz
|
127
|
+
executables: []
|
128
|
+
extensions: []
|
129
|
+
extra_rdoc_files: []
|
130
|
+
files:
|
131
|
+
- LICENSE
|
132
|
+
- README.adoc
|
133
|
+
- asciidoctor-rouge.gemspec
|
134
|
+
- lib/asciidoctor-rouge.rb
|
135
|
+
- lib/asciidoctor/rouge.rb
|
136
|
+
- lib/asciidoctor/rouge/callouts_substitutor.rb
|
137
|
+
- lib/asciidoctor/rouge/docinfo_processor.rb
|
138
|
+
- lib/asciidoctor/rouge/html_formatter.rb
|
139
|
+
- lib/asciidoctor/rouge/passthroughs_substitutor.rb
|
140
|
+
- lib/asciidoctor/rouge/treeprocessor.rb
|
141
|
+
- lib/asciidoctor/rouge/version.rb
|
142
|
+
homepage: https://github.com/jirutka/asciidoctor-rouge
|
143
|
+
licenses:
|
144
|
+
- MIT
|
145
|
+
metadata: {}
|
146
|
+
post_install_message:
|
147
|
+
rdoc_options: []
|
148
|
+
require_paths:
|
149
|
+
- lib
|
150
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
151
|
+
requirements:
|
152
|
+
- - ">="
|
153
|
+
- !ruby/object:Gem::Version
|
154
|
+
version: '2.1'
|
155
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
156
|
+
requirements:
|
157
|
+
- - ">="
|
158
|
+
- !ruby/object:Gem::Version
|
159
|
+
version: '0'
|
160
|
+
requirements: []
|
161
|
+
rubyforge_project:
|
162
|
+
rubygems_version: 2.6.11
|
163
|
+
signing_key:
|
164
|
+
specification_version: 4
|
165
|
+
summary: Rouge code highlighter support for Asciidoctor
|
166
|
+
test_files: []
|