User:Tommi/Alejandro
Alejandro is a Bash script made to quickly and simply parse a Wiki page in a plain standalone HTML, and add some styling to it.
Actually, not particularly quickly
: I planned to spend a few minutes working on it, but I ended up procrastinating and refining it for several hours.
Naming
This tool is named Alejandro
because it is the title of the song by Lady Gaga I was listening to while I was creating it.
Code
#!/bin/bash
PAGE_TITLE=User:Tommi/T2_Assessment
TITLE='Tommi’s T2 Assessment'
BASE_URL=https://pzwiki.wdka.nl
WIKI_API=$BASE_URL/mw-mediadesign/index.php
PAGE_URL="$WIKI_API?action=render&title=$PAGE_TITLE"
OUTPUT=~/public_html/t2.html
STYLE="@import url('$WIKI_API?action=raw&ctype=text/css&title=User:Tommi/T2_Assessment/style.css');"
PAGE=`curl --location $PAGE_URL` # Using curl is better than wget to store the content in stdout. Nevertheless, absolute links to resources must be transformed at a later time.
# The following is a very basic HTML template from which data is read.
CONTENT="<!DOCTYPE HTML><html lang=en><head><title>$TITLE</title><meta charset=UTF-8><style>$STYLE</style></head><body><main><h1>$TITLE</h1><article>$PAGE</article></main></body></html>"
# Saving the HTML content parsed by MediaWiki to a local output file
echo "$CONTENT" > "$OUTPUT"
# This regular expression goes through the output file and prepends the wiki’s BASE_URL to all absolute links
sed -E --in-place 's/("| )\/mw-mediadesign/\1https:\/\/pzwiki.wdka.nl\/mw-mediadesign/g' "$OUTPUT"
Improvements
- Some variables should be passed as arguments, instead of hardcoded
- Some variables should be stored as environment variables, instead of hardcoded
- The HTML content template could be read from a template file