scripts/vym2txt.sh
author insilmaril
Wed, 15 Mar 2006 13:53:53 +0000
changeset 250 0e994bf2346b
parent 36 67e7883ab157
permissions -rwxr-xr-x
hide export for floatimages.
     1 #!/bin/sh
     2 #
     3 # vym2txt.sh
     4 #
     5 VERSION="0.11"
     6 # Date: 20040417
     7 # Author: Clemens Kraus (http://www.clemens-kraus.de)
     8 #
     9 #echo $@
    10 
    11 
    12 unpacker()
    13 # Unpack vym-file, only if it is one
    14 {
    15   echo $VYMFILE_EXT | grep -F ".vym" 1>/dev/null
    16 
    17   if [ $? = 0 ] ; then
    18 	  echo ">> Unpacking files ..."
    19 	  unzip $VYMFILE_EXT -d $VYMFILE_PATH 1>/dev/null
    20 	  if [ $? -gt 0 ] ; then
    21 		echo ">>> Error in unzip! Aborting."
    22 	  	exit 4
    23 	  fi
    24   fi
    25 }
    26 
    27 
    28 txt2xml()
    29 # change all txt-files into xml-format
    30 {
    31   for i in `ls $VYMFILE-note-*.txt 2>/dev/null`
    32   do
    33     # Check whether already modified
    34 	grep "<note>" $i 1>/dev/null
    35 	
    36 	if [ $? -gt 0 ] ; then
    37 	  echo ">> Modifying: "$i
    38 	  # Each line gets an additional <line>-tag, because of the indents!
    39 	  sed -e 's,^,<line><![CDATA[,g' -e 's,$,]]>\&#xA;</line>,g' $i > $i"_tmp"
    40 	  
    41 	  #cp $i $i"_tmp"
    42 	  echo "<?xml version=\"1.0\" encoding=\"iso-8859-1\"?>" > $i
    43 	  echo "<note>" >> $i
    44 	  #echo "<![CDATA[" >> $i
    45 	  cat $i"_tmp" >> $i
    46 	  #echo "]]>" >> $i
    47 	  echo "</note>" >> $i
    48   
    49 	  rm $i"_tmp"
    50 	fi
    51   done
    52 }
    53 
    54 
    55 transform()
    56 {
    57   echo ">> Starting XSLT transformation ..."
    58 # sabcmd vym2html.xsl $VYMFILE".xml"  \$filenamep=$VYMFILE \$wikistylep=$WIKISTYLEP \$genimagep=$GENIMAGEP \$stylesheetp=$STYLESHEETP > $VYMFILE".html"
    59   xsltproc -o $VYMFILE".txt" --stringparam filenamep `pwd`/"$VYMFILE" `dirname $STYLESHEETP`/vym2txt.xsl  $VYMFILE".xml"
    60   
    61   if [ $? -gt 0 ] ; then
    62 	  echo ">>> Error in xsltproc! Aborting."
    63 	  exit 3
    64   fi
    65 }
    66 
    67 
    68 remove_files()
    69 # remove all temporary unpacked vym-files
    70 {
    71   echo $VYMFILE_EXT | grep -F ".vym" 1>/dev/null
    72 	
    73   if [ $? = 0 ] ; then
    74 	echo ">> Removing temporary files ..."
    75 	for i in `ls $VYMFILE-note-*.txt 2>/dev/null`
    76 	do
    77 	  rm $i
    78 	done
    79   
    80 	for i in `ls $VYMFILE-image-*.* 2>/dev/null`
    81 	do
    82 	  rm $i
    83 	done
    84   
    85 	rm $VYMFILE".xml" 2>/dev/null
    86   fi
    87 }
    88 
    89 # -------------------- Parameter check -----------------------
    90 STYLESHEETP=""
    91 
    92 USAGE="USAGE:\t`basename $0`  vymfile.[vym|xml]  -sp=\077  [Options]\n"
    93 USAGE=$USAGE"\t-sp=\077: absolute stylesheet path (including name of stylesheet)\n"
    94 USAGE=$USAGE"Output:\tvymfile.txt\n\n"
    95 USAGE=$USAGE"Options:\n"
    96 USAGE=$USAGE"-v: prints the version of vym2txt\n"
    97 
    98 if [ "$1" = '-v' ]; then
    99 	  echo "vym2txt Version: "$VERSION
   100 	  exit 0
   101 fi
   102 if [ $# -lt 1 -o $# -gt 2 -o "$1" = '-help' ]; then 
   103 	echo -e $USAGE
   104 	exit 1
   105 else
   106 	VYMFILE_EXT=$1
   107 	VYMFILE=`echo $VYMFILE_EXT | cut -d. -f1`
   108 	VYMFILE_PATH=`dirname $VYMFILE_EXT`
   109 fi
   110 
   111 for arg in $2
   112 do
   113   if [ ${arg:0:3} = '-sp' ]; then		# take first 3 chars
   114 	  STYLESHEETP=`echo $arg | cut -d= -f2`
   115   elif [ "$arg" = '-help' ]; then
   116 	echo -e $USAGE
   117 	exit 1
   118   else
   119 	echo -e $USAGE
   120 	exit 1
   121   fi
   122 done
   123 
   124 
   125 # ---------------------- Los geht's --------------------------
   126 echo ">> Processing file '$VYMFILE_EXT' ..."
   127 
   128 # Unpack vym-file
   129 unpacker
   130 
   131 txt2xml
   132 
   133 # Transform
   134 transform
   135 
   136 # clean up
   137 remove_files
   138 
   139 echo ">> Ready!"
   140 echo ">> ---------------------"
   141 
   142 exit 0
   143 
   144