summaryrefslogtreecommitdiffstats
path: root/solenv/bin/mkdocs.sh
blob: 0f92ff87e532adbaacd715bd73180186df47066d (plain)
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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
#!/usr/bin/env bash
#
# This file is part of the LibreOffice project.
#
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
#
# Doxygen / README doc generation
#
# See git for contributors
#

function header {
  title=$1
  breadcrumb=$2
  output=$3

  cat - > $output <<EOF
<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML//EN">
<html>
<head>
    <title>$title</title>

    <style>
    * { margin: 0; padding: 0; }
    body { font-family: sans-serif; font-size: 12px; }
        #head { padding: 20px; background: #00A500; }
    #head a { color: #000; }
    #body { padding: 20px; }
    #foot { padding: 10px; font-size: 9px; border-top: 1px #18A303 solid; margin-top: 25px; }
    p { line-height: 1.7em; margin-bottom: 1em; }
    pre { margin-bottom: 0.5em; }
        .multi-col { -moz-column-width: 20em; -webkit-column-width: 20em; -moz-column-gap: 1em; -webkit-column-gap: 1em; }
        h1 { margin-bottom: 0.5em; }
    h2,h3,h4 { margin: 1.3em 0 0.5em 0; }
    ul, ol { margin: 0.5em 1.5em; }
    </style>
</head>
<body>
    <div id="head">
    <h1>$title</h1>
    <p>$breadcrumb</p>
    </div>
        <div id="body" style="multi-col">
EOF
}

function footer {
  output=$1

  cat - >> $output <<EOF

</div>
<div id="foot">
  <small>
    <p>Generated by Libreoffice <a href="https://cgit.freedesktop.org/libreoffice/core/plain/solenv/bin/mkdocs.sh">Module Description Tool</a></p>
    <p>Last updated:
EOF
LANG= date >> $output
cat - >> $output <<EOF
    </p>
  </small>
</div>
</body>
</html>
EOF

}

function proc_text {
  # Local links: [[...]]
  # Git links: [git:...]
  # Other remote links: [...]
  # Headings: == bleh ==
  # Paragraphs: \n\n
  sed -re ' s/\[\[([-_a-zA-Z0-9]+)\]\]/<a href="\1.html">\1<\/a>/g' - \
  | sed -re ' s/\[git:([^]]+)\]/<a href="https:\/\/cgit.freedesktop.org\/libreoffice\/core\/tree\/\1">\1<\/a>/g' \
  | sed -re ' s/\[([^]]+)\]/<a href="\1">\1<\/a>/g' \
  | sed -re ' s/====([^=]+)====/<h4>\1<\/h4>/g' \
  | sed -re ' s/===([^=]+)===/<h3>\1<\/h3>/g' \
  | sed -re ' s/==([^=]+)==/<h2>\1<\/h2>/g' \
  | sed -re ':a;N;$!ba;s/\n\n/<\/p><p>/g' \
  | awk 'BEGIN { print "<p>" } { print } END { print "</p>" }'
}

function proc_text_markdown {
  sed -re ' s/\[\[([-_a-zA-Z0-9]+)\]\]/<a href="\1.html">\1<\/a>/g' - \
  | sed -re ' s/\[git:([^]]+)\]/<a href="https:\/\/cgit.freedesktop.org\/libreoffice\/core\/tree\/\1">\1<\/a>/g'
}

function check_cmd {
  cmds_needed="$1"
  error_msg="$2"

  found=0; cmd=
  for cmd_needed in $cmds_needed; do
    which $cmd_needed > /dev/null 2>&1 && { found=1; cmd=$cmd_needed; }
  done
  if [ $found = 0 ]; then
      echo "$error_msg" >&2
      exit 1
  fi
  export "${cmds_needed%% *}"=$cmd
}

function setup {
  parm=$1
  if [ -z "${!parm}" ] ; then
    echo "grep \"${parm}=\" ./config_host.mk | sed -re \" s/${parm}=//\")"
    echo "$parm=$(grep \"${parm}=\" ./config_host.mk | sed -re \" s/${parm}=//\")"
    eval "$parm=$(grep \"${parm}=\" ./config_host.mk | sed -re \" s/${parm}=//\")"
  fi
  if [ -z "${!parm}" ] ; then
    echo "could not determine $parm" >&2
    exit 1
  fi
}

# binaries that we need
check_cmd doxygen "You need doxygen for doc generation"
check_cmd dot "You need the graphviz tools to create the nice inheritance graphs"
check_cmd "markdown markdown2 markdown2-3" "You need either markdown or markdown2 in order to convert README.md into html"

# suck setup
setup "SOLARINC"
shopt -s nullglob

# Title of the documentation
DOXYGEN_PROJECT_PREFIX="LibreOffice"

# get list of modules
if [ -z "$INPUT_PROJECTS" ]; then
    INPUT_PROJECTS="`ls */Module_*.mk | sed 's#/.*##'`"
fi


# output directory for generated documentation
BASE_OUTPUT="$1"
mkdir -p "$BASE_OUTPUT" || {
    echo "Cannot create $BASE_OUTPUT"
    exit 1
}

# paths for binary and configuration file
BASE_PATH=`pwd`
DOXYGEN_CFG="$2"
if test ! -f "$DOXYGEN_CFG"; then
    echo "doxygen.cfg not found"
    exit 1
fi

# strip -I. and bin -I prefix; exclude system headers
DOXYGEN_INCLUDE_PATH=`echo $SOLARINC | sed -e 's/-I\.//g' -e 's/ -I/ /'g -e 's/ -isystem/ /g' -e 's|/usr/[^ ]*| |g'`

