source: trunk/adm/extract_rst.sh @ 83

Last change on this file since 83 was 83, checked in by pinsard, 14 years ago

no more direct usage of docutils

  • Property svn:executable set to *
File size: 6.5 KB
Line 
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#
117# FILES
118# =====
119#
120# /usr/home/fplod/incas/varamma/varamma_ws/adm/extract_rst.sh sur aedon.locean-ipsl.upmc.fr
121#
122# EVOLUTIONS
123# ==========
124#
125# $Id$
126#
127# - fplod 2009-04-20T08:13:37Z aedon.locean-ipsl.upmc.fr (Darwin)
128#
129#   * add CAUTIONS paragraph to warn about possible FORTRAN compiling problem
130#
131#
132# - fplod 2009-04-03T14:53:18Z aedon.locean-ipsl.upmc.fr (Darwin)
133#
134#   * usage of tr instead of sed to remove ``\r``
135#     due to difference between ``/sw/bin/sed`` and ``/usr/bin/sed`` (the last
136#     one do not work coorectly on ``\r`` interpertation ie: remove the first occurence of
137#     ``r``)
138#
139# - fplod 2009-02-10T10:46:23Z aedon.locean-ipsl.upmc.fr (Darwin)
140#
141#   * add language fortran for FORTRAN source in fixed form
142#
143# - fplod 2009-01-05T11:41:33Z aedon.locean-ipsl.upmc.fr (Darwin)
144#
145#   * remove \\r (CRLF) from file before awk and sed (otherwise ReST block
146#     was not found in "ISO-8859 text, with CRLF line terminators" files
147#
148# - fplod 2008-12-22T10:37:37Z aedon.locean-ipsl.upmc.fr (Darwin)
149#
150#   * add matlab (octave)
151#
152# - fplod 2008-09-17T13:40:37Z aedon.locean-ipsl.upmc.fr (Darwin)
153#
154#   * add php language
155#
156# - fplod 2008-09-08T09:34:04Z aedon.locean-ipsl.upmc.fr (Darwin)
157#
158#   * add F90 language
159#
160# - fplod 2008-08-08T08:28:30Z aedon.locean-ipsl.upmc.fr (Darwin)
161#
162#   * add KML files (with other XML files)
163#   * add parameters ``-i`` ``-l`` ``-o``
164#
165# - fplod 200807
166#
167#   * creation
168#
169#-
170#
171system=$(uname)
172case "${system}" in
173   AIX|IRIX64)
174      echo " www : no specific posix checking"
175   ;;
176   *)
177      set -o posix
178   ;;
179esac
180#
181command=$(basename ${0})
182log_date=$(date -u +"%Y%m%dT%H%M%SZ")
183log=/tmp/$(basename ${command} .sh).log.${log_date}
184#
185usage=" Usage : ${command} -i filein -l language -o fileout"
186#
187minargcount=6
188#echo " narg ${#}"
189if [ ${#} -lt ${minargcount} ]
190then
191   echo "eee : not enought arguments"
192   echo "${usage}"
193   exit 1
194fi
195#
196# default
197# n.a.
198#
199set +u
200while [ ! -z "${1}" ]
201do
202   case ${1} in
203      -i)
204         filein=${2}
205         shift
206      ;;
207      -o)
208         fileout=${2}
209         shift
210      ;;
211      -l)
212         language=${2}
213         shift
214      ;;
215      -h)
216         echo "${usage}"
217         exit 0
218      ;;
219      *)
220         echo "eee : unknown option ${1}"
221         echo "${usage}"
222         exit 1
223      ;;
224   esac
225   # next flag
226   shift
227done
228#
229set -u
230#
231# ++ check param
232#
233case "${language}" in
234   fortran)
235      awkblockstart="^C\+$"
236      awkblockend="^C-$"
237      sedblockstart="^C+$"
238      sedblockend="^C-$"
239      comment="^C"
240   ;;
241   F90)
242      awkblockstart="^!\+$"
243      awkblockend="^!-$"
244      sedblockstart="^!+$"
245      sedblockend="^!-$"
246      comment="^!"
247   ;;
248   IDL)
249      awkblockstart="^;\+$"
250      awkblockend="^;-$"
251      sedblockstart="^;+$"
252      sedblockend="^;-$"
253      comment="^;"
254   ;;
255   xml)
256      awkblockstart="^<!--rst$"
257      awkblockend="-->$"
258      sedblockstart="^<!--rst$"
259      sedblockend="-->$"
260      comment=""
261   ;;
262   sh)
263      # iii : awk '/^\#\+/,/^\#\-/' $file
264      awkblockstart="^\#\+$"
265      awkblockend="^\#\-$"
266      sedblockstart="^#+"
267      sedblockend="^#-"
268      comment="^#"
269   ;;
270   dot|php)
271      awkblockstart="^\/\*rst$"
272      awkblockend="*\/"
273      sedblockstart="^\/\*rst$"
274      sedblockend="^\*\/"
275      comment=""
276   ;;
277   matlab)
278      awkblockstart="^%\+$"
279      awkblockend="^%-$"
280      sedblockstart="^%+$"
281      sedblockend="^%-$"
282      comment="^%"
283   ;;
284   *)
285      echo "eee : ${language} not implemented"
286      exit 1
287   ;;
288esac
289#
290# just in case suppress \r at the end of lines
291tr -d '\r' < ${filein} > /tmp/${$}_0
292#
293# put rst blocks in one temporary file
294#awk '/^;+/,/^;-/' a.pro | sed -e "/^;+$/d" -e "/^;-$/d" -e "s/^;//"
295cmdawk="awk '/${awkblockstart}/,/${awkblockend}/' /tmp/${$}_0 > /tmp/${$}_1" #++
296eval ${cmdawk}
297if [ ! -s /tmp/${$}_1 ]
298then
299   rm /tmp/${$}_0 /tmp/${$}_1
300   echo "iii : no rst comments in ${filein}"
301   exit 1
302fi
303#
304# suppress begin and end of each block
305sedcmd="sed -e \"/${sedblockstart}/d\" -e \"/${sedblockend}/d\" /tmp/${$}_1 > /tmp/${$}_2"
306eval ${sedcmd}
307#
308# suppress comment at the beginning of each line
309if [ "${comment}" != "" ]
310then
311   sedcmd="sed -e \"s/${comment}//\" /tmp/${$}_2 > /tmp/${$}_3"
312   eval ${sedcmd}
313   # suppress first blank
314   cp /tmp/${$}_3 /tmp/${$}_2
315   sed -e "s/^ //" /tmp/${$}_2 > /tmp/${$}_3
316   cp /tmp/${$}_3 ${fileout}
317else
318   cp /tmp/${$}_2 ${fileout}
319fi
320#
321echo "iii : rst lines of ${filein} are in ${fileout}"
322#
323# clean
324rm /tmp/${$}_0 /tmp/${$}_1 /tmp/${$}_2 /tmp/${$}_3 2> /dev/null
325#
326# exit
327exit 0
Note: See TracBrowser for help on using the repository browser.