aboutsummaryrefslogtreecommitdiff
#!/bin/sh
set -eu


usage() {
	cat <<-'EOF'
		Usage:
		  slugify [CONTENT...]
		  slugify -h
	EOF
}

help() {
	cat <<-'EOF'


		Options:
		  -h, --help    show this message

		  CONTENT       a literal string to be slugified


		"slugify" the input string, removing diacritics and punctuation
		from the string, on a best-effort basis.  If CONTENT is not
		given, get data from STDIN.


		Examples:

		  Slugify the input string:

		    $ slugify 'Saçi-pererê, tomando açaí!!'
		    saci-perere-tomando-acai
	EOF
}


for flag in "$@"; do
	case "$flag" in
		(--)
			break
			;;
		(--help)
			usage
			help
			exit
			;;
		(*)
			;;
	esac
done

while getopts 'h' flag; do
	case "$flag" in
		(h)
			usage
			help
			exit
			;;
		(*)
			usage >&2
			exit 2
	esac
done
shift $((OPTIND - 1))


esc() {
	iconv -ct ASCII//TRANSLIT |
		tr '[:upper:]' '[:lower:]' |
		sed \
			-e 's/[^a-z0-9]/-/g' \
			-e 's/--*/-/g'       \
			-e 's/^-//'          \
			-e 's/-$//'
}

if [ $# = 0 ]; then
	esc
else
	for s in "$@"; do
		printf '%s\n' "$s" | esc
	done
fi