# setup version string
DOXYGEN_VERSION="master"


###################################################
#
# Generate docs
#
###################################################

# cleanup
echo "cleaning up" && rm -rf $BASE_OUTPUT/*

# make the stuff world-readable
umask 022

# generate docs
echo "generating doxygen docs"
DOXYGEN_REF_TAGFILES=""
for PROJECT in $INPUT_PROJECTS;
do
  # avoid processing of full project subdirs, only add source and inc

  # project header files can be in $PROJECT/inc and/or include/$PROJECT
  if [ -d "$PROJECT/inc" ]; then
      PROJECT_INCLUDE="$PROJECT/inc"
  else
      PROJECT_INCLUDE=""
  fi

  if [ -d "include/$PROJECT" ]; then
      PROJECT_INCLUDE="$PROJECT_INCLUDE include/$PROJECT"
      if [ "$PROJECT" = "sal" ]; then
          PROJECT_INCLUDE="$PROJECT_INCLUDE include/osl include/rtl"
      fi
  fi

  DOXYGEN_INPUT=`printf "%s" "$PROJECT/source $PROJECT_INCLUDE"`

  DOXYGEN_OUTPUT="$BASE_OUTPUT/$PROJECT"
  DOXYGEN_OUR_TAGFILE="$DOXYGEN_OUTPUT/$PROJECT.tags"
  DOXYGEN_PROJECTNAME="$DOXYGEN_PROJECT_PREFIX Module $PROJECT"

  # export variables referenced in doxygen config file
  export DOXYGEN_INPUT
  export DOXYGEN_OUTPUT
  export DOXYGEN_INCLUDE_PATH
  export DOXYGEN_VERSION
  export DOXYGEN_OUR_TAGFILE
  export DOXYGEN_REF_TAGFILES
  export DOXYGEN_PROJECTNAME

  # debug
  echo "Calling $DOXYGEN_PATH/doxygen $DOXYGEN_CFG with"
  echo "Input:      $DOXYGEN_INPUT"
  echo "Output:     $DOXYGEN_OUTPUT"
  echo "Include:    $DOXYGEN_INCLUDE_PATH"
  echo "Version:    $DOXYGEN_VERSION"
  echo "Tagfile:    $DOXYGEN_OUR_TAGFILE"
  echo "Ref-Tags:   $DOXYGEN_REF_TAGFILES"
  echo "Title:      $DOXYGEN_PROJECTNAME"

  nice -15 doxygen "$DOXYGEN_CFG" >>$BASE_OUTPUT/doxygen.log 2>&1 || exit 1

  # setup referenced tagfiles for next round
  DOXYGEN_REF_TAGFILES="$DOXYGEN_REF_TAGFILES $DOXYGEN_OUR_TAGFILE=$BASE_URL/$PROJECT/html"
done

# generate entry page
echo "generating index page"
header "LibreOffice Modules" " " "$BASE_OUTPUT/index.html"
for module_name in *; do
  if [ -d $module_name ]; then
    cur_file=$(echo $module_name/README* $module_name/readme.txt*)
    if [ -f "$cur_file" ]; then
      # write index.html entry
      text="<h2><a href=\"${module_name}.html\">${module_name}</a></h2>\n"

      if [ ${cur_file: -3} == ".md" ]; then
        # This is a markdown file.
        header_text="$(head -n1 $cur_file)"
        header_text="$(echo ${header_text} | sed -e 's/^\#*//g')"
        text="${text}${header_text}"
      else
        text="${text}$(head -n1 $cur_file | proc_text)"
      fi
      echo -e $text >> "$BASE_OUTPUT/index.html"

      # write detailed module content
      header "$module_name" "<a href=\"index.html\">LibreOffice</a> &raquo; ${module_name}" "$BASE_OUTPUT/${module_name}.html"
      text="<p><b>View module in:</b>"
      text="${text} &nbsp; <a href=\"https://cgit.freedesktop.org/libreoffice/core/tree/${module_name}\">cgit</a>"
      if $(echo $INPUT_PROJECTS | grep -q $module_name); then
        text="${text} &nbsp; <a href=\"${module_name}/html/classes.html\">Doxygen</a>"
      fi
      text="${text} </p><p>&nbsp;</p>"
      echo -e $text >> "$BASE_OUTPUT/${module_name}.html"

      if [ ${cur_file: -3} == ".md" ]; then
        # This is a markdown file.
        text="$(${markdown} $cur_file | proc_text_markdown)"
        echo $text >> "$BASE_OUTPUT/${module_name}.html"
      else
        proc_text < $cur_file >> "$BASE_OUTPUT/${module_name}.html"
      fi
      footer "$BASE_OUTPUT/${module_name}.html"
    else
      empty_modules[${#empty_modules[*]}]=$module_name
    fi
  fi
done

if [ ${#empty_modules[*]} -gt 0 ]; then
  echo -e "<p>&nbsp;</p><p>READMEs were not available for these modules:</p><ul>\n" >> "$BASE_OUTPUT/index.html"
  for module_name in "${empty_modules[@]}"; do
    echo -e "<li><a href=\"https://cgit.freedesktop.org/libreoffice/core/tree/${module_name}\">${module_name}</a></li>\n" >> "$BASE_OUTPUT/index.html"
  done
  echo -e "</ul>\n" >> "$BASE_OUTPUT/index.html"
fi

footer "$BASE_OUTPUT/index.html"

## done