[2] | 1 | #! /bin/sh |
---|
| 2 | #+ |
---|
| 3 | # |
---|
| 4 | # .. extract.sh: |
---|
| 5 | # |
---|
| 6 | # ========== |
---|
| 7 | # extract.sh |
---|
| 8 | # ========== |
---|
| 9 | # |
---|
| 10 | # ----------------------------------------- |
---|
| 11 | # extract ReStructuredText from source file |
---|
| 12 | # ----------------------------------------- |
---|
| 13 | # |
---|
| 14 | # SYNOPSIS |
---|
| 15 | # ======== |
---|
| 16 | # |
---|
| 17 | # ``extract_rst.sh -i filein -l language -o fileout`` |
---|
| 18 | # |
---|
| 19 | # DESCRIPTION |
---|
| 20 | # =========== |
---|
| 21 | # |
---|
| 22 | # ``extract_rst.sh`` extracts ReST comments from the file given in argument |
---|
| 23 | # |
---|
| 24 | # -i input file |
---|
| 25 | # -o output file (ReST) |
---|
| 26 | # -l language |
---|
| 27 | # |
---|
| 28 | # Comment block (start, end) identification depends on language : |
---|
| 29 | # |
---|
| 30 | # *F90* |
---|
| 31 | # FORTRAN source free form |
---|
| 32 | # *fortran* |
---|
| 33 | # FORTRAN source fixed form |
---|
| 34 | # *sh* |
---|
| 35 | # shell scripts |
---|
| 36 | # *IDL* |
---|
| 37 | # IDL source |
---|
| 38 | # *xml* |
---|
| 39 | # XML and XSL |
---|
| 40 | # *dot* |
---|
| 41 | # graphviz files |
---|
| 42 | # *php* |
---|
| 43 | # PHP files |
---|
| 44 | # *matlab* |
---|
| 45 | # matlab or octave files |
---|
| 46 | # |
---|
| 47 | # EXAMPLES |
---|
| 48 | # ======== |
---|
| 49 | # |
---|
| 50 | # To extract ReST comments of this shell script: |
---|
| 51 | # :: |
---|
| 52 | # |
---|
| 53 | # $ extract_rst.sh -i extract_rst.sh -l sh -o extract_rst.sh.rst |
---|
| 54 | # iii : rst lines of extract_rst.sh are in extract_rst.sh.rst |
---|
| 55 | # |
---|
| 56 | # |
---|
| 57 | # You can produce HTML file from this new file: |
---|
| 58 | # :: |
---|
| 59 | # |
---|
| 60 | # $ rst2html.py --input-encoding=ISO-8859-15 extract_rst.sh.rst \ |
---|
| 61 | # /usr/temp/${LOGNAME}/extract_rst.sh.html |
---|
| 62 | # |
---|
| 63 | # |
---|
| 64 | # You can produce PDF file from this new file: |
---|
| 65 | # :: |
---|
| 66 | # |
---|
| 67 | # $ rst2newlatex.py --input-encoding=ISO-8859-15 extract_rst.sh.rst \ |
---|
| 68 | # /usr/temp/${LOGNAME}/extract_rst.sh.tex |
---|
| 69 | # $ pdflatex extract_rst.sh.tex |
---|
| 70 | # |
---|
| 71 | # Of course beware of consistency of path on links. |
---|
| 72 | # |
---|
| 73 | # CAUTIONS |
---|
| 74 | # ======== |
---|
| 75 | # |
---|
| 76 | # Becaue of poor implementation of Standard FORTRAN in cpp (prepocessing) |
---|
| 77 | # within gfortran and g95, ReST comments might induce trouble in |
---|
| 78 | # FORTRAN sources. |
---|
| 79 | # |
---|
| 80 | # For example following line is pointed out be gfortran with |
---|
| 81 | # ``error: unterminated comment``. |
---|
| 82 | # This is because ``/*`` is the beginning of a C style comment !! |
---|
| 83 | # :: |
---|
| 84 | # |
---|
| 85 | # ! **MEAN** = sum( *X*\ (:) )/*ntime* |
---|
| 86 | # |
---|
| 87 | # |
---|
| 88 | # |
---|
| 89 | # One can modify this ReST line with |
---|
| 90 | # :: |
---|
| 91 | # |
---|
| 92 | # ! **MEAN** = sum( *X*\ (:) ) / *ntime* |
---|
| 93 | # |
---|
| 94 | # |
---|
| 95 | # |
---|
| 96 | # TODO |
---|
| 97 | # ==== |
---|
| 98 | # |
---|
| 99 | # check parameters |
---|
| 100 | # |
---|
| 101 | # log |
---|
| 102 | # |
---|
| 103 | # add perl |
---|
| 104 | # |
---|
| 105 | # SEE ALSO |
---|
| 106 | # ======== |
---|
| 107 | # |
---|
| 108 | # ReStructuredText_ |
---|
| 109 | # |
---|
| 110 | # .. _ReStructuredText: http://docutils.sourceforge.net/rst.html |
---|
| 111 | # |
---|
| 112 | # Docutils_ |
---|
| 113 | # |
---|
| 114 | # .. _Docutils: http://docutils.sourceforge.net/ |
---|
| 115 | # |
---|
| 116 | # EVOLUTIONS |
---|
| 117 | # ========== |
---|
| 118 | # |
---|
| 119 | # $Id$ |
---|
| 120 | # |
---|
| 121 | # - fplod 20100630T103629Z aedon.locean-ipsl.upmc.fr (Darwin) |
---|
| 122 | # |
---|
| 123 | # * add language ferret |
---|
| 124 | # |
---|
| 125 | # - fplod 2009-04-20T08:13:37Z aedon.locean-ipsl.upmc.fr (Darwin) |
---|
| 126 | # |
---|
| 127 | # * add CAUTIONS paragraph to warn about possible FORTRAN compiling problem |
---|
| 128 | # |
---|
| 129 | # |
---|
| 130 | # - fplod 2009-04-03T14:53:18Z aedon.locean-ipsl.upmc.fr (Darwin) |
---|
| 131 | # |
---|
| 132 | # * usage of tr instead of sed to remove ``\r`` |
---|
| 133 | # due to difference between ``/sw/bin/sed`` and ``/usr/bin/sed`` (the last |
---|
| 134 | # one do not work coorectly on ``\r`` interpertation ie: remove the first occurence of |
---|
| 135 | # ``r``) |
---|
| 136 | # |
---|
| 137 | # - fplod 2009-02-10T10:46:23Z aedon.locean-ipsl.upmc.fr (Darwin) |
---|
| 138 | # |
---|
| 139 | # * add language fortran for FORTRAN source in fixed form |
---|
| 140 | # |
---|
| 141 | # - fplod 2009-01-05T11:41:33Z aedon.locean-ipsl.upmc.fr (Darwin) |
---|
| 142 | # |
---|
| 143 | # * remove \\r (CRLF) from file before awk and sed (otherwise ReST block |
---|
| 144 | # was not found in "ISO-8859 text, with CRLF line terminators" files |
---|
| 145 | # |
---|
| 146 | # - fplod 2008-12-22T10:37:37Z aedon.locean-ipsl.upmc.fr (Darwin) |
---|
| 147 | # |
---|
| 148 | # * add matlab (octave) |
---|
| 149 | # |
---|
| 150 | # - fplod 2008-09-17T13:40:37Z aedon.locean-ipsl.upmc.fr (Darwin) |
---|
| 151 | # |
---|
| 152 | # * add php language |
---|
| 153 | # |
---|
| 154 | # - fplod 2008-09-08T09:34:04Z aedon.locean-ipsl.upmc.fr (Darwin) |
---|
| 155 | # |
---|
| 156 | # * add F90 language |
---|
| 157 | # |
---|
| 158 | # - fplod 2008-08-08T08:28:30Z aedon.locean-ipsl.upmc.fr (Darwin) |
---|
| 159 | # |
---|
| 160 | # * add KML files (with other XML files) |
---|
| 161 | # * add parameters ``-i`` ``-l`` ``-o`` |
---|
| 162 | # |
---|
| 163 | # - fplod 200807 |
---|
| 164 | # |
---|
| 165 | # * creation |
---|
| 166 | # |
---|
| 167 | #- |
---|
| 168 | # |
---|
| 169 | system=$(uname) |
---|
| 170 | case "${system}" in |
---|
| 171 | AIX|IRIX64) |
---|
| 172 | echo " www : no specific posix checking" |
---|
| 173 | ;; |
---|
| 174 | *) |
---|
| 175 | set -o posix |
---|
| 176 | ;; |
---|
| 177 | esac |
---|
| 178 | # |
---|
| 179 | command=$(basename ${0}) |
---|
| 180 | log_date=$(date -u +"%Y%m%dT%H%M%SZ") |
---|
| 181 | log=/tmp/$(basename ${command} .sh).log.${log_date} |
---|
| 182 | # |
---|
| 183 | usage=" Usage : ${command} -i filein -l language -o fileout" |
---|
| 184 | # |
---|
| 185 | minargcount=6 |
---|
| 186 | #echo " narg ${#}" |
---|
| 187 | if [ ${#} -lt ${minargcount} ] |
---|
| 188 | then |
---|
| 189 | echo "eee : not enought arguments" |
---|
| 190 | echo "${usage}" |
---|
| 191 | exit 1 |
---|
| 192 | fi |
---|
| 193 | # |
---|
| 194 | # default |
---|
| 195 | # n.a. |
---|
| 196 | # |
---|
| 197 | set +u |
---|
| 198 | while [ ! -z "${1}" ] |
---|
| 199 | do |
---|
| 200 | case ${1} in |
---|
| 201 | -i) |
---|
| 202 | filein=${2} |
---|
| 203 | shift |
---|
| 204 | ;; |
---|
| 205 | -o) |
---|
| 206 | fileout=${2} |
---|
| 207 | shift |
---|
| 208 | ;; |
---|
| 209 | -l) |
---|
| 210 | language=${2} |
---|
| 211 | shift |
---|
| 212 | ;; |
---|
| 213 | -h) |
---|
| 214 | echo "${usage}" |
---|
| 215 | exit 0 |
---|
| 216 | ;; |
---|
| 217 | *) |
---|
| 218 | echo "eee : unknown option ${1}" |
---|
| 219 | echo "${usage}" |
---|
| 220 | exit 1 |
---|
| 221 | ;; |
---|
| 222 | esac |
---|
| 223 | # next flag |
---|
| 224 | shift |
---|
| 225 | done |
---|
| 226 | # |
---|
| 227 | set -u |
---|
| 228 | # |
---|
| 229 | # ++ check param |
---|
| 230 | # |
---|
| 231 | case "${language}" in |
---|
| 232 | fortran) |
---|
| 233 | awkblockstart="^C\+$" |
---|
| 234 | awkblockend="^C-$" |
---|
| 235 | sedblockstart="^C+$" |
---|
| 236 | sedblockend="^C-$" |
---|
| 237 | comment="^C" |
---|
| 238 | ;; |
---|
| 239 | F90) |
---|
| 240 | awkblockstart="^!\+$" |
---|
| 241 | awkblockend="^!-$" |
---|
| 242 | sedblockstart="^!+$" |
---|
| 243 | sedblockend="^!-$" |
---|
| 244 | comment="^!" |
---|
| 245 | ;; |
---|
| 246 | ferret) |
---|
| 247 | awkblockstart="^!\+$" |
---|
| 248 | awkblockend="^!-$" |
---|
| 249 | sedblockstart="^!+$" |
---|
| 250 | sedblockend="^!-$" |
---|
| 251 | comment="^!" |
---|
| 252 | ;; |
---|
| 253 | IDL) |
---|
| 254 | awkblockstart="^;\+$" |
---|
| 255 | awkblockend="^;-$" |
---|
| 256 | sedblockstart="^;+$" |
---|
| 257 | sedblockend="^;-$" |
---|
| 258 | comment="^;" |
---|
| 259 | ;; |
---|
| 260 | xml) |
---|
| 261 | awkblockstart="^<!--rst$" |
---|
| 262 | awkblockend="-->$" |
---|
| 263 | sedblockstart="^<!--rst$" |
---|
| 264 | sedblockend="-->$" |
---|
| 265 | comment="" |
---|
| 266 | ;; |
---|
| 267 | sh) |
---|
| 268 | # iii : awk '/^\#\+/,/^\#\-/' $file |
---|
| 269 | awkblockstart="^\#\+$" |
---|
| 270 | awkblockend="^\#\-$" |
---|
| 271 | sedblockstart="^#+" |
---|
| 272 | sedblockend="^#-" |
---|
| 273 | comment="^#" |
---|
| 274 | ;; |
---|
| 275 | dot|php) |
---|
| 276 | awkblockstart="^\/\*rst$" |
---|
| 277 | awkblockend="*\/" |
---|
| 278 | sedblockstart="^\/\*rst$" |
---|
| 279 | sedblockend="^\*\/" |
---|
| 280 | comment="" |
---|
| 281 | ;; |
---|
| 282 | matlab) |
---|
| 283 | awkblockstart="^%\+$" |
---|
| 284 | awkblockend="^%-$" |
---|
| 285 | sedblockstart="^%+$" |
---|
| 286 | sedblockend="^%-$" |
---|
| 287 | comment="^%" |
---|
| 288 | ;; |
---|
| 289 | *) |
---|
| 290 | echo "eee : ${language} not implemented" |
---|
| 291 | exit 1 |
---|
| 292 | ;; |
---|
| 293 | esac |
---|
| 294 | # |
---|
| 295 | # just in case suppress \r at the end of lines |
---|
| 296 | tr -d '\r' < ${filein} > /tmp/${$}_0 |
---|
| 297 | # |
---|
| 298 | # put rst blocks in one temporary file |
---|
| 299 | #awk '/^;+/,/^;-/' a.pro | sed -e "/^;+$/d" -e "/^;-$/d" -e "s/^;//" |
---|
| 300 | cmdawk="awk '/${awkblockstart}/,/${awkblockend}/' /tmp/${$}_0 > /tmp/${$}_1" #++ |
---|
| 301 | eval ${cmdawk} |
---|
| 302 | if [ ! -s /tmp/${$}_1 ] |
---|
| 303 | then |
---|
| 304 | rm /tmp/${$}_0 /tmp/${$}_1 |
---|
| 305 | echo "iii : no rst comments in ${filein}" |
---|
| 306 | exit 1 |
---|
| 307 | fi |
---|
| 308 | # |
---|
| 309 | # suppress begin and end of each block |
---|
| 310 | sedcmd="sed -e \"/${sedblockstart}/d\" -e \"/${sedblockend}/d\" /tmp/${$}_1 > /tmp/${$}_2" |
---|
| 311 | eval ${sedcmd} |
---|
| 312 | # |
---|
| 313 | # suppress comment at the beginning of each line |
---|
| 314 | if [ "${comment}" != "" ] |
---|
| 315 | then |
---|
| 316 | sedcmd="sed -e \"s/${comment}//\" /tmp/${$}_2 > /tmp/${$}_3" |
---|
| 317 | eval ${sedcmd} |
---|
| 318 | # suppress first blank |
---|
| 319 | cp /tmp/${$}_3 /tmp/${$}_2 |
---|
| 320 | sed -e "s/^ //" /tmp/${$}_2 > /tmp/${$}_3 |
---|
| 321 | cp /tmp/${$}_3 ${fileout} |
---|
| 322 | else |
---|
| 323 | cp /tmp/${$}_2 ${fileout} |
---|
| 324 | fi |
---|
| 325 | # |
---|
| 326 | echo "iii : rst lines of ${filein} are in ${fileout}" |
---|
| 327 | # |
---|
| 328 | # clean |
---|
| 329 | rm /tmp/${$}_0 /tmp/${$}_1 /tmp/${$}_2 /tmp/${$}_3 2> /dev/null |
---|
| 330 | # |
---|
| 331 | # exit |
---|
| 332 | exit 0 |
---|