Skip to content

Commit

Permalink
Add UTF-8 support for Ruby 1.9.
Browse files Browse the repository at this point in the history
  • Loading branch information
jgarber committed Feb 20, 2009
1 parent c941af1 commit 002a02f
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 20 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG
@@ -1,6 +1,6 @@
=== Edge

* Make compatible with Ruby 1.9
* Make compatible with Ruby 1.9.

* Image URLs and image titles can now contain parentheses. #71

Expand Down
30 changes: 24 additions & 6 deletions ext/redcloth_scan/redcloth.h
Expand Up @@ -7,6 +7,24 @@
#define RSTRING_PTR(str) RSTRING(str)->ptr
#endif


// Different string conversions for ruby 1.8 and ruby 1.9. For 1.9,
// 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())

// For Ruby 1.8
#else
#define STR_NEW(p,n) rb_str_new((p),(n))
#define STR_NEW2(p) rb_str_new2((p))

#endif


/* variable defs */
#ifndef redcloth_scan_c
extern VALUE super_ParseError, mRedCloth, super_RedCloth;
Expand Down Expand Up @@ -34,9 +52,9 @@ VALUE red_pass_code(VALUE, VALUE, VALUE, ID);
#define CLEAR_REGS() regs = rb_hash_new();
#define RESET_REG() reg = NULL
#define CAT(H) rb_str_cat(H, ts, te-ts)
#define CLEAR(H) H = rb_str_new2("")
#define CLEAR(H) H = STR_NEW2("")
#define RSTRIP_BANG(H) rb_funcall(H, rb_intern("rstrip!"), 0)
#define SET_PLAIN_BLOCK(T) plain_block = rb_str_new2(T)
#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()
Expand All @@ -55,7 +73,7 @@ VALUE red_pass_code(VALUE, VALUE, VALUE, ID);
#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)), rb_str_new2(V));
#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 SET_ATTRIBUTES() \
SET_ATTRIBUTE("class_buf", "class"); \
Expand All @@ -74,15 +92,15 @@ VALUE red_pass_code(VALUE, VALUE, VALUE, ID);
}
#define STORE(T) \
if (p > reg && reg >= ts) { \
VALUE str = rb_str_new(reg, p-reg); \
VALUE str = STR_NEW(reg, p-reg); \
rb_hash_aset(regs, ID2SYM(rb_intern(T)), str); \
/*printf("STORE(" T ") '%s' (p:'%s' reg:'%s')\n", RSTRING_PTR(str), p, reg);*/ \
} else { \
rb_hash_aset(regs, ID2SYM(rb_intern(T)), Qnil); \
}
#define STORE_B(T) \
if (p > bck && bck >= ts) { \
VALUE str = rb_str_new(bck, p-bck); \
VALUE str = STR_NEW(bck, p-bck); \
rb_hash_aset(regs, ID2SYM(rb_intern(T)), str); \
/*printf("STORE_B(" T ") '%s' (p:'%s' reg:'%s')\n", RSTRING_PTR(str), p, reg);*/ \
} else { \
Expand Down Expand Up @@ -154,7 +172,7 @@ VALUE red_pass_code(VALUE, VALUE, VALUE, ID);
} \
rb_hash_aset(regs, ID2SYM(rb_intern("nest")), INT2NUM(nest)); \
rb_str_append(html, rb_funcall(self, rb_intern(listm), 1, regs)); \
rb_ary_store(list_layout, nest-1, rb_str_new2(list_type)); \
rb_ary_store(list_layout, nest-1, STR_NEW2(list_type)); \
CLEAR_REGS(); \
ASET("first", "true"); \
} \
Expand Down
12 changes: 6 additions & 6 deletions ext/redcloth_scan/redcloth_inline.c.rl
Expand Up @@ -65,10 +65,10 @@ red_parse_title(VALUE regs, VALUE ref)
}
--p;
}
VALUE title = rb_str_new(p + 1, RSTRING_PTR(txt) + RSTRING_LEN(txt) - 2 - p );
VALUE title = STR_NEW(p + 1, RSTRING_PTR(txt) + RSTRING_LEN(txt) - 2 - p );
if (p > RSTRING_PTR(txt) && *(p - 1) == ' ') --p;
if (p != RSTRING_PTR(txt)) {
rb_hash_aset(regs, ref, rb_str_new(RSTRING_PTR(txt), p - RSTRING_PTR(txt) ));
rb_hash_aset(regs, ref, STR_NEW(RSTRING_PTR(txt), p - RSTRING_PTR(txt) ));
rb_hash_aset(regs, ID2SYM(rb_intern("title")), title);
}
}
Expand All @@ -81,7 +81,7 @@ red_pass_code(VALUE self, VALUE regs, VALUE ref, ID meth)
{
VALUE txt = rb_hash_aref(regs, ref);
if (!NIL_P(txt)) {
VALUE txt2 = rb_str_new2("");
VALUE txt2 = STR_NEW2("");
rb_str_cat_escaped_for_preformatted(self, txt2, RSTRING_PTR(txt), RSTRING_PTR(txt) + RSTRING_LEN(txt));
rb_hash_aset(regs, ref, txt2);
}
Expand Down Expand Up @@ -149,7 +149,7 @@ redcloth_inline(self, p, pe, refs)
int cs, act;
char *ts = NULL, *te = NULL, *reg = NULL, *eof = NULL;
char *orig_p = p;
VALUE block = rb_str_new2("");
VALUE block = STR_NEW2("");
VALUE regs = Qnil;

%% write init;
Expand All @@ -169,7 +169,7 @@ rb_str_cat_escaped(self, str, ts, te)
VALUE self, str;
char *ts, *te;
{
VALUE source_str = rb_str_new(ts, te-ts);
VALUE source_str = STR_NEW(ts, te-ts);
VALUE escaped_str = rb_funcall(self, rb_intern("escape"), 1, source_str);
rb_str_concat(str, escaped_str);
}
Expand All @@ -179,7 +179,7 @@ rb_str_cat_escaped_for_preformatted(self, str, ts, te)
VALUE self, str;
char *ts, *te;
{
VALUE source_str = rb_str_new(ts, te-ts);
VALUE source_str = STR_NEW(ts, te-ts);
VALUE escaped_str = rb_funcall(self, rb_intern("escape_pre"), 1, source_str);
rb_str_concat(str, escaped_str);
}
Expand Down
14 changes: 7 additions & 7 deletions ext/redcloth_scan/redcloth_scan.c.rl
Expand Up @@ -33,9 +33,9 @@ redcloth_transform(self, p, pe, refs)
char *orig_p = p, *orig_pe = pe;
int cs, act, nest = 0;
char *ts = NULL, *te = NULL, *reg = NULL, *bck = NULL, *eof = NULL;
VALUE html = rb_str_new2("");
VALUE table = rb_str_new2("");
VALUE block = rb_str_new2("");
VALUE html = STR_NEW2("");
VALUE table = STR_NEW2("");
VALUE block = STR_NEW2("");
VALUE regs; CLEAR_REGS()


Expand Down Expand Up @@ -84,7 +84,7 @@ redcloth_html_esc(int argc, VALUE* argv, VALUE self) //(self, str, level)

rb_scan_args(argc, argv, "11", &str, &level);

VALUE new_str = rb_str_new2("");
VALUE new_str = STR_NEW2("");
if (str == Qnil)
return new_str;

Expand Down Expand Up @@ -142,7 +142,7 @@ redcloth_html_esc(int argc, VALUE* argv, VALUE self) //(self, str, level)
static VALUE
redcloth_latex_esc(VALUE self, VALUE str)
{
VALUE new_str = rb_str_new2("");
VALUE new_str = STR_NEW2("");

if (str == Qnil)
return new_str;
Expand Down Expand Up @@ -181,7 +181,7 @@ redcloth_latex_esc(VALUE self, VALUE str)
if (t2 > t)
rb_str_cat(new_str, t, t2-t);
VALUE opts = rb_hash_new();
rb_hash_aset(opts, ID2SYM(rb_intern("text")), rb_str_new2(ch));
rb_hash_aset(opts, ID2SYM(rb_intern("text")), STR_NEW2(ch));
rb_str_concat(new_str, rb_funcall(self, rb_intern("entity"), 1, opts));
t = t2 + 1;
}
Expand All @@ -201,7 +201,7 @@ static VALUE
redcloth_to(self, formatter)
VALUE self, formatter;
{
rb_funcall(self, rb_intern("delete!"), 1, rb_str_new2("\r"));
rb_funcall(self, rb_intern("delete!"), 1, STR_NEW2("\r"));
VALUE working_copy = rb_obj_clone(self);
rb_extend_object(working_copy, formatter);

Expand Down

0 comments on commit 002a02f

Please sign in to comment.