Skip to content
This repository
tree: 21cb3c710a
Fetching contributors…

Octocat-spinner-32-eaf2f5

Cannot retrieve contributors at this time

file 470 lines (384 sloc) 11.813 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469
/*
* Copyright (c) 2011, Vicent Marti
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/

#include "redcarpet.h"

#define SPAN_CALLBACK(method_name, ...) {\
struct redcarpet_renderopt *opt = opaque;\
VALUE ret = rb_funcall(opt->self, rb_intern(method_name), __VA_ARGS__);\
if (NIL_P(ret)) return 0;\
Check_Type(ret, T_STRING);\
bufput(ob, RSTRING_PTR(ret), RSTRING_LEN(ret));\
return 1;\
}

#define BLOCK_CALLBACK(method_name, ...) {\
struct redcarpet_renderopt *opt = opaque;\
VALUE ret = rb_funcall(opt->self, rb_intern(method_name), __VA_ARGS__);\
if (NIL_P(ret)) return;\
Check_Type(ret, T_STRING);\
bufput(ob, RSTRING_PTR(ret), RSTRING_LEN(ret));\
}

extern VALUE rb_mRedcarpet;
VALUE rb_mRender;
VALUE rb_cRenderBase;
VALUE rb_cRenderHTML;
VALUE rb_cRenderHTML_TOC;
VALUE rb_mSmartyPants;

#ifdef HAVE_RUBY_ENCODING_H
#define buf2str(t) ((t) ? redcarpet_str_new((t)->data, (t)->size, opt->active_enc) : Qnil)
#else
#define buf2str(t) ((t) ? redcarpet_str_new((t)->data, (t)->size, NULL) : Qnil)
#endif

static void
rndr_blockcode(struct buf *ob, const struct buf *text, const struct buf *lang, void *opaque)
{
BLOCK_CALLBACK("block_code", 2, buf2str(text), buf2str(lang));
}

static void
rndr_blockquote(struct buf *ob, const struct buf *text, void *opaque)
{
BLOCK_CALLBACK("block_quote", 1, buf2str(text));
}

static void
rndr_raw_block(struct buf *ob, const struct buf *text, void *opaque)
{
BLOCK_CALLBACK("block_html", 1, buf2str(text));
}

static void
rndr_header(struct buf *ob, const struct buf *text, int level, void *opaque)
{
BLOCK_CALLBACK("header", 2, buf2str(text), INT2FIX(level));
}

static void
rndr_hrule(struct buf *ob, void *opaque)
{
BLOCK_CALLBACK("hrule", 0);
}

static void
rndr_list(struct buf *ob, const struct buf *text, int flags, void *opaque)
{
BLOCK_CALLBACK("list", 2, buf2str(text),
(flags & MKD_LIST_ORDERED) ? CSTR2SYM("ordered") : CSTR2SYM("unordered"));
}

static void
rndr_listitem(struct buf *ob, const struct buf *text, int flags, void *opaque)
{
BLOCK_CALLBACK("list_item", 2, buf2str(text),
(flags & MKD_LIST_ORDERED) ? CSTR2SYM("ordered") : CSTR2SYM("unordered"));
}

static void
rndr_paragraph(struct buf *ob, const struct buf *text, void *opaque)
{
BLOCK_CALLBACK("paragraph", 1, buf2str(text));
}

static void
rndr_table(struct buf *ob, const struct buf *header, const struct buf *body, void *opaque)
{
BLOCK_CALLBACK("table", 2, buf2str(header), buf2str(body));
}

static void
rndr_tablerow(struct buf *ob, const struct buf *text, void *opaque)
{
BLOCK_CALLBACK("table_row", 1, buf2str(text));
}

static void
rndr_tablecell(struct buf *ob, const struct buf *text, int align, void *opaque)
{
VALUE rb_align;

switch (align) {
case MKD_TABLE_ALIGN_L:
rb_align = CSTR2SYM("left");
break;

case MKD_TABLE_ALIGN_R:
rb_align = CSTR2SYM("right");
break;

case MKD_TABLE_ALIGN_CENTER:
rb_align = CSTR2SYM("center");
break;

default:
rb_align = Qnil;
break;
}

BLOCK_CALLBACK("table_cell", 2, buf2str(text), rb_align);
}




/***
* SPAN LEVEL
*/
static int
rndr_autolink(struct buf *ob, const struct buf *link, enum mkd_autolink type, void *opaque)
{
SPAN_CALLBACK("autolink", 2, buf2str(link),
type == MKDA_NORMAL ? CSTR2SYM("url") : CSTR2SYM("email"));
}

