Download code snippets (http://codesnippets.joyent.com) from the command line and convert them to text files using man textutil (on Mac OS X 10.4).
Usage:
snippet 345
snippet 1268
snippet 1281
snippet 182
snippet 1282
snippet 1115
snippet 5
snippet 23444
function snippet() {
declare NL OPWD download_dir outputfile postnum title url
if [[ "${1//[[:digit:]]/}" != "" ]]; then echo "Argument error. No positive integer: ${1}"; return 1; fi
postnum="${1}"
url="http://codesnippets.joyent.com/posts/show/${postnum}"
download_dir="${HOME}/Desktop/Snippets"
download_dir="${download_dir%/}"
/bin/mkdir -p "${download_dir}"
OPWD="$PWD"
cd "${download_dir}"
/usr/bin/curl -L -O -s --max-time 25 "${url}" || exit 1
file="${download_dir}/${url##*/}"
title="$(/usr/bin/sed -E -n -e '/<[tT][iI][tT][lL][eE]>/{s/^.*<[tT][iI][tT][lL][eE]>(.*)<\/[tT][iI][tT][lL][eE]>.*$/\1/p;q;}' "${file}" | \
sed -E -e 's/\[[^][:space:]]*\]//g')"
title="${title//CodeSnippets:/}"
title="${title//\//:}"
title="${title// /_}"
title="${title//[[:cntrl:]]/}"
title="${title%"${title
if [[ $title == '_CodeDrive_Snippets_courtesy_of_Peter_Coopers_handy_little_app' ]] || [[ -z "$title" ]]; then
printf "\e[0K\e[31m%s\e[0m: %s\n" "couldn't access" "${url}"
/bin/rm "${file}"
return 1
fi
outputfile="${HOME}/Desktop/Snippets/${postnum}_${title}.txt"
printf "\n\e[0K\e[1;30m%s\e[0m: %s\n\n" "saved as" "${outputfile}"
/usr/bin/textutil -output "${outputfile}" -convert txt "${file}"
/bin/rm "${file}"
outputfile="${outputfile//\\/\\\\}"
NL=$'\\\n'
cat <<EOF | /bin/ed -s "${outputfile}"
H
,g/Snippets is a public source code repository/1,/Snippets is a public source code repository/d
,g/You need to create an account or log in to post comments to this site//You need to create an account or log in to post comments to this site/,\$d
,g|(See related posts)$|s|.See related posts.|${NL}${NL}|
,g|^to.* by.* on .*[[:digit:]]$|s|^to\(.*\) by\(.*\) on \(.*[[:digit:]]\)$|${NL}${NL}Author:\2${NL}Date: \3${NL}URL: ${url}${NL}Tags:\1${NL}|
,g|^Comments on this post$|s|\(Comments on this post\)|${NL}\1:|
,g| posts on .* at |s|\(.* posts on .* at .*\)|${NL}\1:|
w
EOF
cd "$OPWD"
return 0
}