-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathslugify
More file actions
executable file
·58 lines (52 loc) · 1.65 KB
/
slugify
File metadata and controls
executable file
·58 lines (52 loc) · 1.65 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
#! /usr/bin/env sh
# This script is a simple tool generating slug from a string passed via a pipe
# or as an argument.
#
# Please check https://github.com/Mayeu/slugify for more details.
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>.
VERSION=1.0.2
# This is the magic
to_slug() {
# Forcing the POSIX local so alnum is only 0-9A-Za-z
export LANG=POSIX
export LC_ALL=POSIX
# Keep only alphanumeric value
sed -e 's/[^[:alnum:]]/-/g' |
# Keep only one dash if there is multiple one consecutively
tr -s '-' |
# Lowercase everything
tr A-Z a-z |
# Remove last dash if there is nothing after
sed -e 's/-$//'
}
# Consume stdin if it exist
if test -p /dev/stdin; then
read -r input
fi
# Now check if there was input in stdin
if test -n "${input}"; then
echo "${input}" | to_slug
exit
# No stdin, let's check if there is an argument
elif test -n "${1}"; then
echo "${1}" | to_slug
exit
else
# Otherwise we return
name=$(basename "${0}")
echo "version 1.0.0"
echo "Usage: ${name} text"
echo " echo text | ${name}"
exit
fi