Class: CommonMarker::Renderer

Inherits:
Object
  • Object
show all
Defined in:
lib/commonmarker/renderer.rb

Direct Known Subclasses

HtmlRenderer

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeRenderer

Returns a new instance of Renderer.



7
8
9
10
11
12
13
14
# File 'lib/commonmarker/renderer.rb', line 7

def initialize
  @stream = StringIO.new
  @need_blocksep = false
  @warnings = Set.new []
  @in_tight = false
  @in_plain = false
  @buffer = ''
end

Instance Attribute Details

#in_plainObject

Returns the value of attribute in_plain.



6
7
8
# File 'lib/commonmarker/renderer.rb', line 6

def in_plain
  @in_plain
end

#in_tightObject

Returns the value of attribute in_tight.



6
7
8
# File 'lib/commonmarker/renderer.rb', line 6

def in_tight
  @in_tight
end

#warningsObject

Returns the value of attribute warnings.



6
7
8
# File 'lib/commonmarker/renderer.rb', line 6

def warnings
  @warnings
end

Instance Method Details

#block(&blk) ⇒ Object



72
73
74
75
76
# File 'lib/commonmarker/renderer.rb', line 72

def block(&blk)
  cr
  blk.call
  cr
end

#blocksepObject



64
65
66
# File 'lib/commonmarker/renderer.rb', line 64

def blocksep
  out("\n")
end

#code_block(node) ⇒ Object



52
53
54
# File 'lib/commonmarker/renderer.rb', line 52

def code_block(node)
  code_block(node)
end

#container(starter, ender, &blk) ⇒ Object



78
79
80
81
82
# File 'lib/commonmarker/renderer.rb', line 78

def container(starter, ender, &blk)
  out(starter)
  blk.call
  out(ender)
end

#containersepObject



68
69
70
# File 'lib/commonmarker/renderer.rb', line 68

def containersep
  cr unless @in_tight
end

#crObject



59
60
61
62
# File 'lib/commonmarker/renderer.rb', line 59

def cr
  return if @buffer.empty? || @buffer[-1] == "\n"
  out("\n")
end

#document(_node) ⇒ Object



48
49
50
# File 'lib/commonmarker/renderer.rb', line 48

def document(_node)
  out(:children)
end

#out(*args) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/commonmarker/renderer.rb', line 16

def out(*args)
  args.each do |arg|
    if arg == :children
      @node.each_child { |child| out(child) }
    elsif arg.is_a?(Array)
      arg.each { |x| render(x) }
    elsif arg.is_a?(Node)
      render(arg)
    else
      @buffer << arg.to_s.force_encoding('utf-8')
      @stream.write(arg)
    end
  end
end

#plain(&blk) ⇒ Object



84
85
86
87
88
89
# File 'lib/commonmarker/renderer.rb', line 84

def plain(&blk)
  old_in_plain = @in_plain
  @in_plain = true
  blk.call
  @in_plain = old_in_plain
end

#reference_def(_node) ⇒ Object



56
57
# File 'lib/commonmarker/renderer.rb', line 56

def reference_def(_node)
end

#render(node) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/commonmarker/renderer.rb', line 31

def render(node)
  @node = node
  if node.type == :document
    document(node)
    return @stream.string
  elsif @in_plain && node.type != :text && node.type != :softbreak
    node.each_child { |child| render(child) }
  else
    begin
      send(node.type, node)
    rescue NoMethodError => e
      @warnings.add('WARNING:  ' + node.type.to_s + ' not implemented.')
      raise e
    end
  end
end