-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathMain.hs
298 lines (268 loc) · 10.4 KB
/
Main.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
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
{-# LANGUAGE QuasiQuotes #-}
module Main where
import GHC.Generics
import Control.Monad
import Control.Monad.IO.Class
import Control.Monad.Trans.State.Strict
import Control.Monad.Trans.Except
import Data.Default
import Data.List
import Data.String
import Data.Text qualified as T
import Data.Text.Encoding qualified as T
import Data.ByteString qualified as BS
import Data.ByteString.Lazy qualified as BL
import Data.Aeson qualified as A
import Data.Yaml.Aeson qualified as Y
import Text.Blaze.Html.Renderer.Utf8 as H
import Text.Hamlet qualified as H
import Text.Pandoc qualified as P
import Text.Pandoc.Builder qualified as P
import Text.Pandoc.Citeproc qualified as P
import System.Directory
import System.FilePath
import System.IO
import Development.Shake
import Development.Shake.FilePath
import Network.URI qualified as U
-- Route
data Route
= RIndex
| RMeta String
| RWiki String
| RMainCss
deriving Show
relativeUrl :: Route -> FilePath
relativeUrl RIndex = ""
relativeUrl (RMeta title) = "meta" </> title ++ ".html"
relativeUrl (RWiki title) = "wiki" </> title ++ ".html"
relativeUrl RMainCss = "css/main.css"
renderUrl :: H.Render Route
renderUrl r _ = T.pack ("/" ++ relativeUrl r)
-- Metadata
data Metadata = Metadata
{ title :: T.Text
, categories :: [T.Text]
, license :: T.Text
}
deriving (Generic, Show)
instance A.FromJSON Metadata
-- HTML templates
mathScript :: H.Html
mathScript =
[H.shamlet|
<script>
MathJax = {
loader: {
load: ['[tex]/bussproofs', '[custom]/xypic.js'],
paths: {custom: 'https://cdn.jsdelivr.net/gh/sonoisa/XyJax-v3@3.0.1/build/'}
},
tex: {
packages: {'[+]': ['bussproofs', 'xypic']}
}
};
<script id="MathJax-script" async src="https://cdn.jsdelivr.net/npm/mathjax@3/es5/tex-mml-chtml.js">
|]
headerTemplate :: H.Html
headerTemplate =
[H.hamlet|
<header>
<div class="content-box">
<a id="site-title" href=@{ RIndex }> PL wiki
|] renderUrl
categoriesTemplate :: [T.Text] -> H.Html
categoriesTemplate cs
| null cs = [H.shamlet| |]
| otherwise =
[H.hamlet|
<div class="categories-list">
<ul>
#{ list }
|] renderUrl
where
list = [ [H.hamlet| <li> #{ c } |] renderUrl | c <- cs ]
wikiTemplate :: Metadata -> H.Html -> H.Html
wikiTemplate (Metadata title categories _) content =
[H.hamlet|
$doctype 5
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title> #{ title }
<link rel="stylesheet" href=@{ RMainCss }>
#{ mathScript }
<body>
#{ headerTemplate }
<main>
<article class="content-box">
<h1 id="document-title"> #{ title }
#{ categoriesTemplate categories }
#{ content }
|] renderUrl
indexTemplate :: [(String, Integer)] -> [(String, Integer)] -> H.Html
indexTemplate metas wikis =
[H.hamlet|
$doctype 5
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title> PL wiki
<link rel="stylesheet" href=@{ RMainCss }>
<body>
#{ headerTemplate }
<main>
<article class="content-box">
PL wiki는 프로그래밍 언어론을 중심으로 컴퓨터 과학, 논리학, 수학, 철학에 대한 정보를 정리하기 위한 위키입니다.
<h1> 색인
<h2> 메타
<ul class="meta-index"> #{ links RMeta metas }
<h2> #{threshold}B 이상 문서
<ul class="wiki-index"> #{ links RWiki wikiBig }
<h2> #{threshold}B 미만 문서
<ul class="wiki-index"> #{ links RWiki wikiSmall }
|] renderUrl
where
threshold = 2500
(wikiSmall, wikiBig) = partition ((<threshold) . snd) wikis
links route titles = [ [H.hamlet|
<li>
<a href=@{ route title }>
#{ title }
|] renderUrl
| (title, _) <- sort titles
]
sitemapTemplate :: [String] -> String
sitemapTemplate locs = unlines $
[ "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
, "<urlset"
, " xmlns=\"http://www.sitemaps.org/schemas/sitemap/0.9\""
, " xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\""
, " xsi:schemaLocation="
, " \"http://www.sitemaps.org/schemas/sitemap/0.9"
, " http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd\">"
, urls
, "</urlset>"
]
where
urls = unlines ["<url>" ++ "<loc>" ++ loc ++ "</loc>" ++ "</url>" | loc <- locs]
-- document translator
readMarkdown :: P.PandocMonad m => T.Text -> m P.Pandoc
readMarkdown = P.readMarkdown options
where
options = def
{ P.readerExtensions = P.githubMarkdownExtensions
<> P.extensionsFromList
[ P.Ext_yaml_metadata_block
, P.Ext_tex_math_single_backslash
, P.Ext_tex_math_dollars
, P.Ext_citations
]
}
writeHtml :: P.PandocMonad m => P.Pandoc -> m H.Html
writeHtml = P.writeHtml5 options
where
options = def
{ P.writerHTMLMathMethod = P.MathJax P.defaultMathJaxURL
}
translateDocument :: P.PandocMonad m => Metadata -> T.Text -> m BL.ByteString
translateDocument meta content = do
ast0 <- readMarkdown content
let ast1 = ( P.setMeta (T.pack "link-citations") True
. P.setMeta (T.pack "csl") (T.pack "src/association-for-computing-machinery.csl")
-- TODO : fix heading number for reference section title
. P.setMeta (T.pack "reference-section-title") (T.pack "참고문헌")
. P.setMeta (T.pack "bibliography") (T.pack "src/bibliography.bib")
) ast0
ast2 <- P.processCitations ast1
html <- wikiTemplate meta <$> writeHtml ast2
pure (H.renderHtml html)
runPandocPure :: P.PandocPure a -> P.CommonState -> P.PureState -> Either P.PandocError a
runPandocPure m cs ps = ( flip evalState ps
. flip evalStateT cs
. runExceptT
. P.unPandocPure
) m
-- Build system
handleError :: Either P.PandocError a -> Action a
handleError (Left e) = fail (T.unpack (P.renderError e))
handleError (Right r) = pure r
readWikiFile :: MonadIO m => FilePath -> m (Metadata, T.Text)
readWikiFile path = liftIO $ do
s <- BS.readFile path
case BS.stripPrefix (fromString "---\n") s of
Nothing -> error "no yaml header begin"
Just s' -> do
let (as, bs) = BS.breakSubstring (fromString "---\n") s'
case BS.stripPrefix (fromString "---\n") bs of
Nothing -> error "yaml header end"
Just bs' -> do
meta <- Y.decodeThrow as
let content = T.decodeUtf8 bs'
return (meta, content)
writeByteString :: MonadIO m => FilePath -> BL.ByteString -> m ()
writeByteString name x = liftIO $ do
createDirectoryIfMissing True (takeDirectory name)
BL.writeFile name x
domain :: String
domain = "https://plwiki.github.io"
main :: IO ()
main = shakeArgs shakeOptions{shakeFiles="_build"} $ mconcat
[ want ["all"]
, "all" ~> do
metaSrcs <- getDirectoryFiles "src/meta" ["//*.md"]
wikiSrcs <- getDirectoryFiles "src/wiki" ["//*.md"]
cssSrcs <- getDirectoryFiles "src/css" ["*.css"]
need $ ["site/index.html", "site/sitemap.xml", "site/google4d4f29e2cef004bd.html"]
++ ["site/meta" </> f -<.> "html" | f <- metaSrcs]
++ ["site/wiki" </> f -<.> "html" | f <- wikiSrcs]
++ ["site/css" </> f | f <- cssSrcs]
, "clean" ~> do
putInfo "Cleaning..."
removeFilesAfter "site" ["//"]
, "site/index.html" %> \out -> do
metaSrcs <- getDirectoryFiles "src/meta" ["//*.md"]
wikiSrcs <- getDirectoryFiles "src/wiki" ["//*.md"]
need $ ["src/meta" </> f | f <- metaSrcs]
++ ["src/wiki" </> f | f <- wikiSrcs]
metaArgs <- liftIO . forM metaSrcs $ \f -> (dropExtension f,) <$> withFile ("src/meta" </> f) ReadMode hFileSize
wikiArgs <- liftIO . forM wikiSrcs $ \f -> (dropExtension f,) <$> withFile ("src/wiki" </> f) ReadMode hFileSize
putInfo "Generating site/index.html"
let result = H.renderHtml (indexTemplate metaArgs wikiArgs)
writeByteString out result
, "site/sitemap.xml" %> \out -> do
metaSrcs <- getDirectoryFiles "src/meta" ["//*.md"]
wikiSrcs <- getDirectoryFiles "src/wiki" ["//*.md"]
need $ ["src/meta" </> f | f <- metaSrcs]
++ ["src/wiki" </> f | f <- wikiSrcs]
putInfo "Generating site/sitemap.txt"
let result = sitemapTemplate $ [domain </> ""]
++ [domain </> "meta" </> U.escapeURIString U.isUnescapedInURI f -<.> "html" | f <- metaSrcs]
++ [domain </> "wiki" </> U.escapeURIString U.isUnescapedInURI f -<.> "html" | f <- wikiSrcs]
writeFile' out result
, "site/meta//*.html" %> \out -> do
let src = replaceDirectory1 out "src" -<.> "md"
need [src, "src/bibliography.bib", "src/association-for-computing-machinery.csl"]
putInfo ("Generating " ++ out)
(meta, content) <- readWikiFile src
files <- liftIO $ foldM P.addToFileTree mempty ["src/bibliography.bib", "src/association-for-computing-machinery.csl"]
result <- handleError $ runPandocPure (translateDocument meta content) def (def {P.stFiles = files})
writeByteString out result
, "site/wiki//*.html" %> \out -> do
let src = replaceDirectory1 out "src" -<.> "md"
need [src, "src/bibliography.bib", "src/association-for-computing-machinery.csl"]
putInfo ("Generating " ++ out)
(meta, content) <- readWikiFile src
files <- liftIO $ foldM P.addToFileTree mempty ["src/bibliography.bib", "src/association-for-computing-machinery.csl"]
result <- handleError $ runPandocPure (translateDocument meta content) def (def {P.stFiles = files})
writeByteString out result
, "site/css/*.css" %> \out -> do
let src = replaceDirectory1 out "src"
putInfo ("Copying " ++ out)
copyFile' src out
, "site/google4d4f29e2cef004bd.html" %> \out -> do
let src = replaceDirectory1 out "src"
putInfo ("Copying " ++ out)
copyFile' src out
]