• Stars
    star
    160
  • Rank 234,703 (Top 5 %)
  • Language
    Elixir
  • License
    MIT License
  • Created almost 10 years ago
  • Updated about 3 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

A Slugger for elixir.

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 -.

Want to use this library with Ecto? Have a look at sobolevn/ecto_autoslug_field.

Installation

Add slugger to your list of dependencies in mix.exs:

def deps do
  [
    {:slugger, "~> 0.3"},
  ]
end

Configuration

The following options can be set in your config.exs and will be used at next compile!

# Char used as separator between words.
config :slugger, separator_char: ?-

# Path to the file containing replacements.
config :slugger, replacement_file: "lib/replacements.exs"

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_downcase("Kluski Śląskie @ Jalapeño Bilingüe")
"kluski-slaskie-at-jalapeno-bilinguee"

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

iex(6)> Slugger.truncate_slug "A-to-long-slug-that-should-be-truncated", 16
"A-to-long-slug"

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 your 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.

Copy that file if you need have own replacement rules, change the config value and recompile.

More Repositories

1

awesome-elixir

A curated list of amazingly awesome Elixir and Erlang libraries, resources and shiny things. Updates:
Elixir
12,518
star
2

wkhtmltopdf-amd64

wkhtmltopdf - Convert html to pdf using webkit (qtwebkit). Linux amd64 Binary.
PHP
444
star
3

wkhtmltoimage-amd64

wkhtmltoimage - Convert html to image using webkit (qtwebkit). Linux amd64 Binary.
203
star
4

AliceFixturesBundle

A Symfony2 bundle for using Alice and Faker with data fixtures. Abled to use Doctrine ORM as well as Doctrine MongoDB ODM.
PHP
76
star
5

phpqatools

A meta package for composer of PHP QA Tools like PHPUnit, PHPLOC, PHPCPD, PHP_Depend, PHPMD, PHP_CodeSniffer
62
star
6

wkhtmltopdf-i386

wkhtmltopdf - Convert html to pdf using webkit (qtwebkit). Linux i386 Binary.
56
star
7

wkhtmltoimage-i386

wkhtmltoimage - Convert html to image using webkit (qtwebkit). Linux i386 Binary.
32
star
8

stack-psr7-bridge

A StackPHP Middleware for using Symfony Http Foundation and PSR-7 transparent.
PHP
15
star
9

multipart

A PHP library for parsing (and generating) RFC1341 Multipart.
PHP
14
star
10

hhvm-progress

Source of the hhvm.h4cc.de website.
PHP
8
star
11

StackLogger

Stack Middleware for logging Requests and Responses to a PSR-3 Logger
PHP
7
star
12

docker-ubuntu-php

Docker images for multiple Ubuntu and PHP version combinations.
Shell
6
star
13

GuzzleRollingBatch

A parallel executor for Guzzle Requests.
PHP
5
star
14

AliceDemo

A example Symfony2 Application using h4cc/AliceFixureBundle in multiple ways.
PHP
5
star
15

LoggerExtraBundle

Symfony2 Bundle for Logging related functionality.
PHP
4
star
16

OsCommand

A Elixir module for running operating system commands.
Elixir
4
star
17

get-to-know-couchdb-presentation

A presentation to introduce and explain CouchDB.
3
star
18

stack-markdown

A StackPHP Middleware for parsing markdown to html.
PHP
2
star
19

Finder

A Elixir module for finding files and directories in a given path.
Elixir
2
star
20

mongrel2

A Handler library for Mongrel2 and the according ZeroMQ message protocol.
PHP
1
star
21

php7-symfony-nodejs

1
star
22

silex-couchdb-blog

Learned me some CouchDB using Silex and DoctrineCouchDBClient by writing a Blog.
PHP
1
star
23

porting-slugger-library-to-elixir-presentation

A short and simple presentation on porting a existing Slugger library to elixir using code generation and protocols.
Makefile
1
star
24

depicter

A aggregating PHP continuous integration tool.
PHP
1
star
25

stack-mongrel2

A adapter for using StackPHP with Mongrel2 via the HttpKernelInterface.
PHP
1
star