| 1 | #!/usr/bin/env perl | ||
| 2 | # XML::Axk::L::L1 - axk language, version 1 | ||
| 3 | # Copyright (c) 2018 cxw42. All rights reserved. Artistic 2. | ||
| 4 | |||
| 5 | # TODO: | ||
| 6 | # - Add a way to mark that a node should be kept in the DOM, even if running | ||
| 7 | # in SAX mode (`stash`?) | ||
| 8 | # - Add a way to re-use the last pattern (`ditto`?) | ||
| 9 | # - Add a static method to XAC to get the next package name, so that each | ||
| 10 | # `axk_script_*` is used by only one Core instance. | ||
| 11 | |||
| 12 | package XML::Axk::L::L1; | ||
| 13 | 1 | ▶ | use XML::Axk::Base qw(:default now_names); |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 14 | |||
| 15 | 1 | ▶ | use XML::Axk::Matcher::XPath; |
|
Subroutine
BEGIN: called CC=1 SCAR=2.6
| |||
| 16 | use XML::Axk::Matcher::Always; | ||
| 17 | use HTML::Selector::XPath qw(selector_to_xpath); | ||
| 18 | |||
| 19 | use Scalar::Util qw(reftype); | ||
| 20 | |||
| 21 | # Packages we invoke by hand | ||
| 22 | require XML::Axk::Language; | ||
| 23 | require Exporter; | ||
| 24 | |||
| 25 | # Names the axk script will have access to | ||
| 26 | our @EXPORT = qw( | ||
| 27 | pre_all pre_file post_file post_all perform | ||
| 28 | always never xpath sel on run entering leaving whenever); | ||
| 29 | our @EXPORT_OK = qw( @SP_names ); | ||
| 30 | |||
| 31 | # Config | ||
| 32 | our $C_WANT_TEXT = 0; | ||
| 33 | |||
| 34 | # Helpers ======================================================== {{{1 | ||
| 35 | |||
| 36 | # Accessor | ||
| 37 | sub _sandbox { | ||
| 38 | my $home = caller(1); | ||
| 39 | no strict 'refs'; | ||
| 40 | return ${"${home}::_AxkSandbox"}; | ||
| 41 | } #_sandbox() | ||
| 42 | |||
| 43 | # }}}1 | ||
| 44 | # Definers for special actions==================================== {{{1 | ||
| 45 | |||
| 46 | sub pre_all :prototype(&) { | ||
| 47 | my $sandbox = _sandbox or croak("Can't find sandbox in pre_all"); | ||
| 48 | #say "core: " . Dumper($sandbox); | ||
| 49 | #say Dumper($sandbox->{pre_all}); | ||
| 50 | #say Dumper(@{$sandbox->{pre_all}}); | ||
| 51 | push @{$sandbox->pre_all}, shift; | ||
| 52 | } #pre_all() | ||
| 53 | |||
| 54 | sub pre_file :prototype(&) { | ||
| 55 | my $sandbox = _sandbox or croak("Can't find sandbox in pre_file"); | ||
| 56 | push @{$sandbox->pre_file}, shift; | ||
| 57 | } #pre_file() | ||
| 58 | |||
| 59 | sub post_file :prototype(&) { | ||
| 60 | my $sandbox = _sandbox or croak("Can't find sandbox in post_file"); | ||
| 61 | push @{$sandbox->post_file}, shift; | ||
| 62 | } #post_file() | ||
| 63 | |||
| 64 | sub post_all :prototype(&) { | ||
| 65 | my $sandbox = _sandbox or croak("Can't find sandbox in post_all"); | ||
| 66 | push @{$sandbox->post_all}, shift; | ||
| 67 | } #post_all() | ||
| 68 | |||
| 69 | # }}}1 | ||
| 70 | # Definers for node actions ====================================== {{{1 | ||
| 71 | |||
| 72 | ## @function private add_to_worklist (&action, matcher[, when]) | ||
| 73 | ## The main way to define pattern/action pairs. This takes the action first | ||
| 74 | ## since that's how Perl's prototypes are set up the cleanest (block first). | ||
| 75 | ## @params required &action A block to execute when the pattern matches | ||
| 76 | ## @params required matcher An object that defines test(%CPs) to determine | ||
| 77 | ## whether the element described in | ||
| 78 | ## core-parameter hash %CPs matches. | ||
| 79 | ## @params optional when If provided, when to run the action: | ||
| 80 | ## HI, BYE, or CIAO. Default is BYE. | ||
| 81 | sub add_to_worklist { | ||
| 82 | my $sandbox = _sandbox or croak "Can't find sandbox"; | ||
| 83 | my ($drAction, $refPattern, $when) = @_; | ||
| 84 | |||
| 85 | $when = $when // BYE; # only on exit, by default | ||
| 86 | $refPattern = \( my $temp = $refPattern ) unless ref($refPattern); | ||
| 87 | |||
| 88 | push @{$sandbox->worklist}, [$refPattern, $drAction, $when]; | ||
| 89 | } #add_to_worklist() | ||
| 90 | |||
| 91 | # User-facing alias for add_to_worklist | ||
| 92 | sub perform :prototype(&@) { | ||
| 93 | goto &add_to_worklist; # Need goto so that _sandbox() can use caller(1) | ||
| 94 | } | ||
| 95 | |||
| 96 | # run { action } [optional <when>] - syntactic sugar for `sub {}, when` | ||
| 97 | sub run :prototype(&;$) { | ||
| 98 | return @_; | ||
| 99 | } #run() | ||
| 100 | |||
| 101 | # pattern-first style - on {} run {} [when]; | ||
| 102 | # The default [when] is BYE so that the text content of the node is | ||
| 103 | # available --- at HI, all you have are the attributes. | ||
| 104 | sub on :prototype(&@) { | ||
| 105 | my ($drMakeMatcher, $drAction, $when) = @_; | ||
| 106 | |||
| 107 | my $matcher = &$drMakeMatcher; | ||
| 108 | @_=($drAction, $matcher, $when); | ||
| 109 | goto &add_to_worklist; | ||
| 110 | } # on() | ||
| 111 | |||
| 112 | # pattern-first style, sugar for symmetry with whenever() and entering() | ||
| 113 | sub leaving :prototype(&@) { | ||
| 114 | goto &on | ||
| 115 | } #entering() | ||
| 116 | |||
| 117 | # pattern-first style, common implementation for HI and CIAO | ||
| 118 | sub _entering_whenever_impl { | ||
| 119 | croak "Too many arguments" if $#_>2; | ||
| 120 | my ($when, $drMakeMatcher, $drAction) = @_; | ||
| 121 | my $matcher = &$drMakeMatcher; | ||
| 122 | @_=($drAction, $matcher, $when); | ||
| 123 | goto &add_to_worklist; | ||
| 124 | } #_leaving_whenever_impl() | ||
| 125 | |||
| 126 | # pattern-first style, specific to entering nodes (HI) - entering {} run {}; | ||
| 127 | sub entering :prototype(&@) { | ||
| 128 | unshift @_, HI; | ||
| 129 | goto &_entering_whenever_impl; | ||
| 130 | } # leaving() | ||
| 131 | |||
| 132 | # pattern-first style, specific to hitting nodes (CIAO) - whenever {} run {}; | ||
| 133 | sub whenever :prototype(&@) { | ||
| 134 | unshift @_, CIAO; | ||
| 135 | goto &_entering_whenever_impl; | ||
| 136 | } # whenever() | ||
| 137 | |||
| 138 | # }}}1 | ||
| 139 | # Definers for matchers ========================================== {{{1 | ||
| 140 | |||
| 141 | # TODO define subs to tag various things as, e.g., selectors, xpath, | ||
| 142 | # attributes, namespaces, ... . This is essentially a DSL for all the ways | ||
| 143 | # you can write a pattern | ||
| 144 | |||
| 145 | # Always match | ||
| 146 | sub always :prototype() { | ||
| 147 | return XML::Axk::Matcher::Always->new(); | ||
| 148 | } #always() | ||
| 149 | |||
| 150 | # Never match - for easily turning off a particular clause | ||
| 151 | sub never :prototype() { | ||
| 152 | return XML::Axk::Matcher::Always->new(always => false); | ||
| 153 | } #never() | ||
| 154 | |||
| 155 | # Make an XPath matcher | ||
| 156 | sub xpath { | ||
| 157 | my $path = shift or croak("No expression provided!"); | ||
| 158 | $path = $$path if ref $path; | ||
| 159 | |||
| 160 | my (undef, $filename, $line) = caller; | ||
| 161 | my $matcher = XML::Axk::Matcher::XPath->new( | ||
| 162 | xpath => $path, | ||
| 163 | file=>$filename, line=>$line, | ||
| 164 | ); | ||
| 165 | return $matcher; | ||
| 166 | } #xpath() | ||
| 167 | |||
| 168 | # Make a selector matcher | ||
| 169 | sub sel { | ||
| 170 | my $path = shift or croak("No expression provided!"); | ||
| 171 | $path = $$path if ref $path; | ||
| 172 | |||
| 173 | my $xp = selector_to_xpath $path; | ||
| 174 | my (undef, $filename, $line) = caller; | ||
| 175 | my $matcher = XML::Axk::Matcher::XPath->new( | ||
| 176 | xpath => $xp, type => 'selector', | ||
| 177 | file=>$filename, line=>$line, | ||
| 178 | ); | ||
| 179 | return $matcher; | ||
| 180 | } #sel() | ||
| 181 | |||
| 182 | # }}}1 | ||
| 183 | |||
| 184 | # Script parameters ============================================== {{{1 | ||
| 185 | |||
| 186 | # Script-parameter names | ||
| 187 | our @SP_names = qw($D $E $NOW); | ||
| 188 | |||
| 189 | sub update { | ||
| 190 | #say "L1::update: ", Dumper(\@_); | ||
| 191 | my $hrSP = shift or croak("Invalid call - No hrSP"); | ||
| 192 | my %CPs = @_; | ||
| 193 | |||
| 194 | $hrSP->{'$D'} = $CPs{document} or croak("No document"); | ||
| 195 | $hrSP->{'$E'} = $CPs{record} or croak("No record"); | ||
| 196 | croak("You are in a timeless maze") unless defined $CPs{NOW}; | ||
| 197 | $hrSP->{'$NOW'} = now_names $CPs{NOW}; | ||
| 198 | } #update() | ||
| 199 | |||
| 200 | # }}}1 | ||
| 201 | |||
| 202 | # Import ========================================================= {{{1 | ||
| 203 | |||
| 204 | sub import { | ||
| 205 | #say "update: ",ref \&update, Dumper(\&update); | ||
| 206 | #say "XAL1 run from $target:\n", Devel::StackTrace->new->as_string; | ||
| 207 | XML::Axk::Language->import( | ||
| 208 | target => caller, | ||
| 209 | sp => \@SP_names, | ||
| 210 | updater => \&update | ||
| 211 | ); | ||
| 212 | # By doing this here rather than in the `use` statement, | ||
| 213 | # we get `caller` and don't have to walk the stack to find the | ||
| 214 | # axk script. | ||
| 215 | goto &Exporter::import; # for @EXPORT &c. @_ is what it was on entry. | ||
| 216 | } #import() | ||
| 217 | |||
| 218 | #}}}1 | ||
| 219 | 1; | ||
| 220 | # === Documentation ===================================================== {{{1 | ||
| 221 | |||
| 222 | =pod | ||
| 223 | |||
| 224 | =encoding UTF-8 | ||
| 225 | |||
| 226 | =head1 NAME | ||
| 227 | |||
| 228 | XML::Axk::Core::L1 - awk-like XML processor, language 1 | ||
| 229 | |||
| 230 | =head1 EXAMPLE | ||
| 231 | |||
| 232 | L1 | ||
| 233 | on { xpath(q<//item>) } run {say "$NOW: " . $E->getTagName}, CIAO | ||
| 234 | # "CIAO" can also be "HI" or "BYE" (default BYE). | ||
| 235 | # "leaving" is a synonym for "on" with no HI/BYE/CIAO. | ||
| 236 | whenever { xpath(q<//item>) } run {say "$NOW: " . $E->getTagName}; | ||
| 237 | # the same as the "on ... CIAO" line | ||
| 238 | entering { xpath(q<//item>) } run {say "$NOW: " . $E->getTagName}; | ||
| 239 | |||
| 240 | =head1 PATTERNS AND ACTIONS | ||
| 241 | |||
| 242 | =over | ||
| 243 | |||
| 244 | =item * C<< on {<matcher>} run {<action>} [, <when>] >> | ||
| 245 | |||
| 246 | Whenever C<< <matcher> >> says that a node matches, run C<< <action> >>. | ||
| 247 | The optional C<< <when> >> parameter says when in the course of processing to | ||
| 248 | run C<< <action> >>: | ||
| 249 | |||
| 250 | =over | ||
| 251 | |||
| 252 | =item C<HI> | ||
| 253 | |||
| 254 | When the node is first reached, before any of its children are processed | ||
| 255 | |||
| 256 | =item C<BYE> | ||
| 257 | |||
| 258 | After all of the node's children have been processed. This is the default | ||
| 259 | so that you have the text content of the node available for inspection. | ||
| 260 | |||
| 261 | =item C<CIAO> | ||
| 262 | |||
| 263 | Both C<HI> and C<BYE>. Suggestions for alternative terminology are welcome. | ||
| 264 | |||
| 265 | =back | ||
| 266 | |||
| 267 | =item * C<entering>, C<whenever> C<leaving> | ||
| 268 | |||
| 269 | entering {<matcher>} run {<action>} | ||
| 270 | whenever {<matcher>} run {<action>} | ||
| 271 | leaving {<matcher>} run {<action>} | ||
| 272 | |||
| 273 | The same as C<on {} run {}>, with C<when> set to C<HI>, C<CIAO>, or C<BYE> | ||
| 274 | respectively. | ||
| 275 | |||
| 276 | =item * C<< perform { <action> } <matcher> [, <when>] >> | ||
| 277 | |||
| 278 | If you prefer RPN, or you want to save some characters, you can put the | ||
| 279 | C<< <matcher> >> after the C<< <action> >> using C<perform>. For example, | ||
| 280 | the following two lines have exactly the same effect: | ||
| 281 | |||
| 282 | on { xpath(q<//item>) } run {say "$NOW: " . $E->getTagName}, CIAO | ||
| 283 | perform {say "$NOW: " . $E->getTagName} xpath(q<//item>), CIAO | ||
| 284 | |||
| 285 | =back | ||
| 286 | |||
| 287 | =head1 VARIABLES | ||
| 288 | |||
| 289 | When an C<< <action> >> is running, it has access to predefined variables | ||
| 290 | that hold the state of the element being matched. This is similar to C<$0>, | ||
| 291 | C<$1>, ... in awk. | ||
| 292 | |||
| 293 | At present, L1 uses L<XML::DOM>. | ||
| 294 | |||
| 295 | =over | ||
| 296 | |||
| 297 | =item B<$D> | ||
| 298 | |||
| 299 | The current XML document (L<XML::DOM::Document>) | ||
| 300 | |||
| 301 | =item B<$E> | ||
| 302 | |||
| 303 | The XML element that was matched (L<XML::DOM::Element>) | ||
| 304 | |||
| 305 | =item B<$NOW> | ||
| 306 | |||
| 307 | The current phase, as a human-readable string: C<entering> for C<HI>, | ||
| 308 | C<leaving> for C<BYE>, and C<both> for C<CIAO>. | ||
| 309 | |||
| 310 | =back | ||
| 311 | |||
| 312 | =head1 MATCHERS | ||
| 313 | |||
| 314 | =over | ||
| 315 | |||
| 316 | =item * C<< xpath('xpath expression') >> | ||
| 317 | |||
| 318 | Match nodes that match the given XPath expression. Remember that Perl will | ||
| 319 | interpolate C<@name> in double-quotes, so single-quote or C<q{}> your XPath | ||
| 320 | expressions. | ||
| 321 | |||
| 322 | =item * C<< sel('selector') >> | ||
| 323 | |||
| 324 | Match nodes that match the given selector. | ||
| 325 | |||
| 326 | =item * C<always>, C<never> | ||
| 327 | |||
| 328 | Always or never match, respectively. | ||
| 329 | |||
| 330 | =back | ||
| 331 | |||
| 332 | =head1 SPECIAL ACTIONS | ||
| 333 | |||
| 334 | =over | ||
| 335 | |||
| 336 | =item * C<< pre_all {<block>} >> | ||
| 337 | |||
| 338 | Run C<< <block> >> before any file is processed. | ||
| 339 | |||
| 340 | =item * C<< pre_file {<block>} >> | ||
| 341 | |||
| 342 | Run C<< <block>($filename) >> before each file is processed. | ||
| 343 | |||
| 344 | =item * C<< post_file {<block>} >> | ||
| 345 | |||
| 346 | Run C<< <block>($filename) >> after each file is processed. | ||
| 347 | |||
| 348 | =item * C<< post_all {<block>} >> | ||
| 349 | |||
| 350 | Run C<< <block> >> after all files have been processed. | ||
| 351 | |||
| 352 | =back | ||
| 353 | |||
| 354 | =head1 AUTHOR | ||
| 355 | |||
| 356 | Christopher White, C<cxwembedded at gmail.com> | ||
| 357 | |||
| 358 | =head1 CONTACT | ||
| 359 | |||
| 360 | For any bug reports, feature requests, or questions, please see the | ||
| 361 | information in L<XML::Axk>. | ||
| 362 | |||
| 363 | =head1 LICENSE AND COPYRIGHT | ||
| 364 | |||
| 365 | Copyright (c) 2018 Christopher White. All rights reserved. | ||
| 366 | |||
| 367 | This program is free software; you can redistribute it and/or modify it | ||
| 368 | under the terms of the the Artistic License (2.0). Details are in the LICENSE | ||
| 369 | file accompanying this distribution. | ||
| 370 | |||
| 371 | =cut | ||
| 372 | |||
| 373 | # }}}1 | ||
| 374 | # vi: set ts=4 sts=4 sw=4 et ai fo-=ro foldmethod=marker: # | ||