source: trunk/adm/extract_rst.sh @ 75

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

add manuals productions using sphinx

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