Passke

↩︎

Construire un site statique avec pandoc

Le principe est relativement simple, il s’agit de construire un site à partir d’un répertoire contenant des fichiers markdown et de générer la version html de chacun de ces fichiers, puis une page d’accueil donnant accès à cette liste.

#!/bin/bash
set -euo pipefail
IFS=$'\n\t'

rm -rf public
mkdir -p public

posts=$(ls -lht *.md|grep -v 'header.md'|awk '{print $9}')

cp styles.css public/
cat header.md | pandoc --metadata-file=metadata.yaml --css=styles.css -s -t html5 |head -n -2 > public/index.html
for post in $posts; do
  echo "$post"
  echo "<!-- $post -->" >> public/index.html
  cat $post|pandoc -t markdown --shift-heading-level-by=1 |head -n 10|pandoc -t html5 >> public/index.html
  post_link=$(echo $post|sed 's/\.md/.html/g')
  echo "[lire...]($post_link)"|pandoc -t html5 >> public/index.html
  cat header.md | pandoc --metadata-file=metadata.yaml --css=styles.css -s -t html5 |head -n -2 > public/$post_link
  cat $post| pandoc -t html5 >> public/$post_link
  echo '</body>' >> public/$post_link
  echo '</html>' >> public/$post_link
done

echo '</body>' >> public/index.html
echo '</html>' >> public/index.html

Le fichier metadata.yaml contient une ou plusieurs metadata reconnues par pandoc (man pandoc).

title: "Un site de simple et statique"
author: "Un illustre inconnu"

Le fichier header.md permet d’ajouter à chaque page un texte commun. Le fichier de style (styles.css) est libre mais peut être inspiré du style par défaut de pandoc.