Skip to content

Instantly share code, notes, and snippets.

@nottrobin
Last active November 22, 2019 10:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nottrobin/dba25d2119e396441bf48d7bdaf18cc3 to your computer and use it in GitHub Desktop.
Save nottrobin/dba25d2119e396441bf48d7bdaf18cc3 to your computer and use it in GitHub Desktop.
Create PDF of server docs from https://ubuntu.com/server/docs
#! /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
@nottrobin
Copy link
Author

nottrobin commented Nov 19, 2019

To use this tool:

sudo apt install --yes curl texlive-full jq
wget https://github.com/jgm/pandoc/releases/download/2.7.3/pandoc-2.7.3-1-amd64.deb
dpkg -i pandoc-2.7.3-1-amd64.deb
wget https://gist.github.com/nottrobin/dba25d2119e396441bf48d7bdaf18cc3/raw/1507fbcf5b56c9991d75c3dc8883424862c3cee6/create-pdf.sh
bash create-pdf.sh

Here's an example of the generated documentation PDF.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment