-
Notifications
You must be signed in to change notification settings - Fork 5
/
site.hs
157 lines (129 loc) · 5.06 KB
/
site.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
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE FlexibleContexts #-}
import Data.Monoid (mappend)
import qualified Data.Set as S
import Data.List (intercalate)
import Control.Monad.State
import Hakyll
import Text.Pandoc.Options
import Text.Pandoc.JSON
import Text.Pandoc.Walk (walk, walkM)
--------------------------------------------------------------------------------
config :: Configuration
config = defaultConfiguration {
destinationDirectory = "public"
}
main :: IO ()
main = hakyllWith config $ do
match "images/*" $ do
route idRoute
compile copyFileCompiler
match "css/*" $ do -- tufte and custom css
route idRoute
compile compressCssCompiler
match "css/*/*/*" $ do -- fonts
route idRoute
compile copyFileCompiler
match "*.markdown" $ do
route $ setExtension "html"
compile $ customPandocCompiler
>>= loadAndApplyTemplate "templates/default.html" postCtx
>>= relativizeUrls
match "notes/*" $ do
route $ setExtension "html"
compile $ customPandocCompiler
>>= loadAndApplyTemplate "templates/default.html" postCtx
>>= relativizeUrls
match "assignments/*" $ do
route $ setExtension "html"
compile $ customPandocCompiler
>>= loadAndApplyTemplate "templates/default.html" postCtx
>>= relativizeUrls
match "solutions/*" $ do
route $ setExtension "html"
compile $ customPandocCompiler
>>= loadAndApplyTemplate "templates/default.html" postCtx
>>= relativizeUrls
match "templates/*" $ compile templateBodyCompiler
--------------------------------------------------------------------------------
postCtx :: Context String
postCtx =
dateField "date" "%B %e, %Y" `mappend`
defaultContext
customPandocCompiler =
let mathExtensions =
extensionsFromList [Ext_tex_math_dollars,
Ext_tex_math_single_backslash,
Ext_latex_macros]
defaultExtensions = writerExtensions defaultHakyllWriterOptions
newExtensions = defaultExtensions <> mathExtensions
writerOptions = defaultHakyllWriterOptions {
writerExtensions = newExtensions,
writerHTMLMathMethod = MathJax ""
}
in pandocCompilerWithTransform defaultHakyllReaderOptions writerOptions usingSideNotes
-------------------------------------------------------------------------------
getFirstStr :: [Inline] -> Maybe String
getFirstStr [] = Nothing
getFirstStr (Str text:_ ) = Just text
getFirstStr (_ :inlines) = getFirstStr inlines
newline :: [Inline]
newline = [LineBreak, LineBreak]
-- This could be implemented more concisely, but I think this is more clear.
getThenIncr :: State Int Int
getThenIncr = do
i <- get
put (i + 1)
return i
-- Extract inlines from blocks
coerceToInline :: [Block] -> [Inline]
coerceToInline = concatMap deBlock . walk deNote
where
deBlock :: Block -> [Inline]
deBlock (Plain ls ) = ls
-- Simulate paragraphs with double LineBreak
deBlock (Para ls ) = ls ++ newline
-- See extension: line_blocks
deBlock (LineBlock lss ) = intercalate [LineBreak] lss ++ newline
-- Pretend RawBlock is RawInline (might not work!)
-- Consider: raw <div> now inside RawInline... what happens?
deBlock (RawBlock fmt str) = [RawInline fmt str]
-- lists, blockquotes, headers, hrs, and tables are all omitted
-- Think they shouldn't be? I'm open to sensible PR's.
deBlock _ = []
deNote (Note _) = Str ""
deNote x = x
filterInline :: Inline -> State Int Inline
filterInline (Note blocks) = do
-- Generate a unique number for the 'for=' attribute
i <- getThenIncr
-- Note has a [Block], but Span needs [Inline]
let content = coerceToInline blocks
-- The '{-}' symbol differentiates between margin note and side note
let nonu = getFirstStr content == Just "{-}"
let content' = if nonu then tail content else content
let labelCls = "margin-toggle" ++ (if nonu then "" else " sidenote-number")
let labelSym = if nonu then "⊕" else ""
let labelHTML =
"<label for=\"sn-"
++ show i
++ "\" class=\""
++ labelCls
++ "\">"
++ labelSym
++ "</label>"
let label = RawInline (Format "html") labelHTML
let inputHTML =
"<input type=\"checkbox\" id=\"sn-"
++ show i
++ "\" "
++ "class=\"margin-toggle\"/>"
let input = RawInline (Format "html") inputHTML
let (ident, _, attrs) = nullAttr
let noteTypeCls = if nonu then "marginnote" else "sidenote"
let note = Span (ident, [noteTypeCls], attrs) content'
return $ Span nullAttr [label, input, note]
filterInline inline = return inline
usingSideNotes :: Pandoc -> Pandoc
usingSideNotes (Pandoc meta blocks) =
Pandoc meta (evalState (walkM filterInline blocks) 0)