# These functions are available within Bash sub-shells, when # writing your Mustache templates and your Markdown. # For example, the function 'uppercase' can be used like so # in your Markdown: # # # # And here's how to use it in your template files: # # {{var | uppercase}} # # These functions are intended to mimic some of the liquid # templating filters, listed here: # https://github.com/Shopify/liquid/wiki/Liquid-for-Designers # get the real `bc`, make sure we're not using an alias bc=$(which bc) str='' function get_stdin { read STDIN; } function timestamp { date +%s } # date filters function date_format { get_stdin case "$1" in basic) date -d $STDIN -u +"%m/%d/%Y" || echo -n $STDIN ;; basic_uk) date -d $STDIN -u +"%d/%m/%Y" || echo -n $STDIN ;; iso8601) date -d $STDIN -u +"%Y-%m-%d" || echo -n $STDIN ;; to_string) date -d $STDIN -u +"%d %b %Y" || echo -n $STDIN ;; to_long_string) date -d $STDIN -u +"%d %B %Y" || echo -n $STDIN ;; rfc822|rfc2822|rss|email) LANG=C LC_ALL=C LC_CTYPE=C date -d $STDIN -u +"%a, %d %b %Y %H:%M:%S %z" || echo -n $STDIN ;; *"%"*) date -d $STDIN -u +"$1" || echo -n $STDIN ;; *) date return 1 ;; esac return 0 } # string filters function base_name { local input="$1" [ ! -f "$1" ] && get_stdin && input="$STDIN" echo -n "${input##*/}" } function dir_name { local input="$1" [ ! -f "$1" ] && get_stdin && input="$STDIN" echo -n "${input%/*}" } function uppercase { tr '[:lower:]' '[:upper:]'; } function lowercase { tr '[:upper:]' '[:lower:]'; } function titlecase { get_stdin echo -n "${STDIN:0:1}" | uppercase echo -n "${STDIN:1}" | lowercase } function capitalize { get_stdin for word in ${STDIN} do echo -n "${word:0:1}" | uppercase echo -n "${word:1}" | lowercase echo -n " " done } alias capitalise=capitalize function pluralize { get_stdin local singular="${1:-item}" local plural="${2:-item}" echo -n "$STDIN " if [ ${STDIN:-1} -gt 1 ] || [ ${STDIN:-0} -eq 0 ] ;then echo -n "$plural" return 0 fi echo -n "$singular" } alias pluralise=pluralize function slugify { get_stdin local slugified="$(echo -n "$STDIN" | sed -e 's/[^[:alnum:]]/-/g' 2>/dev/null | tr -s '-' | tr A-Z a-z | sed -e 's/-$//g' 2>/dev/null)" local RETVAL=$? if [ "$slugified" = "" ] || [ $RETVAL -gt 0 ];then slugified="$(echo "$STDIN" | iconv -t ascii//TRANSLIT | sed -r s/[^a-zA-Z0-9]+/-/g | sed -r s/^-+\|-+$//g | tr A-Z a-z)" RETVAL=$? fi [ "$slugified" != "" ] && echo -n "$slugified" || return 1 } function camelcase_to_slug { sed 's/\(.\)\([A-Z]\)/\1-\2/g' | lowercase } function slug_to_camelcase { get_stdin IFS=- read -ra newstr <<<"${STDIN}" printf '%s' "${newstr[@]^}" } function lstrip { sed 's/^ *//'; } function rstrip { sed 's/ *$//'; } function strip { sed -e 's/^ *//' -e 's/ *$//'; } function strip_html { sed 's/<[^>]*>//g'; } function strip_newlines { tr -d '\n'; } function escape_html { sed -e 's/>/\>/g' -e 's//g' -e 's/\</}" } function br_to_newline { get_stdin local out="$(echo "${STDIN//
/ }")" out="${out//
/ }" out="${out/// }" echo -e "$out" } function reverse { rev; } function replace_first { sed "s/$1/$2/"; } function replace_last { sed "s/\(.*\)$1/\1$2/"; } function replace_all { sed "s/$1/$2/g"; } function prepend { get_stdin; echo -n "$1$STDIN"; } function append { get_stdin; echo -n "$STDIN$1"; } function __print_truncation { get_stdin echo -n "$1" if [ "$1" = "$2" ];then return 1 else echo -n "$3" return 0 fi } function truncate { get_stdin local truncated="$(cut -b-${1:-9999} <<< "$STDIN")" __print_truncation "$truncated" "$STDIN" "$2" } function truncate_words { get_stdin local truncated="$(echo "$STDIN" | cut -d' ' -f1-${1:-9999})" __print_truncation "$truncated" "$STDIN" "$2" } function urlencode { get_stdin local LC_CTYPE=C local length="${#STDIN}" for (( i = 0; i < length; i++ )); do local c="${STDIN:i:1}" case $c in [a-zA-Z0-9.~_-]) printf "$c" ;; *) printf '%%%02X' "'$c" esac done } function urldecode { get_stdin STDIN="${STDIN:-$@}" local LC_CTYPE=C #while read; do : "${STDIN//%/\\x}"; echo -e ${_//+/ }; done # bash only, from https://stackoverflow.com/questions/6250698/how-to-decode-url-encoded-string-in-shell local url_encoded="${STDIN//+/ }" printf '%b' "${url_encoded//%/\\x}" } function base64_encode { #perl -MMIME::Base64 -0777 -ne 'print encode_base64($_)' # perl solution base64 } function base64_decode { #perl -MMIME::Base64 -ne 'print decode_base64($_)' # perl solution base64 -d } function html_decode { local LC_CTYPE=C [ "`which python3`" != "" ] && python3 -c 'import html, sys; [print(html.unescape(l), end="") for l in sys.stdin]' && return perl -MHTML::Entities -pe 'decode_entities($_);' } function html_encode { local LC_CTYPE=C [ "`which python3`" != "" ] && python3 -c 'import html, sys; [print(html.escape(l), end="") for l in sys.stdin]' && return perl -MHTML::Entities -pe 'encode_entities($_);' } function rgb2hex { get_stdin; printf "#%02x%02x%02x\n" ${STDIN}; } function hex2rgb { get_stdin; STDIN="$(echo "${STDIN}" | sed 's/^#//')"; [ "${#STDIN}" = "3" ] && STDIN="$STDIN$STDIN" printf "%d %d %d\n" 0x${STDIN:0:2} 0x${STDIN:2:2} 0x${STDIN:4:2}; } function md5 { md5sum | sed 's/ .*$//'; } function sha1 { sha1sum | sed 's/ .*$//'; } function sha256 { sha256sum | sed 's/ .*$//'; } function sha512 { sha512sum | sed 's/ .*$//'; } function slice { local from=$((${1:-0} + 1)) local to=$((${2:-0} + 1)) from=${from//0/1} to="-${to//0/1}" [ -z "$2" ] && to='' cut -b${from}${to} 2>/dev/null } function absolute_url { get_stdin; echo -n "https://${site_domain}${site_url}/$STDIN"; } function relative_url { get_stdin; echo -n "${site_url}/$STDIN"; } function asset_url { get_stdin local asset_file="$(find assets -type f -name "$STDIN")" asset_file="${asset_file:-$STDIN}" echo -n "${asset_file}?$(date +%s)" } function time_tag { get_stdin local datetime="$STDIN" # format the innerText in of the