nutools/doc/update-md

149 lines
3.5 KiB
Plaintext
Raw Permalink Normal View History

2013-08-27 15:14:44 +04:00
#!/bin/bash
# -*- coding: utf-8 mode: sh -*- vim:sw=4:sts=4:et:ai:si:sta:fenc=utf-8
2014-07-07 22:06:38 +04:00
source "$(dirname "$0")/../lib/ulib/ulib" || exit 1
urequire DEFAULTS
2013-08-27 15:14:44 +04:00
function display_help() {
uecho "$scriptname: Mettre à jour la documentation pour les outils de nutools
USAGE
$scriptname [options]
OPTIONS
2016-05-11 07:30:14 +04:00
-t, --tools
Ne générer la documentation que pour les outils. S'il ne faut regénérer
la documentation que pour certains outils, il suffit de les lister en
arguments.
-l, --ulib
Ne générer la documentation que pour ulib
2013-08-27 15:14:44 +04:00
Par défaut, la documentation est (re)générée pour les outils et pour ulib"
}
function dump_content() {
local mdpage="$1" content="$2"
if [ -f "$mdpage" ]; then
2013-08-27 15:14:44 +04:00
awk '
BEGIN { dump = 0 }
!dump && ($0 == "" || $0 ~ /^[^#]/) { dump = 1 }
dump { print }' <"$mdpage" >"$content"
2013-08-27 15:14:44 +04:00
else
>"$content"
fi
}
function write_mdpage() {
local oldcontent="$1" title="$2" newcontent="$3" mdpage="$4"
2013-08-27 15:14:44 +04:00
if testdiff "$oldcontent" "$newcontent"; then
local created="$(date +"%d/%m/%Y %H:%M")"
echo "# $title" >"$mdpage"
cat "$newcontent" >>"$mdpage"
echo -n "
-*- coding: utf-8 mode: markdown -*- vim:sw=4:sts=4:et:ai:si:sta:fenc=utf-8:noeol:binary" >>"$mdpage"
2013-08-27 15:14:44 +04:00
fi
}
auto=1
tools=
ulib=
force=
parse_opts "${PRETTYOPTS[@]}" \
--help '$exit_with display_help' \
2016-05-11 07:30:14 +04:00
-t,--tools '$auto=; tools=1' \
-l,--ulib '$auto=; ulib=1' \
2013-08-27 15:14:44 +04:00
--force force=1 \
@ args -- "$@" && set -- "${args[@]}" || die "$args"
[ -n "$auto" ] && {
tools=1
ulib=1
}
ac_set_tmpfile oldcontent
ac_set_tmpfile newcontent
cd "$scriptdir"
if [ -n "$tools" ]; then
if [ -n "$*" ]; then
cmds=("$@")
else
array_lsfiles cmds ..
fi
2015-08-20 07:58:17 +04:00
2016-11-28 09:16:41 +04:00
mkdir -p tools
2013-08-27 15:14:44 +04:00
for cmd in "${cmds[@]}"; do
cmdname="$(basename "$cmd")"
2016-11-28 09:16:41 +04:00
mdpage="tools/$cmdname.md"
2013-08-27 15:14:44 +04:00
[ -x "$cmd" ] || continue
estep "$cmdname"
dump_content "$mdpage" "$oldcontent"
2013-08-27 15:14:44 +04:00
echo "
~~~
2013-08-27 15:14:44 +04:00
$("$cmd" --help)
~~~" >"$newcontent"
2015-08-20 07:58:17 +04:00
write_mdpage "$oldcontent" "$cmdname" "$newcontent" "$mdpage"
2013-08-27 15:14:44 +04:00
done
fi
if [ -n "$ulib" ]; then
2016-11-28 09:16:41 +04:00
mkdir -p ulib
2014-07-07 22:06:38 +04:00
array_from_lines ulibnames "$(list_files ../lib/ulib)"
2013-08-27 15:14:44 +04:00
# faire l'entête
dump_content ulib.md "$oldcontent"
2013-08-27 15:14:44 +04:00
echo "
## Liste des librairies de ulib" >"$newcontent"
2013-08-27 15:14:44 +04:00
for ulibname in "${ulibnames[@]}"; do
2016-11-28 09:16:41 +04:00
echo "* [ulib/$ulibname]()" >>"$newcontent"
2013-08-27 15:14:44 +04:00
done
write_mdpage "$oldcontent" ulib "$newcontent" ulib.md
2013-08-27 15:14:44 +04:00
# faire les pages
for ulibname in "${ulibnames[@]}"; do
2014-07-07 22:06:38 +04:00
ulib="../lib/ulib/$ulibname"
2016-11-28 09:16:41 +04:00
mdpage="ulib/$ulibname.md"
2013-08-27 15:14:44 +04:00
title="ulib/$ulibname"
dump_content "$mdpage" "$oldcontent"
2013-08-27 15:14:44 +04:00
awkrun 'BEGIN {
in_func = 0
dump_doc = 0
dumped_doc = 0
print
}
!in_func && $0 ~ /^function / {
if (match($0, /function +([^ ]+)\(\)/, vs)) {
funcname = vs[1]
if (funcname !~ /^_/) {
in_func = 1
dump_doc = 1
dumped_doc = 0
print "## `" funcname "`"
2013-08-27 15:14:44 +04:00
if ($0 ~ /}$/) {
in_func = 0
dump_doc = 0
dumped_doc = 0
}
next
}
}
}
in_func && dump_doc && $0 !~ /^ *#/ {
dump_doc = 0
}
in_func && dump_doc && $0 ~ /^ *#/ {
if (!dumped_doc) print "~~~"
2013-08-27 15:14:44 +04:00
gsub(/^ *#+ */, "")
print
dumped_doc = 1
}
in_func && $0 ~ /}$/ {
if (dumped_doc) print "~~~"
2013-08-27 15:14:44 +04:00
in_func = 0
dump_doc = 0
dumped_doc = 0
}
END { if (in_func) print "~~~" }
2013-08-27 15:14:44 +04:00
' <"$ulib" >"$newcontent"
write_mdpage "$oldcontent" "$title" "$newcontent" "$mdpage"
2013-08-27 15:14:44 +04:00
done
fi