summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJohn MacFarlane <jgm@berkeley.edu>2014-11-21 11:17:09 -0800
committerJohn MacFarlane <jgm@berkeley.edu>2014-11-21 11:17:09 -0800
commit1a2d23e9e2718ed6bd62faf74f5a9bfc8879fb3c (patch)
tree824200f36d2f3a29c059c49a94cca37ef73b1d0c
parentf4947a4a82b212f524c42a437a4cde6c7655144b (diff)
commonmark.rb - implemented headers.
-rw-r--r--commonmark.rb18
1 files changed, 16 insertions, 2 deletions
diff --git a/commonmark.rb b/commonmark.rb
index 7c9c6d2..4546845 100644
--- 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) }