Class: Asciidoctor::Prawn::CodeRayEncoder

Inherits:
CodeRay::Encoders::Encoder
  • Object
show all
Defined in:
lib/asciidoctor/pdf/ext/prawn/coderay_encoder.rb

Constant Summary collapse

COLORS =

Manni theme from Pygments

{
  default: '333333',
  annotation: '9999FF',
  attribute_name: '4F9FCF',
  attribute_value: 'D44950',
  class: '00AA88',
  class_variable: '003333',
  color: 'FF6600',
  comment: '999999',
  constant: '336600',
  directive: '006699',
  doctype: '009999',
  entity: '999999',
  float: 'FF6600',
  function: 'CC00FF',
  important: '9999FF',
  inline_delimiter: 'EF804F',
  instance_variable: '003333',
  integer: 'FF6600',
  key: '006699',
  keyword: '006699',
  method: 'CC00FF',
  namespace: '00CCFF',
  predefined_type: '007788',
  regexp: '33AAAA',
  string: 'CC3300',
  symbol: 'FFCC33',
  tag: '2F6F9F',
  type: '007788',
  value: '336600',
}
LF =
?\n
NoBreakSpace =
?\u00a0
InnerIndent =
LF + ' '
GuardedIndent =
?\u00a0
GuardedInnerIndent =
LF + GuardedIndent

Instance Method Summary collapse

Instance Method Details

#begin_group(kind) ⇒ Object



97
98
99
# File 'lib/asciidoctor/pdf/ext/prawn/coderay_encoder.rb', line 97

def begin_group kind
  @open << kind
end

#end_group(_kind) ⇒ Object



101
102
103
# File 'lib/asciidoctor/pdf/ext/prawn/coderay_encoder.rb', line 101

def end_group _kind
  @open.pop
end

#setup(options) ⇒ Object



68
69
70
71
72
73
74
# File 'lib/asciidoctor/pdf/ext/prawn/coderay_encoder.rb', line 68

def setup options
  super
  @out  = []
  @open = []
  # NOTE: tracks whether text token begins at the start of a line
  @start_of_line = true
end

#text_token(text, kind) ⇒ Object



76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/asciidoctor/pdf/ext/prawn/coderay_encoder.rb', line 76

def text_token text, kind
  if text == LF
    @out << { text: text }
    @start_of_line = true
  # NOTE: text is nil and kind is :error when CodeRay ends parsing on an error
  elsif text
    # NOTE: add guard character to prevent Prawn from trimming indentation
    text[0] = GuardedIndent if @start_of_line && (text.start_with? ' ')
    text.gsub! InnerIndent, GuardedInnerIndent if text.include? InnerIndent

    # NOTE: this optimization assumes we don't support/use background colors
    if text.rstrip.empty?
      @out << { text: text }
    else
      # QUESTION: should we default to no color?
      @out << { text: text, color: (COLORS[kind] || COLORS[@open[-1]] || COLORS[:default]) }
    end
    @start_of_line = text.end_with? LF
  end
end