static int
rndr_codespan(struct buf *ob, const struct buf *text, void *opaque)
{
SPAN_CALLBACK("codespan", 1, buf2str(text));
}

static int
rndr_double_emphasis(struct buf *ob, const struct buf *text, void *opaque)
{
SPAN_CALLBACK("double_emphasis", 1, buf2str(text));
}

static int
rndr_emphasis(struct buf *ob, const struct buf *text, void *opaque)
{
SPAN_CALLBACK("emphasis", 1, buf2str(text));
}

static int
rndr_image(struct buf *ob, const struct buf *link, const struct buf *title, const struct buf *alt, void *opaque)
{
SPAN_CALLBACK("image", 3, buf2str(link), buf2str(title), buf2str(alt));
}

static int
rndr_linebreak(struct buf *ob, void *opaque)
{
SPAN_CALLBACK("linebreak", 0);
}

static int
rndr_link(struct buf *ob, const struct buf *link, const struct buf *title, const struct buf *content, void *opaque)
{
SPAN_CALLBACK("link", 3, buf2str(link), buf2str(title), buf2str(content));
}

static int
rndr_raw_html(struct buf *ob, const struct buf *text, void *opaque)
{
SPAN_CALLBACK("raw_html", 1, buf2str(text));
}

static int
rndr_triple_emphasis(struct buf *ob, const struct buf *text, void *opaque)
{
SPAN_CALLBACK("triple_emphasis", 1, buf2str(text));
}

static int
rndr_strikethrough(struct buf *ob, const struct buf *text, void *opaque)
{
SPAN_CALLBACK("strikethrough", 1, buf2str(text));
}

static int
rndr_superscript(struct buf *ob, const struct buf *text, void *opaque)
{
SPAN_CALLBACK("superscript", 1, buf2str(text));
}

/**
* direct writes
*/
static void
rndr_entity(struct buf *ob, const struct buf *text, void *opaque)
{
BLOCK_CALLBACK("entity", 1, buf2str(text));
}

static void
rndr_normal_text(struct buf *ob, const struct buf *text, void *opaque)
{
BLOCK_CALLBACK("normal_text", 1, buf2str(text));
}

static void
rndr_doc_header(struct buf *ob, void *opaque)
{
BLOCK_CALLBACK("doc_header", 0);
}

static void
rndr_doc_footer(struct buf *ob, void *opaque)
{
BLOCK_CALLBACK("doc_footer", 0);
}

static int
rndr_link_attribute(VALUE key, VALUE val, VALUE self)
{
struct rb_redcarpet_rndr *rndr;
Data_Get_Struct(self, struct rb_redcarpet_rndr, rndr);
key = rb_obj_as_string(key);
val = rb_obj_as_string(val);
bufprintf(rndr->ob, " %s=\"%s\"", StringValueCStr(key), StringValueCStr(val));
return ST_CONTINUE;
}

static void
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");
}
}

static struct sd_callbacks rb_redcarpet_callbacks = {
rndr_blockcode,
rndr_blockquote,
rndr_raw_block,
rndr_header,
rndr_hrule,
rndr_list,
rndr_listitem,
rndr_paragraph,
rndr_table,
rndr_tablerow,
rndr_tablecell,

rndr_autolink,
rndr_codespan,
rndr_double_emphasis,
rndr_emphasis,
rndr_image,
rndr_linebreak,
rndr_link,
rndr_raw_html,
rndr_triple_emphasis,
rndr_strikethrough,
rndr_superscript,

rndr_entity,
rndr_normal_text,

rndr_doc_header,
rndr_doc_footer,
};

static const char *rb_redcarpet_method_names[] = {
"block_code",
"block_quote",
"block_html",
"header",
"hrule",
"list",
"list_item",
"paragraph",
"table",
"table_row",
"table_cell",

"autolink",
"codespan",
"double_emphasis",
"emphasis",
"image",
"linebreak",
"link",
"raw_html",
"triple_emphasis",
"strikethrough",
"superscript",

"entity",
"normal_text",

"doc_header",
"doc_footer"
};

static const size_t rb_redcarpet_method_count = sizeof(rb_redcarpet_method_names)/sizeof(char *);

static VALUE rb_redcarpet_rbase_alloc(VALUE klass)
{
struct rb_redcarpet_rndr *rndr = ALLOC(struct rb_redcarpet_rndr);
memset(rndr, 0x0, sizeof(struct rb_redcarpet_rndr));
return Data_Wrap_Struct(klass, NULL, NULL, rndr);
}

