nutools/lib/templates/www

447 lines
14 KiB
Plaintext
Raw Normal View History

2013-10-31 10:49:07 +04:00
#!/bin/bash
# -*- coding: utf-8 mode: sh -*- vim:sw=4:sts=4:et:ai:si:sta:fenc=utf-8
function display_help() {
uecho "$scriptname: générer un fichier $NAME
USAGE
$scriptname [options] <file>
OPTIONS
-t TEMPLATE
Indiquer le modèle de fichier à utiliser pour la génération. Les valeurs
valides sont:
${TEMPLATES[*]}
-e, --edit
-g, --no-edit
Editer (resp. ne pas éditer) le fichier après l'avoir généré.
Par défaut, l'éditeur est lancé après la génération.
-f, --overwrite
Ecraser le fichier s'il existe déjà
-E, --encoding ENCODING
2017-02-23 16:33:08 +04:00
Spécifier l'encoding à utiliser pour la génération du fichier
--doctype DOCTYPE
-4, --html4
-5, --html5
Spécifier le type de document. Les options -4 et -5 sont équivalentes à
--doctype html4 et --doctype html5, respectivement.
-b, --baseurl BASEURL
2017-02-23 16:33:08 +04:00
Spécifier le chemin à partir de la racine qui contient les resources
blueprint et bootstrap
-B, --bspath, --bppath CLPATH
Spécifier le chemin sous BASEURL à partir duquel charger bootstrap ou
blueprint. La valeur par défaut est 'bootstrap' pour l'option --bs,
'blueprint' pour l'option --bp
2017-02-23 16:33:08 +04:00
-s, --bs, --bootstrap
Activer le support de bootstrap. L'option --bs est automatiquement
activée si l'option --bspath n'est pas utilisée et le répertoire
2017-02-23 16:33:08 +04:00
[BASEURL/]bootstrap/ existe
-p, --bp, --blueprint
Activer le support de blueprint. L'option --bp est automatiquement
activée si l'option --bppath n'est pas utilisée et le répertoire
[BASEURL/]blueprint/ existe
-n, --nobs, --no-bootstrap, --nobp, --no-blueprint
Désactiver le support de bootstrap et blueprint
2017-02-23 16:33:08 +04:00
-j, --jquery
-k, --no-jquery
Activer (resp. désactiver) le chargement de jQuery à partir de BASEURL
pour les pages nues et avec blueprint. Avec bootstrap, le chargement de
jQuery est toujours effectué et cette option est ignorée.
--hl, --titlehl TITLEHL
Spécifier le tag à utiliser pour le titre. Vaut par défaut 'h1' pour les
templates HTML et PHP, 'h2' pour les templates overview et package
-T, --title TITLE
Spécifier le titre du document.
--head-title HTITLE
Spécifier le titre pour le tag <title> dans la section <head>. La valeur
par défaut est celle de l'option --title."
2013-10-31 10:49:07 +04:00
}
NAME=www
2013-10-31 12:59:41 +04:00
TEMPLATES=(html javadoc-package javadoc-overview php cakephp-ctp css javascript)
2013-10-31 10:49:07 +04:00
NAMES=(package.html overview.html)
EXTS=(html htm php phps ctp css js)
if [ $# -eq 2 ]; then
if [ "$1" == "--matches-template" ]; then
for template in "${TEMPLATES[@]}"; do
[ "$template" == "$2" ] && exit 0
done
exit 1
elif [ "$1" == "--matches-name" ]; then
for name in "${NAMES[@]}"; do
[ "$name" == "$2" ] && exit 0
done
exit 1
elif [ "$1" == "--matches-ext" ]; then
for ext in "${EXTS[@]}"; do
[ "$ext" == "$2" ] && exit 0
done
exit 1
fi
fi
2014-07-07 22:06:38 +04:00
#source /etc/ulib || exit 1
2014-07-08 11:03:07 +04:00
source "$(dirname "$0")/../ulib/ulib" || exit 1
2014-07-07 22:06:38 +04:00
urequire DEFAULTS
2013-10-31 10:49:07 +04:00
function check_overwrite() {
if [ -e "$1" -a -z "$overwrite" ]; then
eerror "$1: refus d'écraser un fichier déjà existant (utiliser -f)"
return 1
fi
return 0
}
2013-10-31 12:59:41 +04:00
function __update_title() {
titlehl="$(strlower "$titlehl")"
2013-10-31 12:59:41 +04:00
[ -n "$titlehl" ] || titlehl=h1
[ "${titlehl:0:1}" == "h" ] || titlehl="h$titlehl"
[ -n "$title" ] || {
title="$(basename "$file")"
title="${title%.*}"
}
[ -n "$head_title" ] || head_title="$title"
}
function __generate_html() {
# $file et $mode doivent être initialisés
2013-10-31 10:49:07 +04:00
2013-10-31 12:59:41 +04:00
local dir="$(dirname "$file")"
local nobpsuppl nobplocal nobpmobile
if [ "$csslib" == auto ]; then
if [ -n "$clpath" ]; then
if [ "$clpath" == bootstrap -o -z "${clpath##*/bootstrap}" ]; then
csslib=bootstrap
elif [ "$clpath" == blueprint -o -z "${clpath##*/blueprint}" ]; then
csslib=blueprint
else
csslib=
fi
elif [ -d "$dir/${baseurl#/}bootstrap" ]; then
csslib=bootstrap
elif [ -d "$dir/${baseurl#/}blueprint" ]; then
csslib=blueprint
2013-10-31 12:59:41 +04:00
else
csslib=
2013-10-31 12:59:41 +04:00
fi
fi
if [ "$csslib" == bootstrap ]; then
[ -n "$clpath" ] || clpath=bootstrap
elif [ "$csslib" == blueprint ]; then
[ -n "$clpath" ] || clpath=blueprint
[ -f "$dir/${baseurl#/}bpsuppl.css" ] || nobpsuppl=1
[ -f "$dir/${baseurl#/}bplocal.css" ] || nobplocal=1
[ -f "$dir/${baseurl#/}bpmobile.css" ] || nobpmobile=1
fi
clpath="$baseurl$clpath"
[ "${clpath:$((-1)):1}" == / ] || clpath="$clpath/"
2013-10-31 12:59:41 +04:00
local jquerymin
if [ "$jquery" == auto ]; then
if [ "$csslib" == bootstrap ]; then
jquery=
elif [ -f "$dir/${baseurl#/}jquery.min.js" ]; then
2013-10-31 12:59:41 +04:00
jquery=1
jquerymin=1
elif [ -f "$dir/${baseurl#/}jquery.js" ]; then
2013-10-31 12:59:41 +04:00
jquery=1
else
jquery=
fi
fi
if [ "$doctype" == auto ]; then
if [ "$csslib" == bootstrap ]; then
doctype=html5
else
doctype=html4
fi
fi
head='<head>'
if [ "$doctype" == html5 ]; then
head="$head
2017-02-28 17:28:10 +04:00
"'<meta charset="'"$encoding"'" />'
else
head="$head
"'<meta http-equiv="Content-Type" content="text/html; charset='"$encoding"'" />'
fi
if [ "$csslib" == bootstrap ]; then
head="$head
"'<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<link rel="shortcut" href="'"$clpath"'favicon.ico" />
<link rel="icon apple-touch-icon" href="'"$clpath"'icon.png" />'
csslib='<link href="'"$clpath"'bootstrap/css/bootstrap.min.css" rel="stylesheet"/>
<script src="'"$clpath"'jquery.min.js" type="text/javascript"></script>
<script src="'"$clpath"'bootstrap/js/bootstrap.min.js" type="text/javascript"></script>
<!--[if lt IE 9]>
<script src="'"$clpath"'html5shiv/html5shiv.js"></script>
<script src="'"$clpath"'respond/respond.min.js"></script>
<![endif]-->'
startcl='<div class="container">'
endcl='</div>'
elif [ "$csslib" == blueprint ]; then
head="$head
"'<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<link rel="shortcut" href="'"$baseurl"'favicon.ico" />
<link rel="icon apple-touch-icon" href="'"$baseurl"'icon.png" />'
csslib='<link rel="stylesheet" href="'"$clpath"'screen.css" type="text/css" media="screen, projection" />
<link rel="stylesheet" href="'"$clpath"'print.css" type="text/css" media="print" />
<!--[if lt IE 8]>
<link rel="stylesheet" href="'"$clpath"'ie.css" type="text/css" media="screen, projection" />
<![endif]-->
<'"${nobpsuppl:+!--}"'link rel="stylesheet" href="'"$baseurl"'bpsuppl.css" type="text/css" media="screen, projection" /'"${nobpsuppl:+--}"'>
<'"${nobplocal:+!--}"'link rel="stylesheet" href="'"$baseurl"'bplocal.css" type="text/css" media="screen, projection" /'"${nobplocal:+--}"'>
<'"${nobpmobile:+!--}"'link rel="stylesheet" href="'"$baseurl"'bpmobile.css" type="text/css" media="handheld, only screen and (max-device-width: 480px)" /'"${nobpmobile:+--}"'>'
startcl='<div class="container">'
endcl='</div>'
else
# pas de csslib ou csslib non supporté
head="$head
"'<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<link rel="shortcut" href="'"$baseurl"'favicon.ico" />
<link rel="icon apple-touch-icon" href="'"$baseurl"'icon.png" />'
csslib=
startcl=
endcl=
fi
if [ -n "$jquery" ]; then
jquery='<script src="'"${baseurl}jquery${jquerymin:+.min}.js"'"></script>'
fi
start='<title>'"$head_title"'</title>
</head>
<body>'
end='</body>
</html>'
2013-10-31 12:59:41 +04:00
case "$doctype" in
html4)
doctype='<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">'
html='<html lang="fr" xmlns="http://www.w3.org/TR/REC-html40">'
;;
html5)
doctype='<!DOCTYPE html>'
html='<html lang="fr">'
;;
*)
doctype=
html='<html>'
;;
esac
}
2014-06-18 11:34:37 +04:00
function __before_write_html() {
doctype="$doctype<!-- -*- coding: $encoding ${mode:+mode: $mode }-*- vim:sw=4:sts=4:et:ai:si:sta:fenc=$encoding
-->"
2013-10-31 12:59:41 +04:00
}
function __write_html() {
# $file et $mode doivent être initialisés
2013-10-31 10:49:07 +04:00
check_overwrite "$1" || return
estep "$(ppath "$file")"
2014-06-18 11:34:37 +04:00
echo >"$file" "$doctype$html
2013-10-31 12:59:41 +04:00
$head
${csslib:+$csslib
2013-10-31 12:59:41 +04:00
}${jquery:+$jquery
}$start
${csslib:+$startcl
2013-10-31 12:59:41 +04:00
}${title:+<$titlehl>$title</$titlehl>
}${csslib:+$endcl
2013-10-31 12:59:41 +04:00
}$end"
2013-10-31 10:49:07 +04:00
}
2013-10-31 12:59:41 +04:00
function generate_html() {
2013-10-31 10:49:07 +04:00
local file="$1"
local mode=html
2013-10-31 12:59:41 +04:00
local titlehl="${titlehl:-h1}"
local title="$title"
__update_title
local doctype="$doctype"
local csslib="$csslib"
2013-10-31 12:59:41 +04:00
local jquery="$jquery"
local html head start startcl endcl end
2013-10-31 12:59:41 +04:00
__generate_html
2014-06-18 11:34:37 +04:00
__before_write_html
2013-10-31 12:59:41 +04:00
__write_html
2013-10-31 10:49:07 +04:00
[ -n "$2" ] && array_add "$2" "$file"
return 0
}
function generate_javadoc_package() {
local file="$1"
local mode=html
2013-10-31 12:59:41 +04:00
local titlehl="${titlehl:-h2}"
local title="$title"
__update_title
# title doit se terminer par un point
[ "${title:$((-1)):1}" == / ] || title="$title."
# Mettre un préfixe au titre
title="Package: $title"
local doctype="$doctype"
local csslib bootstrap jquery html head start startcl endcl end
2013-10-31 12:59:41 +04:00
__generate_html
2014-06-18 11:34:37 +04:00
__before_write_html
2013-10-31 12:59:41 +04:00
__write_html
2013-10-31 10:49:07 +04:00
[ -n "$2" ] && array_add "$2" "$file"
return 0
}
function generate_javadoc_overview() {
local file="$1"
local mode=html
2013-10-31 12:59:41 +04:00
local titlehl="${titlehl:-h2}"
local title="$title"
__update_title
# title doit se terminer par un point
[ "${title:$((-1)):1}" == / ] || title="$title."
# Mettre un préfixe au titre
title="Projet: $title"
local doctype="$doctype"
local csslib bootstrap jquery html head start startcl endcl end
2013-10-31 12:59:41 +04:00
__generate_html
2014-06-18 11:34:37 +04:00
__before_write_html
2013-10-31 12:59:41 +04:00
__write_html
2013-10-31 10:49:07 +04:00
[ -n "$2" ] && array_add "$2" "$file"
return 0
}
function generate_php() {
local file="$1"
local mode=php
2013-10-31 12:59:41 +04:00
local titlehl="${titlehl:-h1}"
local title="$title"
__update_title
local doctype="$doctype"
local csslib="$csslib"
2013-10-31 12:59:41 +04:00
local jquery="$jquery"
local html head start startcl endcl end
2013-10-31 12:59:41 +04:00
__generate_html
doctype="<?php # -*- coding: $encoding ${mode:+mode: $mode }-*- vim:sw=2:sts=2:et:ai:si:sta:fenc=$encoding
2014-06-18 11:34:37 +04:00
?>${doctype:+$doctype
}"
2013-10-31 12:59:41 +04:00
__write_html
2013-10-31 10:49:07 +04:00
[ -n "$2" ] && array_add "$2" "$file"
return 0
}
function generate_cakephp_ctp() {
local file="$1"
local mode=php
check_overwrite "$1" || return
estep "$(ppath "$file")"
2013-10-31 12:59:41 +04:00
echo >"$file" "<?php # -*- coding: $encoding ${mode:+mode: $mode }-*- vim:sw=2:sts=2:et:ai:si:sta:fenc=$encoding"
2013-10-31 10:49:07 +04:00
[ -n "$2" ] && array_add "$2" "$file"
return 0
}
function generate_css() {
local file="$1"
local mode=css
check_overwrite "$1" || return
estep "$(ppath "$file")"
2013-10-31 12:59:41 +04:00
echo >"$file" "/* -*- coding: $encoding ${mode:+mode: $mode }-*- vim:sw=4:sts=4:et:ai:si:sta:fenc=$encoding
2014-06-06 15:21:53 +04:00
*/@CHARSET \"$encoding\";"
2013-10-31 14:58:03 +04:00
2013-10-31 10:49:07 +04:00
[ -n "$2" ] && array_add "$2" "$file"
return 0
}
function generate_javascript() {
local file="$1"
local mode=javascript
check_overwrite "$1" || return
estep "$(ppath "$file")"
2013-10-31 12:59:41 +04:00
echo >"$file" "/* -*- coding: $encoding ${mode:+mode: $mode }-*- vim:sw=4:sts=4:et:ai:si:sta:fenc=$encoding
*/"
2013-10-31 14:58:03 +04:00
2013-10-31 10:49:07 +04:00
[ -n "$2" ] && array_add "$2" "$file"
return 0
}
template=
edit=1
overwrite=
encoding=
doctype=auto
2013-10-31 12:59:41 +04:00
baseurl=/
clpath=
csslib=auto
jquery=auto
2013-10-31 12:59:41 +04:00
titlehl=
title=
head_title=
2013-10-31 10:49:07 +04:00
parse_opts "${PRETTYOPTS[@]}" \
--help '$exit_with display_help' \
-t:,--template: template= \
-e,--edit edit=1 \
-g,--no-edit edit= \
-f,--overwrite overwrite=1 \
-E:,--encoding: encoding= \
2013-10-31 12:59:41 +04:00
--doctype: doctype= \
-4,--html4 doctype=html4 \
-5,--html5 doctype=html5 \
-b:,--baseurl: baseurl= \
-B:,--bspath:,--bppath: clpath= \
-s,--bootstrap,--bs csslib=bootstrap \
-p,--blueprint,--bp csslib=blueprint \
-n,--no-bootstrap,--nobootstrap,--nobs,--no-blueprint,--noblueprint,--nobp csslib= \
2013-10-31 12:59:41 +04:00
-j,--jquery jquery=1 \
-k,--no-jquery,--nojquery jquery= \
--titlehl:,--hl: titlehl= \
-T:,--title: title= \
--head-title: head_title= \
2013-10-31 10:49:07 +04:00
@ args -- "$@" && set -- "${args[@]}" || die "$args"
[ -n "$encoding" ] || encoding=utf-8
2013-10-31 12:59:41 +04:00
[ -z "$baseurl" -o "${baseurl:$((-1)):1}" == / ] || baseurl="$baseurl/"
2013-10-31 10:49:07 +04:00
files2edit=()
r=0
for file in "$@"; do
t="$template"
if [ -z "$t" ]; then
dir="$(dirname "$file")"
name="$(basename "$file")"
# Si template n'est pas spécifié, le déterminer le cas échéant à partir
# du nom de fichier. Rajouter ici le code approprié
2013-10-31 12:59:41 +04:00
case "$name" in
overview.html) t=javadoc-overview;;
package.html) t=javadoc-package;;
esac
2013-10-31 10:49:07 +04:00
fi
2013-10-31 14:46:06 +04:00
case "$t" in
html|htm) generate_html "$file" files2edit || r=$?;;
javadoc-package|jdp) generate_javadoc_package "$file" files2edit || r=$?;;
jadadoc-overview|jdo) generate_javadoc_overview "$file" files2edit || r=$?;;
php|phps) generate_php "$file" files2edit || r=$?;;
cakephp-ctp|ctp) generate_cakephp_ctp "$file" files2edit || r=$?;;
css) generate_css "$file" files2edit || r=$?;;
javascript|js) generate_javascript "$file" files2edit || r=$?;;
*) die "$NAME: template invalide: $t";;
2013-10-31 14:58:03 +04:00
esac
2013-10-31 10:49:07 +04:00
done
if [ -n "$edit" -a "${#files2edit[*]}" -gt 0 ]; then
"${EDITOR:-vi}" "${files2edit[@]}"
fi
exit $r