mhonarc-commits
[Top] [All Lists]

CVS: mhonarc/MHonArc/lib mhtxtenrich.pl,2.8,2.9

2003-01-17 20:10:41
Update of /cvsroot/mhonarc/mhonarc/MHonArc/lib
In directory subversions:/tmp/cvs-serv8982

Modified Files:
	mhtxtenrich.pl 
Log Message:
* Added handling of some text/richtext tags.
* Escape unrecognized tags.


Index: mhtxtenrich.pl
===================================================================
RCS file: /cvsroot/mhonarc/mhonarc/MHonArc/lib/mhtxtenrich.pl,v
retrieving revision 2.8
retrieving revision 2.9
diff -C2 -r2.8 -r2.9
*** mhtxtenrich.pl	19 Dec 2002 05:14:23 -0000	2.8
--- mhtxtenrich.pl	18 Jan 2003 03:10:35 -0000	2.9
***************
*** 37,40 ****
--- 37,68 ----
  package m2h_text_enriched;
  
+ my %enriched_tags = (
+     'bigger' => 1,
+     'bold' => 1,
+     'center' => 1,
+     'color' => 1,
+     'comment' => 1,
+     'excerpt' => 1,
+     'fixed' => 1,
+     'flushboth' => 1,
+     'flushleft' => 1,
+     'flushright' => 1,
+     'fontfamily' => 1,
+     'indent' => 1,
+     'indentright' => 1,
+     'italic' => 1,
+     'lang' => 1,
+     'lt' => 1,
+     'nl' => 1,
+     'nofill' => 1,
+     'paraindent' => 1,
+     'param' => 1,
+     'signature' => 1,
+     'smaller' => 1,
+     'subscript' => 1,
+     'superscript' => 1,
+     'underline' => 1,
+ );
+ 
  my %special_to_char = (
      'lt'  => '<',
***************
*** 56,59 ****
--- 84,91 ----
      $args   = ""  unless defined($args);
  
+     ## Get content-type
+     my($ctype) = $fields->{'content-type'}[0] =~ m%^\s*([\w\-\./]+)%;
+     my $richtext = $ctype =~ /\btext\/richtext\b/i;
+ 
      if (defined($charcnv) && defined(&$charcnv)) {
  	$$data = &$charcnv($$data, $real_charset_name);
***************
*** 74,80 ****
  
      ## Convert specials
!     $$data =~ s/<</\&lt;/g;
  
-     ## Translate text/enriched commands
      $innofill = 0;
      foreach $chunk (split(m|(</?nofill>)|i, $$data)) {
--- 106,122 ----
  
      ## Convert specials
!     if (!$richtext) {
! 	$$data =~ s/<</\&lt;/g;
!     }
! 
!     ## Make sure only non-enriched tags are escaped
!     $$data =~ s{<(/?)([^>]*)>}
!     {
! 	my $eot = $1;
! 	my $tag = lc $2;
! 	$tag =~ s/\s+//g;
! 	($enriched_tags{$tag}) ? '<'.$eot.$tag.'>' : '&lt;'.$eot.$tag.'&gt;';
!     }gexs;
  
      $innofill = 0;
      foreach $chunk (split(m|(</?nofill>)|i, $$data)) {
***************
*** 89,94 ****
  	    next;
  	}
! 	convert_tags(\$chunk);
! 	if (!$innofill) {
  	    $chunk =~ s/(\n\s*)/&nl_seq_to_brs($1)/ge;
  	}
--- 131,136 ----
  	    next;
  	}
! 	convert_tags(\$chunk, $richtext);
! 	if (!$richtext && !$innofill) {
  	    $chunk =~ s/(\n\s*)/&nl_seq_to_brs($1)/ge;
  	}
***************
*** 102,135 ****
  ##
  sub convert_tags {
!     my($str) = shift;
  
!     $$str =~ s|<(/?)bold>|<$1b>|gi;
!     $$str =~ s|<(/?)italic>|<$1i>|gi;
!     $$str =~ s|<(/?)underline>|<$1u>|gi;
!     $$str =~ s|<(/?)fixed>|<$1tt>|gi;
!     $$str =~ s|<(/?)smaller>|<$1small>|gi;
!     $$str =~ s|<(/?)bigger>|<$1big>|gi;
! 
!     $$str =~ s|<fontfamily>\s*<param>([^<]+)</param>|<font face="$1">|gi;
!     $$str =~ s|</fontfamily>|</font>|gi;
!     $$str =~ s|<color>\s*<param>\s*(\S+)\s*</param>|<font color="$1">|gi;
!     $$str =~ s|</color>|</font>|gi;
!     $$str =~ s|<center>|<p align="center">|gi;
!     $$str =~ s|</center>|</p>|gi;
!     $$str =~ s|<flushleft>|<p align="left">|gi;
!     $$str =~ s|</flushleft>|</p>|gi;
!     $$str =~ s|<flushright>|<p align="right">|gi;
!     $$str =~ s|</flushright>|</p>|gi;
!     $$str =~ s|<flushboth>|<p align="justify">|gi;
!     $$str =~ s|</flushboth>|</p>|gi;
!     $$str =~ s|<paraindent>\s*<param>([^<]*)</param>|<blockquote>|gi;
!     $$str =~ s|</paraindent>|</blockquote>|gi;
! 
!     $$str =~ s|<excerpt>\s*(<param>([^<]*)</param>)?|<blockquote>|gi;
!     $$str =~ s|</excerpt>|</blockquote>|gi;
! 
!     # Not supported commands
!     $$str =~ s|<lang>\s*<param>([^<]*)</param>|<div lang="$1">|gi;
!     $$str =~ s|</lang>|</div>|gi;
  }
  
--- 144,203 ----
  ##
  sub convert_tags {
!     my $str  = shift;
!     my $richtext = shift;
! 
!     $$str =~ s{<comment\s*>.*?</comment\s*>}{}gis;
! 
!     $$str =~ s{<(/?)bold\s*>}{<$1b>}gi;
!     $$str =~ s{<(/?)italic\s*>}{<$1i>}gi;
!     $$str =~ s{<(/?)underline\s*>}{<$1u>}gi;
!     $$str =~ s{<(/?)fixed\s*>}{<$1tt>}gi;
!     $$str =~ s{<(/?)smaller\s*>}{<$1small>}gi;
!     $$str =~ s{<(/?)bigger\s*>}{<$1big>}gi;
!     $$str =~ s{<(/?)signature\s*>}{<$1pre>}gi;
! 
!     $$str =~ s{<fontfamily\s*>\s*<param\s*>([^<]+)</param\s*>}
! 	      {<font face="$1">}gix;
!     $$str =~ s|</fontfamily\s*>|</font>|gi;
!     $$str =~ s{<color\s*>\s*<param\s*>\s*(\S+)\s*</param\s*>}
! 	      {<font color="$1">}gix;
!     $$str =~ s|</color\s*>|</font>|gi;
!     $$str =~ s|<center\s*>|<p align="center">|gi;
!     $$str =~ s|</center\s*>|</p>|gi;
!     $$str =~ s|<flushleft\s*>|<p align="left">|gi;
!     $$str =~ s|</flushleft\s*>|</p>|gi;
!     $$str =~ s|<flushright\s*>|<p align="right">|gi;
!     $$str =~ s|</flushright\s*>|</p>|gi;
!     $$str =~ s|<flushboth\s*>|<p align="justify">|gi;
!     $$str =~ s|</flushboth\s*>|</p>|gi;
!     $$str =~ s|<paraindent\s*>\s*<param\s*>([^<]*)</param\s*>|<blockquote>|gi;
!     $$str =~ s|</paraindent\s*>|</blockquote>|gi;
! 
!     $$str =~ s|<excerpt\s*>\s*(<param\s*>([^<]*)</param\s*>)?|<blockquote>|gi;
!     $$str =~ s|</excerpt\s*>|</blockquote>|gi;
! 
!     $$str =~ s|<lang\s*>\s*<param\s*>([^<]*)</param\s*>|<div lang="$1">|gi;
!     $$str =~ s|</lang\s*>|</div>|gi;
! 
!     # richtext commands
!     $$str =~ s{<(/?)subscript\s*>}{<$1sub>}gi;
!     $$str =~ s{<(/?)superscript\s*>}{<$1sup>}gi;
!     $$str =~ s{<lt\s*>}{&lt;}gi;
!     $$str =~ s{<np\s*>}{\f}gi;
!     $$str =~ s{<paragraph\s*>}{<p>}gi;
!     $$str =~ s{</paragraph\s*>\n?}{</p>}gis;
!     $$str =~ s{<indent\s*>}{<p style="margin-left: 1em;">}gi;
!     $$str =~ s{</indent\s*>}{</p>}gi;
!     $$str =~ s{<indentright\s*>}{<p style="margin-right: 1em;">}gi;
!     $$str =~ s{</indentright\s*>}{</p>}gi;
! 
!     if ($richtext) {
! 	$$str =~ s{<nl\s*>\n?}{<br>}gis;
!     } else {
! 	$$str =~ s{<nl\s*>}{}gis;
!     }
  
!     # Cleanup bad tags
!     $$str =~ s{</?(?:para(?:m|indent)|excerpt|lang|color|fontfamily)\s*>}{}g;
  }
  

---------------------------------------------------------------------
To sign-off this list, send email to majordomo(_at_)mhonarc(_dot_)org with the
message text UNSUBSCRIBE MHONARC-DEV