diff options
author | John MacFarlane <jgm@berkeley.edu> | 2014-11-20 23:31:13 -0800 |
---|---|---|
committer | John MacFarlane <jgm@berkeley.edu> | 2014-11-20 23:31:13 -0800 |
commit | 382cde3793431fe2bae9fe9f836ac4a68f999f68 (patch) | |
tree | ea7d1d1a1458ecd81843bdd625f60d93f81b0e85 | |
parent | 2ed5b4bd6edff79e2a3cf55a7c43869a2c9062cc (diff) |
commonmark.rb: more progress.
-rw-r--r-- | commonmark.rb | 38 |
1 files changed, 28 insertions, 10 deletions
diff --git a/commonmark.rb b/commonmark.rb index 06366c5..b782f1b 100644 --- a/commonmark.rb +++ b/commonmark.rb @@ -1,4 +1,5 @@ require 'ffi' +require 'stringio' module CMark extend FFI::Library @@ -23,7 +24,7 @@ module CMark attach_function :cmark_node_get_string_content, [:node], :string class Node - attr_accessor :type, :children, :string_content + attr_accessor :type, :children, :string_content, :pointer def initialize(pointer) if pointer.null? return nil @@ -38,29 +39,46 @@ module CMark b = CMark::cmark_node_next(b) end @string_content = CMark::cmark_node_get_string_content(pointer) - # Free? + if @type == :document + self.free + end end - def print - printf("%s\n", self.type) - if self.string_content - printf("'%s'\n", self.string_content) - end - for child in self.children do - child.print + def to_html_stream(file) + file.printf("[%s]\n", self.type.to_s) + case self.type + when :document + self.children.each do |child| + child.to_html_stream(file) + end + when :paragraph + self.children.each do |child| + child.to_html_stream(file) + end + when :str + file.puts(self.string_content) + else end end + def to_html + self.to_html_stream(StringIO.new) + end + def self.from_markdown(s) len = s.bytes.length Node.new(CMark::cmark_parse_document(s, len)) end + + def free + CMark::cmark_free_nodes(@pointer) + end end end doc = CMark::Node.from_markdown(STDIN.read()) -doc.print +doc.to_html_stream(STDOUT) # def markdown_to_html(s) # len = s.bytes.length |