summaryrefslogtreecommitdiff
path: root/src/Site.hs
diff options
context:
space:
mode:
Diffstat (limited to 'src/Site.hs')
-rw-r--r--src/Site.hs53
1 files changed, 35 insertions, 18 deletions
diff --git a/src/Site.hs b/src/Site.hs
index e821322..dcb9a7d 100644
--- a/src/Site.hs
+++ b/src/Site.hs
@@ -13,18 +13,19 @@ import Data.List (take, reverse, nub, groupBy, concatMap)
13import Data.Function (on) 13import Data.Function (on)
14import Data.Default 14import Data.Default
15import Text.Pandoc 15import Text.Pandoc
16import Text.Pandoc.Walk (query) 16import Text.Pandoc.Walk (query, walkM)
17import Text.Pandoc.Error 17import Text.Pandoc.Error
18import Control.Applicative (Alternative(..), Applicative(..)) 18import Control.Applicative (Alternative(..), Applicative(..))
19 19
20import System.FilePath (takeBaseName, (</>), (<.>))
21
20import qualified Crypto.Hash.SHA256 as SHA256 (hash) 22import qualified Crypto.Hash.SHA256 as SHA256 (hash)
21import qualified Data.ByteString.Char8 as CBS 23import qualified Data.ByteString.Char8 as CBS
22import Data.Hex 24import Data.Hex (hex)
23import Data.Char (toLower) 25import Data.Char (toLower)
24 26
25import System.FilePath (takeBaseName, (</>), (<.>))
26
27import Math (compileMath) 27import Math (compileMath)
28import Text.Printf (printf)
28 29
29main :: IO () 30main :: IO ()
30main = hakyllWith config $ do 31main = hakyllWith config $ do
@@ -34,24 +35,24 @@ main = hakyllWith config $ do
34 route idRoute 35 route idRoute
35 compile copyFileCompiler 36 compile copyFileCompiler
36 37
38 math <- getMath "posts/*" mathTranslation'
39 forM_ math $ \(_, mathStr) -> create [mathTranslation' mathStr] $ do
40 route idRoute
41 compile $ do
42 item <- makeItem mathStr
43 >>= loadAndApplyTemplate "templates/math.tex" defaultContext
44 >>= withItemBody (unsafeCompiler . compileMath)
45 saveSnapshot "alignment" $ fmap snd item
46 return $ fmap fst item
47
37 match "posts/*" $ do 48 match "posts/*" $ do
38 route $ setExtension ".html" 49 route $ setExtension ".html"
39 compile $ do 50 compile $ do
40 getResourceBody >>= saveSnapshot "content" 51 getResourceBody >>= saveSnapshot "content"
41 pandocCompiler 52 pandocCompilerWithTransformM defaultHakyllReaderOptions defaultHakyllWriterOptions mathTransform
42 >>= loadAndApplyTemplate "templates/default.html" defaultContext 53 >>= loadAndApplyTemplate "templates/default.html" defaultContext
43 >>= relativizeUrls 54 >>= relativizeUrls
44 55
45 math <- getMath "posts/*" mathTranslation'
46 forM_ math $ \(deps, mathStr) ->
47 rulesExtraDependencies (map IdentifierDependency deps) $
48 create [mathTranslation' mathStr] $ do
49 route idRoute
50 compile $ do
51 makeItem mathStr
52 >>= loadAndApplyTemplate "templates/math.tex" defaultContext
53 >>= withItemBody (unsafeCompiler . compileMath)
54
55 tags <- buildTags "posts/*" tagTranslation' >>= addTag "All Posts" "posts/*" 56 tags <- buildTags "posts/*" tagTranslation' >>= addTag "All Posts" "posts/*"
56 57
57 tagsRules tags $ \tag pattern -> do 58 tagsRules tags $ \tag pattern -> do
@@ -143,14 +144,14 @@ tagTranslation = mapMaybe charTrans
143 | isAlphaNum c = Just $ toLower c 144 | isAlphaNum c = Just $ toLower c
144 | otherwise = Nothing 145 | otherwise = Nothing
145 146
146mathTranslation' :: String -> Identifier
147mathTranslation' = fromCapture "math/*.svg" . map toLower . CBS.unpack . hex . SHA256.hash . CBS.pack
148
149addTag :: MonadMetadata m => String -> Pattern -> Tags -> m Tags 147addTag :: MonadMetadata m => String -> Pattern -> Tags -> m Tags
150addTag name pattern tags = do 148addTag name pattern tags = do
151 ids <- getMatches pattern 149 ids <- getMatches pattern
152 return $ tags { tagsMap = tagsMap tags ++ [(name, ids)] } 150 return $ tags { tagsMap = tagsMap tags ++ [(name, ids)] }
153 151
152mathTranslation' :: String -> Identifier
153mathTranslation' = fromCapture "math/*.svg" . map toLower . CBS.unpack . hex . SHA256.hash . CBS.pack
154
154getMath :: Pattern -> (String -> Identifier) -> Rules [([Identifier], String)] 155getMath :: Pattern -> (String -> Identifier) -> Rules [([Identifier], String)]
155getMath pattern makeId = do 156getMath pattern makeId = do
156 ids <- getMatches pattern 157 ids <- getMatches pattern
@@ -193,6 +194,22 @@ readPandoc' path = readFile path >>= either fail return . result'
193 194
194 addExt ro e = ro {readerExtensions = Set.insert e $ readerExtensions ro} 195 addExt ro e = ro {readerExtensions = Set.insert e $ readerExtensions ro}
195 196
197mathTransform :: Pandoc -> Compiler Pandoc
198mathTransform = walkM mathTransform'
199 where
200 mathTransform' :: Inline -> Compiler Inline
201 mathTransform' (Math mathType tex) = do
202 alignment <- loadSnapshotBody texId "alignment"
203 let
204 html = printf "<object data=\"/%s\" type=\"image/svg+xml\" style=\"vertical-align:-%s\">%s</object>"
205 (toFilePath texId) (alignment :: String) tex
206 return $ Span ("", [classOf mathType], []) [RawInline (Format "html") html]
207 where
208 texId = mathTranslation' tex
209 classOf DisplayMath = "display-math"
210 classOf InlineMath = "inline-math"
211 mathTransform' x = return x
212
196toFilePath' :: Identifier -> FilePath 213toFilePath' :: Identifier -> FilePath
197toFilePath' = (providerDirectory config </>) . toFilePath 214toFilePath' = (providerDirectory config </>) . toFilePath
198 215