ext/redcloth_scan/redcloth.h in BBRedCloth-0.8.8 vs ext/redcloth_scan/redcloth.h in BBRedCloth-0.9.0.alpha1

- old
+ new

@@ -19,12 +19,12 @@ // we need to set the encoding of the string. // For Ruby 1.9 #ifdef HAVE_RUBY_ENCODING_H #include "ruby/encoding.h" -#define STR_NEW(p,n) rb_enc_str_new((p),(n),rb_utf8_encoding()) -#define STR_NEW2(p) rb_enc_str_new((p),strlen(p),rb_utf8_encoding()) +#define STR_NEW(p,n) rb_enc_str_new((p),(n),rb_enc_from_index(ENCODING_GET(self))) +#define STR_NEW2(p) rb_enc_str_new((p),strlen(p),rb_enc_from_index(ENCODING_GET(self))) // For Ruby 1.8 #else #define STR_NEW(p,n) rb_str_new((p),(n)) #define STR_NEW2(p) rb_str_new2((p)) @@ -44,10 +44,11 @@ VALUE redcloth_inline(VALUE, char *, char *, VALUE); VALUE redcloth_inline2(VALUE, VALUE, VALUE); VALUE redcloth_attribute_parser(int, VALUE, char *, char *); VALUE redcloth_attributes(VALUE, VALUE); VALUE redcloth_link_attributes(VALUE, VALUE); +VALUE red_parse_title(VALUE, VALUE, VALUE); VALUE redcloth_transform(VALUE, char *, char *, VALUE); VALUE redcloth_transform2(VALUE, VALUE); void red_inc(VALUE, VALUE); VALUE red_block(VALUE, VALUE, VALUE, VALUE); VALUE red_blockcode(VALUE, VALUE, VALUE); @@ -66,44 +67,50 @@ #define UNLESS_DISABLED_INLINE(H, T, F) \ if (rb_funcall(rb_funcall(self, rb_intern("disable_inline"), 0),rb_intern("include?"), 1,ID2SYM(rb_intern(#T))) == Qtrue) { rb_str_append(H,red_passthrough(self, ts, te, refs)); } \ else { /*VALUE test = STR_NEW(ts,te-ts); printf("\nred_passthrough_else() '%s'\n", RSTRING(test)->ptr);*/ F } #define UNLESS_DISABLED_ATTRIBUTE(T, F) \ if (!(rb_funcall(rb_funcall(self, rb_intern("disable_inline"), 0),rb_intern("include?"), 1,ID2SYM(rb_intern(#T))) == Qtrue)) { F } //if !(rb_funcall(rb_funcall(self, rb_intern("disable_inline"), 0),rb_intern("include?"), 1,ID2SYM(rb_intern(#T))) == Qtrue) { F } -#define CLEAR_REGS() regs = rb_hash_new(); +#define CLEAR_REGS() regs = rb_hash_new(); attr_regs = rb_hash_new(); #define RESET_REG() reg = NULL +#define MARK() reg = p; +#define MARK_B() bck = p; +#define MARK_ATTR() attr_reg = p; #define CAT(H) rb_str_cat(H, ts, te-ts) #define CLEAR(H) H = STR_NEW2("") +#define RSTRIP_BANG(H) rb_funcall(H, rb_intern("rstrip!"), 0) #define SET_PLAIN_BLOCK(T) plain_block = STR_NEW2(T) #define RESET_TYPE(T) rb_hash_aset(regs, ID2SYM(rb_intern("type")), plain_block) #define INLINE(H, T) rb_str_append(H, rb_funcall(self, rb_intern(T), 1, regs)) #define DONE(H) rb_str_append(html, H); CLEAR(H); CLEAR_REGS() #define BBDONE() failed_start = STR_NEW2(""); html = STR_NEW2(""); CLEAR_REGS(); RESET_REG(); #define PASS(H, A, T) rb_str_append(H, red_pass(self, regs, ID2SYM(rb_intern(A)), rb_intern(T), refs)) #define PARSE_ATTR(A) red_parse_attr(self, regs, ID2SYM(rb_intern(A))) #define PARSE_LINK_ATTR(A) red_parse_link_attr(self, regs, ID2SYM(rb_intern(A))) -#define PASS_CODE(H, A, T, O) rb_str_append(H, red_pass_code(self, regs, ID2SYM(rb_intern(A)), rb_intern(T))) +#define PARSE_IMAGE_ATTR(A) red_parse_image_attr(self, regs, ID2SYM(rb_intern(A))) +#define PASS_CODE(H, A, T) rb_str_append(H, red_pass_code(self, regs, ID2SYM(rb_intern(A)), rb_intern(T))) #define ADD_BLOCK() \ rb_str_append(html, red_block(self, regs, block, refs)); \ extend = Qnil; \ CLEAR(block); \ CLEAR_REGS() #define ADD_EXTENDED_BLOCK() rb_str_append(html, red_block(self, regs, block, refs)); CLEAR(block); rb_hash_aset(regs,ID2SYM(rb_intern("fallback")),Qnil); #define END_EXTENDED() extend = Qnil; CLEAR_REGS(); -#define IS_NOT_EXTENDED() NIL_P(extend) #define ADD_BLOCKCODE() rb_str_append(html, red_blockcode(self, regs, block)); CLEAR(block); CLEAR_REGS() #define ADD_EXTENDED_BLOCKCODE() rb_str_append(html, red_blockcode(self, regs, block)); CLEAR(block); #define ASET(T, V) rb_hash_aset(regs, ID2SYM(rb_intern(T)), STR_NEW2(V)); -#define AINC(T) red_inc(regs, ID2SYM(rb_intern(T))); +#define ATTR_SET(T, V) rb_hash_aset(attr_regs, ID2SYM(rb_intern(T)), STR_NEW2(V)); +#define ATTR_INC(T) red_inc(attr_regs, ID2SYM(rb_intern(T))); +#define INC(N) N++; #define SET_ATTRIBUTES() \ - VALUE buf = Qnil; \ SET_ATTRIBUTE("class_buf", "class"); \ SET_ATTRIBUTE("id_buf", "id"); \ SET_ATTRIBUTE("lang_buf", "lang"); \ - SET_ATTRIBUTE("style_buf", "style"); + SET_ATTRIBUTE("style_buf", "style"); \ + rb_funcall(regs, rb_intern("merge!"), 1, attr_regs); \ + attr_regs = rb_hash_new(); #define SET_ATTRIBUTE(B, A) \ - buf = rb_hash_aref(regs, ID2SYM(rb_intern(B))); \ - if (buf != Qnil) rb_hash_aset(regs, ID2SYM(rb_intern(A)), buf); + if (rb_hash_aref(regs, ID2SYM(rb_intern(B))) != Qnil) rb_hash_aset(regs, ID2SYM(rb_intern(A)), rb_hash_aref(regs, ID2SYM(rb_intern(B)))); #define TRANSFORM(T) \ if (p > reg && reg >= ts) { \ VALUE str = redcloth_transform(self, reg, p, refs); \ rb_hash_aset(regs, ID2SYM(rb_intern(T)), str); \ /* printf("TRANSFORM(" T ") '%s' (p:'%d' reg:'%d')\n", RSTRING(str)->ptr, p, reg);*/ \ @@ -125,19 +132,28 @@ /* printf("STORE_B(" T ") '%s' (p:'%d' reg:'%d')\n", RSTRING(str)->ptr, p, reg);*/ \ } else { \ rb_hash_aset(regs, ID2SYM(rb_intern(T)), Qnil); \ } #define BBCODE_ENABLED() rb_funcall(self, rb_intern("bbcode"), 0) == Qtrue +#define STORE_ATTR(T) \ + if (p > attr_reg && attr_reg >= ts) { \ + VALUE str = STR_NEW(attr_reg, p-attr_reg); \ + rb_hash_aset(attr_regs, ID2SYM(rb_intern(T)), str); \ + /*printf("STORE_B(" T ") '%s' (p:'%s' reg:'%s')\n", RSTRING_PTR(str), p, reg);*/ \ + } else { \ + rb_hash_aset(attr_regs, ID2SYM(rb_intern(T)), Qnil); \ + } + #define STORE_URL(T) \ if (p > reg && reg >= ts) { \ char punct = 1; \ while (p > reg && punct == 1) { \ switch (*(p - 1)) { \ case ')': \ { /*needed to keep inside chars scoped for less memory usage*/\ char *temp_p = p - 1; \ - char level = -1; \ + signed char level = -1; \ while (temp_p > reg) { \ switch(*(temp_p - 1)) { \ case '(': ++level; break; \ case ')': --level; break; \ } \ @@ -160,24 +176,28 @@ rb_hash_aset(regs, ID2SYM(rb_intern(T)), rb_hash_aref(refs, rb_hash_aref(regs, ID2SYM(rb_intern(T))))); \ } #define STORE_LINK_ALIAS() \ rb_hash_aset(refs_found, rb_hash_aref(regs, ID2SYM(rb_intern("text"))), rb_hash_aref(regs, ID2SYM(rb_intern("href")))) #define CLEAR_LIST() list_layout = rb_ary_new() -#define LIST_ITEM() \ +#define SET_LIST_TYPE(T) list_type = T; +#define NEST() nest ++; +#define RESET_NEST() nest = 0; +#define LIST_LAYOUT() \ int aint = 0; \ VALUE aval = rb_ary_entry(list_index, nest-1); \ if (aval != Qnil) aint = NUM2INT(aval); \ - if (strcmp(list_type, "ol") == 0) \ + if (strcmp(list_type, "ol") == 0 && nest > 0) \ { \ rb_ary_store(list_index, nest-1, INT2NUM(aint + 1)); \ } \ if (nest > RARRAY_LEN(list_layout)) \ { \ + SET_ATTRIBUTES(); \ sprintf(listm, "%s_open", list_type); \ - if (list_continue == 1) \ + if (!NIL_P(rb_hash_aref(regs, ID2SYM(rb_intern("list_continue"))))) \ { \ - list_continue = 0; \ + rb_hash_aset(regs, ID2SYM(rb_intern("list_continue")), Qnil); \ rb_hash_aset(regs, ID2SYM(rb_intern("start")), rb_ary_entry(list_index, nest-1)); \ } \ else \ { \ VALUE start = rb_hash_aref(regs, ID2SYM(rb_intern("start"))); \ @@ -196,20 +216,26 @@ rb_ary_store(list_layout, nest-1, STR_NEW2(list_type)); \ CLEAR_REGS(); \ ASET("first", "true"); \ } \ LIST_CLOSE(); \ + if (nest != 0) LIST_ITEM_CLOSE(); \ + CLEAR_REGS(); \ rb_hash_aset(regs, ID2SYM(rb_intern("nest")), INT2NUM(RARRAY_LEN(list_layout))); \ - ASET("type", "li_open") + ASET("type", "li_open"); +#define LIST_ITEM_CLOSE() \ + if ( rb_hash_aref(regs, ID2SYM(rb_intern("first"))) == Qnil ) \ + rb_str_append(html, rb_funcall(self, rb_intern("li_close"), 1, regs)); #define LIST_CLOSE() \ while (nest < RARRAY_LEN(list_layout)) \ { \ rb_hash_aset(regs, ID2SYM(rb_intern("nest")), INT2NUM(RARRAY_LEN(list_layout))); \ VALUE end_list = rb_ary_pop(list_layout); \ if (!NIL_P(end_list)) \ { \ StringValue(end_list); \ sprintf(listm, "%s_close", RSTRING_PTR(end_list)); \ + LIST_ITEM_CLOSE(); \ rb_str_append(html, rb_funcall(self, rb_intern(listm), 1, regs)); \ } \ }