File: guidoc_wikifier.sh

package info (click to toggle)
gemrb 0.8.5-1
  • links: PTS, VCS
  • area: contrib
  • in suites: buster
  • size: 32,476 kB
  • sloc: cpp: 125,206; python: 31,280; ansic: 2,012; objc: 756; sh: 568; makefile: 467; perl: 178; java: 105
file content (131 lines) | stat: -rwxr-xr-x 4,588 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
#!/bin/bash
# GemRB - Infinity Engine Emulator
# Copyright (C) 2009 The GemRB Project
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
#
#
# this script takes the guiscript docs and prepares them to shine on dokuwiki

docdir="${1:-$PWD/gemrb/docs/en/GUIScript}"
out_dir="${2:-$PWD/guiscript-docs.wikified}"
scriptdir="${3:-$docdir/../../../GUIScripts}"
plugincpp="${4:-$docdir/../../../plugins/GUIScript/GUIScript.cpp}"
index_title="GUIScript function listing"
see_also_str="See also:"

mkdir -p "$out_dir"
rm -f "$out_dir"/*.txt
test -d "$docdir" || exit 1

function dumpDocs() {
  # extract the functions docs from the doc strings into individual files
  sed -n '/PyDoc_STRVAR/,/PyObject/ { /"=/,/"$/ { s,\\n,,; s,\\,,; p} }' "$plugincpp" |
    awk -v RS='"' -v out="$out_dir" \
    '{ split($0, t); title=tolower(t[2]); if (title) print $0 >  out "/" title ".txt" }' || return 1

  # append a link to the function index
  for txt_file in "$out_dir"/*.txt; do
    echo -e '\n[[guiscript:index|Function index]]' >> "$txt_file"
  done

  # dump the special pages
  cp "$docdir"/*.txt "$out_dir"
  rm "$out_dir"/doc_template.txt

  echo "Done, now manually copy the contents of $out_dir to wiki/data/pages/guiscript"
}

function convertDocs() {

# intertwine the doc pages based on the "See also" entries
echo Linking ...
for txt_file in "$docdir"/*.txt; do
  cp "$txt_file" "$out_dir/$(basename $txt_file)" || exit 2
  # get the linked pages (all are in one line)
  links=$(grep -m1 "$see_also_str" "$txt_file")
  if [[ -n $links ]]; then
    # we have some links and first we strip off the "See also:"
    links=${links##*:}
    for link in $links; do
      # when there's multiple links, we have to deal with the commas
      if [[ ! -f $docdir/${link//,/}.txt ]]; then
        echo "Dangling link in $(basename $txt_file): ${link//,/}"
        continue
      fi
      if [[ ${link##*,} == "" ]]; then
        # has comma, which we strip and readd, so it doesn't affect the link
        sed_cmd="/$see_also_str/ s@$link@[[guiscript:${link//,/}]],@g"
      else
        sed_cmd="/$see_also_str/ s@$link\>@[[guiscript:$link]]@g"
      fi
      sed -i "$sed_cmd" "$out_dir/$(basename $txt_file)"
    done
  fi
done

echo Formatting ...
cd "$out_dir"
echo > indexX
# add some formatting to the doc pages
for txt_file in *; do
  # extra newlines
  #sed -i "s,$,\n," $txt_file

  # format the title
  if grep -qR --include="*.py" "GemRB\.${txt_file%.*}" "$scriptdir"; then
    # actual script command
    sed -i "1 s,^,===== ${txt_file%.*} =====\n," $txt_file
  if [[ ${txt_file%.*} == controls ]]; then echo juhu0; fi
else
    # miscellaneous doc
    if [[ ${txt_file%.*} != indexX ]]; then
      if [[ ${txt_file//_/} == $txt_file ]]; then
        sed -i "1 s,^,===== ${txt_file%.*} =====\n," $txt_file
      else
        # misc doc with multiword title
        sed -i "1 s,^\([^.]*\)\.*\s*$,===== \1 =====," $txt_file
      fi
    fi
  fi

  # bold the headlines, itemize the parameters and add some links
  sed -i "/\[\[guiscript:/! s@^[^:]\{,20\}:@\n**&**@" $txt_file
  sed -i "/^$see_also_str/ s@^[^:]\{,10\}:@\n**&**@" $txt_file
  echo -e "\n\n[[guiscript:index|Function index]]" >> $txt_file
  sed -i "/Parameters:/,/^[^:]*:\*\*/ { /^[^:]*:\*\*/!s,^\(\s*\S\S*\),  * \1,}" $txt_file
  sed -i '/^\s*$/ {N; /\n\s*$/D }' $txt_file # squeeze repeats of more than 2 newlines
  echo "  * [[guiscript:${txt_file%.*}]]" >> indexX

  [[ ${txt_file%.*} == indexX ]] && continue
  lo_file=$(tr '[[:upper:]]' '[[:lower:]]' <<< $txt_file)
  if [[ $lo_file != $txt_file ]]; then
    mv $txt_file $lo_file
  fi
done
sort -o index.txt indexX
rm indexX
sed -i -e '/guiscript:index/d; /indexX/d' -e "1 s,^,===== $index_title =====\n," index.txt
echo Done.

}

# old logic switch; all paths or no paths
if [[ $1 == convert || $5 == convert ]]; then
  [[ $1 == convert ]] && docdir="$PWD/gemrb/docs/en/GUIScript"
  convertDocs
else
  dumpDocs
fi