Copyright | © 2015–2016 Mark Karpov |
---|---|
License | BSD 3 clause |
Maintainer | Mark Karpov <markkarpov@openmailbox.org> |
Stability | experimental |
Portability | portable |
Safe Haskell | None |
Language | Haskell2010 |
Type-safe slug implementation for Yesod ecosystem.
- data Slug
- mkSlug :: MonadThrow m => Text -> m Slug
- unSlug :: Slug -> Text
- parseSlug :: MonadThrow m => Text -> m Slug
- truncateSlug :: MonadThrow m => Int -> Slug -> m Slug
- data SlugException
Documentation
Slug. Textual value inside is always guaranteed to have the following qualities:
- it's not empty;
- it consists only of alpha-numeric groups of characters (words)
separated by
'-'
dashes in such a way that entire slug cannot start or end in a dash and also two dashes in a row cannot be found; - every character with defined notion of case is lower-cased.
Slugs are good for semantic URLs and also can be used as identifier of a sort in some cases.
mkSlug :: MonadThrow m => Text -> m Slug Source
Create Slug
from Text
, all necessary transformations are
applied. Argument of this function can be title of an article or
something like that.
Note that result is inside MonadThrow
, that means you can just get it
in Maybe
, in more complex contexts it will throw SlugException
exception using InvalidInput
constructor.
This function also has a useful property:
mkSlug = mkSlug >=> mkSlug . unSlug
parseSlug :: MonadThrow m => Text -> m Slug Source
Convert Text
into Slug
only when it is already valid slug.
This function can throw SlugException
exception using InvalidSlug
constructor.
:: MonadThrow m | |
=> Int | Maximum length of slug, must be greater than 0 |
-> Slug | Original non-truncated slug |
-> m Slug | Truncated slug |
Ensure that given Slug
is not longer than given maximum number of
characters. If truncated slug ends in a dash, remove that dash too. (Dash
at the end would violate properties described in documentation for
Slug
.)
If the first argument is not a positive number, SlugException
is thrown
using InvalidLength
constructor.
data SlugException Source
InvalidInput Text | Slug cannot be generated for given text |
InvalidSlug Text | Input is not a valid slug, see |
InvalidLength Int | Requested slug length is not a positive number |