Skip to content

Instantly share code, notes, and snippets.

@nybblr
Last active December 21, 2017 12:48
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 nybblr/43c2ff7b83c390283081945e0e535d74 to your computer and use it in GitHub Desktop.
Save nybblr/43c2ff7b83c390283081945e0e535d74 to your computer and use it in GitHub Desktop.
Crawl a local Ghost blog instance and convert to a production-ready static site!
#!/bin/bash
DEV_HOST="localhost:2368"
PROD_HOST="blog.yellowscale.com"
BUILD_DEST=${1:-./build}
DEV_URL="http://${DEV_HOST}"
PROD_URL="https://${PROD_HOST}"
# Hop into build directory
pushd $BUILD_DEST > /dev/null
# Clean build directory
git rm -rfq .
git clean -fxdq
git reset -q
git checkout -q .gitignore
# Crawl the entire site
# Requires wget: brew install wget
wget -q -r -nH \
"${DEV_URL}/" \
"${DEV_URL}/sitemap.xml" \
"${DEV_URL}/sitemap-pages.xml" \
"${DEV_URL}/sitemap-posts.xml" \
"${DEV_URL}/sitemap-authors.xml" \
"${DEV_URL}/sitemap-tags.xml" \
"${DEV_URL}/rss"
wget -q -x -nH --content-on-error \
"${DEV_URL}/404.html"
# Strip query params from file names
for i in `find . -type d -path "./.git" -prune -o -type f -print`
do
mv $i `echo $i | cut -d? -f1`
done
# Update production URL
# Requires gnu-sed on macOS: brew install gnu-sed
ESCAPE_REGEX='s/[]\/$*.^[]/\\&/g'
ESCAPED_DEV_HOST="$(echo $DEV_HOST | sed -e $ESCAPE_REGEX)"
ESCAPED_PROD_HOST="$(echo $PROD_HOST | sed -e $ESCAPE_REGEX)"
ESCAPED_DEV_URL="$(echo $DEV_URL | sed -e $ESCAPE_REGEX)"
ESCAPED_PROD_URL="$(echo $PROD_URL | sed -e $ESCAPE_REGEX)"
find . -type d -path "./.git" -prune -o -type f -exec gsed -i'' -e "s/${ESCAPED_DEV_URL}/${ESCAPED_PROD_URL}/g" {} +
find . -type d -path "./.git" -prune -o -type f -exec gsed -i'' -e "s/${ESCAPED_DEV_HOST}/${ESCAPED_PROD_HOST}/g" {} +
# Return to original directory
popd > /dev/null
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment