summaryrefslogtreecommitdiff
path: root/commonmark.rb
blob: 32a4a72a2e76edb5de21720a13f9d5b8a84ba165 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
require 'ffi'
require 'stringio'
require 'cgi'
require 'set'

module CMark
  extend FFI::Library
  ffi_lib ['libcmark', 'cmark']
  typedef :pointer, :node
  enum :node_type, [:document, :blockquote, :list, :list_item,
                    :fenced_code, :indented_code, :html, :paragraph,
                    :atx_header, :setext_header, :hrule, :reference_def,
                    :str, :softbreak, :linebreak, :code, :inline_html,
                    :emph, :strong, :link, :image]

  attach_function :cmark_free_nodes, [:node], :void
  attach_function :cmark_node_unlink, [:node], :void

  attach_function :cmark_markdown_to_html, [:string, :int], :string
  attach_function :cmark_parse_document, [:string, :int], :node
  attach_function :cmark_node_first_child, [:node], :node
  attach_function :cmark_node_parent, [:node], :node
  attach_function :cmark_node_next, [:node], :node
  attach_function :cmark_node_previous, [:node], :node
  attach_function :cmark_node_get_type, [:node], :node_type
  attach_function :cmark_node_get_string_content, [:node], :string
  attach_function :cmark_node_get_header_level, [:node], :int
end

class Node
  attr_accessor :type, :children, :string_content, :header_level
  def initialize(pointer)
    if pointer.null?
      return nil
    end
    @pointer = pointer
    @type = CMark::cmark_node_get_type(pointer)
    @children = []
    first_child = CMark::cmark_node_first_child(pointer)
    b = first_child
    while !b.null?
      @children << Node.new(b)
      b = CMark::cmark_node_next(b)
    end
    @string_content = CMark::cmark_node_get_string_content(pointer)
    @header_level = CMark::cmark_node_get_header_level(pointer)
    if @type == :document
      self.free
    end
  end

  def self.parse_string(s)
    Node.new(CMark::cmark_parse_document(s, s.bytesize))
  end

  def self.parse_file(f)
    s = f.read()
    self.parse_string(s)
  end

  def free
      CMark::cmark_free_nodes(@pointer)
  end
end

class Renderer
  attr_reader :warnings
  def initialize(stream = nil)
    if stream
      @stream = stream
      @stringwriter = false
    else
      @stringwriter = true
      @stream = StringIO.new
    end
    @need_blocksep = false
    @warnings = Set.new []
  end

  def outf(format, *args)
    @stream.printf(format, *args)
  end

  def out(*args)
    args.each do |arg|
      if arg.kind_of?(String)
        @stream.write(arg)
      elsif arg.kind_of?(Node)
        self.render(arg)
      elsif arg.kind_of?(Array)
        arg.each { |x| self.out(x) }
      else
        @stream.write(arg)
      end
    end
  end

  def render(node)
    @node = node
    if node.type == :document
      self.document(node)
      self.out("\n")
      if @stringwriter
        return @stream.string
      end
    else
      begin
        self.send(node.type, node)
      rescue NoMethodError
        @warnings.add("WARNING:  " + node.type.to_s + " not implemented.")
        self.out(node.children)
      end
    end
  end

  def document(node)
    self.out(node.children)
  end

  def indented_code(node)
    self.code_block(node)
  end

  def fenced_code(node)
    self.code_block(node)
  end

  def setext_header(node)
    self.header(node)
  end

  def atx_header(node)
    self.header(node)
  end

  def reference_def(node)
  end

  def blocksep
    self.out("\n\n")
  end

  def asblock(&blk)
    if @need_blocksep
      self.blocksep
    end
    blk.call
    @need_blocksep = true
  end
end

class HtmlRenderer < Renderer
  def header(node)
    asblock do
      self.out("<h", node.header_level, ">", node.children,
             "</h", node.header_level, ">")
    end
  end

  def paragraph(node)
    asblock do
      self.out("<p>", node.children, "</p>")
    end
  end

  def hrule(node)
    asblock do
      self.out("<hr />")
    end
  end

  def code_block(node)
    asblock do
      self.out("<pre><code>")
      self.out(CGI.escapeHTML(node.string_content))
      self.out("</code></pre>")
    end
  end

  def emph(node)
    self.out("<em>", node.children, "</em>")
  end

  def strong(node)
    self.out("<strong>", node.children, "</strong>")
  end

  def str(node)
    self.out(CGI.escapeHTML(node.string_content))
  end

  def code(node)
    self.out("<code>")
    self.out(CGI.escapeHTML(node.string_content))
    self.out("</code>")
  end

  def softbreak(node)
    self.out("\n")
  end
end

doc = Node.parse_file(STDIN)
renderer = HtmlRenderer.new(STDOUT)
renderer.render(doc)
renderer.warnings.each do |w|
  STDERR.write(w)
  STDERR.write("\n")
end
# def markdown_to_html(s)
#   len = s.bytes.length
#   CMark::cmark_markdown_to_html(s, len)
# end
# print markdown_to_html(STDIN.read())