New URL for NEMO forge!   http://forge.nemo-ocean.eu

Since March 2022 along with NEMO 4.2 release, the code development moved to a self-hosted GitLab.
This present forge is now archived and remained online for history.
extract_rst.sh in branches/DEV_r1879_FCM/NEMOGCM/TOOLS/adm – NEMO

source: branches/DEV_r1879_FCM/NEMOGCM/TOOLS/adm/extract_rst.sh @ 1972

Last change on this file since 1972 was 1972, checked in by flavoni, 14 years ago

Add TOOLS directory and scripts to compile with FCM, see ticket: #685

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