-
Notifications
You must be signed in to change notification settings - Fork 13
/
Util.hs
155 lines (127 loc) · 4.55 KB
/
Util.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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
{-# OPTIONS_GHC -fno-warn-tabs #-}
{-# LANGUAGE TupleSections, OverloadedStrings, ViewPatterns #-}
module Util (
mconcat, (.), (++), Text, replace, xml, spanTag, h, getDigit, startsWith, urlChars,
anchor, Anchor(..), writeFile, readFile, greekAlphabet, mapLast, mapHead, stripInfix, dropTrailingWs,
textStripInfix, textSubRegex, splitOn, intercalateBuilders, replaceXmlChars, stripAnyPrefix, trimString,
spanJust, measure, partitionBy
) where
import Prelude hiding ((.), (++), writeFile)
import qualified Data.Text as Text
import qualified Data.Map as Map
import Data.List (stripPrefix, intersperse)
import Data.Char (ord, isDigit, isSpace)
import Data.Text (Text, replace)
import Data.Text.IO (writeFile)
import Data.Time (getCurrentTime, diffUTCTime)
import Control.Arrow (first)
import Text.Regex (subRegex, Regex)
import qualified Data.Text.Lazy.Builder as TextBuilder
(.) :: Functor f => (a -> b) -> (f a -> f b)
(.) = fmap
(++) :: Monoid a => a -> a -> a
(++) = mappend
xml :: Text -> [(Text, Text)] -> TextBuilder.Builder -> TextBuilder.Builder
xml t attrs = (TextBuilder.fromText ("<" ++ t ++ " " ++ Text.unwords (map f attrs) ++ ">") ++) . (++ TextBuilder.fromText ("</" ++ t ++ ">"))
where
f (n, v) = n ++ "='" ++ v ++ "'"
spanTag :: Text -> TextBuilder.Builder -> TextBuilder.Builder
spanTag = xml "span" . (:[]) . ("class",)
h :: Int -> TextBuilder.Builder -> TextBuilder.Builder
h = flip xml [] . ("h" ++) . Text.pack . show
data Anchor = Anchor
{ aClass, aId, aHref :: Text
, aText :: TextBuilder.Builder
, aStyle, aTitle :: Text }
intercalateBuilders :: TextBuilder.Builder -> [TextBuilder.Builder] -> TextBuilder.Builder
intercalateBuilders x y = mconcat $ intersperse x y
anchor :: Anchor
anchor = Anchor{aClass="", aId="", aHref="", aText=TextBuilder.fromText "", aStyle="", aTitle=""}
greekAlphabet :: [(String, Char)]
greekAlphabet =
[ ("alpha" , 'α')
, ("beta" , 'β')
, ("gamma" , 'γ')
, ("delta" , 'δ')
, ("mu" , 'μ')
, ("nu" , 'ν')
, ("lambda" , 'λ')
, ("pi" , 'π')
, ("phi" , 'φ')
, ("rho" , 'ρ')
, ("sigma" , 'σ')
, ("theta" , 'θ')
, ("zeta" , 'ζ')
, ("Gamma" , 'Γ')
, ("Pi" , 'Π') ]
mapLast :: (a -> a) -> [a] -> [a]
mapLast _ [] = []
mapLast f [x] = [f x]
mapLast f (x:xx) = x : mapLast f xx
mapHead :: (a -> a) -> [a] -> [a]
mapHead f (x:y) = f x : y
mapHead _ [] = []
getDigit :: Char -> Maybe Int
getDigit c
| isDigit c = Just $ ord c - ord '0'
| otherwise = Nothing
stripInfix :: Eq a => [a] -> [a] -> Maybe ([a], [a])
stripInfix p s | Just r <- stripPrefix p s = Just ([], r)
stripInfix p (hd:t) = first (hd:) . stripInfix p t
stripInfix _ _ = Nothing
textStripInfix :: Text -> Text -> Maybe (Text, Text)
textStripInfix inf (Text.breakOn inf -> (a, b))
| b == "" = Nothing
| otherwise = Just (a, Text.drop (Text.length inf) b)
startsWith :: (Char -> Bool) -> (Text -> Bool)
startsWith _ "" = False
startsWith p t = p (Text.head t)
dropTrailingWs :: String -> String
dropTrailingWs = reverse . dropWhile isSpace . reverse
urlChars :: Text -> Text
urlChars =
replace "'" "'" .
replace "<" "%3c" .
replace ">" "%3e" .
replace "\"" "%22" .
replace "#" "%23" .
replace "{" "%7b" .
replace "|" "%7c" .
replace "}" "%7d" .
replace "[" "%5b" .
replace "\\" "%5c" .
replace "]" "%5d" .
replace "^" "%5e" .
replace " " "%20" .
replace "%" "%25"
textSubRegex :: Regex -> String -> Text -> Text
textSubRegex pat repl txt = Text.pack $ subRegex pat (Text.unpack txt) repl
splitOn :: (a -> Bool) -> [a] -> [[a]]
splitOn _ [] = [[]]
splitOn sep (x:y)
| sep x = [] : splitOn sep y
| otherwise = mapHead (x :) $ splitOn sep y
replaceXmlChars :: Text -> Text
replaceXmlChars =
replace ">" ">" .
replace "<" "<" .
replace "&" "&"
stripAnyPrefix :: [Text] -> Text -> Maybe (Text, Text)
stripAnyPrefix [] _ = Nothing
stripAnyPrefix (x:y) z
| Just a <- Text.stripPrefix x z = Just (x, a)
| otherwise = stripAnyPrefix y z
trimString :: String -> String
trimString = reverse . dropWhile isSpace . reverse . dropWhile isSpace
spanJust :: [a] -> (a -> Maybe b) -> ([b], [a])
spanJust (x : z) f
| Just y <- f x = first (y :) (spanJust z f)
spanJust z _ = ([], z)
measure :: IO a -> IO (a, Float)
measure f = do
start <- getCurrentTime
r <- f
end <- getCurrentTime
return (r, realToFrac $ diffUTCTime end start)
partitionBy :: (Ord b, Eq b) => (a -> b) -> [a] -> [(b, [a])]
partitionBy f l = Map.assocs $ Map.fromListWith (flip (++)) [(f x, [x]) | x <- l]