source: trunk/adm/extract_rst.sh @ 164

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

no more direct usage of docutils

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