rename sanitize to to_xml_string, add to_html_string
[catagits/Web-Simple.git] / lib / XML / Tags.pm
1 package XML::Tags;
2
3 use strict;
4 use warnings FATAL => 'all';
5
6 use File::Glob ();
7
8 my $IN_SCOPE = 0;
9
10 sub 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
22 sub to_xml_string {
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
32 sub _find_tags { shift; @_ }
33
34 sub _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
41 sub _set_glob {
42   # stupid insanity. delete anything already there so we disassociated
43   # the *CORE::GLOBAL::glob typeglob. Then the string reference call
44   # revivifies it - i.e. creates us a new glob, which we get a reference
45   # to, which we can then assign to.
46   # doing it without the quotes doesn't - it binds to the version in scope
47   # at compile time, which means after a delete you get a nice warm segv.
48   delete ${CORE::GLOBAL::}{glob};
49   no strict 'refs';
50   *{'CORE::GLOBAL::glob'} = $_[0];
51 }
52
53 sub _export_tags_into {
54   my ($class, $into, @tags) = @_;
55   foreach my $tag (@tags) {
56     no strict 'refs';
57     tie *{"${into}::${tag}"}, 'XML::Tags::TIEHANDLE', \"<${tag}>";
58   }
59   _set_glob(sub { \('<'.$_[0].'>'); });
60   return sub {
61     foreach my $tag (@tags) {
62       no strict 'refs';
63       delete ${"${into}::"}{$tag}
64     }
65     _set_glob(\&File::Glob::glob);
66     $IN_SCOPE = 0;
67   };
68 }
69
70 sub _install_unexporter {
71   my ($class, $unex) = @_;
72   $^H |= 0x120000; # localize %^H
73   $^H{'XML::Tags::Unex'} = bless($unex, 'XML::Tags::Unex');
74 }
75
76 package XML::Tags::TIEHANDLE;
77
78 sub TIEHANDLE { my $str = $_[1]; bless \$str, $_[0] }
79 sub READLINE { ${$_[0]} }
80
81 package XML::Tags::Unex;
82
83 sub DESTROY { local $@; eval { $_[0]->(); 1 } || warn "ARGH: $@" }
84
85 1;