sub escape_html_data { make_static(\@_); my($line) = shift; if (!defined $line) {return undef;} # #39 = Apostrophe. ' does not work in some versions of IE. my %html_escape_sequences = ('&' => '&', '>' => '>', '<' => '<', '"' => '"', '\'' => ''', ); $line =~ s/([&><"'])/$html_escape_sequences{$1}/g; return $line;}
use xMarkdown;use University::Tidy;my $output_text;my $field_type = $field->type();if ( $field_type eq 'markdown' ){ $output_text = xMarkdown::markdown( $field->value() ); $output_text = University::Tidy::tidyString( $output_text );} elsif ( $field_type eq 'visualformat' ) { $output_text = $field->value(); $output_text = University::Tidy::tidyString( $output_text );}
package University::Tidy;use Cwd;#use strict;# Yeah, I know, I know...my $g_tidyPath = "/usr/local/bin";my $g_tidyOptions = " -quiet"; $g_tidyOptions .= " --wrap 0"; $g_tidyOptions .= " --output-xhtml yes"; $g_tidyOptions .= " --drop-font-tags yes"; $g_tidyOptions .= " --drop-proprietary-attributes yes"; $g_tidyOptions .= " --word-2000 yes"; $g_tidyOptions .= " --bare yes";# $g_tidyOptions .= " --clean yes"; $g_tidyOptions .= " --indent auto"; $g_tidyOptions .= " --indent-attributes no"; $g_tidyOptions .= " --vertical-space no"; $g_tidyOptions .= " --drop-empty-paras yes"; $g_tidyOptions .= " --logical-emphasis yes"; $g_tidyOptions .= " --wrap-sections no"; $g_tidyOptions .= " -f /dev/null";sub tidyString { #my ( $parent, $myEmptyAttrib, $myEmptyAttribStr) = @_; # usage: tidyString(string STRING[, int NODOCTAGS]) # result: STRING with tidy-parsed HTML die("usage: tidyString(string STRING[, int NODOCTAGS])") unless @_; return ($_[0]) if ($_[0] !~ /<[^>]*>/); # return STRING if it has no HTML tags, nothing to parse if (!defined $_[1]) { $_[1] = 1; } # NODOCTAGS shall default to 1, if omitted # (set it to 0 if you do want to receive # a complete HTML document; to 2 if SCRIPT and STYLE # tags in the header shall be returned with the body) pipe (READ_FROM_PARENT, WRITE_TO_CHILD) or die "can't pipe(1): $!"; pipe (READ_FROM_CHILD, WRITE_TO_PARENT) or die "can't pipe(2): $!"; $parent = fork(); die ("fork failed: $!") unless defined $parent; unless ($parent) { # we are the child my $tidy = $g_tidyPath . "/tidy"; # close obsolete file handles close(READ_FROM_CHILD); close(WRITE_TO_CHILD); # redirect STDIN close(STDIN); open(STDIN, "<&READ_FROM_PARENT"); # redirect STDOUT close(STDOUT); open(STDOUT, ">&WRITE_TO_PARENT"); select(STDIN); $| = 1; # switch process to tidy exec("$tidy$g_tidyOptions") or die("exec of \'$tidy\' failed: $!"); # END of child } else { # we are the parent my $tidied_data = ""; # close obsolete file handles close(READ_FROM_PARENT); close(WRITE_TO_PARENT); # print the to-be-tidied string to the child's STDIN print WRITE_TO_CHILD $_[0]; # close the file handle close(WRITE_TO_CHILD); # read everything from the child's STDOUT into $tidied_data while () { $tidied_data .= $_; } # close the file handle close(READ_FROM_CHILD); # reap the child waitpid($parent, 0); if ($_[1] > 0) { # remove unwanted document tags # (we want only what is between and ) if ($tidied_data !~ /]*>/i) { # cancel, if no body return ("[could not find BODY in message]"); } if ($_[1] > 1) { # we'd like to have JavaScript and CSS codes returned my $JavaScript = ""; my $CSS = ""; $tidied_data =~ s\(<SCRIPT[^>]*>.*?</SCRIPT[^>]*>)\$1\si; $JavaScript = $1 . "\n" unless ($1 eq ""); $tidied_data =~ s\(]*&amp;amp;gt;.*?&amp;amp;lt;/STYLE[^&amp;amp;gt;]*&amp;amp;gt\$1\si; $CSS = $1 . "\n" unless ($1 eq ""); # add the JavaScript and StyleSheet tags to the output # at the beginning of BODY $tidied_data = $JavaScript . $CSS . $tidied_data; $tidied_data =~ s/(&amp;amp;lt;BODY[^&amp;amp;gt;]*&amp;amp;gt;\n?)/$JavaScript$CSS$1/si; } # strip everything before &amp;amp;lt;BODY&amp;amp;gt; and after &amp;amp;lt;/BODY&amp;amp;gt; $tidied_data =~ s/^.*&amp;amp;lt;BODY[^&amp;amp;gt;]*&amp;amp;gt;\n?//si; $tidied_data =~ s\&amp;amp;lt;/BODY[^&amp;amp;gt;]*&amp;amp;gt;.*&amp;amp;lt;/HTML[^&amp;amp;gt;]*&amp;amp;gt;.*$\\si; } $tidied_data =~ s/&amp;amp;lt;FONT[^&amp;amp;gt;]*&amp;amp;gt;&amp;amp;lt;\/FONT[^&amp;amp;gt;]*&amp;amp;gt;\n?//gsi; while ($tidied_data =~ /\s*(BORDERCOLOR([a-z]*))\s*=\s*".*?"/i) { $tidied_data =~ s/\s*$1\s*=\s*".*?"//gsi; } # drop unknown empty attributes, ALT shall be allowed to be empty while ($tidied_data =~ /\s*([a-z]+)\s*=\s*""/i) { $myEmptyAttrib = $1; if ($myEmptyAttrib =~ /ALT/i) { # replace the recognized empty attribute with a placeholder $myEmptyAttribStr = "myEmpty" . $myEmptyAttrib . "Attrib"; $tidied_data =~ s/(&amp;amp;lt;[^&amp;amp;gt;]*?)\s(?:[a-z]+)\s*=\s*""([^&amp;amp;gt;]*&amp;amp;gt/$1 $myEmptyAttribStr$2/gsi; } else { # drop the unrecognized empty attribute $tidied_data =~ s/(&amp;amp;lt;[^&amp;amp;gt;]*?)\s(?:[a-z]+)\s*=\s*""([^&amp;amp;gt;]*&amp;amp;gt/$1$2/gsi; } } # replace the placeholder with the appropriate empty attribute $tidied_data =~ s/myEmpty([a-z]+)Attrib/$1=""/gsi; return $tidied_data; # END of parent }}&amp;amp;lt;/pre&amp;amp;gt;I also compiled the latest version of libtidy/tidy from sourceforge &amp;amp;lt;A HREF="http://tidy.sf.net/" TARGET='_blank'&amp;amp;gt;tidy.sf.net&amp;amp;lt;/A&amp;amp;gt; and installed the binary in /usr/local (although you could put it anywhere). Obviously, this doesn't solve the issue of applying this to existing DCRs, but I figure this is useful code (and the config options I've listed in my perl module would help you in tidying old code anyway). Let me know if you want more information &amp;amp;lt;/p&amp;amp;gt;&amp;amp;lt;/p&amp;amp;gt;
open SOURCEDCR, '<:utf8', "$dcrfilename" or die ("No such file"); read( SOURCEDCR, $inxml, -s SOURCEDCR); close SOURCEDCR; my $rootnode = TeamSite::XMLnode->new($inxml); foreach my $node ($rootnode->get_node_list()) { if ($node->type() is a VisualFormat node) { $dirtyhtml = $node->value(); # tidy_html will call HTMLTidy to clean the code $cleanhtml = tidy_html($dirtyhtml); $node->get_node('CDATA')->set_inner_xml($cleanhtml); } } $outxml = $rootnode->get_xml(); my $header = '<?xml version="1.0" encoding="UTF-8"?>'."\n"; $header = $header.'<!DOCTYPE your doctype SYSTEM "your DTD">'."\n"; $outxml = $header.$outxml; open DESTDCR, '>:utf8', "$dcrfilename" or die ("Can't open output file $dcrfilename"); print DESTDCR $outxml; close DESTDCR;
$inxml =~ s!<item name="([^"]*)"/>!<item name="\1"><value></value></item>!g;