Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Start wrapping C in Ruby #1

Merged
merged 74 commits into from
May 13, 2015
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
74 commits
Select commit Hold shift + click to select a range
cd7da61
Add cmark submodule
gjtorikian May 7, 2015
a9e113c
Add script to update gems + submodule
gjtorikian May 7, 2015
944a8b6
Ignore things that need ignoring
gjtorikian May 7, 2015
b7dcd32
Ignore modifications to submodule
gjtorikian May 7, 2015
358f4ef
Produce a C bundle for the Ruby gem
gjtorikian May 7, 2015
dfeb041
Indicate extension in gemspec
gjtorikian May 7, 2015
7f8d244
Bring all the files over into the gemspec
gjtorikian May 7, 2015
eedac36
Define VERSION in a separate location
gjtorikian May 7, 2015
c1f3aa3
Trash lib-ffi
gjtorikian May 7, 2015
a4e0e12
Add script to run a single test
gjtorikian May 7, 2015
e2e5902
Simplify test setup
gjtorikian May 7, 2015
1858c19
Start creating nodes and documents
gjtorikian May 7, 2015
89c3606
Clean up node types in Ruby
gjtorikian May 8, 2015
d2ff420
Uncomment compilation in single test
gjtorikian May 8, 2015
a6fbd23
Support walking down the document
gjtorikian May 9, 2015
a4554d1
Finish all the basic tests
gjtorikian May 10, 2015
089e8f0
Add good ol' Travis file
gjtorikian May 10, 2015
a51ca31
Somehow, I'm in control now
gjtorikian May 10, 2015
d3c081c
typo
gjtorikian May 10, 2015
ba3f222
Remove this line
gjtorikian May 10, 2015
9445691
come on, travis
gjtorikian May 10, 2015
af8d3fc
This line is not needed here
gjtorikian May 10, 2015
2fb7218
We need a newer cmake
gjtorikian May 10, 2015
40d351c
Updates here and there
gjtorikian May 10, 2015
b2c54bb
Flippin' YAML
gjtorikian May 10, 2015
b4b42bd
Update spec test update
gjtorikian May 10, 2015
3289df7
Split out files
gjtorikian May 10, 2015
c580693
Fix several style issues
gjtorikian May 10, 2015
ea12974
Simplify if statement
gjtorikian May 10, 2015
d692e64
omg sudo
gjtorikian May 10, 2015
6ae90bf
Add a bunch of missing methods
gjtorikian May 10, 2015
ca40f90
Try -fPIC for Travis
gjtorikian May 10, 2015
e736b19
Use proper .so extension for Linux
gjtorikian May 10, 2015
4f48429
Include `LDFLAGS` for Linux
gjtorikian May 11, 2015
17b73f3
Export `LD_LIBRARY_PATH` for Linux
gjtorikian May 11, 2015
7907490
`LDFLAG` simplification for Linux
gjtorikian May 11, 2015
9a79fc8
Add minitest dependency
gjtorikian May 11, 2015
54b69d3
Add awesome_print
gjtorikian May 11, 2015
ab43241
Style changes
gjtorikian May 11, 2015
1c16fc8
no magic strings
gjtorikian May 11, 2015
d3dd72e
Rename spec test generator
gjtorikian May 11, 2015
e4cf4e5
Regenerate spec_tests.json
gjtorikian May 11, 2015
008c0ba
Minor test updates
gjtorikian May 11, 2015
6afa02c
Match cmark's settings
gjtorikian May 11, 2015
b6a75fa
Fix `markdown_to_html`
gjtorikian May 11, 2015
fb0cdbb
Ignore my test output
gjtorikian May 11, 2015
f91616c
Ignore HTML renderer for now
gjtorikian May 11, 2015
89659a6
Add CIBuild script
gjtorikian May 11, 2015
9358667
Fix test inheritance
gjtorikian May 11, 2015
14ebf61
Open up HTML renderer for Travis
gjtorikian May 11, 2015
8fe4704
Fix benchmark suite run
gjtorikian May 11, 2015
5a5be04
Update cmark to bc5f16f146558a1faf9be6abe034755421f6ecce
gjtorikian May 11, 2015
7cdbb2e
Add separate update script
gjtorikian May 11, 2015
3c9af75
Take HTMLRenderer back out for now
gjtorikian May 11, 2015
b748a40
Enhance `clean` with `make clean`
gjtorikian May 11, 2015
793f491
Remove temp build dir
gjtorikian May 12, 2015
c72ec93
Single quotes when able
gjtorikian May 12, 2015
a1bce42
Uncomment HTML Renderer now
gjtorikian May 12, 2015
46d3d64
Catch false attributes
gjtorikian May 12, 2015
9620927
Rewrite some HTML output
gjtorikian May 12, 2015
9822f3d
Clean up benchmark script
gjtorikian May 12, 2015
cddf986
Add node_parent
gjtorikian May 12, 2015
f81b954
Add `node_previous`
gjtorikian May 12, 2015
ff9dcc3
Clean up if/elsif
gjtorikian May 12, 2015
1072369
Use new Ruby 2.0 %i syntax
gjtorikian May 12, 2015
ec1a3bf
Add `next` call
gjtorikian May 13, 2015
40bf72e
Let's call this half-baked shit good enough for now
gjtorikian May 13, 2015
ea6158a
Parse the string once
gjtorikian May 13, 2015
4a2b0e3
Full disclosure, normal renderer cleanup
gjtorikian May 13, 2015
457448e
Store buffer
gjtorikian May 13, 2015
49aff2e
Get all the beautiful tests to pass
gjtorikian May 13, 2015
34a7784
A lot of style updates
gjtorikian May 13, 2015
8be1237
Update README
gjtorikian May 13, 2015
82283bb
Update README.md
gjtorikian May 13, 2015
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Split out files
  • Loading branch information
