Class: Asciidoctor::SyntaxHighlighter::PygmentsAdapter

Inherits:
Base
  • Object
show all
Extended by:
Styles
Includes:
Loader, Styles
Defined in:
lib/asciidoctor/syntax_highlighter/pygments.rb

Defined Under Namespace

Modules: Loader, Styles

Constant Summary

Constants included from DefaultFactory

DefaultFactory::PROVIDED

Instance Attribute Summary

Attributes included from Asciidoctor::SyntaxHighlighter

#name

Instance Method Summary collapse

Methods included from Styles

read_stylesheet, stylesheet_basename

Methods included from DefaultFactory

#for, #register

Methods included from Factory

#create, #for, #register

Constructor Details

#initialize(*args) ⇒ PygmentsAdapter

Returns a new instance of PygmentsAdapter.



6
7
8
9
10
# File 'lib/asciidoctor/syntax_highlighter/pygments.rb', line 6

def initialize *args
  super
  @requires_stylesheet = nil
  @style = nil
end

Instance Method Details

#docinfo(location, doc, opts) ⇒ Object



59
60
61
62
63
64
65
66
67
# File 'lib/asciidoctor/syntax_highlighter/pygments.rb', line 59

def docinfo location, doc, opts
  if opts[:linkcss]
    %(<link rel="stylesheet" href="#{doc.normalize_web_path (stylesheet_basename @style), (doc.attr 'stylesdir', ''), false}"#{opts[:self_closing_tag_slash]}>)
  else
    %(<style>
#{read_stylesheet @style}
</style>)
  end
end

#docinfo?(location) ⇒ Boolean

Returns:

  • (Boolean)


55
56
57
# File 'lib/asciidoctor/syntax_highlighter/pygments.rb', line 55

def docinfo? location
  @requires_stylesheet && location == :footer
end

#format(node, lang, opts) ⇒ Object



47
48
49
50
51
52
53
# File 'lib/asciidoctor/syntax_highlighter/pygments.rb', line 47

def format node, lang, opts
  if opts[:css_mode] != :class && (@style = (style = opts[:style]) && (style_available? style) || DEFAULT_STYLE) &&
      (pre_style_attr_val = base_style @style)
    opts[:transform] = proc {|pre| pre['style'] = pre_style_attr_val }
  end
  super
end

#highlight(node, source, lang, opts) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/asciidoctor/syntax_highlighter/pygments.rb', line 16

def highlight node, source, lang, opts
  lexer = (::Pygments::Lexer.find_by_alias lang) || (::Pygments::Lexer.find_by_mimetype 'text/plain')
  @requires_stylesheet = true unless (noclasses = opts[:css_mode] != :class)
  highlight_opts = {
    classprefix: TOKEN_CLASS_PREFIX,
    cssclass: WRAPPER_CLASS,
    nobackground: true,
    noclasses: noclasses,
    startinline: lexer.name == 'PHP' && !(node.option? 'mixed'),
    stripnl: false,
    style: (@style ||= (style = opts[:style]) && (style_available? style) || DEFAULT_STYLE),
  }
  if (highlight_lines = opts[:highlight_lines])
    highlight_opts[:hl_lines] = highlight_lines.join ' '
  end
  if (linenos = opts[:number_lines]) && (highlight_opts[:linenostart] = opts[:start_line_number]) && (highlight_opts[:linenos] = linenos) == :table
    if (highlighted = lexer.highlight source, options: highlight_opts)
      highlighted = highlighted.sub StyledLinenoColumnStartTagsRx, LinenoColumnStartTagsCs if noclasses
      highlighted = highlighted.sub WrapperTagRx, PreTagCs
      opts[:callouts] ? [highlighted, (idx = highlighted.index CodeCellStartTagCs) ? idx + CodeCellStartTagCs.length : nil] : highlighted
    else
      node.sub_specialchars source # handles nil response from ::Pygments::Lexer#highlight
    end
  elsif (highlighted = lexer.highlight source, options: highlight_opts)
    highlighted = highlighted.gsub StyledLinenoSpanTagRx, LinenoSpanTagCs if linenos && noclasses
    highlighted.sub WrapperTagRx, '\1'
  else
    node.sub_specialchars source # handles nil response from ::Pygments::Lexer#highlight
  end
end

#highlight?Boolean

Returns:

  • (Boolean)


12
13
14
# File 'lib/asciidoctor/syntax_highlighter/pygments.rb', line 12

def highlight?
  library_available?
end

#write_stylesheet(doc, to_dir) ⇒ Object



73
74
75
# File 'lib/asciidoctor/syntax_highlighter/pygments.rb', line 73

def write_stylesheet doc, to_dir
  ::File.write (::File.join to_dir, (stylesheet_basename @style)), (read_stylesheet @style), mode: FILE_WRITE_MODE
end

#write_stylesheet?(doc) ⇒ Boolean

Returns:

  • (Boolean)


69
70
71
# File 'lib/asciidoctor/syntax_highlighter/pygments.rb', line 69

def write_stylesheet? doc
  @requires_stylesheet
end