• Stars
    star
    526
  • Rank 81,001 (Top 2 %)
  • Language
    Ruby
  • License
    MIT License
  • Created almost 14 years ago
  • Updated 10 months ago

Reviews

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

Repository Details

A library for creating slugs. Babosa is an extraction and improvement of the string code from FriendlyId, intended to help developers create similar libraries or plugins.

Babosa

Build Status

Babosa is a library for creating human-friendly identifiers, aka "slugs". It can also be useful for normalizing and sanitizing data.

It is an extraction and improvement of the string code from FriendlyId. I have released this as a separate library to help developers who want to create libraries similar to FriendlyId.

Features / Usage

Transliterate UTF-8 characters to ASCII

"Gölcük, Turkey".to_slug.transliterate.to_s #=> "Golcuk, Turkey"

Locale sensitive transliteration, with support for many languages

"Jürgen Müller".to_slug.transliterate.to_s           #=> "Jurgen Muller"
"Jürgen Müller".to_slug.transliterate(:german).to_s  #=> "Juergen Mueller"

Currently supported languages include:

  • Bulgarian
  • Danish
  • German
  • Greek
  • Hindi
  • Macedonian
  • Norwegian
  • Romanian
  • Russian
  • Serbian
  • Spanish
  • Swedish
  • Turkish
  • Ukrainian
  • Vietnamese

Additionally there are generic transliterators for transliterating from the Cyrillic alphabet and Latin alphabet with diacritics. The Latin transliterator can be used, for example, with Czech. There is also a transliterator named "Hindi" which may be sufficient for other Indic languages using Devanagari, but I do not know enough to say whether the transliterations would make sense.

I'll gladly accept contributions from fluent speakers to support more languages.

Strip non-ASCII characters

"Gölcük, Turkey".to_slug.to_ascii.to_s #=> "Glck, Turkey"

Truncate by characters

"üüü".to_slug.truncate(2).to_s #=> "üü"

Truncate by bytes

This can be useful to ensure the generated slug will fit in a database column whose length is limited by bytes rather than UTF-8 characters.

"üüü".to_slug.truncate_bytes(2).to_s #=> "ü"

Remove punctuation chars

"this is, um, **really** cool, huh?".to_slug.word_chars.to_s #=> "this is um really cool huh"

All-in-one

"Gölcük, Turkey".to_slug.normalize.to_s #=> "golcuk-turkey"

Other stuff

Using Babosa With FriendlyId 4+

require "babosa"

class Person < ActiveRecord::Base
  friendly_id :name, use: :slugged

  def normalize_friendly_id(input)
    input.to_s.to_slug.normalize(transliterations: :russian).to_s
  end
end

UTF-8 support

Babosa normalizes all input strings to NFC.

Ruby Method Names

Babosa can generate strings for Ruby method names. (Yes, Ruby 1.9+ can use UTF-8 chars in method names, but you may not want to):

"this is a method".to_slug.to_ruby_method! #=> this_is_a_method
"über cool stuff!".to_slug.to_ruby_method! #=> uber_cool_stuff!

# You can also disallow trailing punctuation chars
"über cool stuff!".to_slug.to_ruby_method(allow_bangs: false) #=> uber_cool_stuff

Easy to Extend

You can add custom transliterators for your language with very little code. For example here's the transliterator for German:

module Babosa
  module Transliterator
    class German < Latin
      APPROXIMATIONS = {
        "ä" => "ae",
        "ö" => "oe",
        "ü" => "ue",
        "Ä" => "Ae",
        "Ö" => "Oe",
        "Ü" => "Ue"
      }
    end
  end
end

And a spec (you can use this as a template):

require "spec_helper"

describe Babosa::Transliterator::German do
  let(:t) { described_class.instance }
  it_behaves_like "a latin transliterator"

  it "should transliterate Eszett" do
    t.transliterate("ß").should eql("ss")
  end

  it "should transliterate vowels with umlauts" do
    t.transliterate("üöä").should eql("ueoeae")
  end
end

Rails 3.x and higher

Some of Babosa's functionality was added to Active Support 3.0.0.

Babosa now differs from ActiveSupport primarily in that it supports non-Latin strings by default, and has per-locale ASCII transliterations already baked-in. If you are considering using Babosa with Rails, you may want to first take a look at Active Support's transliterate and parameterize to see if they suit your needs.

Please see the API docs and source code for more info.

Getting it

Babosa can be installed via Rubygems:

gem install babosa

You can get the source code from its Github repository.

