Class: Asciidoctor::SyntaxHighlighter::RougeAdapter

Inherits:
Base
  • Object
show all
Extended by:
Styles
Includes:
Loader, Styles
Defined in:
lib/asciidoctor/syntax_highlighter/rouge.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) ⇒ RougeAdapter

Returns a new instance of RougeAdapter.



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

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

Instance Method Details

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



75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/asciidoctor/syntax_highlighter/rouge.rb', line 75

def create_formatter node, source, lang, opts
  formatter = opts[:css_mode] == :class ?
    (::Rouge::Formatters::HTML.new inline_theme: @style) :
    (::Rouge::Formatters::HTMLInline.new (::Rouge::Theme.find @style).new)
  if (number_lines = opts[:number_lines])
    formatter = RougeExt::Formatters::HTMLLineHighlighter.new formatter, lines: opts[:highlight_lines]
    number_lines == :table ?
      (RougeExt::Formatters::HTMLTableLineNumberer.new formatter, start_line: opts[:start_line_number]) :
      (RougeExt::Formatters::HTMLLineNumberer.new formatter, start_line: opts[:start_line_number])
  elsif (highlight_lines = opts[:highlight_lines])
    RougeExt::Formatters::HTMLLineHighlighter.new formatter, lines: highlight_lines
  else
    formatter
  end
end

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



61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/asciidoctor/syntax_highlighter/rouge.rb', line 61

def create_lexer node, source, lang, opts
  if lang.include? '?'
    # NOTE cgi-style options only properly supported in Rouge >= 2.1
    if (lexer = ::Rouge::Lexer.find_fancy lang)
      unless lexer.tag != 'php' || (node.option? 'mixed') || ((lexer_opts = lexer.options).key? 'start_inline')
        lexer = lexer.class.new lexer_opts.merge 'start_inline' => true
      end
    end
  elsif (lexer = ::Rouge::Lexer.find lang)
    lexer = lexer.tag == 'php' && !(node.option? 'mixed') ? (lexer.new start_inline: true) : lexer.new
  end if lang
  lexer || ::Rouge::Lexers::PlainText.new
end

#docinfo(location, doc, opts) ⇒ Object



43
44
45
46
47
48
49
50
51
# File 'lib/asciidoctor/syntax_highlighter/rouge.rb', line 43

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)


39
40
41
# File 'lib/asciidoctor/syntax_highlighter/rouge.rb', line 39

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

#format(node, lang, opts) ⇒ Object



29
30
31
32
33
34
35
36
37
# File 'lib/asciidoctor/syntax_highlighter/rouge.rb', line 29

def format node, lang, opts
  if (query_idx = lang && (lang.index '?'))
    lang = lang.slice 0, query_idx
  end
  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
# File 'lib/asciidoctor/syntax_highlighter/rouge.rb', line 16

def highlight node, source, lang, opts
  @style ||= (style = opts[:style]) && (style_available? style) || DEFAULT_STYLE
  @requires_stylesheet = true if opts[:css_mode] == :class
  lexer = create_lexer node, source, lang, opts
  formatter = create_formatter node, source, lang, opts
  highlighted = formatter.format lexer.lex source
  if opts[:number_lines] && opts[:callouts]
    [highlighted, (idx = highlighted.index CodeCellStartTagCs) ? idx + CodeCellStartTagCs.length : nil]
  else
    highlighted
  end
end

#highlight?Boolean

Returns:

  • (Boolean)


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

def highlight?
  library_available?
end

#write_stylesheet(doc, to_dir) ⇒ Object



57
58
59
# File 'lib/asciidoctor/syntax_highlighter/rouge.rb', line 57

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)


53
54
55
# File 'lib/asciidoctor/syntax_highlighter/rouge.rb', line 53

def write_stylesheet? doc
  @requires_stylesheet
end