/
Util.hs
235 lines (225 loc) · 9.23 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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
{-# LANGUAGE QuasiQuotes #-}
{-# LANGUAGE OverloadedStrings #-}
module Util
( renderContent
, validateContent
, validateDita
, userGravatar
, prettyDate
, prettyMonthYear
) where
import Debug.Trace (trace)
import Model (TopicFormat (..), User (userEmail), TopicId)
import Data.Text (Text, pack, unpack)
import Data.Text.Encoding (encodeUtf8)
import Text.Hamlet (Html, preEscapedText, toHtml, preEscapedString)
import Text.HTML.SanitizeXSS (sanitizeBalance)
import Text.Hamlet.NonPoly (html)
import Data.Digest.Pure.MD5 (md5)
import Data.Char (isSpace, toLower)
import Data.Maybe (fromMaybe, catMaybes)
import qualified Data.ByteString.Lazy as L
import Text.Pandoc (writeHtmlString, defaultWriterOptions, readMarkdown, defaultParserState)
import qualified Data.Text as T
import Yesod.Form (Textarea (Textarea))
import System.Locale
import Data.Time (formatTime, UTCTime, fromGregorian)
import Text.XML.Enumerator.Parse (parseText, decodeEntities)
import Text.XML.Enumerator.Document (fromEvents)
import Data.Enumerator (run, ($$), joinI, enumList, run_)
import Data.Enumerator.List (consume)
import Data.Functor.Identity (runIdentity)
import Data.XML.Types (Node (..), Content (..), Document (..), Element (..), Event (..))
import Wiki (WikiRoute (..), fromSinglePiece)
import Data.Maybe (fromJust)
import Text.Hamlet (Hamlet)
import qualified Data.Set as Set
import Control.Monad.Trans.State (evalState, get, put)
import Text.XML.Enumerator.Render (renderText)
import Data.Monoid (mconcat)
import System.IO.Unsafe (unsafePerformIO)
import Yesod.Core (toSinglePiece)
renderContent :: TopicId -> TopicFormat -> Text -> Hamlet WikiRoute
renderContent _ TFHtml t = const $ preEscapedText t
renderContent _ TFText t = const $ toHtml $ Textarea t
renderContent _ TFMarkdown t = const $ preEscapedString $ writeHtmlString defaultWriterOptions $ readMarkdown defaultParserState $ unpack t
renderContent tid TFDitaConcept t = ditaToHtml tid t
renderContent tid TFDitaTopic t = ditaToHtml tid t
ditaToHtml :: TopicId -> Text -> Hamlet WikiRoute
ditaToHtml topic txml render =
case runIdentity $ run $ enumList 3 ["<body>", txml, "</body>"] $$ joinI $ parseText decodeEntities $$ fromEvents of
Left e -> toHtml $ show e
Right (Document _ (Element _ _ nodes) _) -> mapM_ go nodes
where
go (NodeContent (ContentText t')) = toHtml t'
go (NodeElement (Element n as children)) = go' n as $ mapM_ go children
go _ = return ()
go' "p" as x =
case lookup "id" as of
Just [ContentText t] -> [html|<p .hascomments #comment-#{toSinglePiece topic}-#{t}>#{x}|]
_ -> [html|<p>#{x}|]
go' "ul" _ x = [html|<ul>#{x}|]
go' "ol" _ x = [html|<ol>#{x}|]
go' "li" _ x = [html|<li>#{x}|]
go' "i" _ x = [html|<i>#{x}|]
go' "b" _ x = [html|<b>#{x}|]
go' "fig" _ x = [html|<figure>#{x}|]
go' "title" _ x = [html|<h1>#{x}|]
go' "image" as x =
case lookup "href" as of
Just [ContentText t] -> [html|<img src=#{toLink t}>|]
_ -> x
go' "xref" as x =
case lookup "href" as of
Just [ContentText t] -> [html|<a href=#{toLink t}>#{x}|]
_ -> x
go' "codeph" _ x = [html|<code>#{x}|]
go' "draft-comment" _ _ = [html||]
go' "indexterm" _ _ = [html||]
go' "lines" _ x = [html|<.lines>#{x}|]
go' "term" _ x = [html|<b>#{x}|]
go' "dl" _ x = [html|<dl>#{x}|]
go' "dlhead" _ x = [html|#{x}|]
go' "sup" _ x = [html|<sup>#{x}|]
go' "dlentry" _ x = [html|#{x}|]
go' "dt" _ x = [html|<dt>#{x}|]
go' "dd" _ x = [html|<dd>#{x}|]
go' "dthd" _ x = [html|<dt .head>#{x}|]
go' "ddhd" _ x = [html|<dd .head>#{x}|]
go' "shortcut" _ x = [html|<i>#{x}|]
go' "figgroup" _ x = [html|<section>#{x}|]
go' "desc" _ x = [html|<div .desc>#{x}|] -- FIXME
go' "ph" _ x = [html|#{x}|]
go' "varname" _ x = [html|<code>#{x}|]
go' "option" _ x = [html|<i>#{x}|]
go' "msgph" _ x = [html|<code>#{x}|]
go' "keyword" _ x = [html|#{x}|]
go' "cite" _ x = [html|<cite>#{x}|]
go' "sl" _ x = [html|<ul>#{x}|]
go' "sli" _ x = [html|<li>#{x}|]
go' "table" _ x = [html|<table>#{x}|]
go' "filepath" _ x = [html|<code>#{x}|]
go' "simpletable" _ x = [html|<table>#{x}|]
go' "strow" _ x = [html|<tr>#{x}|]
go' "stentry" _ x = [html|<td>#{x}|]
go' "entry" _ x = [html|<td>#{x}|]
go' "userinput" _ x = [html|<kbd>#{x}|]
go' "lq" _ x = [html|<blockquote>#{x}|]
go' "menucascade" _ x = [html|#{x}|]
go' "parml" _ x = [html|<dl>#{x}|]
go' "plentry" _ x = [html|#{x}|]
go' "pt" _ x = [html|<dt>#{x}|]
go' "pd" _ x = [html|<dd>#{x}|]
go' "q" _ x = [html|<q>#{x}|]
go' "uicontrol" _ x = [html|<code>#{x}|]
go' "colspec" _ x = [html|#{x}|]
go' "sthead" _ x = [html|<thead>#{x}|]
go' "thead" _ x = [html|<thead>#{x}|]
go' "tbody" _ x = [html|<tbody>#{x}|]
go' "row" _ x = [html|<tr>#{x}|]
go' "tgroup" _ x = [html|#{x}|]
go' "pre" _ x = [html|<pre>#{x}|]
go' "example" _ x = [html|<section>
<h1>Example
\#{x}|]
go' "section" _ x = [html|<section>#{x}|]
go' "apiname" _ x = [html|<a href="http://hackage.haskell.org/package/#{x}">#{x}|]
go' "codeblock" _ x = [html|<pre>
<code>#{x}|]
go' "note" as x =
case lookup "type" as of
Just [ContentText nt]
| nt == "other" ->
case lookup "othertype" as of
Just [ContentText ot] -> [html|<aside .note-#{ot}>#{x}|]
_ -> [html|<aside .note-#{x}>|]
| otherwise -> [html|<aside .#{nt}>#{x}|]
_ -> [html|<aside .note>#{x}|]
go' n _ _ =
trace ("Unknown DITA element: " ++ show n) $
[html|<h1 style=color:red>Unknown DITA element: #{show n}|]
toLink t
| topicPref `T.isPrefixOf` t =
let suffix = T.drop (T.length topicPref) t
(tid, rest) = T.break (== '#') suffix
in render (TopicR $ fromJust $ fromSinglePiece tid) [] `T.append` rest
| staticPref `T.isPrefixOf` t = render (StaticContentR $ fromJust $ fromSinglePiece $ T.drop (T.length staticPref) t) []
| "yw://" `T.isPrefixOf` t = "FIXME: " `T.append` t
| otherwise = t
topicPref = "yw://topic/"
staticPref = "yw://static/"
validateContent :: TopicFormat -> Text -> Text
validateContent TFHtml t = pack $ sanitizeBalance $ unpack t
validateContent TFText t = t
validateContent TFMarkdown t = T.filter (/= '\r') t
validateContent TFDitaConcept t = validateDita t
validateContent TFDitaTopic t = validateDita t
validateDita :: Text -> Text
validateDita txml =
case runIdentity $ run $ enumList 3 ["<body>", txml, "</body>"] $$ joinI $ parseText decodeEntities $$ fromEvents of
Left e -> pack $ show e
Right (Document _ (Element _ _ nodes) _) -> nodesToText $ flip evalState (Set.empty, 1 :: Int) $ do
nodes' <- mapM checkUnused nodes
mapM addIds nodes'
where
checkUnused (NodeElement (Element e as ns)) = do
as' <- fmap catMaybes $ mapM checkUnused' as
ns' <- mapM checkUnused ns
return $ NodeElement $ Element e as' ns'
checkUnused n = return n
checkUnused' ("id", [ContentText t]) = do
(s, i) <- get
if t `Set.member` s
then return Nothing
else do
put (Set.insert t s, i)
return $ Just ("id", [ContentText t])
checkUnused' ("id", _) = return Nothing
checkUnused' a = return $ Just a
addIds (NodeElement (Element e as ns)) = do
as' <-
case lookup "id" as of
Just _ -> return as
Nothing -> do
i <- getId
return $ ("id", [ContentText i]) : as
ns' <- mapM addIds ns
return $ NodeElement $ Element e as' ns'
addIds n = return n
getId = do
(s, i0) <- get
let go i =
let t = pack $ 'x' : show i
in if t `Set.member` s
then go $ i + 1
else (t, i + 1)
let (t, i') = go i0
put (Set.insert t s, i')
return t
nodesToText :: [Node] -> Text
nodesToText input =
mconcat $ unsafePerformIO $ run_ $ enumList 8 (goN input []) $$ joinI $ renderText $$ consume
where
goN :: [Node] -> [Event] -> [Event]
goN [] e = e
goN (NodeContent c:ns) e = EventContent c : goN ns e
goN (NodeInstruction i:ns) e = EventInstruction i : goN ns e
goN (NodeComment c:ns) e = EventComment c : goN ns e
goN (NodeElement el:ns) e = goE el $ goN ns e
goE :: Element -> [Event] -> [Event]
goE (Element n as ns) evs =
EventBeginElement n as
: goN ns
( EventEndElement n
: evs)
userGravatar :: User -> Html
userGravatar u =
[html|<img src="http://www.gravatar.com/avatar/#{hash}?d=identicon&s=100">|]
where
email = fromMaybe "" $ userEmail u
hash = pack $ show $ md5 $ L.fromChunks $ return $ encodeUtf8 $ pack $ map toLower $ trim $ unpack email
trim = reverse . dropWhile isSpace . reverse . dropWhile isSpace
prettyDate :: UTCTime -> String
prettyDate = formatTime defaultTimeLocale "%B %e, %Y" -- FIXME i18n
prettyMonthYear :: Int -> Int -> String
prettyMonthYear year month = formatTime defaultTimeLocale "%B %Y" $ fromGregorian (fromIntegral year) month 1 -- FIXME i18n