Skip to content

Instantly share code, notes, and snippets.

@pbrisbin
Created December 31, 2014 15:19
Show Gist options
  • Select an option

  • Save pbrisbin/f9ff933a29d8a80bc5d4 to your computer and use it in GitHub Desktop.

Select an option

Save pbrisbin/f9ff933a29d8a80bc5d4 to your computer and use it in GitHub Desktop.
Find image links in markdown sources, put to S3 and update the link
#!/bin/sh
#
# pbrisbin 2015 - look for markdown links to local images, move them to S3 then
# update the markdown to link to it.
#
# Things to tweak:
#
# - Processes posts/*.md
# - Looks for "/img/image.ext"
# - Puts img/image.ext to s3://$bucket/$(prefix $post)image.ext
# - Replaces link with "http://$domain/$(prefix $post)image.ext"
#
###
process() {
local post="$1" img="$2" path
path="$(prefix "$post")$(basename "$img")"
s3cmd --acl-public put "$img" "s3://$bucket/$path" &&
sed -i "s%(/$img)%(http://$domain/$path)%" "$post"
}
prefix() {
# "post/YYYY-MM-DD-title.md" --> "title/"
echo "$(basename "$1" .md)/" | cut -c 12-
}
get_posts() { grep -Fl '/img/' posts/*.md; }
get_images() { sed '/.*\/\(img\/[^)]*\).*/!d; s//\1/' "$1"; }
bucket='images.pbrisbin.com'
domain="$bucket"
get_posts | while read post; do
get_images "$post" | while read image; do
process "$post" "$image"
done
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment