diff options
Diffstat (limited to 'lib/VNDB/Func.pm')
-rw-r--r-- | lib/VNDB/Func.pm | 24 |
1 files changed, 24 insertions, 0 deletions
diff --git a/lib/VNDB/Func.pm b/lib/VNDB/Func.pm index 54acfe4f..3b315c93 100644 --- a/lib/VNDB/Func.pm +++ b/lib/VNDB/Func.pm @@ -6,10 +6,12 @@ use warnings; use TUWF ':html'; use Exporter 'import'; use POSIX 'strftime', 'ceil', 'floor'; +use JSON::XS; use VNDBUtil; our @EXPORT = (@VNDBUtil::EXPORT, qw| clearfloat cssicon tagscore mt minage fil_parse fil_serialize parenttags childtags charspoil imgpath imgurl fmtvote + jsonEncode jsonDecode script_json mtvoiced mtani mtvnlen mtrlstat mtvnlstat mtbloodt |); @@ -202,6 +204,28 @@ sub fmtvote { } +my $JSON; # cache + +# JSON::XS::encode_json converts input to utf8, whereas these functions +# operate on wide character strings. +sub jsonEncode ($) { + ($JSON ||= JSON::XS->new)->encode(@_); +} + +sub jsonDecode ($) { + ($JSON ||= JSON::XS->new)->decode(@_); +} + +# Insert JSON-encoded data as script, arguments: id, object +sub script_json { + script id => $_[0], type => 'application/json'; + my $js = jsonEncode $_[1]; + $js =~ s/</\\u003C/g; # escape HTML tags like </script> and <!-- + lit $js; + end; +} + + # mt() wrappers for data-dependent translation strings that have a special # value for 'unknown'. sub mtvoiced { !$_[0] ? mt '_unknown' : mt '_voiced_'.$_[0]; } |