gjtorikian committed May 10, 2015
commit 3289df72f19b70ae29bb505d2358a59f558e9096
266 changes: 2 additions & 264 deletions lib/commonmarker.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
#!/usr/bin/env ruby
require 'commonmarker/commonmarker'
require 'commonmarker/config'
require 'commonmarker/renderer'
require 'commonmarker/renderer/html_renderer'
require 'stringio'
require 'cgi'
require 'set'
Expand All @@ -13,42 +15,6 @@
:emph, :strong, :link, :image]
LIST_TYPES = [:no_list, :bullet_list, :ordered_list]

# module CMark

# attach_function :cmark_node_new, [:node_type], :node
# attach_function :cmark_node_free, [:node], :void
# attach_function :cmark_node_unlink, [:node], :void
# attach_function :cmark_node_insert_before, [:node, :node], :int
# attach_function :cmark_node_insert_after, [:node, :node], :int
# attach_function :cmark_node_prepend_child, [:node, :node], :int
# attach_function :cmark_node_append_child, [:node, :node], :int
# attach_function :cmark_markdown_to_html, [:string, :int], :string
# attach_function :cmark_render_html, [:node], :string
# attach_function :cmark_parse_document, [:string, :int], :node
# attach_function :cmark_node_first_child, [:node], :node
# attach_function :cmark_node_last_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_literal, [:node], :string
# attach_function :cmark_node_set_literal, [:node, :string], :int
# attach_function :cmark_node_get_url, [:node], :string
# attach_function :cmark_node_set_url, [:node, :string], :int
# attach_function :cmark_node_get_title, [:node], :string
# attach_function :cmark_node_set_title, [:node, :string], :int
# attach_function :cmark_node_get_header_level, [:node], :int
# attach_function :cmark_node_set_header_level, [:node, :int], :int
# attach_function :cmark_node_get_list_type, [:node], :list_type
# attach_function :cmark_node_set_list_type, [:node, :list_type], :int
# attach_function :cmark_node_get_list_start, [:node], :int
# attach_function :cmark_node_set_list_start, [:node, :int], :int
# attach_function :cmark_node_get_list_tight, [:node], :bool
# attach_function :cmark_node_set_list_tight, [:node, :bool], :int
# attach_function :cmark_node_get_fence_info, [:node], :string
# attach_function :cmark_node_set_fence_info, [:node, :string], :int
# end

module CommonMarker
class NodeError < StandardError
end
Expand Down Expand Up @@ -357,232 +323,4 @@ def free
end
end

class Renderer
attr_accessor :in_tight, :warnings, :in_plain
def initialize
@stream = StringIO.new
@need_blocksep = false
@warnings = Set.new []
@in_tight = false
@in_plain = false
end

def out(*args)
args.each do |arg|
if arg == :children
@node.each_child do |child|
self.out(child)
end
elsif arg.kind_of?(Array)
arg.each do |x|
self.render(x)
end
elsif arg.kind_of?(Node)
self.render(arg)
else
@stream.write(arg)
end
end
end

def render(node)
@node = node
if node.type == :document
self.document(node)
return @stream.string
elsif self.in_plain && node.type != :text && node.type != :softbreak
node.each_child 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(: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 render(node)
result = super(node)
if node.type == :document
result += "\n"
end
end

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

def paragraph(node)
block do
if self.in_tight
self.out(:children)
else
self.out("<p>", :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(:children)
end
else
start = node.list_start == 1 ? '' :
(' start="' + node.list_start.to_s + '"')
container(start, "</ol>") do
self.out(:children)
end
end
end
self.in_tight = old_in_tight
end

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

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

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

def code_block(node)
block do
self.out("<pre><code")
if node.fence_info && node.fence_info.length > 0
self.out(" class=\"language-", node.fence_info.split(/\s+/)[0], "\">")
else
self.out(">")
end
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>", :children, "</em>")
end

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

def link(node)
self.out('<a href="', node.url.nil? ? '' : URI.escape(node.url), '"')
if node.title && node.title.length > 0
self.out(' title="', CGI.escapeHTML(node.title), '"')
end
self.out('>', :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="', :children, '" />')
end
end

def text(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

end
94 changes: 94 additions & 0 deletions lib/commonmarker/renderer.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
module CommonMarker
class Renderer
attr_accessor :in_tight, :warnings, :in_plain
def initialize
@stream = StringIO.new
@need_blocksep = false
@warnings = Set.new []
@in_tight = false
@in_plain = false
end

def out(*args)
args.each do |arg|
if arg == :children
@node.each_child do |child|
self.out(child)
end
elsif arg.kind_of?(Array)
arg.each do |x|
self.render(x)
end
elsif arg.kind_of?(Node)
self.render(arg)
else
@stream.write(arg)
end
end
end

def render(node)
@node = node
if node.type == :document
self.document(node)
return @stream.string
elsif self.in_plain && node.type != :text && node.type != :softbreak
node.each_child 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(: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
end
Loading