Coder Social home page Coder Social logo

slugger's Introduction

Slugger

Build Status

This package provides a library and a protocol to create slugs for given strings.

By default, a slug will be containing only chars A-Za-z0-9 and the default seperator -.

Library

Using the library is straightforward, check out a few examples:

iex(1)> Slugger.slugify " A b C "
"A-b-C"

iex(2)> Slugger.slugify_downcase " A b C "
"a-b-c"

iex(3)> Slugger.slugify "A cool title of a blog post"
"A-cool-title-of-a-blog-post"

iex(4)> Slugger.slugify "Wikipedia Style", ?_
"Wikipedia_Style"

Protocol

Next to the library, a protocol is provided to ease creating slugs for own data structures. By default, the protocol will try to run Slugger.slugify(Kernel.to_string(your_data)), so if your_data implements String.Chars, the returned string will be slugified. If you want to provide a own way to create a slug, check out the following example:

iex(10)> defmodule User do
...(10)>   defstruct name: "Julius Beckmann"
...(10)> end

iex(11)> defimpl Slugify, for: User do   
...(11)>   def slugify(user), do: Slugger.slugify(user.name)
...(11)> end

iex(12)> Slugify.slugify %User{}                          
"Julius-Beckmann"

Replacements

Special chars like äöüéáÁÉ will be replaced by rules given in the file lib/replacements.exs.

Modify that file if you need have own replacement rules and recompile.

slugger's People

Contributors

h4cc avatar triptec avatar lowks avatar

Watchers

James Cloos avatar  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.