From 3d53b0d6b8d9aa8e52c96c3ed6ac9f7dc1d660f4 Mon Sep 17 00:00:00 2001 From: Yorhel Date: Wed, 2 Sep 2020 13:46:45 +0200 Subject: BBCode: Consolidate bb2html & bb2text + only convert ids in thread titles Fixing bb2html to only convert ids would complicate options a lot, adding a new formatting function to only convert ids would make sense, but then all formatting functions kind of look alike, so I figured a single bb_format() to support all use cases may be a better approach. Trigger for this was that people do (understandably) put [spoiler] in thread titles, and that should not be interpreted as the spoiler tag. --- util/bbcode-test.pl | 56 ++++++++++++++++++++++++++++++----------------------- 1 file changed, 32 insertions(+), 24 deletions(-) (limited to 'util') diff --git a/util/bbcode-test.pl b/util/bbcode-test.pl index 230277b5..e306c952 100755 --- a/util/bbcode-test.pl +++ b/util/bbcode-test.pl @@ -12,7 +12,7 @@ use Benchmark 'timethese'; our($ROOT, %S); BEGIN { ($ROOT = abs_path $0) =~ s{/util/bbcode-test\.pl$}{}; } use lib "$ROOT/lib"; -use VNDB::BBCode qw/bb2html bb2text/; +use VNDB::BBCode; my @tests = ( @@ -30,11 +30,11 @@ my @tests = ( '[quote]some quote[/quote]', '
some quote
', - 'some quote', + '"some quote"', "[code]some code\n\nalso newlines;[/code]", '
some code

also newlines;
', - "some code\n\nalso newlines;", + "`some code\n\nalso newlines;`", '[spoiler]some spoiler[/spoiler]', 'some spoiler', @@ -42,7 +42,7 @@ my @tests = ( '[b][i][u][s]Formatting![/s][/u][/i][/b]', 'Formatting!', - 'Formatting!', + '*/_-Formatting!-_/*', "[raw][quote]not parsed\n[url=https://vndb.org/]valid url[/url]\n[url=asdf]invalid url[/url][/quote][/raw]", "[quote]not parsed
[url=https://vndb.org/]valid url[/url]
[url=asdf]invalid url[/url][/quote]", @@ -50,11 +50,11 @@ my @tests = ( '[quote]basic [spoiler]single[/spoiler]-line [spoiler][url=/g]tag[/url] nesting [raw](without [url=/v3333]special[/url] cases)[/raw][/spoiler][/quote]', '
basic single-line tag nesting (without [url=/v3333]special[/url] cases)
', - 'basic -line ', + '"basic -line "', '[quote][b]more [spoiler]nesting [code]mkay?', '
more nesting [code]mkay?
', - 'more ', + '"*more *"', '[url=/v][b]does not work here[/b][/url]', '[b]does not work here[/b]', @@ -62,11 +62,11 @@ my @tests = ( '[s] v5 [url=/p1]x[/url] [/s]', ' v5 x ', - ' v5 x ', + '- v5 x -', "[quote]rmnewline after closing tag[/quote]\n", '
rmnewline after closing tag
', - "rmnewline after closing tag\n", + "\"rmnewline after closing tag\"", '[url=/v19]some vndb url[/url]', 'some vndb url', @@ -74,20 +74,20 @@ my @tests = ( "quite\n\n\n\n\n\n\na\n\n\n\n\n lot of\n\n\n\nunneeded whitespace", 'quite

a

lot of



unneeded whitespace', - "quite\n\n\n\n\n\n\na\n\n\n\n\n lot of\n\n\n\nunneeded whitespace", + "quite\n\na\n\n lot of\n\n\n\nunneeded whitespace", "[quote]\nsimple\nrmnewline\ntest\n[/quote]", '
simple
rmnewline
test
', - "\nsimple\nrmnewline\ntest\n", + "\"simple\nrmnewline\ntest\n\"", # the new implementation doesn't special-case [code], as the first newline shouldn't matter either way "[quote]\n\nhello, rmnewline test[code]\n#!/bin/sh\n\nfunction random_username() {\n /dev/null\n}\n[/code]\nsome text after the code tag\n[/quote]\n\n[spoiler]\nsome newlined spoiler\n[/spoiler]", '

hello, rmnewline test
#!/bin/sh

function random_username() {
</dev/urandom tr -cd \'a-zA-Z0-9\' | dd bs=1 count=16 2>/dev/null
}
some text after the code tag


some newlined spoiler
', - "\n\nhello, rmnewline test\n#!/bin/sh\n\nfunction random_username() {\n /dev/null\n}\n\nsome text after the code tag\n\n\n", + "\"\nhello, rmnewline test`#!/bin/sh\n\nfunction random_username() {\n /dev/null\n}\n`some text after the code tag\n\"\n", "[quote]\n[raw]\nrmnewline test with made-up elements\n[/raw]\nwelp\n[dumbtag]\nnone\n[/dumbtag]\n[/quote]", '

rmnewline test with made-up elements

welp
[dumbtag]
none
[/dumbtag]
', - "\n\nrmnewline test with made-up elements\n\nwelp\n[dumbtag]\nnone\n[/dumbtag]\n", + "\"\nrmnewline test with made-up elements\n\nwelp\n[dumbtag]\nnone\n[/dumbtag]\n\"", '[url=http://example.com/]markup in [raw][url][/raw][/url]', 'markup in [url]', @@ -111,7 +111,7 @@ my @tests = ( '[Quote]non-lowercase tags [SpOILER]here[/sPOilER][/qUOTe]', '
non-lowercase tags here
', - 'non-lowercase tags ', + '"non-lowercase tags "', 'some text [spoiler]with (v17) tags[/spoiler] and internal ids such as s1', 'some text with (v17) tags and internal ids such as s1', @@ -152,11 +152,11 @@ my @tests = ( # TODO: This isn't ideal '[quote][spoiler]stray open tag (nested)[/quote]', '
stray open tag (nested)[/quote]
', - '', + '""', '[quote][spoiler]two stray open tags', '
two stray open tags
', - '', + '""', "[url=https://cat.xyz/]that's [spoiler]some [quote]uncommon[/quote][/spoiler] combination[/url]", 'that\'s [spoiler]some [quote]uncommon[/quote][/spoiler] combination', @@ -170,13 +170,21 @@ my @tests = ( #'link (literal ipv6 address)', # test shortening - [ "[url=https://cat.xyz/]that's [spoiler]some [quote]uncommon[/quote][/spoiler] combination[/url]", 10 ], + [ "[url=https://cat.xyz/]that's [spoiler]some [quote]uncommon[/quote][/spoiler] combination[/url]", maxlength => 10 ], 'that\'s ', - "that's [spoiler]some [quote]uncommon[/quote][/spoiler] combination", + "that's ", - [ "A https://blicky.net/ only takes 4 characters", 8 ], + [ "A https://blicky.net/ only takes 4 characters", maxlength => 8 ], 'A link', - "A https://blicky.net/ only takes 4 characters", + "A https", # Yeah, uh... word boundary + + [ 'vndbids only [url=/v9]nothing[/url] [b] [spoiler]p5', idonly => 1 ], + 'vndbids only [url=/v9]nothing[/url] [b] [spoiler]p5', + 'vndbids only [url=/v9]nothing[/url] [b] [spoiler]p5', + + [ 'this [spoiler]spoiler will be[/spoiler] kept', keepspoil => 1 ], + 'this spoiler will be kept', + 'this spoiler will be kept', ); @@ -212,8 +220,8 @@ sub test { my @arg = ref $input ? @$input : ($input); (my $msg = $arg[0]) =~ s/\n/\\n/g; is identity($arg[0]), $arg[0], "id: $msg"; - is bb2html(@arg), $html, "html: $msg"; - is bb2text($arg[0]), $plain, "plain: $msg"; + is bb_format(@arg), $html, "html: $msg"; + is bb_format(@arg, text => 1), $plain, "plain: $msg"; } } @@ -224,9 +232,9 @@ sub bench { my $short = "Nobody ev3r v10 uses v5 so s1 many [url=https://blicky.net/]x[raw]y[/raw][/url] tags. "; my $heavy = $short x100; timethese(0, { - short => sub { bb2html($short) }, - plain => sub { bb2html($plain) }, - heavy => sub { bb2html($heavy) }, + short => sub { bb_format($short) }, + plain => sub { bb_format($plain) }, + heavy => sub { bb_format($heavy) }, }); # old: # heavy: 3 wallclock secs ( 3.15 usr + 0.00 sys = 3.15 CPU) @ 357.46/s (n=1126) -- cgit v1.2.3