Skip to content

Commit

Permalink
Revert "cmark extensions"
Browse files Browse the repository at this point in the history
  • Loading branch information
gjtorikian authored Oct 11, 2016
1 parent bd8b246 commit 2bd0073
Show file tree
Hide file tree
Showing 16 changed files with 38 additions and 300 deletions.
2 changes: 1 addition & 1 deletion .gitmodules
Original file line number Diff line number Diff line change
@@ -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
3 changes: 0 additions & 3 deletions Rakefile
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,6 @@ require 'rake/clean'
require 'rake/extensiontask'
require 'digest/md5'

host_os = RbConfig::CONFIG['host_os']
require 'devkit' if host_os == 'mingw32'

task :default => [:test]

# Gem Spec
Expand Down
16 changes: 0 additions & 16 deletions appveyor.yml

This file was deleted.

22 changes: 3 additions & 19 deletions bin/commonmarker
Original file line number Diff line number Diff line change
@@ -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]
Expand All @@ -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
2 changes: 1 addition & 1 deletion ext/commonmarker/cmark
Submodule cmark updated from 90bde9 to e91dc1
147 changes: 15 additions & 132 deletions ext/commonmarker/commonmarker.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}
Expand Down Expand Up @@ -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.
*
Expand Down Expand Up @@ -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"));
Expand All @@ -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);
Expand All @@ -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);
Expand All @@ -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);
}
4 changes: 0 additions & 4 deletions ext/commonmarker/commonmarker.h
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
#ifndef COMMONMARKER_H
#define COMMONMARKER_H

#ifndef __MSXML_LIBRARY_DEFINED__
#define __MSXML_LIBRARY_DEFINED__
#endif

#include "cmark.h"
#include "ruby.h"

Expand Down
17 changes: 5 additions & 12 deletions ext/commonmarker/extconf.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,30 +23,23 @@
FileUtils.mkdir_p(CMARK_BUILD_DIR)

Dir.chdir(CMARK_BUILD_DIR) do
if host_os == 'mingw32'
system 'cmake .. -G "MSYS Makefiles"'
else
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"
system 'cmake .. -DCMAKE_C_FLAGS=-fPIC'
system 'make libcmark_static'
# 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"
$CFLAGS << " -DCMARK_STATIC_DEFINE"
$LDFLAGS << " -L#{CMARK_BUILD_DIR}/src -lcmark"
$CFLAGS << " -O2 -I#{CMARK_DIR}/src -I#{CMARK_BUILD_DIR}/src"

create_makefile('commonmarker/commonmarker')
Loading

0 comments on commit 2bd0073

Please sign in to comment.