/
Ref.hs
31 lines (25 loc) · 1.14 KB
/
Ref.hs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
--------------------------------------------------------------------------------
-- | This module contains utilities for creating text fragments to identify
-- forms.
{-# LANGUAGE OverloadedStrings #-}
module Text.Digestive.Ref
( makeRef
, makeRefs
) where
--------------------------------------------------------------------------------
import qualified Data.ByteString as B
import Data.Text (Text)
import qualified Data.Text as T
import qualified Data.Text.Encoding as T
import Text.Printf (printf)
--------------------------------------------------------------------------------
-- | Convert an arbitrary text value (possibly containing spaces, dots etc. to
-- a text value that safely be used as an identifier in forms.
makeRef :: Text -> Text
makeRef =
-- We simply UTF-8 encode and then hex encode, so all characters are valid.
T.append "df-" . T.pack . (printf "%02x" =<<) . B.unpack . T.encodeUtf8
--------------------------------------------------------------------------------
-- | Create an infinite list of refs.
makeRefs :: [Text]
makeRefs = map (T.pack . show) [0 :: Int ..]