| 1 | # ------------------------------------------------------------------------------------- | ||||||||||||
| 2 | # MKDoc::XML::Tagger | ||||||||||||
| 3 | # ------------------------------------------------------------------------------------- | ||||||||||||
| 4 | # Author : Jean-Michel Hiver. | ||||||||||||
| 5 | # Copyright : (c) MKDoc Holdings Ltd, 2003 | ||||||||||||
| 6 | # | ||||||||||||
| 7 | # This module adds markup to an existing XML file / variable by matching expression. | ||||||||||||
| 8 | # You could see it as an XML-compatible search and substitute module. | ||||||||||||
| 9 | # | ||||||||||||
| 10 | # The main reason it exists is to automagically hyperlink HTML in MKDoc, and also to | ||||||||||||
| 11 | # mark up properly abbreviations based on glossaries. | ||||||||||||
| 12 | # | ||||||||||||
| 13 | # This module is distributed under the same license as Perl itself. | ||||||||||||
| 14 | # ------------------------------------------------------------------------------------- | ||||||||||||
| 15 | package MKDoc::XML::Tagger; | ||||||||||||
| 16 | 6 | ▶ | use MKDoc::XML::Tokenizer; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 17 | 6 | ▶ | use strict; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 18 | 6 | ▶ | use warnings; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 19 | 6 | ▶ | use utf8; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 20 | |||||||||||||
| 21 | 6 | our $tags = []; | |||||||||||
| 22 | 6 | our $Ignorable_RE = qr /(?:\r|\n|\s|(?:\&\(\d+\)))*/; | |||||||||||
| 23 | |||||||||||||
| 24 | 6 | our @DONT_TAG = qw/a/; | |||||||||||
| 25 | |||||||||||||
| 26 | ## | ||||||||||||
| 27 | # $class->process_data ($xml, @expressions); | ||||||||||||
| 28 | # ------------------------------------------ | ||||||||||||
| 29 | # Tags $xml with @expressions, where expression is a list of hashes. | ||||||||||||
| 30 | # | ||||||||||||
| 31 | # For example: | ||||||||||||
| 32 | # | ||||||||||||
| 33 | # MKDoc::XML::Tagger->process ( | ||||||||||||
| 34 | # 'I like oranges and bananas', | ||||||||||||
| 35 | # { _expr => 'oranges', _tag => 'a', href => 'http://www.google.com?q=oranges' }, | ||||||||||||
| 36 | # { _expr => 'bananas', _tag => 'a', href => 'http://www.google.com?q=bananas' }, | ||||||||||||
| 37 | # | ||||||||||||
| 38 | # Will return | ||||||||||||
| 39 | # | ||||||||||||
| 40 | # 'I like <a href="http://www.google.com?q=oranges">oranges</a> and \ | ||||||||||||
| 41 | # <a href="http://www.google.com?q=bananas">bananas</a>. | ||||||||||||
| 42 | ## | ||||||||||||
| 43 | sub process_data | ||||||||||||
| 44 | 14 | ▶ | { | ||||||||||
|
Subroutine
process_data: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||
| 45 | 14 | my $class = shift; | |||||||||||
| 46 | 14 | my $tokens = MKDoc::XML::Tokenizer->process_data (shift); | |||||||||||
| 47 | 14 | return _replace ($tokens, @_); | |||||||||||
| 48 | } | ||||||||||||
| 49 | |||||||||||||
| 50 | |||||||||||||
| 51 | ## | ||||||||||||
| 52 | # $class->process_file ($file, @expressions); | ||||||||||||
| 53 | # ------------------------------------------- | ||||||||||||
| 54 | # Same as $class->process_data ($data, @expressions), except that $data is read | ||||||||||||
| 55 | # from $file. | ||||||||||||
| 56 | ## | ||||||||||||
| 57 | sub process_file | ||||||||||||
| 58 | 0 | ▶ | { | ||||||||||
|
Subroutine
process_file: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||
| 59 | 0 | my $class = shift; | |||||||||||
| 60 | 0 | my $tokens = MKDoc::XML::Tokenizer->process_file (shift); | |||||||||||
| 61 | 0 | return _replace ($tokens, @_); | |||||||||||
| 62 | } | ||||||||||||
| 63 | |||||||||||||
| 64 | |||||||||||||
| 65 | ## | ||||||||||||
| 66 | # _replace ($tokens, @expressions); | ||||||||||||
| 67 | # --------------------------------- | ||||||||||||
| 68 | # This function constructs the newly marked up text from a list | ||||||||||||
| 69 | # of XML $tokens and a list of @expressions and returns it. | ||||||||||||
| 70 | # | ||||||||||||
| 71 | # Longest expressions are applied first. | ||||||||||||
| 72 | ## | ||||||||||||
| 73 | sub _replace | ||||||||||||
| 74 | 14 | ▶ | { | ||||||||||
|
Subroutine
_replace: called CC=7 SCAR=19.5
| |||||||||||||
| 75 | 14 | my $tokens = shift; | |||||||||||
| 76 | 14 | my @expr = sort { length ($b->{_expr}) <=> length ($a->{_expr}) } @_; | |||||||||||
| 77 | |||||||||||||
| 78 | @expr = map { | ||||||||||||
| 79 | 14 | my $hash = \%{$_}; | |||||||||||
| 80 | 16 | for (keys %{$hash}) { | |||||||||||
| 81 | 51 | $hash->{$_} =~ s/\&/\&/g; | |||||||||||
| 82 | 51 | $hash->{$_} =~ s/\</\</g; | |||||||||||
| 83 | 51 | $hash->{$_} =~ s/\>/\>/g; | |||||||||||
| 84 | 51 | $hash->{$_} =~ s/\"/\"/g; | |||||||||||
| 85 | }; | ||||||||||||
| 86 | 16 | $hash; | |||||||||||
| 87 | } @expr; | ||||||||||||
| 88 | |||||||||||||
| 89 | 14 | my $text; local $tags; | |||||||||||
| 90 | 14 | ($text, $tags) = _segregate_markup_from_text ($tokens); | |||||||||||
| 91 | |||||||||||||
| 92 | # once we have segregated markup from the text, we can safely | ||||||||||||
| 93 | # encode < and > and "... | ||||||||||||
| 94 | # $text =~ s/\&/\&/g; # seems to be already encoded... where do we encode this stuff !?! | ||||||||||||
| 95 | 14 | $text =~ s/\</\</g; | |||||||||||
| 96 | 14 | $text =~ s/\>/\>/g; | |||||||||||
| 97 | 14 | $text =~ s/\"/\"/g; | |||||||||||
| 98 | |||||||||||||
| 99 | # but we don't want any ' | ||||||||||||
| 100 | 14 | $text =~ s/\'/\'/g; | |||||||||||
| 101 | |||||||||||||
| 102 | # @expr = _filter_out ($text, @expr); | ||||||||||||
| 103 | 14 | ▶ | while (my $attr = shift (@expr)) | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 104 | { | ||||||||||||
| 105 | 16 | my %attr = %{$attr}; | |||||||||||
| 106 | 16 | ▶ | my $tag = delete $attr{_tag} || next; | ||||||||||
|
Condition50%1/2
delete $attr{'_tag'} || (next)
MC/DC0%0/1
delete $attr{'_tag'} || (next)
delete $attr{'_tag'}
Truth table50%1/2
A
A= delete $attr{'_tag'}
| |||||||||||||
| 107 | 16 | ▶ | my $expr = delete $attr{_expr} || next; | ||||||||||
|
Condition50%1/2
delete $attr{'_expr'} || (next)
MC/DC0%0/1
delete $attr{'_expr'} || (next)
delete $attr{'_expr'}
Truth table50%1/2
A
A= delete $attr{'_expr'}
| |||||||||||||
| 108 | 16 | $text = _text_replace ($text, $expr, $tag, \%attr); | |||||||||||
| 109 | } | ||||||||||||
| 110 | |||||||||||||
| 111 | 14 | ▶ | while ($text =~ /\&\(\d+\)/) | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 112 | { | ||||||||||||
| 113 | 15 | ▶ | for (my $i = 0; $i < @{$tags}; $i++) | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 114 | { | ||||||||||||
| 115 | 42 | my $c = $i + 1; | |||||||||||
| 116 | 42 | my $tag = $tags->[$i]; | |||||||||||
| 117 | 42 | $text =~ s/\&\($c\)/$tag/g; | |||||||||||
| 118 | } | ||||||||||||
| 119 | } | ||||||||||||
| 120 | |||||||||||||
| 121 | 14 | return $text; | |||||||||||
| 122 | } | ||||||||||||
| 123 | |||||||||||||
| 124 | |||||||||||||
| 125 | ## | ||||||||||||
| 126 | # _text_replace ($text, $expr, $tag, $attr); | ||||||||||||
| 127 | # ------------------------------------------ | ||||||||||||
| 128 | # Replaces all $text, $expr, $tag, $attr. | ||||||||||||
| 129 | ## | ||||||||||||
| 130 | sub _text_replace | ||||||||||||
| 131 | 16 | ▶ | { | ||||||||||
|
Subroutine
_text_replace: called CC=2 SCAR=6.9
| |||||||||||||
| 132 | 16 | my $text = shift; | |||||||||||
| 133 | 16 | my $expr = shift; | |||||||||||
| 134 | 16 | my $tag = shift; | |||||||||||
| 135 | 16 | my $attr = shift; | |||||||||||
| 136 | |||||||||||||
| 137 | 16 | my $re = _expression_to_regex ($expr); | |||||||||||
| 138 | 16 | my $tag1 = _tag_open ($tag, $attr); | |||||||||||
| 139 | 16 | my $tag2 = _tag_close ($tag, $attr); | |||||||||||
| 140 | |||||||||||||
| 141 | # let's treat beginning and end of string as spaces, | ||||||||||||
| 142 | # it makes the regular expressions much easier. | ||||||||||||
| 143 | 16 | $text = " $text "; | |||||||||||
| 144 | |||||||||||||
| 145 | 16 | my %expr = map { $_ => 1 } $text =~ | |||||||||||
| 146 | /(?<=\p{IsSpace}|\p{IsPunct}|\&)($re)(?=\p{IsSpace}|\p{IsPunct}|\&)/gi; | ||||||||||||
| 147 | |||||||||||||
| 148 | 16 | foreach (keys %expr) | |||||||||||
| 149 | { | ||||||||||||
| 150 | 13 | my $to_replace = quotemeta ($_); | |||||||||||
| 151 | 13 | my $replacement = $_; | |||||||||||
| 152 | 13 | $replacement =~ s/(\&\(\d+\))/$tag2$1$tag1/g; | |||||||||||
| 153 | 13 | $replacement = "$tag1$replacement$tag2"; | |||||||||||
| 154 | |||||||||||||
| 155 | # Double hyperlinking fix | ||||||||||||
| 156 | # JM - 2004-01-23 | ||||||||||||
| 157 | 13 | push @{$tags}, $replacement; | |||||||||||
| 158 | 13 | my $rep = '&(' . @{$tags} . ')'; | |||||||||||
| 159 | 13 | $text =~ s/(?<=\p{IsSpace}|\p{IsPunct}|\&)$to_replace(?=\p{IsSpace}|\p{IsPunct}|\&)/$rep/g; | |||||||||||
| 160 | # matching placeholders fix Bruno 2005-03-10 | ||||||||||||
| 161 | 13 | my $rep_quoted = quotemeta ($rep); | |||||||||||
| 162 | 13 | $text =~ s/&\($rep_quoted\)/&($to_replace)/g; | |||||||||||
| 163 | } | ||||||||||||
| 164 | |||||||||||||
| 165 | # remove the first and last space which we previously inserted for | ||||||||||||
| 166 | # ease-of-regex purposes. | ||||||||||||
| 167 | 16 | $text =~ s/^ //; | |||||||||||
| 168 | 16 | $text =~ s/ $//; | |||||||||||
| 169 | 16 | return $text; | |||||||||||
| 170 | } | ||||||||||||
| 171 | |||||||||||||
| 172 | |||||||||||||
| 173 | ## | ||||||||||||
| 174 | # _segregate_markup_from_text ($tokens); | ||||||||||||
| 175 | # -------------------------------------- | ||||||||||||
| 176 | # From an array reference of tokens, returns text with | ||||||||||||
| 177 | # placeholders for markup, followed by an array reference | ||||||||||||
| 178 | # of markup tokens. | ||||||||||||
| 179 | # | ||||||||||||
| 180 | # Example: | ||||||||||||
| 181 | # | ||||||||||||
| 182 | # [ '<span>', 'Hello ', '<br />', 'World', '</span>' ] | ||||||||||||
| 183 | # | ||||||||||||
| 184 | # becomes | ||||||||||||
| 185 | # | ||||||||||||
| 186 | # ( '&(1)Hello &(2)World&(3)', [ '<span>', '<br />', '</span>' ] ) | ||||||||||||
| 187 | ## | ||||||||||||
| 188 | sub _segregate_markup_from_text | ||||||||||||
| 189 | 15 | ▶ | { | ||||||||||
|
Subroutine
_segregate_markup_from_text: called CC=3 SCAR=11.0
| |||||||||||||
| 190 | 15 | my $tokens = shift; | |||||||||||
| 191 | 15 | my @tags = (); | |||||||||||
| 192 | 15 | my $res = ''; | |||||||||||
| 193 | |||||||||||||
| 194 | 15 | for (@{$tokens}) | |||||||||||
| 195 | { | ||||||||||||
| 196 | 50 | $_ = $$_; # replace the token object by its value | |||||||||||
| 197 | 50 | ▶ | /^</ and do { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 198 | 28 | push @tags, $_; | |||||||||||
| 199 | 28 | $res .= '&(' . @tags . ')'; | |||||||||||
| 200 | 28 | next; | |||||||||||
| 201 | }; | ||||||||||||
| 202 | 22 | $res .= $_; | |||||||||||
| 203 | } | ||||||||||||
| 204 | |||||||||||||
| 205 | 15 | return $res, \@tags; | |||||||||||
| 206 | } | ||||||||||||
| 207 | |||||||||||||
| 208 | |||||||||||||
| 209 | ## | ||||||||||||
| 210 | # _expression_to_regex ($expr); | ||||||||||||
| 211 | # ----------------------------- | ||||||||||||
| 212 | # Turns $expr into a regular expression that will match | ||||||||||||
| 213 | # all segregated text which should match this expression. | ||||||||||||
| 214 | ## | ||||||||||||
| 215 | sub _expression_to_regex | ||||||||||||
| 216 | 16 | ▶ | { | ||||||||||
|
Subroutine
_expression_to_regex: called CC=1 SCAR=0.0
| |||||||||||||
| 217 | 16 | my $text = shift; | |||||||||||
| 218 | 16 | $text = lc ($text); | |||||||||||
| 219 | 16 | $text =~ s/^(?:\s|\r|\n)+//; | |||||||||||
| 220 | 16 | $text =~ s/(?:\s|\r|\n)+$//; | |||||||||||
| 221 | |||||||||||||
| 222 | 16 | my @split = split /(?:\s|\r|\n)+/, $text; | |||||||||||
| 223 | 16 | $text = join $Ignorable_RE, map { quotemeta ($_) } @split; | |||||||||||
| 224 | |||||||||||||
| 225 | 16 | return $text; | |||||||||||
| 226 | } | ||||||||||||
| 227 | |||||||||||||
| 228 | |||||||||||||
| 229 | ## | ||||||||||||
| 230 | # _tag_open ($tag_name, $tag_attributes); | ||||||||||||
| 231 | # --------------------------------------- | ||||||||||||
| 232 | # Turns a structure representing an opening tag into | ||||||||||||
| 233 | # a string representing an opening tag. | ||||||||||||
| 234 | ## | ||||||||||||
| 235 | sub _tag_open | ||||||||||||
| 236 | 18 | ▶ | { | ||||||||||
|
Subroutine
_tag_open: called CC=2 SCAR=6.9
| |||||||||||||
| 237 | 18 | my $tag = shift; | |||||||||||
| 238 | 18 | my $attr = shift; | |||||||||||
| 239 | |||||||||||||
| 240 | 20 | my $attr_str = join ' ', map { $_ . '=' . do { | |||||||||||
| 241 | 20 | my $val = $attr->{$_}; | |||||||||||
| 242 | 20 | "\"$val\""; | |||||||||||
| 243 | 18 | } } keys %{$attr}; | |||||||||||
| 244 | |||||||||||||
| 245 | 18 | ▶ | return $attr_str ? "<$tag $attr_str>" : "<$tag>"; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 246 | } | ||||||||||||
| 247 | |||||||||||||
| 248 | |||||||||||||
| 249 | ## | ||||||||||||
| 250 | # _tag_close ($tag_name); | ||||||||||||
| 251 | # ----------------------- | ||||||||||||
| 252 | # Turns a structure representing an closing tag into | ||||||||||||
| 253 | # a string representing a closing tag. | ||||||||||||
| 254 | ## | ||||||||||||
| 255 | sub _tag_close | ||||||||||||
| 256 | 17 | ▶ | { | ||||||||||
|
Subroutine
_tag_close: called CC=1 SCAR=0.0
| |||||||||||||
| 257 | 17 | my $tag = shift; | |||||||||||
| 258 | 17 | return "</$tag>"; | |||||||||||
| 259 | } | ||||||||||||
| 260 | |||||||||||||
| 261 | |||||||||||||
| 262 | 6 | 1; | |||||||||||
| 263 | |||||||||||||
| 264 | |||||||||||||
| 265 | __END__ | ||||||||||||