cmark

Unnamed repository; edit this file 'description' to name the repository.

commit 1a2d23e9e2718ed6bd62faf74f5a9bfc8879fb3c
parent f4947a4a82b212f524c42a437a4cde6c7655144b
Author: John MacFarlane <jgm@berkeley.edu>
Date:   Fri, 21 Nov 2014 11:17:09 -0800

commonmark.rb - implemented headers.

Diffstat:
Mcommonmark.rb | 18++++++++++++++++--
1 file changed, 16 insertions(+), 2 deletions(-)
diff --git a/commonmark.rb b/commonmark.rb
@@ -23,10 +23,11 @@ module CMark
   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, :pointer
+  attr_accessor :type, :children, :string_content, :header_level
   def initialize(pointer)
     if pointer.null?
       return nil
@@ -41,6 +42,7 @@ class Node
       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
@@ -72,7 +74,7 @@ class Renderer
   end
 
   def out(arg)
-    @stream.puts(arg)
+    @stream.write(arg)
   end
 
   def render(node)
@@ -84,6 +86,8 @@ class Renderer
       end
     when :paragraph
       self.paragraph(node.children)
+    when :setext_header, :atx_header
+      self.header(node.header_level, node.children)
     when :str
       self.str(node.string_content)
     else
@@ -95,6 +99,10 @@ class Renderer
     children.each { |x| render(x) }
   end
 
+  def header(level, children)
+    children.each { |x| render(x) }
+  end
+
   def paragraph(children)
     children.each { |x| render(x) }
   end
@@ -105,6 +113,12 @@ class Renderer
 end
 
 class HtmlRenderer < Renderer
+  def header(level, children)
+    self.outf("<h%d>", level)
+    children.each { |x| render(x) }
+    self.outf("</h%d>\n", level)
+  end
+
   def paragraph(children)
     self.out("<p>")
     children.each { |x| render(x) }