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

59 lines
1.4 KiB
C
Raw Normal View History

2015-11-25 11:07:06 -05:00
#include <ruby.h>
2015-11-26 11:18:02 -05:00
static ID id_temple, id_utils, id_escape_html;
static ID id_flatten, id_join;
2015-11-25 12:12:05 -05:00
static VALUE
attr_build_id(VALUE escape_attrs, VALUE ids)
{
VALUE truthy_ids, id, attr_value, mUtils;
2015-11-26 11:25:31 -05:00
int i, len;
2015-11-25 12:12:05 -05:00
2015-11-26 11:18:02 -05:00
ids = rb_funcall(ids, id_flatten, 0);
2015-11-25 12:12:05 -05:00
truthy_ids = rb_ary_new();
2015-11-26 11:25:31 -05:00
len = RARRAY_LEN(ids);
for (i = 0; i < len; i++) {
2015-11-25 12:12:05 -05:00
id = rb_ary_entry(ids, i);
if (!NIL_P(id) && id != Qfalse) {
rb_ary_push(truthy_ids, id);
}
}
2015-11-26 11:18:02 -05:00
attr_value = rb_funcall(truthy_ids, id_join, 1, rb_str_new_literal("_"));
2015-11-25 12:12:05 -05:00
if (RTEST(escape_attrs)) {
2015-11-26 11:18:02 -05:00
mUtils = rb_const_get(rb_const_get(rb_cObject, id_temple), id_utils);
attr_value = rb_funcall(mUtils, id_escape_html, 1, attr_value);
2015-11-25 12:12:05 -05:00
}
return attr_value;
}
static VALUE
rb_attr_build_id(int argc, VALUE *argv, RB_UNUSED_VAR(VALUE self))
{
VALUE array;
rb_check_arity(argc, 1, UNLIMITED_ARGUMENTS);
rb_scan_args(argc - 1, argv + 1, "*", &array);
return attr_build_id(argv[0], array);
}
2015-11-25 11:07:06 -05:00
void
Init_hamlit(void)
{
2015-11-25 12:12:05 -05:00
VALUE mHamlit, mAttributeBuilder;
mHamlit = rb_define_module("Hamlit");
mAttributeBuilder = rb_define_module_under(mHamlit, "AttributeBuilder");
rb_define_singleton_method(mAttributeBuilder, "build_id", rb_attr_build_id, -1);
2015-11-26 11:18:02 -05:00
id_temple = rb_intern("Temple");
id_utils = rb_intern("Utils");
id_escape_html = rb_intern("escape_html");
id_flatten = rb_intern("flatten");
id_join = rb_intern("join");
2015-11-25 11:07:06 -05:00
}