static void rb_redcarpet__overload(VALUE self, VALUE base_class)
{
struct rb_redcarpet_rndr *rndr;

Data_Get_Struct(self, struct rb_redcarpet_rndr, rndr);
rndr->options.self = self;
rndr->options.base_class = base_class;

if (rb_obj_class(self) == rb_cRenderBase)
rb_raise(rb_eRuntimeError,
"The Redcarpet::Render::Base class cannot be instantiated. "
"Create an inheriting class instead to implement a custom renderer.");

if (rb_obj_class(self) != base_class) {
void **source = (void **)&rb_redcarpet_callbacks;
void **dest = (void **)&rndr->callbacks;
size_t i;

for (i = 0; i < rb_redcarpet_method_count; ++i) {
if (rb_respond_to(self, rb_intern(rb_redcarpet_method_names[i])))
dest[i] = source[i];
}
}
}

static VALUE rb_redcarpet_rbase_init(VALUE self)
{
rb_redcarpet__overload(self, rb_cRenderBase);
return Qnil;
}

static VALUE rb_redcarpet_html_init(int argc, VALUE *argv, VALUE self)
{
struct rb_redcarpet_rndr *rndr;
unsigned int render_flags = 0;
VALUE hash;

Data_Get_Struct(self, struct rb_redcarpet_rndr, rndr);

if (rb_scan_args(argc, argv, "01", &hash) == 1)
{
Check_Type(hash, T_HASH);

/* escape_html */
if (rb_hash_aref(hash, CSTR2SYM("escape_html")) == Qtrue)
render_flags |= HTML_ESCAPE;

/* filter_html */
if (rb_hash_aref(hash, CSTR2SYM("filter_html")) == Qtrue)
render_flags |= HTML_SKIP_HTML;

/* no_image */
if (rb_hash_aref(hash, CSTR2SYM("no_images")) == Qtrue)
render_flags |= HTML_SKIP_IMAGES;

/* no_links */
if (rb_hash_aref(hash, CSTR2SYM("no_links")) == Qtrue)
render_flags |= HTML_SKIP_LINKS;

/* filter_style */
if (rb_hash_aref(hash, CSTR2SYM("no_styles")) == Qtrue)
render_flags |= HTML_SKIP_STYLE;

/* safelink */
if (rb_hash_aref(hash, CSTR2SYM("safe_links_only")) == Qtrue)
render_flags |= HTML_SAFELINK;

if (rb_hash_aref(hash, CSTR2SYM("with_toc_data")) == Qtrue)
render_flags |= HTML_TOC;

if (rb_hash_aref(hash, CSTR2SYM("hard_wrap")) == Qtrue)
render_flags |= HTML_HARD_WRAP;

if (rb_hash_aref(hash, CSTR2SYM("xhtml")) == Qtrue)
render_flags |= HTML_USE_XHTML;

rndr->options.link_attributes = 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);

return Qnil;
}

static VALUE rb_redcarpet_htmltoc_init(VALUE self)
{
struct rb_redcarpet_rndr *rndr;
Data_Get_Struct(self, struct rb_redcarpet_rndr, rndr);

sdhtml_toc_renderer(&rndr->callbacks, (struct html_renderopt *)&rndr->options.html);
rb_redcarpet__overload(self, rb_cRenderHTML_TOC);

return Qnil;
}

static VALUE rb_redcarpet_smartypants_render(VALUE self, VALUE text)
{
VALUE result;
struct buf *output_buf;

Check_Type(text, T_STRING);

output_buf = bufnew(128);

sdhtml_smartypants(output_buf, RSTRING_PTR(text), RSTRING_LEN(text));
result = redcarpet_str_new(output_buf->data, output_buf->size, rb_enc_get(text));

bufrelease(output_buf);
return result;
}

void Init_redcarpet_rndr()
{
rb_mRender = rb_define_module_under(rb_mRedcarpet, "Render");

rb_cRenderBase = rb_define_class_under(rb_mRender, "Base", rb_cObject);
rb_define_alloc_func(rb_cRenderBase, rb_redcarpet_rbase_alloc);
rb_define_method(rb_cRenderBase, "initialize", rb_redcarpet_rbase_init, 0);

rb_cRenderHTML = rb_define_class_under(rb_mRender, "HTML", rb_cRenderBase);
rb_define_method(rb_cRenderHTML, "initialize", rb_redcarpet_html_init, -1);

rb_cRenderHTML_TOC = rb_define_class_under(rb_mRender, "HTML_TOC", rb_cRenderBase);
rb_define_method(rb_cRenderHTML_TOC, "initialize", rb_redcarpet_htmltoc_init, 0);

rb_mSmartyPants = rb_define_module_under(rb_mRender, "SmartyPants");
rb_define_method(rb_mSmartyPants, "postprocess", rb_redcarpet_smartypants_render, 1);
}
Something went wrong with that request. Please try again.