Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Cleanup link attributes implementation

  • Loading branch information...
commit 24f92a1c766673370e72fc45aa5c7ba322797523 1 parent 4c14d08
Vicent Marti authored
34 ext/redcarpet/rc_render.c
View
@@ -235,13 +235,12 @@ rndr_doc_footer(struct buf *ob, void *opaque)
}
static int
-rndr_link_attribute(VALUE key, VALUE val, VALUE self)
+cb_link_attribute(VALUE key, VALUE val, VALUE payload)
{
- struct rb_redcarpet_rndr *rndr;
- Data_Get_Struct(self, struct rb_redcarpet_rndr, rndr);
+ struct buf *ob = (struct buf *)payload;
key = rb_obj_as_string(key);
val = rb_obj_as_string(val);
- bufprintf(rndr->ob, " %s=\"%s\"", StringValueCStr(key), StringValueCStr(val));
+ bufprintf(ob, " %s=\"%s\"", StringValueCStr(key), StringValueCStr(val));
return ST_CONTINUE;
}
@@ -250,17 +249,10 @@ rndr_link_attributes(struct buf *ob, const struct buf *url, void *opaque)
{
struct redcarpet_renderopt *opt = opaque;
struct rb_redcarpet_rndr *rndr;
+
Data_Get_Struct(opt->self, struct rb_redcarpet_rndr, rndr);
- if (rb_obj_is_kind_of(opt->link_attributes, rb_cHash))
- {
- rndr->ob = ob;
- rb_hash_foreach(opt->link_attributes, rndr_link_attribute, opt->self);
- rndr->ob = NULL;
- }
- else
- {
- rb_raise(rb_eTypeError, "Hash required");
- }
+ Check_Type(opt->link_attributes, T_HASH);
+ rb_hash_foreach(opt->link_attributes, &cb_link_attribute, (VALUE)ob);
}
static struct sd_callbacks rb_redcarpet_callbacks = {
@@ -371,12 +363,11 @@ static VALUE rb_redcarpet_html_init(int argc, VALUE *argv, VALUE self)
{
struct rb_redcarpet_rndr *rndr;
unsigned int render_flags = 0;
- VALUE hash;
+ VALUE hash, link_attr = Qnil;
Data_Get_Struct(self, struct rb_redcarpet_rndr, rndr);
- if (rb_scan_args(argc, argv, "01", &hash) == 1)
- {
+ if (rb_scan_args(argc, argv, "01", &hash) == 1) {
Check_Type(hash, T_HASH);
/* escape_html */
@@ -412,14 +403,17 @@ static VALUE rb_redcarpet_html_init(int argc, VALUE *argv, VALUE self)
if (rb_hash_aref(hash, CSTR2SYM("xhtml")) == Qtrue)
render_flags |= HTML_USE_XHTML;
- rndr->options.link_attributes = rb_hash_aref(hash, CSTR2SYM("link_attributes"));
+ link_attr = rb_hash_aref(hash, CSTR2SYM("link_attributes"));
}
sdhtml_renderer(&rndr->callbacks, (struct html_renderopt *)&rndr->options.html, render_flags);
- if (rndr->options.link_attributes != Qnil)
- rndr->options.html.link_attributes = rndr_link_attributes;
rb_redcarpet__overload(self, rb_cRenderHTML);
+ if (!NIL_P(link_attr)) {
+ rndr->options.link_attributes = link_attr;
+ rndr->options.html.link_attributes = &rndr_link_attributes;
+ }
+
return Qnil;
}
1  ext/redcarpet/redcarpet.h
View
@@ -33,7 +33,6 @@ struct redcarpet_renderopt {
struct rb_redcarpet_rndr {
struct sd_callbacks callbacks;
struct redcarpet_renderopt options;
- struct buf *ob;
};
#endif
6 test/redcarpet_test.rb
View
@@ -127,6 +127,12 @@ def test_that_hard_wrap_works
assert rd =~ /<br>/
end
+
+ def test_that_link_attributes_work
+ rndr = Redcarpet::Render::HTML.new(:link_attributes => {:rel => 'blank'})
+ md = Redcarpet::Markdown.new(rndr)
+ assert md.render('This is a [simple](http://test.com) test.').include?('rel="blank"')
+ end
end
class MarkdownTest < Test::Unit::TestCase
Please sign in to comment.
Something went wrong with that request. Please try again.