| 1 | ########################################################################### | ||
| 2 | # Copyright 2004 Lab-01 LLC <http://lab-01.com/> | ||
| 3 | # | ||
| 4 | # Licensed under the Apache License, Version 2.0 (the "License"); | ||
| 5 | # you may not use this file except in compliance with the License. | ||
| 6 | # You may obtain a copy of the License at | ||
| 7 | # | ||
| 8 | # http://www.apache.org/licenses/LICENSE-2.0 | ||
| 9 | # | ||
| 10 | # Unless required by applicable law or agreed to in writing, software | ||
| 11 | # distributed under the License is distributed on an "AS IS" BASIS, | ||
| 12 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| 13 | # See the License for the specific language governing permissions and | ||
| 14 | # limitations under the License. | ||
| 15 | # | ||
| 16 | # Tmojo(tm) is a trademark of Lab-01 LLC. | ||
| 17 | ########################################################################### | ||
| 18 | |||
| 19 | package HTML::Tmojo::Filters; | ||
| 20 | |||
| 21 | use strict; | ||
| 22 | require Exporter; | ||
| 23 | our @ISA = qw(Exporter); | ||
| 24 | our @EXPORT = qw( | ||
| 25 | html_attribute | ||
| 26 | html_content | ||
| 27 | reduce_indentation | ||
| 28 | ); | ||
| 29 | |||
| 30 | sub html_attribute { | ||
| 31 | my ($val) = @_; | ||
| 32 | |||
| 33 | for ($val) { | ||
| 34 | s/'/'/g; | ||
| 35 | s/"/"/g; | ||
| 36 | } | ||
| 37 | |||
| 38 | return $val; | ||
| 39 | } | ||
| 40 | |||
| 41 | sub html_content { | ||
| 42 | my ($val) = @_; | ||
| 43 | |||
| 44 | for ($val) { | ||
| 45 | s/&/&/g; | ||
| 46 | s/</</g; | ||
| 47 | s/>/>/g; | ||
| 48 | } | ||
| 49 | |||
| 50 | return $val; | ||
| 51 | } | ||
| 52 | |||
| 53 | sub reduce_indentation { | ||
| 54 | my $text = shift; | ||
| 55 | my %args = @_; | ||
| 56 | |||
| 57 | if ($args{tabs_to_spaces} ne '') { | ||
| 58 | $text =~ s/\t/' ' x $args{tabs_to_spaces}/eg; | ||
| 59 | } | ||
| 60 | |||
| 61 | $text =~ /^(\s+)/; | ||
| 62 | my $remove = $1; | ||
| 63 | |||
| 64 | my @lines = split /\n/, $text; | ||
| 65 | foreach my $line (@lines) { | ||
| 66 | $line =~ s/^$remove//; | ||
| 67 | if ($args{base_indentation} ne '') { | ||
| 68 | $line = $args{base_indentation} . $line; | ||
| 69 | } | ||
| 70 | } | ||
| 71 | |||
| 72 | return join("\n", @lines); | ||
| 73 | } | ||
| 74 | |||
| 75 | |||
| 76 | 1; |