comment this up the arseholes
[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 sanitize {
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 compilation of the eval
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 eval 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   *{eval '\*CORE::GLOBAL::glob'} = $_[0];
50 }
51
52 sub _export_tags_into {
53   my ($class, $into, @tags) = @_;
54   foreach my $tag (@tags) {
55     no strict 'refs';
56     tie *{"${into}::${tag}"}, 'XML::Tags::TIEHANDLE', \"<${tag}>";
57   }
58   _set_glob(sub { \('<'.$_[0].'>'); });
59   return sub {
60     foreach my $tag (@tags) {
61       no strict 'refs';
62       delete ${"${into}::"}{$tag}
63     }
64     _set_glob(\&File::Glob::glob);
65     $IN_SCOPE = 0;
66   };
67 }
68
69 sub _install_unexporter {
70   my ($class, $unex) = @_;
71   $^H |= 0x120000; # localize %^H
72   $^H{'XML::Tags::Unex'} = bless($unex, 'XML::Tags::Unex');
73 }
74
75 package XML::Tags::TIEHANDLE;
76
77 sub TIEHANDLE { my $str = $_[1]; bless \$str, $_[0] }
78 sub READLINE { ${$_[0]} }
79
80 package XML::Tags::Unex;
81
82 sub DESTROY { local $@; eval { $_[0]->(); 1 } || warn "ARGH: $@" }
83
84 1;