lib/asciidoctor/syntax_highlighter/pygments.rb in asciidoctor-2.0.16 vs lib/asciidoctor/syntax_highlighter/pygments.rb in asciidoctor-2.0.17

- old
+ new

@@ -34,11 +34,17 @@ opts[:callouts] ? [highlighted, (idx = highlighted.index CodeCellStartTagCs) ? idx + CodeCellStartTagCs.length : nil] : highlighted else node.sub_source source, false # handles nil response from ::Pygments::Lexer#highlight end elsif (highlighted = lexer.highlight source, options: highlight_opts) - highlighted = highlighted.gsub StyledLinenoSpanTagRx, LinenoSpanTagCs if linenos && noclasses + if linenos + if noclasses + highlighted = highlighted.gsub StyledLinenoSpanTagRx, LinenoSpanTagCs + elsif highlighted.include? LegacyLinenoSpanStartTagCs + highlighted = highlighted.gsub LegacyLinenoSpanTagRx, LinenoSpanTagCs + end + end highlighted.sub WrapperTagRx, '\1' else node.sub_source source, false # handles nil response from ::Pygments::Lexer#highlight end end @@ -112,38 +118,40 @@ style end end @@stylesheet_cache = ::Hash.new do |cache, key| if (stylesheet = ::Pygments.css BASE_SELECTOR, classprefix: TOKEN_CLASS_PREFIX, style: key) + stylesheet = stylesheet.slice (stylesheet.index BASE_SELECTOR), stylesheet.length unless stylesheet.start_with? BASE_SELECTOR @@stylesheet_cache = cache.merge key => stylesheet stylesheet end end DEFAULT_STYLE = 'default' BASE_SELECTOR = 'pre.pygments' TOKEN_CLASS_PREFIX = 'tok-' - BaseStyleRx = /^#{BASE_SELECTOR.gsub '.', '\\.'} +\{([^}]+?)\}/ private_constant :BASE_SELECTOR, :TOKEN_CLASS_PREFIX, :BaseStyleRx end extend Styles # exports static methods include Styles # adds methods to instance include Loader # adds methods to instance CodeCellStartTagCs = '<td class="code">' + LegacyLinenoSpanStartTagCs = '<span class="lineno">' + LegacyLinenoSpanTagRx = %r(#{LegacyLinenoSpanStartTagCs}( *\d+) ?</span>) LinenoColumnStartTagsCs = '<td class="linenos"><div class="linenodiv"><pre>' - LinenoSpanTagCs = '<span class="lineno">\1 </span>' + LinenoSpanTagCs = '<span class="linenos">\1</span>' PreTagCs = '<pre>\1</pre>' StyledLinenoColumnStartTagsRx = /<td><div class="linenodiv" style="[^"]+?"><pre style="[^"]+?">/ StyledLinenoSpanTagRx = %r((?<=^|<span></span>)<span style="[^"]+">( *\d+) ?</span>) WRAPPER_CLASS = 'lineno' # doesn't appear in output; Pygments appends "table" to this value to make nested table class # NOTE <pre> has style attribute when pygments-css=style # NOTE <div> has trailing newline when pygments-linenums-mode=table # NOTE initial <span></span> preserves leading blank lines WrapperTagRx = %r(<div class="#{WRAPPER_CLASS}"><pre\b[^>]*?>(.*)</pre></div>\n*)m - private_constant :CodeCellStartTagCs, :LinenoColumnStartTagsCs, :LinenoSpanTagCs, :PreTagCs, :StyledLinenoColumnStartTagsRx, :StyledLinenoSpanTagRx, :WrapperTagRx, :WRAPPER_CLASS + private_constant :CodeCellStartTagCs, :LegacyLinenoSpanStartTagCs, :LegacyLinenoSpanTagRx, :LinenoColumnStartTagsCs, :LinenoSpanTagCs, :PreTagCs, :StyledLinenoColumnStartTagsRx, :StyledLinenoSpanTagRx, :WrapperTagRx, :WRAPPER_CLASS end end