From 1a2d23e9e2718ed6bd62faf74f5a9bfc8879fb3c Mon Sep 17 00:00:00 2001 From: John MacFarlane Date: Fri, 21 Nov 2014 11:17:09 -0800 Subject: commonmark.rb - implemented headers. --- commonmark.rb | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) (limited to 'commonmark.rb') 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("", level) + children.each { |x| render(x) } + self.outf("\n", level) + end + def paragraph(children) self.out("

") children.each { |x| render(x) } -- cgit v1.2.3