Reporting bugs

Please use Babosa's Github issue tracker.

Misc

"Babosa" means "slug" in Spanish.

Maintainers

Contributors

Many thanks to the following people for their help:

Copyright

Copyright (c) 2010-2021 Norman Clarke and contributors

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

More Repositories

1

friendly_id

FriendlyId is the “Swiss Army bulldozer” of slugging and permalink plugins for ActiveRecord. It allows you to create pretty URL’s and work with human-friendly strings as if they were numeric ids for ActiveRecord models.
Ruby
6,088
star
2

disqus

A Ruby library for the Disqus commenting API and Javascript widgets.
Ruby
249
star
3

telescope

A highly customizable test library for Lua that allows declarative tests with nested contexts.
Lua
159
star
4

haml-scaffold

Rails scaffold generator that outputs Haml and better functional tests.
Ruby
114
star
5

squirm_rails

Easily use and manage Postgres stored procedures with Active Record.
Ruby
73
star
6

friendly_id-globalize

Globalize support for FriendlyId
Ruby
65
star
7

ambry

Ambry is a database and ORM replacement for (mostly) static models and small datasets. It provides ActiveModel compatibility, and flexible searching and storage.
Ruby
56
star
8

lua-haml

Haml for Lua
Lua
52
star
9

hello-lua

A demo of how to make simple C modules for Lua
C
33
star
10

squirm

A library that simplifies working with Postgres stored procedures.
Ruby
18
star
11

grackle

A static blog generator written in Lua
Lua
18
star
12

lua-postgres

A basic Postgres driver for Lua
C
16
star
13

has_image

A lightweight and hackable library for attaching images to ActiveRecord models.
Ruby
16
star
14

phonenumber

Allows parsing and formatting of phone numbers
Ruby
16
star
15

luacov

LuaCov is a simple coverage analyzer for Lua code. (fork of official CVS repo)
15
star
16

hops

A lightweight, pluggable web framework for Lua
Lua
15
star
17

lua-devtools

An irb-workalike for Lua, and a command-line debugger with readline support.
Lua
15
star
18

tlua

A simple task runner for Lua - now abandoned because I think it's just easier to use plain old Makefiles.
Lua
14
star
19

spanish

Linguistic utilities for working with Spanish words.
Ruby
12
star
20

enc

Notes and slides from my Encodings talk at RubyConf Brasil 2010
Ruby
12
star
21

nearby

Quick and easy geocoding using Geonames.org data and TokyoCabinet.
Ruby
10
star
22

nt54

Argentine phone number parsing, validating, formatting and meta-info
Ruby
10
star
23

active_record_random

Monkey patch to ActiveRecord to allow :order => :random that works the same for MySQL, SQLite and Postgres.
9
star
24

fatalistic

Table locking for Active Record
Ruby
9
star
25

yourbugreportneedsmore.info

The yourbugreportneedsmore.info website
HTML
8
star
26

friendly_id_manual_slug_demo

A Rails 3 app showing how to manually control FriendlyId slugs
Ruby
7
star
27

mongrel2_wsapi

In-progress Lua WSAPI adapter for Mongrel2
Lua
6
star
28

base-site-generator

The current base layout and helpers I'm using to scaffold quick projects. Feel free to use if and as you wish.
Ruby
6
star
29

squirm_model

Model API for Squirm
Ruby
5
star
30

micro_factory

Minimal factories for Active Record.
Ruby
4
star
31

utf8_utils

Utilities for cleaning up UTF8 strings.
Ruby
4
star
32

phonology

Phonology utilities for Ruby
Ruby
3
star
33

dotfiles

My dotfiles
Vim Script
2
star
34

wsapi_test

Mock connector for unit testing WSAPI apps. TEMP. Go here instead: http://github.com/norman/wsapi
Lua
2
star
35

hash_formatter

Hash Formatter is a library that formats Ruby hashes for code editors.
Ruby
2
star
36

gem_init

My Ruby Gem initialization biolerplate
Ruby
2
star
37

luaargentina

luaargentina.org website
2
star
38

luadns-zones

My DNS zones hosted on LuaDNS.
Lua
1
star
39

friendly_id_join_test

Temporary debugging repo, don't follow
Ruby
1
star
40

haml_textarea_bug

Demonstrates a bug in Haml
Ruby
1
star
41

vim-files

My vim files
Vim Script
1
star
42

friendly_id_sequel

An adapter which allows you to use FriendlyId with Sequel::Model
Ruby
1
star