-
Notifications
You must be signed in to change notification settings - Fork 0
/
View.hs
52 lines (48 loc) · 1.49 KB
/
View.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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
module View (
suggestions,
getTemplate
) where
import Data.Char (toLower)
import Data.Maybe (catMaybes)
import Text.Regex.Posix ((=~))
import Text.StringTemplate (getStringTemplate, STGroup, StringTemplate)
{-|
Function which converts the input string to lowercase, then
strips out all of the non [a-z_] characters to make template
names easier to handle.
-}
formatSuggPart :: String -> String
formatSuggPart =
strip . map toLower
where strip = concat . (=~ "[_a-z]")
{-|
Takes the names of the module, action, section and ID of the
piece being rendered and returns a list of possible template
names for rendering the object.
-}
suggestions :: String -> String -> String -> Integer -> [String]
suggestions mod act sec id =
let sid = show id in
let lmod = map toLower mod in
let lact = map toLower act in
let lsec = map toLower sec in
[
lmod ++ "-" ++ lact ++ "-" ++ lsec ++ "-" ++ sid,
lmod ++ "-" ++ lact ++ "-" ++ lsec,
lmod ++ "-" ++ lsec ++ "-" ++ sid,
lmod ++ "-" ++ lsec,
lsec ++ "-" ++ sid,
lsec
]
{-|
Takes a STGroup and a list of template suggestions and returns a
single StringTemplate (the first one in the suggestion list with
a corresponding template. If there is no matching template it
will raise an error.
-}
getTemplate :: STGroup String -> [String] -> StringTemplate String
getTemplate tpls suggs =
let tpls' = catMaybes $ map (\x -> getStringTemplate x tpls) suggs in
case tpls' of
[] -> error $ "No matching template in " ++ show suggs
_ -> tpls' !! 0