1
0
Fork 0
mirror of https://github.com/haml/haml.git synced 2022-11-09 12:33:31 -05:00
haml--haml/ext/hamlit/hamlit.c

93 lines
2 KiB
C
Raw Normal View History

2015-11-25 11:07:06 -05:00
#include <ruby.h>
#include <ruby/encoding.h>
#include "houdini.h"
2015-11-25 11:07:06 -05:00
2015-11-26 11:57:18 -05:00
VALUE mAttributeBuilder;
2015-11-27 14:32:09 -05:00
static ID id_flatten, id_underscore;
2015-11-26 11:18:02 -05:00
2015-11-28 01:45:56 -05:00
static void
delete_falsey_values(VALUE values)
{
VALUE value;
long i;
for (i = RARRAY_LEN(values) - 1; 0 <= i; i--) {
value = rb_ary_entry(values, i);
if (!RTEST(value)) {
rb_ary_delete_at(values, i);
}
}
}
static VALUE
escape_html(VALUE str)
{
gh_buf buf = GH_BUF_INIT;
Check_Type(str, T_STRING);
if (houdini_escape_html0(&buf, (const uint8_t *)RSTRING_PTR(str), RSTRING_LEN(str), 0)) {
str = rb_enc_str_new(buf.ptr, buf.size, rb_utf8_encoding());
gh_buf_free(&buf);
}
return str;
}
2015-11-28 01:45:56 -05:00
static VALUE
escape_attribute(VALUE escape_attrs, VALUE str)
{
if (RTEST(escape_attrs)) {
return escape_html(str);
} else {
return str;
}
}
2015-11-27 13:32:53 -05:00
static VALUE
rb_escape_html(RB_UNUSED_VAR(VALUE self), VALUE str)
{
2015-11-27 13:52:10 -05:00
return escape_html(rb_convert_type(str, T_STRING, "String", "to_s"));
2015-11-27 13:32:53 -05:00
}
2015-11-25 12:12:05 -05:00
static VALUE
2015-11-28 01:45:56 -05:00
hamlit_build_id(VALUE escape_attrs, VALUE values)
2015-11-25 12:12:05 -05:00
{
2015-11-28 01:45:56 -05:00
VALUE attr_value;
2015-11-25 12:12:05 -05:00
2015-11-28 01:45:56 -05:00
values = rb_funcall(values, id_flatten, 0);
delete_falsey_values(values);
2015-11-25 12:12:05 -05:00
2015-11-28 01:45:56 -05:00
attr_value = rb_ary_join(values, rb_const_get(mAttributeBuilder, id_underscore));
return escape_attribute(escape_attrs, attr_value);
2015-11-25 12:12:05 -05:00
}
static VALUE
2015-11-28 01:41:47 -05:00
rb_hamlit_build_id(int argc, VALUE *argv, RB_UNUSED_VAR(VALUE self))
2015-11-25 12:12:05 -05:00
{
VALUE array;
rb_check_arity(argc, 1, UNLIMITED_ARGUMENTS);
rb_scan_args(argc - 1, argv + 1, "*", &array);
2015-11-28 01:41:47 -05:00
return hamlit_build_id(argv[0], array);
2015-11-25 12:12:05 -05:00
}
2015-11-25 11:07:06 -05:00
void
Init_hamlit(void)
{
2015-11-27 13:32:53 -05:00
VALUE mHamlit, mUtils;
2015-11-27 22:08:47 -05:00
mHamlit = rb_define_module("Hamlit");
mUtils = rb_define_module_under(mHamlit, "Utils");
2015-11-25 12:12:05 -05:00
mAttributeBuilder = rb_define_module_under(mHamlit, "AttributeBuilder");
2015-11-26 11:18:02 -05:00
2015-11-27 22:08:47 -05:00
rb_define_singleton_method(mUtils, "escape_html", rb_escape_html, 1);
2015-11-28 01:41:47 -05:00
rb_define_singleton_method(mAttributeBuilder, "build_id", rb_hamlit_build_id, -1);
2015-11-26 11:57:18 -05:00
2015-11-27 22:08:47 -05:00
id_flatten = rb_intern("flatten");
2015-11-26 11:57:18 -05:00
id_underscore = rb_intern("UNDERSCORE");
2015-11-27 22:08:47 -05:00
2015-11-26 12:02:18 -05:00
rb_const_set(mAttributeBuilder, id_underscore, rb_obj_freeze(rb_str_new_cstr("_")));
2015-11-25 11:07:06 -05:00
}