rename sanitize to to_xml_string, add to_html_string
[catagits/Web-Simple.git] / lib / XML / Tags.pm
CommitLineData
afe60e53 1package XML::Tags;
2
3use strict;
4use warnings FATAL => 'all';
5
cb5717ef 6use File::Glob ();
7
afe60e53 8my $IN_SCOPE = 0;
9
10sub import {
11 die "Can't import XML::Tags into a scope when already compiling one that uses it"
12 if $IN_SCOPE;
13 my ($class, @args) = @_;
14 my $opts = shift(@args) if ref($args[0]) eq 'HASH';
15 my $target = $class->_find_target(0, $opts);
16 my @tags = $class->_find_tags(@args);
17 my $unex = $class->_export_tags_into($target => @tags);
18 $class->_install_unexporter($unex);
19 $IN_SCOPE = 1;
20}
21
49a6c0b5 22sub to_xml_string {
cc050137 23 map { # string == text -> HTML, scalarref == raw HTML, other == passthrough
24 ref($_)
25 ? (ref $_ eq 'SCALAR' ? $$_ : $_)
26 : do { local $_ = $_; # copy
27 s/&/&amp;/g; s/"/&quot/g; s/</&lt;/g; s/>/&gt;/g; $_;
28 }
29 } @_
30}
31
afe60e53 32sub _find_tags { shift; @_ }
33
34sub _find_target {
35 my ($class, $extra_levels, $opts) = @_;
36 return $opts->{into} if defined($opts->{into});
37 my $level = ($opts->{into_level} || 1) + $extra_levels;
38 return (caller($level))[0];
39}
40
06e0b420 41sub _set_glob {
62346684 42 # stupid insanity. delete anything already there so we disassociated
49a6c0b5 43 # the *CORE::GLOBAL::glob typeglob. Then the string reference call
62346684 44 # revivifies it - i.e. creates us a new glob, which we get a reference
45 # to, which we can then assign to.
49a6c0b5 46 # doing it without the quotes doesn't - it binds to the version in scope
62346684 47 # at compile time, which means after a delete you get a nice warm segv.
43a70ddb 48 delete ${CORE::GLOBAL::}{glob};
49a6c0b5 49 no strict 'refs';
50 *{'CORE::GLOBAL::glob'} = $_[0];
afe60e53 51}
52
53sub _export_tags_into {
54 my ($class, $into, @tags) = @_;
55 foreach my $tag (@tags) {
56 no strict 'refs';
5f44889f 57 tie *{"${into}::${tag}"}, 'XML::Tags::TIEHANDLE', \"<${tag}>";
afe60e53 58 }
06e0b420 59 _set_glob(sub { \('<'.$_[0].'>'); });
afe60e53 60 return sub {
61 foreach my $tag (@tags) {
62 no strict 'refs';
63 delete ${"${into}::"}{$tag}
64 }
06e0b420 65 _set_glob(\&File::Glob::glob);
afe60e53 66 $IN_SCOPE = 0;
67 };
68}
69
70sub _install_unexporter {
71 my ($class, $unex) = @_;
72 $^H |= 0x120000; # localize %^H
73 $^H{'XML::Tags::Unex'} = bless($unex, 'XML::Tags::Unex');
74}
75
76package XML::Tags::TIEHANDLE;
77
78sub TIEHANDLE { my $str = $_[1]; bless \$str, $_[0] }
79sub READLINE { ${$_[0]} }
80
81package XML::Tags::Unex;
82
83sub DESTROY { local $@; eval { $_[0]->(); 1 } || warn "ARGH: $@" }
84
851;