asciidoctor-rouge 0.1.1 → 0.2.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
  SHA1:
3
- metadata.gz: c67c5170e4c94b6625d49dee4ff5c46edc39255e
4
- data.tar.gz: 79add3dd5c89b66bf866b14a997edb53a98470b7
3
+ metadata.gz: 7baa8b7c49247f40e58fd422dd7fd460a543562b
4
+ data.tar.gz: 0b8ad27e185a07a2812fdc072c1c296631672fb7
5
5
  SHA512:
6
- metadata.gz: a900bf73225718348c4d65ee6ba1e09c3f335652058a501b1d90345744038c511f63ba31f4ed46361b426362d68cc3ebf107709126e7830778c9fa3aba0f8f1b
7
- data.tar.gz: c6772dec55296fa2858b1fde19221cc45ddd3fc1997ed458655ccbe8a4ab9c6737f0ec40272b8e94a94d0c135fdb2c1799fe4d9603362ac1f3bc4e118a156b9b
6
+ metadata.gz: 02566ec5a875e6881edbc2bc6da92cb45c320c40f9414a53d5ba4c76bfafbc23a6ee42c0ed20091598535264a09bd6b157de46dabf788dfb9971783d7c4953be
7
+ data.tar.gz: d69c5765a92aaad6b80765070b88370c70b8937bb6551ad13716655bae5968dd83daacd0f280dbcea1e141dee1a2e547ae7627436ce0a867b7690ee3a0eb8003
data/README.adoc CHANGED
@@ -1,8 +1,8 @@
1
1
  = Asciidoctor Rouge
2
- :source-language: ruby
2
+ :source-language: shell
3
3
  // custom
4
4
  :gem-name: asciidoctor-rouge
5
- :gem-version: 0.1.1
5
+ :gem-version: 0.2.0
6
6
  :gh-name: jirutka/{gem-name}
7
7
  :gh-branch: master
8
8
  :codacy-id: d2ed58f5f3f949a19bab7637fe7d0bdb
@@ -27,23 +27,39 @@ This extension is highly customizable and modular.
27
27
 
28
28
  To install (or update to the latest version):
29
29
 
30
- [source, sh, subs="+attributes"]
30
+ [source, subs="+attributes"]
31
31
  gem install {gem-name}
32
32
 
33
33
  or to install the latest development version:
34
34
 
35
- [source, sh, subs="+attributes"]
35
+ [source, subs="+attributes"]
36
36
  gem install {gem-name} --pre
37
37
 
38
38
 
39
39
  == Usage
40
40
 
41
- Set attribute `source-highlighter` to `rouge`; globally or in the document.
41
+ Assign `rouge` to the `source-highlighter` attribute in your document’s header or via command-line argument.
42
42
 
43
- [source, sh, subs="+attributes"]
43
+ [source, subs="+attributes"]
44
44
  asciidoctor -r {gem-name} -a source-highlighter=rouge Example.adoc
45
45
 
46
46
 
47
+ === Attributes
48
+
49
+ You can further customize the source block output with additional Rouge attributes:
50
+
51
+ rouge-css::
52
+ Controls what method is used for applying CSS to the tokens.
53
+ Can be `class` (CSS classes) or `style` (inline styles).
54
+ When `class` is used, Rouge styles for the specified theme are included in an HTML header.
55
+ Default is `class`.
56
+
57
+ rouge-theme::
58
+ Sets the name of the Rouge colour theme to use.
59
+ Look into https://github.com/jneen/rouge/tree/master/lib/rouge/themes[lib/rouge/themes] in the Rouge repository for a list of available themes.
60
+ Default is `github`.
61
+
62
+
47
63
  == License
48
64
 
49
65
  This project is licensed under http://opensource.org/licenses/MIT/[MIT License].
@@ -29,7 +29,7 @@ module Asciidoctor::Rouge
29
29
  escape_char = ::Asciidoctor::Substitutors::RS
30
30
  @callouts.clear
31
31
 
