Class: Asciidoctor::SyntaxHighlighter::CodeRayAdapter

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

#format

Methods included from Asciidoctor::SyntaxHighlighter

#format

Methods included from DefaultFactory

#for, #register

Methods included from Factory

#create, #for, #register

Constructor Details

#initialize(*args) ⇒ CodeRayAdapter

Returns a new instance of CodeRayAdapter.



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

def initialize *args
  super
  @pre_class = 'CodeRay'
  @requires_stylesheet = nil
end

Instance Method Details

#docinfo(location, doc, opts) ⇒ Object



38
39
40
41
42
43
44
45
46
# File 'lib/asciidoctor/syntax_highlighter/coderay.rb', line 38

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

#docinfo?(location) ⇒ Boolean

Returns:

  • (Boolean)


34
35
36
# File 'lib/asciidoctor/syntax_highlighter/coderay.rb', line 34

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

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



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/asciidoctor/syntax_highlighter/coderay.rb', line 16

def highlight node, source, lang, opts
  @requires_stylesheet = true if (css_mode = opts[:css_mode]) == :class
  lang = lang ? (::CodeRay::Scanners[lang = lang.to_sym] && lang rescue :text) : :text
  highlighted = ::CodeRay::Duo[lang, :html,
    css: css_mode,
    line_numbers: (line_numbers = opts[:number_lines]),
    line_number_start: opts[:start_line_number],
    line_number_anchors: false,
    highlight_lines: opts[:highlight_lines],
    bold_every: false,
  ].highlight source
  if line_numbers == :table && 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/coderay.rb', line 12

def highlight?
  library_available?
end

#write_stylesheet(doc, to_dir) ⇒ Object



52
53
54
# File 'lib/asciidoctor/syntax_highlighter/coderay.rb', line 52

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

#write_stylesheet?(doc) ⇒ Boolean

Returns:

  • (Boolean)


48
49
50
# File 'lib/asciidoctor/syntax_highlighter/coderay.rb', line 48

def write_stylesheet? doc
  @requires_stylesheet
end