xrb 0.1 → 0.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (63) hide show
  1. checksums.yaml +7 -0
  2. checksums.yaml.gz.sig +1 -0
  3. data/bake/xrb/entities.rb +60 -0
  4. data/bake/xrb/parsers.rb +66 -0
  5. data/ext/Makefile +270 -0
  6. data/ext/XRB_Extension.bundle +0 -0
  7. data/ext/escape.o +0 -0
  8. data/ext/extconf.h +5 -0
  9. data/ext/extconf.rb +21 -0
  10. data/ext/markup.o +0 -0
  11. data/ext/mkmf.log +122 -0
  12. data/ext/query.o +0 -0
  13. data/ext/tag.o +0 -0
  14. data/ext/template.o +0 -0
  15. data/ext/xrb/escape.c +152 -0
  16. data/ext/xrb/escape.h +15 -0
  17. data/ext/xrb/markup.c +1949 -0
  18. data/ext/xrb/markup.h +6 -0
  19. data/ext/xrb/markup.rl +226 -0
  20. data/ext/xrb/query.c +619 -0
  21. data/ext/xrb/query.h +6 -0
  22. data/ext/xrb/query.rl +82 -0
  23. data/ext/xrb/tag.c +204 -0
  24. data/ext/xrb/tag.h +21 -0
  25. data/ext/xrb/template.c +1114 -0
  26. data/ext/xrb/template.h +6 -0
  27. data/ext/xrb/template.rl +77 -0
  28. data/ext/xrb/xrb.c +72 -0
  29. data/ext/xrb/xrb.h +132 -0
  30. data/ext/xrb.o +0 -0
  31. data/lib/xrb/buffer.rb +103 -0
  32. data/lib/xrb/builder.rb +229 -0
  33. data/lib/xrb/entities.rb +2137 -0
  34. data/lib/xrb/entities.xrb +15 -0
  35. data/lib/xrb/error.rb +81 -0
  36. data/lib/xrb/fallback/markup.rb +1657 -0
  37. data/lib/xrb/fallback/markup.rl +227 -0
  38. data/lib/xrb/fallback/query.rb +548 -0
  39. data/lib/xrb/fallback/query.rl +88 -0
  40. data/lib/xrb/fallback/template.rb +829 -0
  41. data/lib/xrb/fallback/template.rl +80 -0
  42. data/lib/xrb/markup.rb +56 -0
  43. data/lib/xrb/native.rb +15 -0
  44. data/lib/xrb/parsers.rb +16 -0
  45. data/lib/xrb/query.rb +80 -0
  46. data/lib/xrb/reference.rb +108 -0
  47. data/lib/xrb/strings.rb +47 -0
  48. data/lib/xrb/tag.rb +115 -0
  49. data/lib/xrb/template.rb +128 -0
  50. data/lib/xrb/uri.rb +100 -0
  51. data/lib/xrb/version.rb +8 -0
  52. data/lib/xrb.rb +11 -0
  53. data/license.md +23 -0
  54. data/readme.md +34 -0
  55. data.tar.gz.sig +0 -0
  56. metadata +118 -58
  57. metadata.gz.sig +2 -0
  58. data/README +0 -60
  59. data/app/helpers/ui_helper.rb +0 -80
  60. data/app/models/xrb/element.rb +0 -9
  61. data/lib/xrb/engine.rb +0 -4
  62. data/rails/init.rb +0 -1
  63. data/xrb.gemspec +0 -12
