diff options
| author | Dan Allen <dan.j.allen@gmail.com> | 2015-07-19 16:19:31 -0600 |
|---|---|---|
| committer | Dan Allen <dan.j.allen@gmail.com> | 2015-07-20 02:29:40 -0600 |
| commit | fbd9d5ecffb139f1aecd39d4143d2aef5264c2b2 (patch) | |
| tree | e2ef13deaa0999523d7ab1589cbd9c21808218cb | |
| parent | f4dd6c013204b4c761794bc8cc44c06affe0da47 (diff) | |
resolves #1429 allow text of selected lines to be highlighted in source block
- use highlight attribute on source block to specify lines to be highlighted
- add method to resolve the highlight lines spec
- disable bold_every option in CodeRay
- darken bolded line numbers in default CodeRay theme
| -rw-r--r-- | data/stylesheets/coderay-asciidoctor.css | 2 | ||||
| -rw-r--r-- | lib/asciidoctor/substitutors.rb | 45 |
2 files changed, 44 insertions, 3 deletions
diff --git a/data/stylesheets/coderay-asciidoctor.css b/data/stylesheets/coderay-asciidoctor.css index 1627a5dc..ad53f53d 100644 --- a/data/stylesheets/coderay-asciidoctor.css +++ b/data/stylesheets/coderay-asciidoctor.css @@ -2,7 +2,7 @@ /*pre.CodeRay {background-color:#f7f7f8;}*/ .CodeRay .line-numbers{border-right:1px solid #d8d8d8;padding:0 0.5em 0 .25em} .CodeRay span.line-numbers{display:inline-block;margin-right:.5em;color:rgba(0,0,0,.3)} -.CodeRay .line-numbers strong{font-weight: normal} +.CodeRay .line-numbers strong{color:rgba(0,0,0,.4)} table.CodeRay{border-collapse:separate;border-spacing:0;margin-bottom:0;border:0;background:none} table.CodeRay td{vertical-align: top;line-height:1.45} table.CodeRay td.line-numbers{text-align:right} diff --git a/lib/asciidoctor/substitutors.rb b/lib/asciidoctor/substitutors.rb index 13d163a9..099acd28 100644 --- a/lib/asciidoctor/substitutors.rb +++ b/lib/asciidoctor/substitutors.rb @@ -1454,13 +1454,21 @@ module Substitutors end linenums_mode = nil + highlight_lines = nil case highlighter when 'coderay' + if (linenums_mode = (attr? 'linenums') ? (@document.attributes['coderay-linenums-mode'] || :table).to_sym : nil) + if attr? 'highlight', nil, false + highlight_lines = resolve_lines_to_highlight(attr 'highlight', nil, false) + end + end result = ::CodeRay::Duo[attr('language', :text, false).to_sym, :html, { :css => (@document.attributes['coderay-css'] || :class).to_sym, - :line_numbers => (linenums_mode = ((attr? 'linenums') ? (@document.attributes['coderay-linenums-mode'] || :table).to_sym : nil)), - :line_number_anchors => false}].highlight source + :line_numbers => linenums_mode, + :line_number_anchors => false, + :highlight_lines => highlight_lines, + :bold_every => false}].highlight source when 'pygments' lexer = ::Pygments::Lexer[attr('language', nil, false)] || ::Pygments::Lexer['text'] opts = { :cssclass => 'pyhl', :classprefix => 'tok-', :nobackground => true } @@ -1468,6 +1476,11 @@ module Substitutors opts[:noclasses] = true opts[:style] = (@document.attributes['pygments-style'] || Stylesheets::DEFAULT_PYGMENTS_STYLE) end + if attr? 'highlight', nil, false + unless (highlight_lines = resolve_lines_to_highlight(attr 'highlight', nil, false)).empty? + opts[:hl_lines] = highlight_lines * ' ' + end + end if attr? 'linenums' # TODO we could add the line numbers in ourselves instead of having to strip out the junk # FIXME move these regular expressions into constants @@ -1531,6 +1544,34 @@ module Substitutors end end + # e.g., highlight="1-5, !2, 10" or highlight=1-5;!2,10 + def resolve_lines_to_highlight spec + lines = [] + spec.delete(' ').split(DataDelimiterRx).map do |entry| + negate = false + if entry.start_with? '!' + entry = entry[1..-1] + negate = true + end + if entry.include? '-' + s, e = entry.split '-', 2 + line_nums = (s.to_i..e.to_i).to_a + if negate + lines -= line_nums + else + lines.concat line_nums + end + else + if negate + lines.delete entry.to_i + else + lines << entry.to_i + end + end + end + lines.sort.uniq + end + # Public: Apply verbatim substitutions on source (for use when highlighting is disabled). # # source - the source code String on which to apply verbatim substitutions |
