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