Git Product home page Git Product logo

bash-slugify's Introduction

BASH Slugify

It slugifies strings. In your BASH. Well, I need to clean filenames — like uploaded images — regularly and this helps me automate it.

What's happening?

  • Optionally convert to upper- or lower case (default: no change).

  • Optionally all special chars ([^a-zA-Z0-9]) are removed or replaced (default: _).

  • Optionally all or some glue characters ([-_.]) are replaced with spaces.

  • Whitespace is consolidated and trimmed.

  • All remaining spaces are replaced (default: -).

Examples

$ ./slugify.sh foo 'Hello World!' Bar
foo
Hello-World!
Bar

$ ./slugify.sh foo -l 'Hello World!' Bar
foo
hello-world!
bar

$ ./slugify.sh -e foo 'Hello World!' Bar
foo-Hello-World!-Bar

$ ./slugify.sh -exl foo Hello World! Bar
foo-hello-world-bar

If you use the -r switch, arguments are threated as file names to be slugifed (and renamed). Here's the output if you run it dry (no renaming) in the directory of this repository — $ ./slugify.sh -rn -lx *:

CHANGELOG.md -> changelog.md
LICENSE -> license
README.md -> readme.md
src -> skipped (no change)
tests -> skipped (no change)

Usage

Slugifies strings or filenames.

usage: slugify [(l|u)|(x|X)|E|D|U|P|A|S|(c< char>|s)|C< char>|e|r|n|f|v|d|h]… <string>…

character options
  -l  convert to lowercase
  -u  convert to uppercase
  -x  remove special chars
  -X  replace special chars

space options
  -D  don't convert dashes to spaces
  -U  don't convert underscores to spaces
  -P  don't convert dots (points) to spaces
  -A  don't remove spaces around dashes and underscores
  -S  don't consolidate multiple spaces

replacement options
  -c <char> replace all spaces with this (default: '-')
  -C <char> replace special chars with this (default: '_')
  -s use underscores for spaces (shortcut for '-c_')

mode options
  -e  extended — treat all arguments as one string and echo one slug for it
  -r  rename files – treat arguments as filenames and rename them

rename options
  -E  don't slugify file extensions
  -n  dry run — only show new file names (no renaming)
  -f  force — overwrite existing files

other options
  -v  verbose output
  -d  debug output
  -h  print help

Return Values

  • 0 all went well
  • 1 error parsing arguments
  • 2 original string is empty
  • 3 slugified string is empty
  • 4 original file not found
  • 5 slugified file exists

Development

Tested with Bats. Okay just barely…

bash-slugify's People

Contributors

brutus avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.