GUIScript: Windows compatible fix to include.py.
[gemrb.git] / admin / guidoc_wikifier.sh
blob13d6554e01579d0667c7fc378cf682913685e21a
1 #!/bin/bash
2 # GemRB - Infinity Engine Emulator
3 # Copyright (C) 2009 The GemRB Project
5 # This program is free software; you can redistribute it and/or
6 # modify it under the terms of the GNU General Public License
7 # as published by the Free Software Foundation; either version 2
8 # of the License, or (at your option) any later version.
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
15 # You should have received a copy of the GNU General Public License
16 # along with this program; if not, write to the Free Software
17 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20 # this script takes the guiscript docs and prepares them to shine on dokuwiki
22 docdir="${1:-$PWD/gemrb/docs/en/GUIScript}"
23 out_dir="${2:-$PWD/guiscript-docs.wikified}"
24 scriptdir="${3:-$docdir/../../../GUIScripts}"
25 index_title="GUIScript function listing"
26 see_also_str="See also:"
28 mkdir -p "$out_dir"
29 rm -f "$out_dir"/*.txt
30 test -d "$docdir" || exit 1
32 # intertwine the doc pages based on the "See also" entries
33 echo Linking ...
34 for txt_file in "$docdir"/*.txt; do
35 cp "$txt_file" "$out_dir/$(basename $txt_file)" || exit 2
36 # get the linked pages (all are in one line)
37 links=$(grep -m1 "$see_also_str" "$txt_file")
38 if [[ -n $links ]]; then
39 # we have some links and first we strip off the "See also:"
40 links=${links##*:}
41 for link in $links; do
42 # when there's multiple links, we have to deal with the commas
43 if [[ ! -f $docdir/${link//,/}.txt ]]; then
44 echo "Dangling link in $(basename $txt_file): ${link//,/}"
45 continue
47 if [[ ${link##*,} == "" ]]; then
48 # has comma, which we strip and readd, so it doesn't affect the link
49 sed_cmd="/$see_also_str/ s@$link@[[guiscript:${link//,/}]],@g"
50 else
51 sed_cmd="/$see_also_str/ s@$link\>@[[guiscript:$link]]@g"
53 sed -i "$sed_cmd" "$out_dir/$(basename $txt_file)"
54 done
56 done
58 echo Formatting ...
59 cd "$out_dir"
60 echo > indexX
61 # add some formatting to the doc pages
62 for txt_file in *; do
63 # extra newlines
64 #sed -i "s,$,\n," $txt_file
66 # format the title
67 if grep -qR --include="*.py" "GemRB\.${txt_file%.*}" "$scriptdir"; then
68 # actual script command
69 sed -i "1 s,^,===== ${txt_file%.*} =====\n," $txt_file
70 if [[ ${txt_file%.*} == controls ]]; then echo juhu0; fi
71 else
72 # miscellaneous doc
73 if [[ ${txt_file%.*} != indexX ]]; then
74 if [[ ${txt_file//_/} == $txt_file ]]; then
75 sed -i "1 s,^,===== ${txt_file%.*} =====\n," $txt_file
76 else
77 # misc doc with multiword title
78 sed -i "1 s,^\([^.]*\)\.*\s*$,===== \1 =====," $txt_file
83 # bold the headlines, itemize the parameters and add some links
84 sed -i "/\[\[guiscript:/! s@^[^:]\{,20\}:@\n**&**@" $txt_file
85 sed -i "/^$see_also_str/ s@^[^:]\{,10\}:@\n**&**@" $txt_file
86 echo -e "\n\n[[guiscript:index|Function index]]" >> $txt_file
87 sed -i "/Parameters:/,/^[^:]*:\*\*/ { /^[^:]*:\*\*/!s,^\(\s*\S\S*\), * \1,}" $txt_file
88 sed -i '/^\s*$/ {N; /\n\s*$/D }' $txt_file # squeeze repeats of more than 2 newlines
89 echo " * [[guiscript:${txt_file%.*}]]" >> indexX
91 [[ ${txt_file%.*} == indexX ]] && continue
92 lo_file=$(tr '[[:upper:]]' '[[:lower:]]' <<< $txt_file)
93 if [[ $lo_file != $txt_file ]]; then
94 mv $txt_file $lo_file
96 done
97 sort -o index.txt indexX
98 rm indexX
99 sed -i -e '/guiscript:index/d; /indexX/d' -e "1 s,^,===== $index_title =====\n," index.txt
100 echo Done.