diff --git a/.gitmodules b/.gitmodules index d61964e6..a8c4fb06 100644 --- a/.gitmodules +++ b/.gitmodules @@ -1,4 +1,4 @@ [submodule "ext/commonmarker/cmark"] path = ext/commonmarker/cmark - url = https://github.com/kivikakk/cmark.git + url = https://github.com/jgm/cmark.git ignore = dirty diff --git a/bin/commonmarker b/bin/commonmarker index 71cf8a02..094db054 100755 --- a/bin/commonmarker +++ b/bin/commonmarker @@ -1,11 +1,9 @@ #!/usr/bin/env ruby -# Usage: commonmarker [--html-renderer] [--list-extensions] [--extension=EXTENSION] [FILE..] +# Usage: commonmarker [--html-renderer] [FILE..] # Convert one or more CommonMark files to HTML and write to standard output. # If no FILE argument is provided, text will be read from STDIN. # With --html-renderer, use the HtmlRenderer renderer rather than the native C # renderer. -# With --extension=EXTENSION, EXTENSION will be used for parsing, and HTML -# output unless --html-renderer is specified. if ARGV.include?('--help') or ARGV.include?('-h') File.read(__FILE__).split("\n").grep(/^# /).each do |line| puts line[2..-1] @@ -24,34 +22,20 @@ end root = File.expand_path('../../', __FILE__) $:.unshift File.expand_path('lib', root) -extensions = CommonMarker.extensions -active_extensions = [] - renderer = nil ARGV.delete_if do |arg| if arg =~ /^--html-renderer$/ renderer = CommonMarker::HtmlRenderer.new true - elsif arg =~ /^--list-extensions$/ - puts extensions - exit 0 - elsif arg =~ /^--extension=(.+)$/ - if extensions.include?($1) - active_extensions << $1 - else - STDERR.puts "extension #$1 not found" - exit 1 - end - true else false end end -doc = CommonMarker.render_doc(ARGF.read, :default, active_extensions) +doc = CommonMarker.render_doc(ARGF.read) if renderer STDOUT.write(renderer.render(doc)) else - STDOUT.write(doc.to_html(:default, active_extensions)) + STDOUT.write(doc.to_html) end diff --git a/ext/commonmarker/cmark b/ext/commonmarker/cmark index 90bde9f9..636d6018 160000 --- a/ext/commonmarker/cmark +++ b/ext/commonmarker/cmark @@ -1 +1 @@ -Subproject commit 90bde9f9cf5df589f03ceb8a5cf8802f97a26999 +Subproject commit 636d6018ea6bc6697b6491e0e47c4ae1ccad32fe diff --git a/ext/commonmarker/commonmarker.c b/ext/commonmarker/commonmarker.c index 073d5962..bed02218 100644 --- a/ext/commonmarker/commonmarker.c +++ b/ext/commonmarker/commonmarker.c @@ -2,10 +2,6 @@ #include "cmark.h" #include "houdini.h" #include "node.h" -#include "registry.h" -#include "parser.h" -#include "syntax_extension.h" -#include "core-extensions.h" static VALUE rb_mNodeError; static VALUE rb_mNode; @@ -95,69 +91,24 @@ static void rb_parent_removed(VALUE val) { RDATA(val)->dfree = rb_free_c_struct; } -static cmark_parser *prepare_parser(VALUE rb_options, VALUE rb_extensions) { - int options; - int extensions_len; - VALUE rb_ext_name; - int i; - - Check_Type(rb_options, T_FIXNUM); - Check_Type(rb_extensions, T_ARRAY); - - options = FIX2INT(rb_options); - extensions_len = RARRAY_LEN(rb_extensions); - - cmark_parser *parser = cmark_parser_new(options); - for (i = 0; i < extensions_len; ++i) { - rb_ext_name = RARRAY_PTR(rb_extensions)[i]; - - if (!SYMBOL_P(rb_ext_name)) { - cmark_parser_free(parser); - rb_raise(rb_eTypeError, "extension names should be Symbols; got a %"PRIsVALUE"", rb_obj_class(rb_ext_name)); - } - - cmark_syntax_extension *syntax_extension = - cmark_find_syntax_extension(rb_id2name(SYM2ID(rb_ext_name))); - - if (!syntax_extension) { - cmark_parser_free(parser); - rb_raise(rb_eArgError, "extension %s not found", rb_id2name(SYM2ID(rb_ext_name))); - } - - cmark_parser_attach_syntax_extension(parser, syntax_extension); - } - - return parser; -} - /* * Internal: Parses a Markdown string into an HTML string. * */ -static VALUE rb_markdown_to_html(VALUE self, VALUE rb_text, VALUE rb_options, VALUE rb_extensions) { - char *str, *html; - int len; - cmark_parser *parser; - cmark_node *doc; +static VALUE rb_markdown_to_html(VALUE self, VALUE rb_text, VALUE rb_options) { + char *str; + int len, options; + Check_Type(rb_text, T_STRING); Check_Type(rb_options, T_FIXNUM); - parser = prepare_parser(rb_options, rb_extensions); - str = (char *)RSTRING_PTR(rb_text); len = RSTRING_LEN(rb_text); + options = FIX2INT(rb_options); - cmark_parser_feed(parser, str, len); - doc = cmark_parser_finish(parser); - if (doc == NULL) { - rb_raise(rb_mNodeError, "error parsing document"); - } - - html = cmark_render_html(doc, FIX2INT(rb_options), parser->syntax_extensions); - cmark_parser_free(parser); - cmark_node_free(doc); - + char *html = cmark_markdown_to_html(str, len, options); VALUE ruby_html = rb_str_new2(html); + free(html); return ruby_html; @@ -245,27 +196,22 @@ static VALUE rb_node_new(VALUE self, VALUE type) { * */ static VALUE rb_parse_document(VALUE self, VALUE rb_text, VALUE rb_len, - VALUE rb_options, VALUE rb_extensions) { + VALUE rb_options) { char *text; int len, options; - cmark_parser *parser; cmark_node *doc; Check_Type(rb_text, T_STRING); Check_Type(rb_len, T_FIXNUM); Check_Type(rb_options, T_FIXNUM); - parser = prepare_parser(rb_options, rb_extensions); - text = (char *)RSTRING_PTR(rb_text); len = FIX2INT(rb_len); options = FIX2INT(rb_options); - cmark_parser_feed(parser, text, len); - doc = cmark_parser_finish(parser); + doc = cmark_parse_document(text, len, options); if (doc == NULL) { rb_raise(rb_mNodeError, "error parsing document"); } - cmark_parser_free(parser); return rb_node_to_value(doc); } @@ -493,67 +439,23 @@ static VALUE rb_node_insert_before(VALUE self, VALUE sibling) { * * Returns a {String}. */ -static VALUE rb_render_html(VALUE n, VALUE rb_options, VALUE rb_extensions) { - int options, extensions_len; - VALUE rb_ext_name; - int i; +static VALUE rb_render_html(VALUE n, VALUE rb_options) { + int options; cmark_node *node; - cmark_llist *extensions = NULL; Check_Type(rb_options, T_FIXNUM); - Check_Type(rb_extensions, T_ARRAY); options = FIX2INT(rb_options); - extensions_len = RARRAY_LEN(rb_extensions); Data_Get_Struct(n, cmark_node, node); - for (i = 0; i < extensions_len; ++i) { - rb_ext_name = RARRAY_PTR(rb_extensions)[i]; - - if (!SYMBOL_P(rb_ext_name)) { - cmark_llist_free(extensions); - rb_raise(rb_eTypeError, "extension names should be Symbols; got a %"PRIsVALUE"", rb_obj_class(rb_ext_name)); - } - - cmark_syntax_extension *syntax_extension = - cmark_find_syntax_extension(rb_id2name(SYM2ID(rb_ext_name))); - - if (!syntax_extension) { - cmark_llist_free(extensions); - rb_raise(rb_eArgError, "extension %s not found\n", rb_id2name(SYM2ID(rb_ext_name))); - } - - extensions = cmark_llist_append(extensions, syntax_extension); - } - - char *html = cmark_render_html(node, options, extensions); + char *html = cmark_render_html(node, options); VALUE ruby_html = rb_str_new2(html); - cmark_llist_free(extensions); free(html); return ruby_html; } -/* Internal: Convert the node to a CommonMark string. - * - * Returns a {String}. - */ -static VALUE rb_render_commonmark(VALUE n, VALUE rb_options) { - int options; - cmark_node *node; - Check_Type(rb_options, T_FIXNUM); - - options = FIX2INT(rb_options); - Data_Get_Struct(n, cmark_node, node); - - char *cmark = cmark_render_commonmark(node, options, 120); - VALUE ruby_cmark = rb_str_new2(cmark); - free(cmark); - - return ruby_cmark; -} - /* * Public: Inserts a node as a sibling after the current node. * @@ -1005,21 +907,6 @@ static VALUE rb_html_escape_html(VALUE self, VALUE rb_text) { return rb_text; } -VALUE rb_extensions(VALUE self) { - cmark_llist *exts, *it; - cmark_syntax_extension *ext; - VALUE ary = rb_ary_new(); - - exts = cmark_list_syntax_extensions(); - for (it = exts; it; it = it->next) { - ext = it->data; - rb_ary_push(ary, rb_str_new2(ext->name)); - } - cmark_llist_free(exts); - - return ary; -} - __attribute__((visibility("default"))) void Init_commonmarker() { VALUE module; sym_document = ID2SYM(rb_intern("document")); @@ -1045,13 +932,12 @@ __attribute__((visibility("default"))) void Init_commonmarker() { sym_ordered_list = ID2SYM(rb_intern("ordered_list")); module = rb_define_module("CommonMarker"); - rb_define_singleton_method(module, "extensions", rb_extensions, 0); rb_mNodeError = rb_define_class_under(module, "NodeError", rb_eStandardError); rb_mNode = rb_define_class_under(module, "Node", rb_cObject); rb_define_singleton_method(rb_mNode, "markdown_to_html", rb_markdown_to_html, - 3); + 2); rb_define_singleton_method(rb_mNode, "new", rb_node_new, 1); - rb_define_singleton_method(rb_mNode, "parse_document", rb_parse_document, 4); + rb_define_singleton_method(rb_mNode, "parse_document", rb_parse_document, 3); rb_define_method(rb_mNode, "string_content", rb_node_get_string_content, 0); rb_define_method(rb_mNode, "string_content=", rb_node_set_string_content, 1); rb_define_method(rb_mNode, "type", rb_node_get_type, 0); @@ -1061,8 +947,7 @@ __attribute__((visibility("default"))) void Init_commonmarker() { rb_define_method(rb_mNode, "first_child", rb_node_first_child, 0); rb_define_method(rb_mNode, "next", rb_node_next, 0); rb_define_method(rb_mNode, "insert_before", rb_node_insert_before, 1); - rb_define_method(rb_mNode, "_render_html", rb_render_html, 2); - rb_define_method(rb_mNode, "_render_commonmark", rb_render_commonmark, 1); + rb_define_method(rb_mNode, "_render_html", rb_render_html, 1); rb_define_method(rb_mNode, "insert_after", rb_node_insert_after, 1); rb_define_method(rb_mNode, "prepend_child", rb_node_prepend_child, 1); rb_define_method(rb_mNode, "append_child", rb_node_append_child, 1); @@ -1086,6 +971,4 @@ __attribute__((visibility("default"))) void Init_commonmarker() { rb_define_method(rb_mNode, "html_escape_href", rb_html_escape_href, 1); rb_define_method(rb_mNode, "html_escape_html", rb_html_escape_html, 1); - - cmark_register_plugin(core_extensions_registration); } diff --git a/ext/commonmarker/extconf.rb b/ext/commonmarker/extconf.rb index 63d2e5fa..8c9081ed 100644 --- a/ext/commonmarker/extconf.rb +++ b/ext/commonmarker/extconf.rb @@ -29,24 +29,22 @@ system 'cmake .. -DCMAKE_C_FLAGS=-fPIC' end system "make libcmark_static" or abort "make libcmark_static failed" - system "make libcmarkextensions_static" or abort "make libcmarkextensions_static failed" # rake-compiler seems to complain about this line, not sure why it's messing with it FileUtils.rm_rf(File.join(CMARK_BUILD_DIR, 'Testing', 'Temporary')) end HEADER_DIRS = [INCLUDEDIR] -LIB_DIRS = [LIBDIR, "#{CMARK_BUILD_DIR}/src", "#{CMARK_BUILD_DIR}/extensions"] +LIB_DIRS = [LIBDIR, "#{CMARK_BUILD_DIR}/src"] dir_config('cmark', HEADER_DIRS, LIB_DIRS) # don't even bother to do this check if using OS X's messed up system Ruby: http://git.io/vsxkn unless sitearch =~ /^universal-darwin/ abort 'libcmark is missing.' unless find_library('cmark', 'cmark_parse_document') - abort 'cmarkextensions is missing.' unless find_library('cmarkextensions', 'core_extensions_registration') end -$LDFLAGS << " -L#{CMARK_BUILD_DIR}/src -L#{CMARK_BUILD_DIR}/extensions -lcmark -lcmarkextensions" -$CFLAGS << " -O2 -I#{CMARK_DIR}/src -I#{CMARK_DIR}/extensions -I#{CMARK_BUILD_DIR}/src" +$LDFLAGS << " -L#{CMARK_BUILD_DIR}/src -lcmark" +$CFLAGS << " -O2 -I#{CMARK_DIR}/src -I#{CMARK_BUILD_DIR}/src" $CFLAGS << " -DCMARK_STATIC_DEFINE" create_makefile('commonmarker/commonmarker') diff --git a/lib/commonmarker.rb b/lib/commonmarker.rb index 838e6ff0..cd2e2281 100755 --- a/lib/commonmarker.rb +++ b/lib/commonmarker.rb @@ -15,14 +15,13 @@ module CommonMarker # # text - A {String} of text # option - Either a {Symbol} or {Array of Symbol}s indicating the render options - # extensions - An {Array of Symbol}s indicating the extensions to use # # Returns a {String} of converted HTML. - def self.render_html(text, options = :default, extensions = []) - fail TypeError, "text must be a String; got a #{text.class}!" unless text.is_a?(String) + def self.render_html(text, options = :default) + fail TypeError, 'text must be a string!' unless text.is_a?(String) opts = Config.process_options(options, :render) text = text.encode('UTF-8') - html = Node.markdown_to_html(text, opts, extensions) + html = Node.markdown_to_html(text, opts) html.force_encoding('UTF-8') end @@ -30,13 +29,12 @@ def self.render_html(text, options = :default, extensions = []) # # string - {String} to be parsed # option - A {Symbol} or {Array of Symbol}s indicating the parse options - # extensions - An {Array of Symbol}s indicating the extensions to use # # Returns the `document` node. - def self.render_doc(text, options = :default, extensions = []) - fail TypeError, "text must be a String; got a #{text.class}!" unless text.is_a?(String) + def self.render_doc(text, options = :default) + fail TypeError, 'text must be a string!' unless text.is_a?(String) opts = Config.process_options(options, :parse) text = text.encode('UTF-8') - Node.parse_document(text, text.bytesize, opts, extensions) + Node.parse_document(text, text.bytesize, opts) end end diff --git a/lib/commonmarker/node.rb b/lib/commonmarker/node.rb index 3bcc70dc..d86c725d 100644 --- a/lib/commonmarker/node.rb +++ b/lib/commonmarker/node.rb @@ -17,22 +17,11 @@ def walk(&block) # Public: Convert the node to an HTML string. # # options - A {Symbol} or {Array of Symbol}s indicating the render options - # extensions - An {Array of Symbol}s indicating the extensions to use # # Returns a {String}. - def to_html(options = :default, extensions = []) + def to_html(options = :default) opts = Config.process_options(options, :render) - _render_html(opts, extensions).force_encoding('utf-8') - end - - # Public: Convert the node to a CommonMark string. - # - # options - A {Symbol} or {Array of Symbol}s indicating the render options - # - # Returns a {String}. - def to_commonmark(options = :default) - opts = Config.process_options(options, :render) - _render_commonmark(opts).force_encoding('utf-8') + _render_html(opts).force_encoding('utf-8') end # Public: Iterate over the children (if any) of the current pointer. diff --git a/lib/commonmarker/version.rb b/lib/commonmarker/version.rb index 6a7f32ea..9260dbd1 100644 --- a/lib/commonmarker/version.rb +++ b/lib/commonmarker/version.rb @@ -1,3 +1,3 @@ module CommonMarker - VERSION = '0.11.0'.freeze + VERSION = '0.12.0'.freeze end diff --git a/script/update_submodules b/script/update_submodules index 18e194de..ebd8f347 100755 --- a/script/update_submodules +++ b/script/update_submodules @@ -3,7 +3,7 @@ set -e if [ -z "$1" ]; then - BRANCH="extensions" + BRANCH="master" else BRANCH=$1 fi @@ -18,4 +18,4 @@ git checkout $BRANCH && git pull sha=`git rev-parse HEAD` cd ../../.. git add ext/commonmarker/cmark -git commit -m "Update cmark to $(git config submodule.ext/commonmarker/cmark.url | sed s_.git\$__)/commit/${sha}" +git commit -m "Update cmark to https://github.com/jgm/cmark/commit/${sha}" diff --git a/test/test_commonmark.rb b/test/test_commonmark.rb deleted file mode 100644 index f8afd9de..00000000 --- a/test/test_commonmark.rb +++ /dev/null @@ -1,34 +0,0 @@ -require 'test_helper' - -class TestCommonmark < Minitest::Test - HTML_COMMENT = %r[\s?] - - def setup - @markdown = <<-MD -Hi *there*! - -1. I am a numeric list. -2. I continue the list. -* Suddenly, an unordered list! -* What fun! - -Okay, _enough_. - -| a | b | -| --- | --- | -| c | d | - MD - end - - def render_doc(doc) - CommonMarker.render_doc(doc, :default, %i[table]) - end - - def test_to_commonmark - compare = render_doc(@markdown).to_commonmark - - assert_equal \ - render_doc(@markdown).to_html.gsub(HTML_COMMENT, ''), - render_doc(compare).to_html.gsub(HTML_COMMENT, '') - end -end diff --git a/test/test_extensions.rb b/test/test_extensions.rb deleted file mode 100644 index 41974ee7..00000000 --- a/test/test_extensions.rb +++ /dev/null @@ -1,50 +0,0 @@ -require 'test_helper' - -class TestExtensions < Minitest::Test - def setup - @markdown = <<-MD -One extension: - -| a | b | -| --- | --- | -| c | d | -| **x** | | - -Another extension: - -~~hi~~ - MD - end - - def test_uses_specified_extensions - CommonMarker.render_html(@markdown, :default, %i[]).tap do |out| - assert out.include?("| a") - assert out.include?("| x") - assert out.include?("~~hi~~") - end - - CommonMarker.render_html(@markdown, :default, %i[table]).tap do |out| - refute out.include?("| a") - %w(
a | c | x).each {|html| assert out.include?(html) } - assert out.include?("~~hi~~") - end - - CommonMarker.render_html(@markdown, :default, %i[strikethrough]).tap do |out| - assert out.include?("| a") - refute out.include?("~~hi~~") - assert out.include?("
---|