source: trunk/adm/extract_rst.sh @ 199

Last change on this file since 199 was 199, checked in by pinsard, 11 years ago

fix svn properties

  • Property svn:executable set to *
  • Property svn:keywords set to Id URL
File size: 6.6 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# 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#
169system=$(uname)
170case "${system}" in
171   AIX|IRIX64)
172      echo " www : no specific posix checking"
173   ;;
174   *)
175     set -o posix
176   ;;
177esac
178#
179set -u
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#
199while [ ${#} -gt 0 ]
200do
201   case ${1} in
202      -i)
203         filein=${2}
204         shift
205      ;;
206      -o)
207         fileout=${2}
208         shift
209      ;;
210      -l)
211         language=${2}
212         shift
213      ;;
214      -h)
215         echo "${usage}"
216         exit 0
217      ;;
218      *)
219         echo "eee : unknown option ${1}"
220         echo "${usage}"
221         exit 1
222      ;;
223   esac
224   # next flag
225   shift
226done
227#
228# ++ check param
229#
230case "${language}" in
231   fortran)
232      awkblockstart="^C\+$"
233      awkblockend="^C-$"
234      sedblockstart="^C+$"
235      sedblockend="^C-$"
236      comment="^C"
237   ;;
238   F90)
239      awkblockstart="^!\+$"
240      awkblockend="^!-$"
241      sedblockstart="^!+$"
242      sedblockend="^!-$"
243      comment="^!"
244   ;;
245   ferret)
246      awkblockstart="^!\+$"
247      awkblockend="^!-$"
248      sedblockstart="^!+$"
249      sedblockend="^!-$"
250      comment="^!"
251   ;;
252   IDL)
253      awkblockstart="^;\+$"
254      awkblockend="^;-$"
255      sedblockstart="^;+$"
256      sedblockend="^;-$"
257      comment="^;"
258   ;;
259   xml)
260      awkblockstart="^<!--rst$"
261      awkblockend="-->$"
262      sedblockstart="^<!--rst$"
263      sedblockend="-->$"
264      comment=""
265   ;;
266   sh)
267      # iii : awk '/^\#\+/,/^\#\-/' $file
268      awkblockstart="^\#\+$"
269      awkblockend="^\#\-$"
270      sedblockstart="^#+"
271      sedblockend="^#-"
272      comment="^#"
273   ;;
274   dot|php)
275      awkblockstart="^\/\*rst$"
276      awkblockend="*\/"
277      sedblockstart="^\/\*rst$"
278      sedblockend="^\*\/"
279      comment=""
280   ;;
281   matlab)
282      awkblockstart="^%\+$"
283      awkblockend="^%-$"
284      sedblockstart="^%+$"
285      sedblockend="^%-$"
286      comment="^%"
287   ;;
288   *)
289      echo "eee : ${language} not implemented"
290      exit 1
291   ;;
292esac
293#
294# just in case suppress \r at the end of lines
295tr -d '\r' < ${filein} > /tmp/${$}_0
296#
297# put rst blocks in one temporary file
298#awk '/^;+/,/^;-/' a.pro | sed -e "/^;+$/d" -e "/^;-$/d" -e "s/^;//"
299cmdawk="awk '/${awkblockstart}/,/${awkblockend}/' /tmp/${$}_0 > /tmp/${$}_1" #++
300eval ${cmdawk}
301if [ ! -s /tmp/${$}_1 ]
302then
303  rm /tmp/${$}_0 /tmp/${$}_1
304  echo "iii : no rst comments in ${filein}"
305  exit 1
306fi
307#
308# suppress begin and end of each block
309sedcmd="sed -e \"/${sedblockstart}/d\" -e \"/${sedblockend}/d\" /tmp/${$}_1 > /tmp/${$}_2"
310eval ${sedcmd}
311#
312# suppress comment at the beginning of each line
313if [ "${comment}" != "" ]
314then
315   sedcmd="sed -e \"s/${comment}//\" /tmp/${$}_2 > /tmp/${$}_3"
316   eval ${sedcmd}
317   # suppress first blank
318   cp /tmp/${$}_3 /tmp/${$}_2
319   sed -e "s/^ //" /tmp/${$}_2 > /tmp/${$}_3
320   cp /tmp/${$}_3 ${fileout}
321else
322   cp /tmp/${$}_2 ${fileout}
323fi
324#
325echo "iii : rst lines of ${filein} are in ${fileout}"
326#
327# clean
328rm /tmp/${$}_0 /tmp/${$}_1 /tmp/${$}_2 /tmp/${$}_3 2> /dev/null
329#
330# exit
331exit 0
Note: See TracBrowser for help on using the repository browser.