Last active
August 29, 2015 14:22
-
-
Save FranciscoG/b9113fa9290dfa638d3f to your computer and use it in GitHub Desktop.
simple shell script to convert a simple php site to flat html files using wget. Pages must end in ".php", do not use url rewriting (which shouldn't matter to you if flat HTML is your desired end result anyways).
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
#!/bin/bash | |
########################################################################### | |
# Change these variables to whatever site you want to grab | |
# and where you want it to be saved | |
# | |
targetDir='' | |
site='' | |
# credentials for basic HTTP auth | |
user='' | |
pw='' | |
########################################################################### | |
# Do not touch below this area | |
# | |
wget="$(which wget)" | |
# check if a command line program exists | |
function exists () { | |
$1 -v > /dev/null 2>&1 | |
} | |
function getBrew () { | |
if ! exists brew ; then | |
echo "installing homebrew" | |
# from: http://brew.sh/ | |
ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)" | |
else | |
echo "Brew already installed" | |
fi | |
} | |
function getWget() { | |
if [ ! -f $wget ]; then | |
echo "installing wget using homebrew" | |
# from: http://www.merenbach.com/software/wget/ | |
brew update && brew install wget | |
wget="$(which wget)" | |
else | |
echo "wget already installed" | |
fi | |
} | |
function runConvert() { | |
# create directoy if it doesn't exist | |
if [ ! -d "$targetDir" ]; then | |
mkdir "$targetDir" | |
fi | |
# go into dir | |
cd $targetDir | |
# run wget to get all files | |
$wget -nH -m -k -p --http-user=$user --http-password=$pw ${site} | |
# replace ".php" with ".html" | |
for file in *.php | |
do | |
sed -i.bak s/.php/.html/g ${file} && mv "$file" "${file%.php}.html" | |
done | |
# do the same replacement in the index.html file since it's the only file that wasn't a .php file | |
sed -i.bak s/.php/.html/g index.html | |
# kill the sed backup files | |
rm -f *.bak | |
} | |
# check for Homebrew and Wget and install them if they do not exist | |
getBrew && getWget && runConvert |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment