| 1 | #!/bin/bash |
|---|
| 2 | |
|---|
| 3 | ## ================================================================ |
|---|
| 4 | ## Logtalk - Open source object-oriented logic programming language |
|---|
| 5 | ## Release 2.31.1 |
|---|
| 6 | ## |
|---|
| 7 | ## Copyright (c) 1998-2008 Paulo Moura. All Rights Reserved. |
|---|
| 8 | ## ================================================================ |
|---|
| 9 | |
|---|
| 10 | if ! [ "$LOGTALKHOME" ]; then |
|---|
| 11 | echo "The environment variable LOGTALKHOME should be defined first, pointing" |
|---|
| 12 | echo "to your Logtalk installation directory!" |
|---|
| 13 | echo "Trying the default locations for the Logtalk installation..." |
|---|
| 14 | if [ -d "/usr/local/share/logtalk" ]; then |
|---|
| 15 | LOGTALKHOME=/usr/local/share/logtalk |
|---|
| 16 | echo "... using Logtalk installation found at /usr/local/share/logtalk" |
|---|
| 17 | elif [ -d "/usr/share/logtalk" ]; then |
|---|
| 18 | LOGTALKHOME=/usr/share/logtalk |
|---|
| 19 | echo "... using Logtalk installation found at /usr/share/logtalk" |
|---|
| 20 | elif [ -d "/opt/local/share/logtalk" ]; then |
|---|
| 21 | LOGTALKHOME=/opt/local/share/logtalk |
|---|
| 22 | echo "... using Logtalk installation found at /opt/local/share/logtalk" |
|---|
| 23 | elif [ -d "/opt/share/logtalk" ]; then |
|---|
| 24 | LOGTALKHOME=/opt/share/logtalk |
|---|
| 25 | echo "... using Logtalk installation found at /opt/share/logtalk" |
|---|
| 26 | else |
|---|
| 27 | echo "... unable to locate Logtalk installation directory!" |
|---|
| 28 | echo |
|---|
| 29 | exit 1 |
|---|
| 30 | fi |
|---|
| 31 | echo |
|---|
| 32 | elif ! [ -d "$LOGTALKHOME" ]; then |
|---|
| 33 | echo "The environment variable LOGTALKHOME points to a non-existing directory!" |
|---|
| 34 | echo "Its current value is: $LOGTALKHOME" |
|---|
| 35 | echo "The variable must be set to your Logtalk installation directory!" |
|---|
| 36 | echo |
|---|
| 37 | exit 1 |
|---|
| 38 | fi |
|---|
| 39 | export LOGTALKHOME |
|---|
| 40 | |
|---|
| 41 | if ! [ "$LOGTALKUSER" ]; then |
|---|
| 42 | echo "The environment variable LOGTALKUSER should be defined first, pointing" |
|---|
| 43 | echo "to your Logtalk user directory!" |
|---|
| 44 | echo "Trying the default location for the Logtalk user directory..." |
|---|
| 45 | export LOGTALKUSER=$HOME/logtalk |
|---|
| 46 | if [ -d "$LOGTALKUSER" ]; then |
|---|
| 47 | echo "... using Logtalk user directory found at $LOGTALKUSER" |
|---|
| 48 | else |
|---|
| 49 | echo "... Logtalk user directory not found at default location. Creating a" |
|---|
| 50 | echo "new Logtalk user directory by running the \"cplgtdirs\" shell script:" |
|---|
| 51 | cplgtdirs |
|---|
| 52 | fi |
|---|
| 53 | elif ! [ -d "$LOGTALKUSER" ]; then |
|---|
| 54 | echo "Cannot find \$LOGTALKUSER directory! Creating a new Logtalk user directory" |
|---|
| 55 | echo "by running the \"cplgtdirs\" shell script:" |
|---|
| 56 | cplgtdirs |
|---|
| 57 | fi |
|---|
| 58 | echo |
|---|
| 59 | |
|---|
| 60 | html_xslt="$LOGTALKUSER/xml/lgthtml.xsl" |
|---|
| 61 | xhtml_xslt="$LOGTALKUSER/xml/lgtxhtml.xsl" |
|---|
| 62 | |
|---|
| 63 | format=xhtml |
|---|
| 64 | |
|---|
| 65 | directory="." |
|---|
| 66 | |
|---|
| 67 | index_file=index.html |
|---|
| 68 | index_title="Entity documentation index" |
|---|
| 69 | |
|---|
| 70 | processor=xsltproc |
|---|
| 71 | # processor=xalan |
|---|
| 72 | # processor=sabcmd |
|---|
| 73 | |
|---|
| 74 | usage_help() |
|---|
| 75 | { |
|---|
| 76 | echo |
|---|
| 77 | echo "This script converts all Logtalk XML files documenting files in the" |
|---|
| 78 | echo "current directory to XHTML or HTML files" |
|---|
| 79 | echo |
|---|
| 80 | echo "Usage:" |
|---|
| 81 | echo " $0 -f format -d directory -i index -t title -p processor" |
|---|
| 82 | echo " $0 -h" |
|---|
| 83 | echo |
|---|
| 84 | echo "Optional arguments:" |
|---|
| 85 | echo " -f output file format (either xhtml or html; default is $format)" |
|---|
| 86 | echo " -d output directory for the generated files (default is $directory)" |
|---|
| 87 | echo " -i name of the index file (default is $index_file)" |
|---|
| 88 | echo " -t title to be used on the index file (default is $index_title)" |
|---|
| 89 | echo " -p XSLT processor (xsltproc, xalan, or sabcmd; default is $processor)" |
|---|
| 90 | echo " -h help" |
|---|
| 91 | echo |
|---|
| 92 | exit 1 |
|---|
| 93 | } |
|---|
| 94 | |
|---|
| 95 | create_index_file() |
|---|
| 96 | { |
|---|
| 97 | echo "" > "$index_file" |
|---|
| 98 | |
|---|
| 99 | case "$format" in |
|---|
| 100 | xhtml) |
|---|
| 101 | echo "<?xml version=\"1.0\" encoding=\"utf-8\"?>" >> "$index_file" |
|---|
| 102 | echo "<?xml-stylesheet href=\"logtalk.css\" type=\"text/css\"?>" >> "$index_file" |
|---|
| 103 | echo "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Strict//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd\">" >> "$index_file" |
|---|
| 104 | echo "<html lang=\"en\" xml:lang=\"en\" xmlns=\"http://www.w3.org/1999/xhtml\">" >> "$index_file" |
|---|
| 105 | ;; |
|---|
| 106 | html) |
|---|
| 107 | echo "<!DOCTYPE html PUBLIC \"-//W3C//DTD HTML 4.01//EN\" \"http://www.w3.org/TR/html4/strict.dtd\">" >> "$index_file" |
|---|
| 108 | echo "<html>" >> "$index_file" |
|---|
| 109 | ;; |
|---|
| 110 | esac |
|---|
| 111 | |
|---|
| 112 | echo "<head>" >> "$index_file" |
|---|
| 113 | echo " <meta http-equiv=\"content-type\" content=\"text/html; charset=utf-8\"/>" >> "$index_file" |
|---|
| 114 | echo " <title>"$index_title"</title>" >> "$index_file" |
|---|
| 115 | echo " <link rel=\"stylesheet\" href=\"logtalk.css\" type=\"text/css\"/>" >> "$index_file" |
|---|
| 116 | echo "</head>" >> "$index_file" |
|---|
| 117 | echo "<body>" >> "$index_file" |
|---|
| 118 | echo "<h1>"$index_title"</h1>" >> "$index_file" |
|---|
| 119 | echo "<ul>" >> "$index_file" |
|---|
| 120 | |
|---|
| 121 | for file in *.xml; do |
|---|
| 122 | name="`expr "$file" : '\(.*\)\.[^./]*$' \| "$file"`" |
|---|
| 123 | entity=${name%_*} |
|---|
| 124 | pars=${name##*_} |
|---|
| 125 | echo " indexing $name.html" |
|---|
| 126 | if [ $pars -gt 0 ] |
|---|
| 127 | then |
|---|
| 128 | echo " <li><a href=\""$name".html\">"$entity"/"$pars"</a></li>" >> "$index_file" |
|---|
| 129 | else |
|---|
| 130 | echo " <li><a href=\""$name".html\">"$entity"</a></li>" >> "$index_file" |
|---|
| 131 | fi |
|---|
| 132 | done |
|---|
| 133 | |
|---|
| 134 | echo "</ul>" >> "$index_file" |
|---|
| 135 | |
|---|
| 136 | date="`eval date`" |
|---|
| 137 | |
|---|
| 138 | echo "<p>Generated on "$date"</p>" >> "$index_file" |
|---|
| 139 | echo "</body>" >> "$index_file" |
|---|
| 140 | echo "</html>" >> "$index_file" |
|---|
| 141 | } |
|---|
| 142 | |
|---|
| 143 | while getopts "f:d:i:t:p:h" Option |
|---|
| 144 | do |
|---|
| 145 | case $Option in |
|---|
| 146 | f) f_arg="$OPTARG";; |
|---|
| 147 | d) d_arg="$OPTARG";; |
|---|
| 148 | i) i_arg="$OPTARG";; |
|---|
| 149 | t) t_arg="$OPTARG";; |
|---|
| 150 | p) p_arg="$OPTARG";; |
|---|
| 151 | h) usage_help;; |
|---|
| 152 | *) usage_help;; |
|---|
| 153 | esac |
|---|
| 154 | done |
|---|
| 155 | |
|---|
| 156 | if [[ "$f_arg" != "" && "$f_arg" != "xhtml" && "$f_arg" != "html" ]] |
|---|
| 157 | then |
|---|
| 158 | echo "Error! Unsupported output format: $f_arg" |
|---|
| 159 | usage_help |
|---|
| 160 | exit 1 |
|---|
| 161 | elif [ "$f_arg" != "" ] |
|---|
| 162 | then |
|---|
| 163 | format=$f_arg |
|---|
| 164 | fi |
|---|
| 165 | |
|---|
| 166 | if [[ "$d_arg" != "" && ! -d "$d_arg" ]] |
|---|
| 167 | then |
|---|
| 168 | echo "Error! directory does not exists: $d_arg" |
|---|
| 169 | usage_help |
|---|
| 170 | exit 1 |
|---|
| 171 | elif [ "$d_arg" != "" ] |
|---|
| 172 | then |
|---|
| 173 | directory=$d_arg |
|---|
| 174 | fi |
|---|
| 175 | |
|---|
| 176 | if [[ "$i_arg" != "" ]] |
|---|
| 177 | then |
|---|
| 178 | index_file=$i_arg |
|---|
| 179 | fi |
|---|
| 180 | |
|---|
| 181 | if [[ "$t_arg" != "" ]] |
|---|
| 182 | then |
|---|
| 183 | index_title=$t_arg |
|---|
| 184 | fi |
|---|
| 185 | |
|---|
| 186 | if [[ "$p_arg" != "" && "$p_arg" != "xsltproc" && "$p_arg" != "xalan" && "$p_arg" != "sabcmd" ]] |
|---|
| 187 | then |
|---|
| 188 | echo "Error! Unsupported XSLT processor: $p_arg" |
|---|
| 189 | usage_help |
|---|
| 190 | exit 1 |
|---|
| 191 | elif [ "$p_arg" != "" ] |
|---|
| 192 | then |
|---|
| 193 | processor=$p_arg |
|---|
| 194 | fi |
|---|
| 195 | |
|---|
| 196 | if [ "$format" = "xhtml" ] |
|---|
| 197 | then |
|---|
| 198 | xslt=$xhtml_xslt |
|---|
| 199 | else |
|---|
| 200 | xslt=$html_xslt |
|---|
| 201 | fi |
|---|
| 202 | |
|---|
| 203 | if ! [[ -a "./logtalk.dtd" ]] |
|---|
| 204 | then |
|---|
| 205 | cp "$LOGTALKHOME"/xml/logtalk.dtd . |
|---|
| 206 | fi |
|---|
| 207 | |
|---|
| 208 | if ! [[ -a "./custom.ent" ]] |
|---|
| 209 | then |
|---|
| 210 | cp "$LOGTALKUSER"/xml/custom.ent . |
|---|
| 211 | fi |
|---|
| 212 | |
|---|
| 213 | if ! [[ -a "./logtalk.xsd" ]] |
|---|
| 214 | then |
|---|
| 215 | cp "$LOGTALKHOME"/xml/logtalk.xsd . |
|---|
| 216 | fi |
|---|
| 217 | |
|---|
| 218 | if ! [[ -a "$directory/logtalk.css" ]] |
|---|
| 219 | then |
|---|
| 220 | cp "$LOGTALKUSER"/xml/logtalk.css "$directory" |
|---|
| 221 | fi |
|---|
| 222 | |
|---|
| 223 | if [[ `(ls *.xml | wc -l) 2> /dev/null` -gt 0 ]] |
|---|
| 224 | then |
|---|
| 225 | echo |
|---|
| 226 | echo "converting XML files..." |
|---|
| 227 | for file in *.xml; do |
|---|
| 228 | echo " converting $file" |
|---|
| 229 | name="`expr "$file" : '\(.*\)\.[^./]*$' \| "$file"`" |
|---|
| 230 | case "$processor" in |
|---|
| 231 | xsltproc) eval xsltproc -o \"$directory\"/\"$name.html\" \"$xslt\" \"$file\";; |
|---|
| 232 | xalan) eval xalan -o \"$directory\"/\"$name.html\" \"$file\" \"$xslt\";; |
|---|
| 233 | sabcmd) eval sabcmd \"$xslt\" \"$file\" \"$directory\"/\"$name.html\";; |
|---|
| 234 | esac |
|---|
| 235 | done |
|---|
| 236 | echo "conversion done" |
|---|
| 237 | echo |
|---|
| 238 | echo "generating index file..." |
|---|
| 239 | index_file="$directory/$index_file" |
|---|
| 240 | create_index_file |
|---|
| 241 | echo "index file generated" |
|---|
| 242 | echo |
|---|
| 243 | else |
|---|
| 244 | echo |
|---|
| 245 | echo "No XML files exist in the current directory!" |
|---|
| 246 | echo |
|---|
| 247 | fi |
|---|
| 248 | |
|---|
| 249 | exit 0 |
|---|