32
- text.each_line.with_index.map { |line, ln|
32
+ text.each_line.with_index(1).map { |line, ln|
33
33
  line.gsub(@callout_rx) do
34
34
  match = $LAST_MATCH_INFO
35
35
  if match[1] == escape_char
@@ -43,20 +43,17 @@ module Asciidoctor::Rouge
43
43
  }.join
44
44
  end
45
45
 
46
- # Converts and restores the extracted callouts for the given _text_.
46
+ # Converts the extracted callout markers for the specified line.
47
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?
48
+ # @param line_num [Integer] the line number (1-based).
49
+ # @return [String] converted callout markers for the _line_num_,
50
+ # or an empty string if there are no callouts for that line.
51
+ def convert_line(line_num)
52
+ return '' unless @callouts.key? line_num
52
53
 
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
54
+ @callouts[line_num]
55
+ .map { |num| convert_callout(num) }
56
+ .join(' ')
60
57
  end
61
58
 
62
59
  protected
@@ -75,7 +72,7 @@ module Asciidoctor::Rouge
75
72
  end
76
73
 
77
74
  # @param number [Integer] callout number.
78
- # @return [String] an HTML markup of callout with the given _number_.
75
+ # @return [String] an HTML markup of a callout marker with the given _number_.
79
76
  def convert_callout(number)
80
77
  ::Asciidoctor::Inline.new(@node, :callout, number, id: next_callout_id).convert
81
78
  end
@@ -3,75 +3,104 @@ require 'asciidoctor/rouge/version'
3
3
  require 'rouge'
4
4
 
5
5
  module Asciidoctor::Rouge
6
- # An HTML Rouge formatter with support for lines ID and highlighted lines.
6
+ # An HTML Rouge formatter for Asciidoctor with support for callouts and
7
+ # highlighted lines.
7
8
  class HtmlFormatter < ::Rouge::Formatter
8
9
 
9
10
  tag 'asciidoctor_html'
10
11
 
11
- # @param parent [Rouge::Formatter] the parent formatter; it must respond
12
- # to method +span(token, value)+. Defaults to +Rouge::Formatters::HTML+.
12
+ # @param callout_markers [#[], nil] callout markers indexed by
13
+ # line numbers to be inserted between the line's content and the line's
14
+ # closing tag.
13
15
  #
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.
16
+ # @param highlighted_class [String] CSS class to use on a line wrapper
17
+ # element for highlighted lines (see above). Defaults to "highlighted".
17
18
  #
18
- # @param highlight_class [String] CSS class to use on an element wrapping
19
- # highlighted lines (see above). Defaults to "highlighted".
19
+ # @param highlighted_lines [Array<Integer>] a list of line numbers
20
+ # (1-based) to be highlighted (i.e. added _highlighted_class_ to a line
21
+ # wrapper element). Defaults to empty array.
20
22
  #
21
- # @param line_id [String] format string specifying +id+ for each line.
22
- # Defaults to "L%i".
23
+ # @param inline_theme [String, Rouge::Theme, Class<Rouge::Theme>, nil]
24
+ # the theme to use for inline styles, or +nil+ to not set inline styles
25
+ # (i.e. use classes). This is ignored if _inner_ is not +nil+.
23
26
  #
24
- # @param line_class [String] CSS class to use on a line wrapper element.
25
- # Defaults to "line".
27
+ # @param line_class [String, nil] CSS class to set on a line wrapper
28
+ # element, or +nil+ to not set a class. Defaults to "line".
26
29
  #
27
- def initialize(parent: ::Rouge::Formatters::HTML.new,
28
- highlight_lines: [],
29
- highlight_class: 'highlighted',
30
+ # @param line_id [String, nil] format string specifying +id+ for each line,
31
+ # or +nil+ to omit +id+. Defaults to "L%i".
32
+ #
33
+ # @param inner [Rouge::Formatter::HTML, #span, nil] the inner HTML
34
+ # formatter to delegate formatting of tokens to, or +nil+ to get
35
+ # +html+ or +html_inline+ formatter from the +Rouge::Formatter+'s
36
+ # registry.
37
+ #
38
+ def initialize(callout_markers: nil,
39
+ highlighted_class: 'highlighted',
40
+ highlighted_lines: [],
41
+ inline_theme: nil,
42
+ line_class: 'line',
30
43
  line_id: 'L%i',
31
- line_class: 'line', **)
32
- @parent = parent
33
- @highlight_lines = highlight_lines
34
- @highlight_class = highlight_class
44
+ inner: nil, **)
45
+
46
+ inner ||= if inline_theme
47
+ ::Rouge::Formatter.find('html_inline').new(inline_theme)
48
+ else
49
+ ::Rouge::Formatter.find('html').new
50
+ end
51
+
52
+ @callout_markers = callout_markers || {}
53
+ @inner = inner
54
+ @highlighted_lines = highlighted_lines || []
55
+ @highlighted_class = highlighted_class
35
56
  @line_id = line_id
36
57
  @line_class = line_class
37
58
  end
38
59
 
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
60
+ def stream(tokens, &block)
61
+ token_lines(tokens).with_index(1) do |line_tokens, lno|
62
+ stream_lines(line_tokens, lno, &block)
63
+ end
64
+ end
51
65
 
52
- yield highlight_close if highlighted
53
- yield line_close
66
+ # Formats tokens on the specified line into HTML.
67
+ #
68
+ # @param tokens [Array<Rouge::Token>] tokens on the line.
69
+ # @param line_nums [Integer] the line number (1-based).
70
+ # @yield [String] gives formatted content.
71
+ def stream_lines(tokens, line_num)
72
+ yield line_start(line_num)
54
73
 
55
- lno += 1
74
+ tokens.each do |token, value|
75
+ yield @inner.span(token, value)
56
76
  end
77
+
78
+ yield line_end(line_num)
57
79
  end
58
80
 
59
81
  protected
60
82
 
61
- def line_open(lno)
62
- %(<span id=#{sprintf(@line_id, lno).inspect} class=#{@line_class.inspect}>)
63
- end
83
+ def line_start(line_num)
84
+ classes = [
85
+ @line_class,
86
+ (@highlighted_class if highlighted? line_num),
87
+ ].compact.join(' ')
64
88
 
65
- def line_close
66
- %(</span>)
89
+ [
90
+ ("\n" if line_num > 1),
91
+ '<span',
92
+ (" id=#{sprintf(@line_id, line_num).inspect}" if @line_id),
93
+ (" class=#{classes.inspect}" if !classes.empty?),
94
+ '>',
95
+ ].compact.join
67
96
  end
68
97
 
69
- def highlight_open
70
- %(<strong class=#{@highlight_class.inspect}>)
98
+ def line_end(line_num)
99
+ %(#{@callout_markers[line_num]}</span>)
71
100
  end
72
101
 
73
- def highlight_close
74
- %(</strong>)
102
+ def highlighted?(line_num)
103
+ @highlighted_lines.include?(line_num)
75
104
  end
76
105
  end
77
106
  end
@@ -51,8 +51,10 @@ module Asciidoctor::Rouge
51
51
 
52
52
  # @param block [Asciidoctor::Block] the listing block to highlight.
53
53
  def process_listing(block)
54
+ document = block.document
54
55
  source = block.source # String
55
56
  subs = block.subs # Array<Symbol>
57
+ opts = {}
56
58
 
57
59
  # Don't escape special characters, Rouge will take care of it.
58
60
  subs.delete(:specialcharacters)
@@ -74,12 +76,18 @@ module Asciidoctor::Rouge
74
76
  lexer = find_lexer(lang)
75
77
  block.set_attr('language', lexer.tag)
76
78
 
79
+ if document.attr?('rouge-css', 'style')
80
+ opts[:inline_theme] = document.attr('rouge-theme', 'github')
81
+ end
82
+
77
83
  if block.attr?('highlight', nil, false)
78
- highlight_lines = block.resolve_highlight_lines(block.attr('highlight', '', false))
84
+ highlight = block.attr('highlight', '', false)
85
+ opts[:highlighted_lines] = block.resolve_highlight_lines(highlight)
79
86
  end
80
87
 
81
- result = highlight(lexer, source, highlight_lines)
82
- result = callouts.restore(result) if callouts
88
+ opts[:callout_markers] = callouts.method(:convert_line) if callouts
89
+
90
+ result = highlight(source, lexer, opts)
83
91
  result = passthroughs.restore(result) if passthroughs
84
92
 
85
93
  block.lines.replace(result.split("\n"))
@@ -91,17 +99,16 @@ module Asciidoctor::Rouge
91
99
  (::Rouge::Lexer.find(language) || ::Rouge::Lexers::PlainText).new
92
100
  end
93
101
 
94
- # @param lexer [Rouge::Lexer] the lexer to use.
95
102
  # @param source [String] the code to highlight.
96
- # @param highlight_lines [Array<Integer>] a list of line numbers (1-based)
97
- # to be highlighted.
103
+ # @param lexer [Rouge::Lexer] the lexer to use.
104
+ # @param opts [Hash] extra options for the formatter; it will be merged
105
+ # with the +formatter_opts+ (see {#initialize}).
98
106
  # @return [String] a highlighted and formatted _source_.
99
- def highlight(lexer, source, highlight_lines = [])
107
+ def highlight(source, lexer, opts = {})
100
108
  tokens = lexer.lex(source)
101
109
 
102
110
  if @formatter.method(:format).arity.abs > 1
103
- opts = @formatter_opts.merge(highlight_lines: highlight_lines || [])
104
- @formatter.format(tokens, opts)
111
+ @formatter.format(tokens, @formatter_opts.merge(opts))
105
112
  else
106
113
  @formatter.format(tokens)
107
114
  end
@@ -2,6 +2,6 @@
2
2
 
3
3
  module Asciidoctor
4
4
  module Rouge
5
- VERSION = '0.1.1'.freeze
5
+ VERSION = '0.2.0'.freeze
6
6
  end
7
7
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: asciidoctor-rouge
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jakub Jirutka
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2017-09-20 00:00:00.000000000 Z
11
+ date: 2017-09-21 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: asciidoctor