summaryrefslogtreecommitdiff
path: root/commonmark.rb
blob: dead84c3a6f76772176192ddb44c3c6b4d138bf2 (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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
#!/usr/bin/env ruby
require 'ffi'
require 'stringio'
require 'cgi'
require 'set'
require 'uri'

module CMark
  extend FFI::Library
  ffi_lib ['libcmark', 'cmark']
  typedef :pointer, :node
  enum :node_type, [:document, :blockquote, :list, :list_item,
                    :code_block, :html, :paragraph,
                    :header, :hrule, :reference_def,
                    :str, :softbreak, :linebreak, :code, :inline_html,
                    :emph, :strong, :link, :image]
  enum :list_type, [:no_list, :bullet_list, :ordered_list]

  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_url, [:node], :string
  attach_function :cmark_node_get_title, [:node], :string
  attach_function :cmark_node_get_header_level, [:node], :int
  attach_function :cmark_node_get_list_type, [:node], :list_type
  attach_function :cmark_node_get_list_start, [:node], :int
  attach_function :cmark_node_get_list_tight, [:node], :bool
end

class Node
  attr_accessor :type, :children, :parent, :string_content, :header_level,
                :list_type, :list_start, :list_tight, :url, :title
  def initialize(pointer)
    if pointer.null?
      return nil
    end
    @pointer = pointer
    @type = CMark::cmark_node_get_type(pointer)
    @children = []
    @parent = nil
    first_child = CMark::cmark_node_first_child(pointer)
    b = first_child
    while !b.null?
      child = Node.new(b)
      child.parent = self
      @children << child
      b = CMark::cmark_node_next(b)
    end
    @string_content = CMark::cmark_node_get_string_content(pointer)
    if @type == :header
      @header_level = CMark::cmark_node_get_header_level(pointer)
    end
    if @type == :list
      @list_type = CMark::cmark_node_get_list_type(pointer)
      @list_start = CMark::cmark_node_get_list_start(pointer)
      @list_tight = CMark::cmark_node_get_list_tight(pointer)
    end
    if @type == :link || @type == :image
      @url = CMark::cmark_node_get_url(pointer)
      if !@url then @url = "" end
      @title = CMark::cmark_node_get_title(pointer)
      if !@title then @title = "" end
    end
    if @type == :document
      self.free
    end
  end

  # An iterator that "walks the tree," returning each node
  def walk(&blk)
    yield self
    self.children.each do |child|
      child.walk(&blk)
    end
  end

  # Walk the tree and transform it.  blk should take one argument,
  # a node.  If its value is a node, that node replaces the node being
  # visited. If its value is an array of nodes, those nodes are spliced
  # in place of the node being visited (so, to delete a node, use an
  # empty array).  Otherwise the node is left as it is.
  def transform(&blk)
    self.walk do |node|
      skip = false
      res = blk.call(node)
      if res.kind_of?(Array)
        splice = res
      elsif res.kind_of?(Node)
        splice = [res]
      else
        skip = true
      end
      unless skip
        parent = node.parent
        if parent
          siblings = node.parent.children
          index = siblings.index(node)
          siblings.replace(siblings.slice(0,index) + splice +
                           siblings.slice(index + 1, siblings.length))
        else # at the document root, just skip
        end
      end
    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

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

class Renderer
  attr_accessor :in_tight, :warnings, :in_plain
  def initialize(stream = nil)
    if stream
      @stream = stream
      @stringwriter = false
    else
      @stringwriter = true
      @stream = StringIO.new
    end
    @need_blocksep = false
    @warnings = Set.new []
    @in_tight = false
    @in_plain = false
  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
    elsif self.in_plain && node.type != :str && node.type != :softbreak
      # pass through looking for str, softbreak
      node.children.each do |child|
        render(child)
      end
    else
      begin
        self.send(node.type, node)
      rescue NoMethodError => e
        @warnings.add("WARNING:  " + node.type.to_s + " not implemented.")
        raise e
      end
    end
  end

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

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

  def reference_def(node)
  end

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

  def containersep
    if !self.in_tight
      self.out("\n")
    end
  end

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

  def container(starter, ender, &blk)
    self.out(starter)
    self.containersep
    @need_blocksep = false
    blk.call
    self.containersep
    self.out(ender)
  end

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

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

  def paragraph(node)
    block do
      if self.in_tight
        self.out(node.children)
      else
        self.out("<p>", node.children, "</p>")
      end
    end
  end

  def list(node)
    old_in_tight = self.in_tight
    self.in_tight = node.list_tight
    block do
      if node.list_type == :bullet_list
        container("<ul>", "</ul>") do
          self.out(node.children)
        end
      else
        start = node.list_start == 1 ? '' :
                (' start="' + node.list_start.to_s + '"')
        container(start, "</ol>") do
          self.out(node.children)
        end
      end
    end
    self.in_tight = old_in_tight
  end

  def list_item(node)
    block do
      container("<li>", "</li>") do
        self.out(node.children)
      end
    end
  end

  def blockquote(node)
    block do
      container("<blockquote>", "</blockquote>") do
        self.out(node.children)
      end
    end
  end

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

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

  def html(node)
    block do
      self.out(node.string_content)
    end
  end

  def inline_html(node)
    self.out(node.string_content)
  end

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

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

  def link(node)
    self.out('<a href="', URI.escape(node.url), '"')
    if node.title && node.title.length > 0
      self.out(' title="', CGI.escapeHTML(node.title), '"')
    end
    self.out('>', node.children, '</a>')
  end

  def image(node)
    self.out('<img src="', URI.escape(node.url), '"')
    if node.title && node.title.length > 0
      self.out(' title="', CGI.escapeHTML(node.title), '"')
    end
    plain do
      self.out(' alt="', node.children, '" />')
    end
  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 linebreak(node)
    self.out("<br/>")
    self.softbreak(node)
  end

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

doc = Node.parse_file(ARGF)

# Walk tree and print URLs for links
doc.walk do |node|
  if node.type == :link
    printf("URL = %s\n", node.url)
  end
end

# Capitalize strings in headers
doc.walk do |node|
  if node.type == :header
    node.walk do |subnode|
      if subnode.type == :str
        subnode.string_content = subnode.string_content.upcase
      end
    end
  end
end

# Walk tree and transform links to regular text
doc.transform do |node|
  if node.type == :link
    node.children
  end
end

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())