Last active
November 22, 2019 10:05
-
-
Save nottrobin/dba25d2119e396441bf48d7bdaf18cc3 to your computer and use it in GitHub Desktop.
Create PDF of server docs from https://ubuntu.com/server/docs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#! /usr/bin/env bash | |
# === | |
# This will pull down the server documentation from https://discourse.ubuntu.com/c/server/guide | |
# === | |
# http://redsymbol.net/articles/unofficial-bash-strict-mode/ | |
set -euo pipefail | |
# Empty the documents directory, so we get fresh documents | |
rm -rf documents | |
mkdir documents | |
# Get and clean up the introduction topic | |
curl -s "https://discourse.ubuntu.com/t/11322.json?include_raw=true" | jq -r '.["post_stream"]["posts"][0]["raw"]' > documents/0000-introduction.md | |
# Get the list of IDs from the introduction topic | |
ids=( $(grep -P '/\d+\)' documents/0000-introduction.md | sed -E 's!^[*] \[[^]]*\]\(/t[^)]*/([0-9]+)\).*!\1!') ) | |
# Remove everything after "## Navigation" | |
sed -i '1,/^## Navigation/!d' documents/0000-introduction.md # Remove everything after the "Navigation" heading | |
sed -i '/^## Navigation/d' documents/0000-introduction.md # Remove the "Navigation" heading itself | |
# Get each topic by ID and save it in the "documents" folder, with an index number for ordering | |
index=0 | |
for id in "${ids[@]}"; do | |
index=$(($index+1)) | |
printf -v padded_index '%04d' $index # Increment the number | |
curl -s "https://discourse.ubuntu.com/t/${id}.json?include_raw=true" | jq -r '.["post_stream"]["posts"][0]["raw"]' > documents/${padded_index}.md | |
echo "Saved ${padded_index}.md" | |
done | |
# Create a PDF from the ordered topic documents | |
pandoc documents/*.md --listings -V header-includes:'\lstset{breaklines=true}' -V geometry:margin=1in --pdf-engine xelatex -o server-documentation.pdf |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
To use this tool:
Here's an example of the generated documentation PDF.