dtext_rb 1.0.11 → 1.0.12
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/VERSION +1 -1
- data/dtext_rb.gemspec +3 -3
- data/ext/dtext/dtext.c +211 -179
- data/ext/dtext/dtext.rl +35 -17
- data/test/dtext_test.rb +3 -1
- metadata +2 -2
data/ext/dtext/dtext.rl
CHANGED
@@ -26,6 +26,7 @@ typedef struct StateMachine {
|
|
26
26
|
const char * b2;
|
27
27
|
bool f_inline;
|
28
28
|
bool f_strip;
|
29
|
+
bool f_mentions;
|
29
30
|
bool list_mode;
|
30
31
|
bool header_mode;
|
31
32
|
GString * output;
|
@@ -321,7 +322,7 @@ inline := |*
|
|
321
322
|
append(sm, true, "<a href=\"");
|
322
323
|
append_segment_html_escaped(sm, sm->b1, sm->b2 - sm->d);
|
323
324
|
append(sm, true, "\">");
|
324
|
-
link_content_sm = parse_helper(sm->a1, sm->a2 - sm->a1, false, true);
|
325
|
+
link_content_sm = parse_helper(sm->a1, sm->a2 - sm->a1, false, true, false);
|
325
326
|
append(sm, true, link_content_sm->output->str);
|
326
327
|
free_machine(link_content_sm);
|
327
328
|
link_content_sm = NULL;
|
@@ -366,23 +367,30 @@ inline := |*
|
|
366
367
|
};
|
367
368
|
|
368
369
|
mention => {
|
369
|
-
if (
|
370
|
-
|
371
|
-
sm
|
370
|
+
if (!sm->f_mentions || (sm->a1 > sm->pb && sm->a1 - 1 > sm->pb && sm->a1[-2] != ' ' && sm->a1[-2] != '\r' && sm->a1[-2] != '\n')) {
|
371
|
+
// handle emails
|
372
|
+
append_c(sm, '@');
|
373
|
+
append_segment_html_escaped(sm, sm->a1, sm->a2 - 1);
|
374
|
+
|
372
375
|
} else {
|
373
|
-
|
374
|
-
|
375
|
-
|
376
|
+
if (is_boundary_c(fc)) {
|
377
|
+
sm->b = true;
|
378
|
+
sm->d = 2;
|
379
|
+
} else {
|
380
|
+
sm->b = false;
|
381
|
+
sm->d = 1;
|
382
|
+
}
|
376
383
|
|
377
|
-
|
378
|
-
|
379
|
-
|
380
|
-
|
381
|
-
|
382
|
-
|
384
|
+
append(sm, true, "<a rel=\"nofollow\" href=\"/users?name=");
|
385
|
+
append_segment_uri_escaped(sm, sm->a1, sm->a2 - sm->d);
|
386
|
+
append(sm, true, "\">");
|
387
|
+
append_c(sm, '@');
|
388
|
+
append_segment_html_escaped(sm, sm->a1, sm->a2 - sm->d);
|
389
|
+
append(sm, true, "</a>");
|
383
390
|
|
384
|
-
|
385
|
-
|
391
|
+
if (sm->b) {
|
392
|
+
append_c_html_escaped(sm, fc);
|
393
|
+
}
|
386
394
|
}
|
387
395
|
};
|
388
396
|
|
@@ -807,6 +815,7 @@ main := |*
|
|
807
815
|
header_with_id => {
|
808
816
|
char header = *sm->a1;
|
809
817
|
GString * id_name = g_string_new_len(sm->b1, sm->b2 - sm->b1);
|
818
|
+
id_name = g_string_prepend(id_name, "dtext-");
|
810
819
|
|
811
820
|
if (sm->f_inline) {
|
812
821
|
header = '6';
|
@@ -1338,6 +1347,7 @@ static void init_machine(StateMachine * sm, const char * src, size_t len) {
|
|
1338
1347
|
sm->b2 = NULL;
|
1339
1348
|
sm->f_inline = false;
|
1340
1349
|
sm->f_strip = false;
|
1350
|
+
sm->f_mentions = true;
|
1341
1351
|
sm->stack = g_array_sized_new(FALSE, TRUE, sizeof(int), 16);
|
1342
1352
|
sm->dstack = g_queue_new();
|
1343
1353
|
sm->list_nest = 0;
|
@@ -1355,7 +1365,7 @@ static void free_machine(StateMachine * sm) {
|
|
1355
1365
|
g_free(sm);
|
1356
1366
|
}
|
1357
1367
|
|
1358
|
-
static StateMachine * parse_helper(const char * src, size_t len, bool f_strip, bool f_inline) {
|
1368
|
+
static StateMachine * parse_helper(const char * src, size_t len, bool f_strip, bool f_inline, bool f_mentions) {
|
1359
1369
|
StateMachine * sm = NULL;
|
1360
1370
|
StateMachine * link_content_sm = NULL;
|
1361
1371
|
|
@@ -1363,6 +1373,7 @@ static StateMachine * parse_helper(const char * src, size_t len, bool f_strip, b
|
|
1363
1373
|
init_machine(sm, src, len);
|
1364
1374
|
sm->f_strip = f_strip;
|
1365
1375
|
sm->f_inline = f_inline;
|
1376
|
+
sm->f_mentions = f_mentions;
|
1366
1377
|
|
1367
1378
|
%% write init;
|
1368
1379
|
%% write exec;
|
@@ -1378,11 +1389,13 @@ static VALUE parse(int argc, VALUE * argv, VALUE self) {
|
|
1378
1389
|
VALUE options;
|
1379
1390
|
VALUE opt_inline;
|
1380
1391
|
VALUE opt_strip;
|
1392
|
+
VALUE opt_mentions;
|
1381
1393
|
VALUE ret;
|
1382
1394
|
rb_encoding * encoding = NULL;
|
1383
1395
|
StateMachine * sm = NULL;
|
1384
1396
|
bool f_strip = false;
|
1385
1397
|
bool f_inline = false;
|
1398
|
+
bool f_mentions = true;
|
1386
1399
|
|
1387
1400
|
g_debug("start\n");
|
1388
1401
|
|
@@ -1412,10 +1425,15 @@ static VALUE parse(int argc, VALUE * argv, VALUE self) {
|
|
1412
1425
|
if (RTEST(opt_inline)) {
|
1413
1426
|
f_inline = true;
|
1414
1427
|
}
|
1428
|
+
|
1429
|
+
opt_mentions = rb_hash_aref(options, ID2SYM(rb_intern("disable_mentions")));
|
1430
|
+
if (RTEST(opt_mentions)) {
|
1431
|
+
f_mentions = false;
|
1432
|
+
}
|
1415
1433
|
}
|
1416
1434
|
}
|
1417
1435
|
|
1418
|
-
sm = parse_helper(RSTRING_PTR(input0), RSTRING_LEN(input0), f_strip, f_inline);
|
1436
|
+
sm = parse_helper(RSTRING_PTR(input0), RSTRING_LEN(input0), f_strip, f_inline, f_mentions);
|
1419
1437
|
|
1420
1438
|
encoding = rb_enc_find("utf-8");
|
1421
1439
|
ret = rb_enc_str_new(sm->output->str, sm->output->len, encoding);
|
data/test/dtext_test.rb
CHANGED
@@ -10,7 +10,9 @@ class DTextTest < Minitest::Test
|
|
10
10
|
assert_parse('<p><a rel="nofollow" href="/users?name=bob">@bob</a></p>', "@bob")
|
11
11
|
assert_parse('<p>hi <a rel="nofollow" href="/users?name=bob">@bob</a></p>', "hi @bob")
|
12
12
|
assert_parse('<p>this is not @.@ @_@ <a rel="nofollow" href="/users?name=bob">@bob</a></p>', "this is not @.@ @_@ @bob")
|
13
|
+
assert_parse('<p>this is an email@address.com and should not trigger</p>', "this is an email@address.com and should not trigger")
|
13
14
|
assert_parse('<p>multiple <a rel="nofollow" href="/users?name=bob">@bob</a> <a rel="nofollow" href="/users?name=anna">@anna</a></p>', "multiple @bob @anna")
|
15
|
+
assert_equal('<p>hi @bob</p>', DTextRagel.parse("hi @bob", :disable_mentions => true))
|
14
16
|
end
|
15
17
|
|
16
18
|
def test_sanitize_heart
|
@@ -83,7 +85,7 @@ class DTextTest < Minitest::Test
|
|
83
85
|
end
|
84
86
|
|
85
87
|
def test_headers_with_ids
|
86
|
-
assert_parse("<h1 id=\"blah-blah\">header</h1>", "h1#blah-blah. header")
|
88
|
+
assert_parse("<h1 id=\"dtext-blah-blah\">header</h1>", "h1#blah-blah. header")
|
87
89
|
end
|
88
90
|
|
89
91
|
def test_headers_with_ids_with_quote
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: dtext_rb
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.0.
|
4
|
+
version: 1.0.12
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- r888888888
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2016-09-
|
11
|
+
date: 2016-09-08 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: minitest
|