in reply to Customize Mojo's html_entities.txt

In the source (and the ref you provided mentions this) of Mojo::Util html_entities.txt is hardcoded. From this file's contents %Mojo::Util::ENTITIES is constructed. So, it looks to me that without changing the filename in Mojo::Util you will not achieve what you want.

However, this being Perl and this being Perlmonks, here is a workaround which should be applied to all script parts which use Mojo::Util;. This may not be surely is not practical.

Mojo::Util provides the wonderfully named monkey_patch() which you can use to alter the only sub therein which uses %ENTITIES, which is Mojo::Util::_entity. And make it use your own (EDIT: e.g. _entity2 which is identical to the original but it uses your locally defined %ENTITIES which you create as you see fit):

use Mojo::Util; my $str = '&#x3c;foo&#x3E;bar&lt;baz&gt;&#x0026;&#34;'; my $res1 = Mojo::Util::html_unescape($str); my %ENTITIES = (lt => '<<<', gt => '>>>'); Mojo::Util::monkey_patch 'Mojo::Util', _entity => \&_entity2; my $res2 = Mojo::Util::html_unescape($str); print "res1: $res1\n"; print "res2: $res2\n"; sub _entity2 { my ($point, $name, $attr) = @_; print "_entity2 called...\n"; # Code point return chr($point !~ /^x/ ? $point : hex $point) unless defined $nam +e; # Named character reference my $rest = my $last = ''; while (length $name) { return $ENTITIES{$name} . reverse $rest if exists $ENTITIES{$name} && (!$attr || $name =~ /;$/ || $last +!~ /[A-Za-z0-9=]/); $rest .= $last = chop $name; } return '&' . reverse $rest; }

bw, bliako

Replies are listed 'Best First'.
Re^2: Customize Mojo's html_entities.txt
by Anonymous Monk on Aug 26, 2021 at 18:00 UTC

    Nice!!!