data/ext/xrb/tag.c ADDED
@@ -0,0 +1,204 @@
1
+
2
+ #include "escape.h"
3
+ #include "tag.h"
4
+
5
+ VALUE XRB_Tag_split(VALUE self, VALUE qualified_name) {
6
+ const char * begin = RSTRING_PTR(qualified_name);
7
+ const char * end = RSTRING_END(qualified_name);
8
+
9
+ const char * p = begin;
10
+
11
+ while (p != end) {
12
+ if (*p == ':') {
13
+ return rb_ary_new_from_args(2,
14
+ rb_enc_str_new(begin, p-begin, rb_enc_get(qualified_name)),
15
+ rb_enc_str_new(p+1, end-p-1, rb_enc_get(qualified_name))
16
+ );
17
+ }
18
+
19
+ p += 1;
20
+ }
21
+
22
+ return rb_ary_new_from_args(2, Qnil, qualified_name);
23
+ }
24
+
25
+ inline static int XRB_Tag_valid_attributes(VALUE value) {
26
+ return (rb_type(value) == T_HASH) || (rb_type(value) == T_ARRAY);
27
+ }
28
+
29
+ // Key can be either symbol or string. This method efficiently converts either to a string.
30
+ inline static VALUE XRB_Tag_key_string(VALUE key) {
31
+ if (SYMBOL_P(key)) {
32
+ return rb_sym2str(key);
33
+ }
34
+
35
+ StringValue(key);
36
+ return key;
37
+ }
38
+
39
+ inline static VALUE XRB_Tag_prefix_key(VALUE prefix, VALUE key) {
40
+ VALUE buffer;
41
+
42
+ if (prefix == Qnil) {
43
+ return XRB_Tag_key_string(key);
44
+ }
45
+
46
+ buffer = rb_str_dup(XRB_Tag_key_string(prefix));
47
+ rb_str_cat_cstr(buffer, "-");
48
+ rb_str_append(buffer, XRB_Tag_key_string(key));
49
+
50
+ return buffer;
51
+ }
52
+
53
+ VALUE XRB_Tag_append_attributes(VALUE self, VALUE buffer, VALUE attributes, VALUE prefix);
54
+
55
+ static void XRB_Tag_append_tag_attribute(VALUE buffer, VALUE key, VALUE value, VALUE prefix) {
56
+ // We skip over attributes with nil value:
57
+ if (value == Qnil || value == Qfalse) return;
58
+
59
+ // Modify key to contain the prefix:
60
+ key = XRB_Tag_prefix_key(prefix, key);
61
+
62
+ if (XRB_Tag_valid_attributes(value)) {
63
+ XRB_Tag_append_attributes(Qnil, buffer, value, key);
64
+ } else {
65
+ rb_str_cat_cstr(buffer, " ");
66
+ rb_str_append(buffer, key);
67
+
68
+ if (value != Qtrue) {
69
+ rb_str_cat_cstr(buffer, "=\"");
70
+ XRB_Markup_append(Qnil, buffer, value);
71
+ rb_str_cat_cstr(buffer, "\"");
72
+ }
73
+ }
74
+ }
75
+
76
+ typedef struct {
77
+ VALUE buffer;
78
+ VALUE prefix;
79
+ } XRB_Tag_Accumulation;
80
+
81
+ static int XRB_Tag_append_tag_attribute_foreach(VALUE key, VALUE value, VALUE _argument) {
82
+ XRB_Tag_Accumulation * argument = (XRB_Tag_Accumulation *)_argument;
83
+
84
+ XRB_Tag_append_tag_attribute(argument->buffer, key, value, argument->prefix);
85
+
86
+ return ST_CONTINUE;
87
+ }
88
+
89
+ VALUE XRB_Tag_append_attributes(VALUE self, VALUE buffer, VALUE attributes, VALUE prefix) {
90
+ int type = rb_type(attributes);
91
+
92
+ if (type == T_HASH) {
93
+ XRB_Tag_Accumulation argument = {buffer, prefix};
94
+ rb_hash_foreach(attributes, &XRB_Tag_append_tag_attribute_foreach, (VALUE)&argument);
95
+ } else if (type == T_ARRAY) {
96
+ long i;
97
+
98
+ for (i = 0; i < RARRAY_LEN(attributes); i++) {
99
+ VALUE attribute = RARRAY_AREF(attributes, i);
100
+ VALUE key = RARRAY_AREF(attribute, 0);
101
+ VALUE value = RARRAY_AREF(attribute, 1);
102
+
103
+ XRB_Tag_append_tag_attribute(buffer, key, value, prefix);
104
+ }
105
+ } else {
106
+ rb_raise(rb_eArgError, "expected hash or array for attributes");
107
+ }
108
+
109
+ return Qnil;
110
+ }
111
+
112
+ VALUE XRB_Tag_append_tag(VALUE self, VALUE buffer, VALUE name, VALUE attributes, VALUE content) {
113
+ StringValue(name);
114
+
115
+ rb_str_cat_cstr(buffer, "<");
116
+ rb_str_buf_append(buffer, name);
117
+
118
+ XRB_Tag_append_attributes(self, buffer, attributes, Qnil);
119
+
120
+ if (content == Qnil || content == Qfalse) {
121
+ rb_str_cat_cstr(buffer, "/>");
122
+ } else {
123
+ rb_str_cat_cstr(buffer, ">");
124
+
125
+ if (content != Qtrue) {
126
+ XRB_Markup_append(self, buffer, content);
127
+ }
128
+
129
+ rb_str_cat_cstr(buffer, "</");
130
+ rb_str_buf_append(buffer, name);
131
+ rb_str_cat_cstr(buffer, ">");
132
+ }
133
+
134
+ return Qnil;
135
+ }
136
+
137
+ VALUE XRB_Tag_format_tag(VALUE self, VALUE name, VALUE attributes, VALUE content) {
138
+ rb_encoding *encoding = rb_enc_get(name);
139
+
140
+ VALUE buffer = rb_enc_str_new(0, 0, encoding);
141
+ rb_str_reserve(buffer, 256);
142
+
143
+ XRB_Tag_append_tag(self, buffer, name, attributes, content);
144
+
145
+ return buffer;
146
+ }
147
+
148
+ VALUE XRB_Tag_write_opening_tag(VALUE self, VALUE buffer) {
149
+ VALUE name = rb_struct_getmember(self, id_name);
150
+ VALUE attributes = rb_struct_getmember(self, id_attributes);
151
+ VALUE closed = rb_struct_getmember(self, id_closed);
152
+
153
+ StringValue(name);
154
+
155
+ rb_str_reserve(buffer, RSTRING_LEN(name) + 256);
156
+
157
+ rb_str_cat_cstr(buffer, "<");
158
+ rb_str_buf_append(buffer, name);
159
+
160
+ XRB_Tag_append_attributes(self, buffer, attributes, Qnil);
161
+
162
+ if (closed == Qtrue) {
163
+ rb_str_cat_cstr(buffer, "/>");
164
+ } else {
165
+ rb_str_cat_cstr(buffer, ">");
166
+ }
167
+
168
+ return Qnil;
169
+ }
170
+
171
+ VALUE XRB_Tag_write_closing_tag(VALUE self, VALUE buffer) {
172
+ VALUE name = rb_struct_getmember(self, id_name);
173
+
174
+ StringValue(name);
175
+
176
+ rb_str_reserve(buffer, RSTRING_LEN(name) + 3);
177
+
178
+ rb_str_cat_cstr(buffer, "</");
179
+ rb_str_buf_append(buffer, name);
180
+ rb_str_cat_cstr(buffer, ">");
181
+
182
+ return Qnil;
183
+ }
184
+
185
+ void Init_xrb_tag() {
186
+ rb_undef_method(rb_class_of(rb_XRB_Tag), "append_attributes");
187
+ rb_define_singleton_method(rb_XRB_Tag, "append_attributes", XRB_Tag_append_attributes, 3);
188
+
189
+ rb_undef_method(rb_class_of(rb_XRB_Tag), "append_tag");
190
+ rb_define_singleton_method(rb_XRB_Tag, "append_tag", XRB_Tag_append_tag, 4);
191
+
192
+ rb_undef_method(rb_class_of(rb_XRB_Tag), "format_tag");
193
+ rb_define_singleton_method(rb_XRB_Tag, "format_tag", XRB_Tag_format_tag, 3);
194
+
195
+ rb_undef_method(rb_class_of(rb_XRB_Tag), "split");
196
+ rb_define_singleton_method(rb_XRB_Tag, "split", XRB_Tag_split, 1);
197
+
198
+ rb_undef_method(rb_XRB_Tag, "write_opening_tag");
199
+ rb_define_method(rb_XRB_Tag, "write_opening_tag", XRB_Tag_write_opening_tag, 1);
200
+
201
+ rb_undef_method(rb_XRB_Tag, "write_closing_tag");
202
+ rb_define_method(rb_XRB_Tag, "write_closing_tag", XRB_Tag_write_closing_tag, 1);
203
+ }
204
+
data/ext/xrb/tag.h ADDED
@@ -0,0 +1,21 @@
1
+
2
+ #pragma once
3
+
4
+ #include "xrb.h"
5
+
6
+ void Init_xrb_tag();
7
+
8
+ // Split a qualified name `namespace:name` into it's components. Return `[nil, name]` if no namespace is present.
9
+ // Usage: namespace, name = XRB::Tag.split(qualified_name)
10
+ VALUE XRB_Tag_split(VALUE self, VALUE name);
11
+
12
+ // Append attributes to the buffer, e.g. {data: {id: 10}} => ' data-id="10"'
13
+ VALUE XRB_Tag_append_attributes(VALUE self, VALUE buffer, VALUE attributes, VALUE prefix);
14
+ // Append a full tag with content to the buffer.
15
+ VALUE XRB_Tag_append_tag(VALUE self, VALUE buffer, VALUE name, VALUE attributes, VALUE content);
16
+ // Same as append but returns the result. Slightly less efficient.
17
+ VALUE XRB_Tag_format_tag(VALUE self, VALUE name, VALUE attributes, VALUE content);
18
+
19
+ // Improve performance of XRB::Tag#write_opening_tag and #write_closing_tag
20
+ VALUE XRB_Tag_write_opening_tag(VALUE self, VALUE buffer);
21
+ VALUE XRB_Tag_write_closing_tag(VALUE self, VALUE buffer);