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

#initialize(options: :DEFAULT, extensions: []) ⇒ Renderer

Returns a new instance of Renderer.



10
11
12
13
14
15
16
17
18
# File 'lib/commonmarker/renderer.rb', line 10

def initialize(options: :DEFAULT, extensions: [])
  @opts = Config.process_options(options, :render)
  @stream = StringIO.new(+'')
  @need_blocksep = false
  @warnings = Set.new []
  @in_tight = false
  @in_plain = false
  @tagfilter = extensions.include?(:tagfilter)
end

Instance Attribute Details

#in_plainObject

Returns the value of attribute in_plain.



8
9
10
# File 'lib/commonmarker/renderer.rb', line 8

def in_plain
  @in_plain
end

#in_tightObject

Returns the value of attribute in_tight.



8
9
10
# File 'lib/commonmarker/renderer.rb', line 8

def in_tight
  @in_tight
end

#warningsObject

Returns the value of attribute warnings.



8
9
10
# File 'lib/commonmarker/renderer.rb', line 8

def warnings
  @warnings
end

Instance Method Details

#blockObject



76
77
78
79
80
# File 'lib/commonmarker/renderer.rb', line 76

def block
  cr
  yield
  cr
end

#blocksepObject



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

def blocksep
  out("\n")
end

#code_block(node) ⇒ Object



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

def code_block(node)
  code_block(node)
end

#container(starter, ender) ⇒ Object



82
83
84
85
86
# File 'lib/commonmarker/renderer.rb', line 82

def container(starter, ender)
  out(starter)
  yield
  out(ender)
end

#containersepObject



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

def containersep
  cr unless @in_tight
end

#crObject



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

def cr
  return if @stream.string.empty? || @stream.string[-1] == "\n"

  out("\n")
end

#document(_node) ⇒ Object



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

def document(_node)
  out(:children)
end

#out(*args) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/commonmarker/renderer.rb', line 20

def out(*args)
  args.each do |arg|
    case arg
    when :children
      @node.each { |child| out(child) }
    when Array
      arg.each { |x| render(x) }
    when Node
      render(arg)
    else
      @stream.write(arg)
    end
  end
end

#plainObject



88
89
90
91
92
93
# File 'lib/commonmarker/renderer.rb', line 88

def plain
  old_in_plain = @in_plain
  @in_plain = true
  yield
  @in_plain = old_in_plain
end

#reference_def(_node) ⇒ Object



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

def reference_def(_node); end

#render(node) ⇒ Object



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/commonmarker/renderer.rb', line 35

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