From 3dad7198e2d4d7e176428904ba6a4176726dc1f8 Mon Sep 17 00:00:00 2001 From: Terrence Brannon Date: Mon, 2 Feb 2009 17:05:06 -0500 Subject: [PATCH] data_map method --- .emacs.desktop | 160 +++++++++++++++++++++++++++++++++ lib/HTML/Element/Library.pm | 29 +++++- t/html/same_as.html | 16 ++++ t/html/same_as/same_as.exp | 15 ++++ t/html/same_as/same_as.gen | 15 ++++ t/html/same_as/same_as.initial | 16 ++++ t/same_as.t | 38 ++++++++ 7 files changed, 288 insertions(+), 1 deletion(-) create mode 100644 .emacs.desktop create mode 100644 t/html/same_as.html create mode 100644 t/html/same_as/same_as.exp create mode 100644 t/html/same_as/same_as.gen create mode 100644 t/html/same_as/same_as.initial create mode 100644 t/same_as.t diff --git a/.emacs.desktop b/.emacs.desktop new file mode 100644 index 0000000..4c1ddc3 --- /dev/null +++ b/.emacs.desktop @@ -0,0 +1,160 @@ +;; -*- mode: emacs-lisp; coding: emacs-mule; -*- +;; -------------------------------------------------------------------------- +;; Desktop File for Emacs +;; -------------------------------------------------------------------------- +;; Created Tue Jan 13 20:12:45 2009 +;; Desktop file format version 206 +;; Emacs version 22.3.1 + +;; Global section: +(setq desktop-missing-file-warning nil) +(setq tags-file-name nil) +(setq tags-table-list nil) +(setq search-ring '("pay" "factory" "TREE_" "tree_content" "tree_con" "shell_content" "contact" "passover" "content_handler" "'")) +(setq regexp-search-ring nil) +(setq register-alist nil) + +;; Buffer section -- buffers listed in same order as in buffer list: +(desktop-create-buffer 206 + "/tmp/leads2/admin/affiliates.cgi" + "affiliates.cgi" + 'cperl-mode + '(global-auto-revert-mode) + 3601 + '(4531 t) + nil + nil + '((buffer-file-coding-system . undecided-unix))) + +(desktop-create-buffer 206 + nil + "admin" + 'dired-mode + '(global-auto-revert-mode) + 542 + '(nil nil) + t + '("/tmp/leads2/admin/") + nil) + +(desktop-create-buffer 206 + "/home/tbrannon/prg/html-seamstress/lib/HTML/Seamstress.pm" + "Seamstress.pm" + 'cperl-mode + '(global-auto-revert-mode) + 661 + '(nil nil) + nil + nil + '((buffer-file-coding-system . undecided-unix))) + +(desktop-create-buffer 206 + "/var/log/httpd/error_log" + "error_log" + 'fundamental-mode + '(global-auto-revert-mode) + 154167 + '(151473 t) + t + nil + '((buffer-file-coding-system . undecided-unix))) + +(desktop-create-buffer 206 + nil + "html-seamstress" + 'dired-mode + '(global-auto-revert-mode) + 669 + '(1778 t) + t + '("/home/tbrannon/prg/html-seamstress/" "/home/tbrannon/prg/html-seamstress/lib/" "/home/tbrannon/prg/html-seamstress/lib/HTML/") + nil) + +(desktop-append-buffer-args 206 + nil + "lib" + 'dired-mode + '(global-auto-revert-mode) + 251 + '(nil nil) + t + '("/home/tbrannon/prg/html-seamstress/lib/") + nil) + +(desktop-append-buffer-args 206 + nil + "HTML" + 'dired-mode + '(global-auto-revert-mode) + 257 + '(nil nil) + t + '("/home/tbrannon/prg/html-seamstress/lib/HTML/") + nil) + +(desktop-append-buffer-args 206 + nil + "html-element-library" + 'dired-mode + '(global-auto-revert-mode) + 1834 + '(1496 t) + t + '("/home/tbrannon/prg/html-element-library/" "/home/tbrannon/prg/html-element-library/lib/" "/home/tbrannon/prg/html-element-library/lib/HTML/" "/home/tbrannon/prg/html-element-library/lib/HTML/Element/") + nil) + +(desktop-append-buffer-args 206 + "/home/tbrannon/emacs/init.el" + "init.el" + 'emacs-lisp-mode + '(global-auto-revert-mode) + 1 + '(3680 t) + nil + nil + '((buffer-file-coding-system . undecided-unix))) + +(desktop-append-buffer-args 206 + nil + "leads2" + 'dired-mode + '(global-auto-revert-mode) + 227 + '(nil nil) + t + '("/tmp/leads2/") + nil) + +(desktop-append-buffer-args 206 + nil + "tmp" + 'dired-mode + '(global-auto-revert-mode) + 405 + '(nil nil) + t + '("/tmp/") + nil) + +(desktop-append-buffer-args 206 + "/home/tbrannon/prg/html-element-library/lib/HTML/Element/Library.pm" + "Library.pm" + 'cperl-mode + '(global-auto-revert-mode) + 18597 + '(17049 t) + nil + nil + '((buffer-file-coding-system . undecided-unix))) + +(desktop-append-buffer-args 206 + nil + "emacs" + 'dired-mode + '(global-auto-revert-mode) + 1608 + '(nil nil) + t + '("/home/tbrannon/emacs/") + nil) + diff --git a/lib/HTML/Element/Library.pm b/lib/HTML/Element/Library.pm index 99abc3d..ef03c78 100644 --- a/lib/HTML/Element/Library.pm +++ b/lib/HTML/Element/Library.pm @@ -12,6 +12,7 @@ use Array::Group qw(:all); use Carp qw(confess); use Data::Dumper; use HTML::Element; +use List::Util qw(first); use List::MoreUtils qw/:all/; use Params::Validate qw(:all); use Scalar::Listify; @@ -36,13 +37,38 @@ sub HTML::Element::siblings { $p->content_list; } +sub HTML::Element::data_map { + + my ($container, @rest) = @_; + + + my %p = validate(@rest, { + href => 1, + with_attr => 1, + excluding => { type => ARRAYREF, default => [] } + } + ); + + my @same_as = $container->look_down('_attr' => $p{with_attr}); + + for my $same_as (@same_as) { + next if first { $same_as eq $_ } @{$p{excluding}} ; + $same_as->replace_content( $p{href}->{ $same_as->attr( $p{with_attr} ) } ) ; + } + +} + + sub HTML::Element::passover { my ($tree, $child_id) = @_; - warn "ARGS: my ($tree, $child_id)"; + warn "ARGS: my ($tree, $child_id)" if $DEBUG; + warn $tree->as_HTML(undef, ' ') if $DEBUG; my $exodus = $tree->look_down(id => $child_id); + warn "E: $exodus" if $DEBUG; + my @s = HTML::Element::siblings($exodus); for my $s (@s) { @@ -188,6 +214,7 @@ sub HTML::Element::iter2 { warn "wrapper_data: " . Dumper $p{wrapper_data} if $p{debug} ; my $container = ref_or_ld($tree, $p{wrapper_ld}); + warn "container: " . $container if $p{debug} ; warn "wrapper_(preproc): " . $container->as_HTML if $p{debug} ; $p{wrapper_proc}->($container) if defined $p{wrapper_proc} ; warn "wrapper_(postproc): " . $container->as_HTML if $p{debug} ; diff --git a/t/html/same_as.html b/t/html/same_as.html new file mode 100644 index 0000000..f70d378 --- /dev/null +++ b/t/html/same_as.html @@ -0,0 +1,16 @@ + + + HO HO HA HA HA + + + + + + + + + +
1(877) 255-3239jim@gmail.com
+ + + \ No newline at end of file diff --git a/t/html/same_as/same_as.exp b/t/html/same_as/same_as.exp new file mode 100644 index 0000000..3eac36a --- /dev/null +++ b/t/html/same_as/same_as.exp @@ -0,0 +1,15 @@ + + + HO HO HA HA HA + + + + + + + + +
1(877) 255-3239should-not-be-touched@seamstress.com
+ + diff --git a/t/html/same_as/same_as.gen b/t/html/same_as/same_as.gen new file mode 100644 index 0000000..3eac36a --- /dev/null +++ b/t/html/same_as/same_as.gen @@ -0,0 +1,15 @@ + + + HO HO HA HA HA + + + + + + + + +
1(877) 255-3239should-not-be-touched@seamstress.com
+ + diff --git a/t/html/same_as/same_as.initial b/t/html/same_as/same_as.initial new file mode 100644 index 0000000..555abaa --- /dev/null +++ b/t/html/same_as/same_as.initial @@ -0,0 +1,16 @@ + + + HO HO HA HA HA + + + + + + + + + +
1(877) 255-3239should-not-be-touched@seamstress.com
+ + + \ No newline at end of file diff --git a/t/same_as.t b/t/same_as.t new file mode 100644 index 0000000..1fc78ba --- /dev/null +++ b/t/same_as.t @@ -0,0 +1,38 @@ +# This might look like shell script, but it's actually -*- perl -*- +use strict; +use lib qw(t/ t/m/); + +use File::Slurp; +use Test::More qw(no_plan); + +use TestUtils; +use HTML::TreeBuilder; +use HTML::Element::Library; + +sub replace_age { + my $branch = shift; + my $age = shift; + $branch->look_down(id => 'age')->replace_content($age); +} + + +sub tage { + + my $root = "t/html/same_as/same_as"; + + my $tree = HTML::TreeBuilder->new_from_file("$root.initial"); + + warn "TREE: $tree" . $tree->as_HTML; + + my %data = (people_id => 888, phone => '444-4444', email => 'dont-you-dare-render@xml.com'); + + $tree->data_map(href => \%data, with_attr => 'sid', excluding => ['email']); + + my $generated_html = ptree($tree, "$root.gen"); + + is ($generated_html, File::Slurp::read_file("$root.exp"), "HTML for same_as"); +} + + +tage(); + -- 2.39.2