xhtmltools-devel
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Xhtmltools-devel] Changes to xhtmltools/bin/xhtml2html


From: Jaime E. Villate
Subject: [Xhtmltools-devel] Changes to xhtmltools/bin/xhtml2html
Date: Fri, 29 Mar 2002 06:32:10 -0500

Index: xhtmltools/bin/xhtml2html
diff -c xhtmltools/bin/xhtml2html:1.2 xhtmltools/bin/xhtml2html:1.3
*** xhtmltools/bin/xhtml2html:1.2       Wed Feb 20 16:26:58 2002
--- xhtmltools/bin/xhtml2html   Fri Mar 29 06:32:10 2002
***************
*** 1,9 ****
! #! /bin/sh
! # $Id: xhtml2html,v 1.2 2002/02/20 21:26:58 villate Exp $
  #
! # xhtml2html - creates an html file from an XHTML file
  #
! # Copyright (C) 2001, 2002, Jaime Villate <address@hidden>
  #
  # This program is part of xhtmltools
  #
--- 1,9 ----
! #! /usr/bin/perl
! # $Id: xhtml2html,v 1.3 2002/03/29 11:32:10 villate Exp $
  #
! # xhtml2html - creates an HTML file from an XML source, using an XSLT file.
  #
! # Copyright (C) 2001, 2002 Jaime Villate <address@hidden>
  #
  # This program is part of xhtmltools
  #
***************
*** 21,97 ****
  # along with this program; if not, write to the Free Software
  # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
  
  
! # edit the following line with the directory where the default XSL pages are
! XSLTHTML='/usr/share/sgml/xhtmltools/xhtml2html.xsl'
! TMP=XHTML2HTML-TEMP-$$
! PARSER='xsltproc -nonet $XSLT $XHTML'
! CONFFILE=~/.xhtmltools-conf
! 
! usage() {
!   echo "Usage:" 1>&2
!   echo "  `basename $0` [-s stylesheet] [-c config] xhtmlfile" 1>&2
!   echo " " 1>&2
!   exit 1
! }
! 
! [[ $# -lt 1 ]] && usage
! 
! while [[ $# -gt 0 ]]
! do
!   case $1 in
!     -s) STYLESHEET=$2
!         shift
!       ;;
!     -h|--help)
!         usage
!         ;;
!     -c) CONFFILE=$2
!         shift
!       ;;
!     -*)
!         usage
!         ;;
!     *)
!       XHTMLFILE=$1
!   esac
!   shift
! done
! 
! [[ -n $XHTMLFILE ]] || usage
! 
! [[ -r $CONFFILE ]] && source $CONFFILE
! 
! XSLTDIR=$(dirname $XSLTHTML)
! 
! if [[ -n $STYLESHEET ]]
! then
!   if [[ -r $STYLESHEET ]]
!   then
!     XSLT=$STYLESHEET
!   elif [[ -r $XSLTDIR/$STYLESHEET ]]
!     then
!       XSLT=$XSLTDIR/$STYLESHEET
!     else
!       XSLT=$STYLESHEET
!   fi
! else
!  XSLT=$XSLTHTML
! fi
! 
! if [[ ! -r $XSLT ]]
! then
!   echo Cannot find stylesheet \"$XSLT\" >&2
!   exit 1
! fi
! if [[ ! -r $XHTMLFILE ]]
! then
!   echo Cannot read XHTML file \"$1\" >&2
!   exit 1
! fi
! 
! perl -pe '$| = 1; s/mode: xml \*\*\*/mode: html \*\*\*/' $XHTMLFILE > $TMP
! XHTML=$TMP
! eval $PARSER || (rm -f $TMP; exit 1)
! rm -f $TMP
! exit 0
--- 21,119 ----
  # along with this program; if not, write to the Free Software
  # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
  
+ # Default configuration
+ my $tmp="XHTML2HTML-TEMP-$$";
+ my $conffile="$ENV{HOME}/.xhtmltools";
+ my $stylesheet;
+ my $config = '';
+ 
+ # parse command line options
+ while ($_ = $ARGV[0], /^-/)
+ {
+     shift;
+     if (/^-s/) { $stylesheet = shift }
+     if (/^(-h|--help)/) { &usage }
+     if (/^-c/) { $conffile = shift }
+ }
+ unshift(@ARGV, '-') unless @ARGV;
+ &usage if ($#ARGV > 0);
+ my $xmlfile = shift;
+ $xmlfile = '-' unless $xmlfile;
+ 
+ # process configuration file. It will be stored in $config an all
+ # newline characters will be removed to allow multiline regexp matching.
+ if (-r $conffile)
+ {
+     open (CNF, "<$conffile");
+     undef $/;
+     $config = <CNF>;
+     close CNF;
+ } else {
+     print STDERR "$0:\n\tConfiguration file $conffile cannot be read.\n";
+     print STDERR "\tYou should prepare a configuration file and\n";
+     print STDERR "\tif it's not in the default location ~/.xhtmltools,\n";
+     print STDERR "\tuse option -c to specify its location.\n\n";
+     exit 1;
+ }
+ $config =~ s/\n/ /g;
+ my $parser = &getnode('xsltparser',\$config);
+ $stylesheet = &getnode('htmlstylesheet',\$config) unless $stylesheet;
+ 
+ # Check whether stylesheet is readable
+ unless (-r $stylesheet)
+ {
+     print STDERR "$0:\n\tCannot read stylesheet $stylesheet\n\n";
+     exit 1;
+ }
+ 
+ # Check whether input file is readable
+ unless (-r $xmlfile || $xmlfile eq '-')
+ {
+     print STDERR "$0:\n\tCannot read file $xmlfile\n\n";
+     exit 1;
+ }
  
! # Create temporary file
! open (TMP,">$tmp") or die "$0:\n\tCannot create temporary file\n\n";
! open (IN,"<$xmlfile") or die "$0:\n\tCannot read the input file\n\n" ;
! $/ = "\n";
! while (<IN>)
! {
!     s/mode: xml \*\*\*/mode: html \*\*\*/;
!     print TMP;
! }
! close IN;
! close TMP;
! 
! # prepare xslt parser command-line
! my $XSLT = $stylesheet;
! my $XHTML = $tmp;
! my $xsltparser = eval "qq{$parser}";
! 
! # parse the XHTML file
! system $xsltparser || (system("rm -f $tmp"), exit 1);
! system("rm -f $tmp"); 
! 
! exit 0;
! 
! # Function to display a program usage summary
! sub usage
! {
!     print STDERR "Usage:\n";
!     print STDERR "$0 [-s stylesheet] [-c config] [xmlfile]\n\n";
!     exit 1;
! }
! 
! # Function to extract the contents of a given element in a given XML
! # document (that should not have newline characters.
! sub getnode
! {
!     my $element = shift;
!     my $config = shift;
!     $$config =~ /<$element>(.*)<\/$element>/;
!     my $node = $1;
!     $node =~ s/<[^>]*>//g;
!     $node =~ s/^\s*//;
!     $node =~ s/\s*$//;
!     return $node;
! }



reply via email to

[Prev in Thread] Current Thread [Next in Thread]