-
Notifications
You must be signed in to change notification settings - Fork 0
/
build.hs
196 lines (164 loc) · 6.81 KB
/
build.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
{-# LANGUAGE OverloadedStrings #-}
import qualified Text.Pandoc as P
import qualified Text.Pandoc.Templates as PT
import Text.DocLayout (render) -- Used to render Doc type into Text type.
import System.IO (writeFile)
import System.Directory (createDirectory, removeDirectoryRecursive, getDirectoryContents, doesDirectoryExist, copyFile)
import System.FilePath ((</>))
import Control.Monad (forM_)
import Control.Arrow ((***))
import qualified Data.ByteString.Lazy as BS -- Using ByteStrings for efficiency since data file is large.
import qualified Data.Text as T
import qualified Data.Text.IO as TIO
--import qualified Data.Text.Encoding as TE
import qualified Data.Aeson as J
import qualified Data.Aeson.Types as J (parseMaybe, parseEither, emptyObject)
import Data.HashMap.Strict as HM (empty, toList)
import Data.Maybe (fromJust, fromMaybe)
import Data.Either (either)
import Data.List (groupBy)
import Data.Function (on)
data Issue = Issue
{ issueNumber :: Int --T.Text
, issueId :: Int --T.Text
, issueUrl :: T.Text
, issueTitle :: T.Text
, issueAuthor :: T.Text
, issueBody :: Maybe T.Text
} deriving (Show, Read)
data Comment = Comment
{ commentId :: Int--T.Text
, commentUrl :: T.Text
, commentIssueUrl :: T.Text
, commentAuthor :: T.Text
, commentBody :: Maybe T.Text
} deriving (Show, Read)
instance J.FromJSON Issue where
parseJSON = J.withObject "Issue" $ \obj -> do
issueNumber <- obj J..: "number"
issueId <- obj J..: "id"
issueUrl <- obj J..: "url"
issueTitle <- obj J..: "title"
userObj <- obj J..: "user"
issueAuthor <- userObj J..: "login"
rawIssueBody <- obj J..: "body"
let issueBody = either (const Nothing) Just (mdToHtml . fromMaybe "" $ rawIssueBody)
return (Issue issueNumber issueId issueUrl (escapeHtml issueTitle) (escapeHtml issueAuthor) issueBody)
instance J.ToJSON Issue where
toJSON (Issue issueNumber _ issueUrl issueTitle issueAuthor issueBody) =
J.object [ "number" J..= issueNumber
, "url" J..= issueUrl
, "title" J..= issueTitle
, "author" J..= issueAuthor
, "body" J..= issueBody
]
instance J.FromJSON Comment where
parseJSON = J.withObject "Comment" $ \obj -> do
commentId <- obj J..: "id"
commentUrl <- obj J..: "url"
commentIssueUrl <- obj J..: "issue_url"
userObj <- obj J..: "user"
commentAuthor <- userObj J..: "login"
rawCommentBody <- obj J..: "body"
let commentBody = either (const Nothing) Just (mdToHtml . fromMaybe "" $ rawCommentBody)
return (Comment commentId commentUrl commentIssueUrl (escapeHtml commentAuthor) commentBody)
instance J.ToJSON Comment where
toJSON (Comment _ commentUrl commentIssueUrl commentAuthor commentBody) =
J.object [ "url" J..= commentUrl
, "issue_url" J..= commentIssueUrl
, "author" J..= commentAuthor
, "body" J..= commentBody
]
readJsonFile :: (J.FromJSON a) => FilePath -> IO a
readJsonFile filepath = handleFailure . J.eitherDecode <$> BS.readFile filepath
where handleFailure result = case result of
Left msg -> error msg
Right val -> val
issuesIO :: IO [Issue]
issuesIO = readJsonFile $ sourceDir </> "issues.json" :: IO [Issue]
commentsIO :: IO [(T.Text, [Comment])]
commentsIO = do
contents <- readJsonFile $ sourceDir </> "comments.json" :: IO [Comment]
return . map (\cs -> (commentIssueUrl $ head cs, cs)) . groupBy ((==) `on` commentIssueUrl) $ contents
copyDir :: FilePath -> FilePath -> IO ()
copyDir src dst = do
createDirectory dst
dirContents <- getDirectoryContents src
forM_ (filter (`notElem` [".", ".."]) dirContents) $ \name -> do
let srcPath = src </> name
let dstPath = dst </> name
isDirectory <- doesDirectoryExist srcPath
if isDirectory
then copyDir srcPath dstPath
else copyFile srcPath dstPath
assetsDir :: FilePath
assetsDir = "./assets/"
sourceDir :: FilePath
sourceDir = "./_data/"
templatesDir :: FilePath
templatesDir = "./_templates/"
targetDir :: FilePath
targetDir = "./issue-tracker-archive/" --"./_site/"
indexFile :: FilePath
indexFile = "./index.html"
loadTemplate :: FilePath -> IO (PT.Template T.Text)
loadTemplate filepath = do
templateIO <- P.runIO $ PT.getTemplate filepath
templateText <- P.handleError templateIO
Right template <- PT.compileTemplate filepath templateText
return template
issueTemplateIO :: IO (PT.Template T.Text)
issueTemplateIO = loadTemplate $ templatesDir </> "issue.html"
issuesIndexTemplateIO :: IO (PT.Template T.Text)
issuesIndexTemplateIO = loadTemplate $ templatesDir </> "issuesIndex.html"
escapeHtml :: T.Text -> T.Text
escapeHtml = T.concatMap sanitize where
sanitize '&' = "&"
sanitize '<' = "<"
sanitize '>' = ">"
sanitize '"' = """
sanitize '\'' = "'"
sanitize other = T.singleton other
mdToHtml :: T.Text -> Either P.PandocError T.Text
mdToHtml md = P.runPure $ do
doc <- P.readMarkdown P.def md
P.writeHtml5String P.def doc
makeContext :: Issue -> [Comment] -> J.Value
makeContext issue comments = J.object [ "issue" J..= issue
, "comments" J..= comments ]
copyAssets :: IO ()
copyAssets = copyDir assetsDir (targetDir </> "assets/")
copyIndex :: IO ()
copyIndex = copyFile indexFile (targetDir </> "index.html")
makeIssuePage :: J.Value -> IO (T.Text)
makeIssuePage context = do
template <- issueTemplateIO
let page = render Nothing $ PT.renderTemplate template context
return page
-- TODO: sort this list
makeIssuesIndexPage :: [Issue] -> IO (T.Text)
makeIssuesIndexPage issues = do
template <- issuesIndexTemplateIO
let context = J.object [ "issues" J..= issues ]
page = render Nothing $ PT.renderTemplate template context
return page
writePage :: FilePath -> T.Text -> IO ()
writePage = TIO.writeFile
main :: IO ()
main = do
issues <- issuesIO
comments <- commentsIO
targetExists <- doesDirectoryExist targetDir
if targetExists then removeDirectoryRecursive targetDir else return ()
createDirectory targetDir
createDirectory $ targetDir </> "issues"
writeFile (targetDir </> ".nojekyll") "" -- Tell GitHub not to build with Jekyll
copyAssets
copyIndex
issuesIndexPage <- makeIssuesIndexPage issues
writePage (targetDir </> "issues" </> "index.html") issuesIndexPage
mapM_ (\issue -> do
let issueComments = fromMaybe [] (lookup (issueUrl issue) comments)
issuePage <- makeIssuePage (makeContext issue issueComments)
writePage (targetDir </> "issues" </> (show (issueNumber issue) ++ ".html")) issuePage)
issues