aboutsummaryrefslogtreecommitdiff
path: root/src/Main.hs
blob: 1e9e6d91e257bd9e6f076310249167457506516b (plain)
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
{-# LANGUAGE OverloadedStrings #-}

import           Control.Applicative (empty)
import qualified Data.Text           as T
import           Hakyll              ((.&&.))
import           Hakyll              (Compiler, Configuration (..),
                                      Context (Context),
                                      ContextField (ListField), Identifier,
                                      Item, MonadMetadata, TmpFile (TmpFile))
import qualified Hakyll              as H
import qualified System.FilePath     as FilePath (replaceExtension,
                                                  takeDirectory)
import qualified System.Process      as Process (rawSystem, readCreateProcess,
                                                 shell, system)
import qualified Text.Pandoc         as Pandoc

main :: IO ()
main = H.hakyllWith configuration $ do

  -- Static files
  H.match "assets/**" $ do
    H.route H.idRoute
    H.compile H.copyFileCompiler

  H.match "css/**.hs" $ do
    H.route . H.customRoute $ const "style.css"
    H.compile $ do
      H.unsafeCompiler (Process.readCreateProcess (Process.shell "cd css && runghc Style.hs") "")
        >>= H.makeItem

  H.match "cv/**" $ H.version "html" $ do
    H.route $ H.setExtension "html"
    let context =
          metadataListField `mappend`
          H.defaultContext
    H.compile $ H.pandocCompiler
      >>= H.loadAndApplyTemplate "templates/resume.html" context
      >>= H.relativizeUrls

  H.match "cv/**" $ H.version "tex" $ do
    H.route $ H.setExtension "tex"
    let context =
          metadataListField `mappend`
          H.defaultContext
    H.compile $ H.getResourceBody
      >>= H.readPandoc
      >>= writeLaTeX
      >>= H.loadAndApplyTemplate "templates/resume.tex" context

  H.match "project/**" $ do
    H.route $ H.setExtension "html"
    let context =
          metadataListField `mappend`
          H.defaultContext
    H.compile $ H.pandocCompiler
      >>= H.loadAndApplyTemplate "templates/project.html" context
      >>= H.relativizeUrls

  H.match "index.html" $ do
    H.route H.idRoute
    let layoutContext =
          H.constField "isResume" "true" `mappend`
          H.defaultContext
    let context =
          H.listField "experience" H.defaultContext (H.loadAll ("cv/experience/*" .&&. H.hasVersion "html")) `mappend`
          H.listField "education" H.defaultContext (H.loadAll ("cv/education/*" .&&. H.hasVersion "html")) `mappend`
          H.listField "skills" H.defaultContext (H.loadAll ("cv/skill/*" .&&. H.hasVersion "html")) `mappend`
          H.listField "hobbies" H.defaultContext (H.loadAll ("cv/hobby/*" .&&. H.hasVersion "html")) `mappend`
          H.defaultContext
    H.compile $
      H.getResourceBody
        >>= H.applyAsTemplate context
        >>= H.loadAndApplyTemplate "templates/layout.html" layoutContext
        >>= H.relativizeUrls

  H.match "projects.html" $ do
    H.route H.idRoute
    let layoutContext =
          H.constField "isProjects" "true" `mappend`
          H.defaultContext
    let context =
          H.listField "projects" H.defaultContext (H.loadAll "project/*") `mappend`
          H.defaultContext
    H.compile $
      H.getResourceBody
        >>= H.applyAsTemplate context
        >>= H.loadAndApplyTemplate "templates/layout.html" layoutContext
        >>= H.relativizeUrls

  H.match "cv.tex" $ do
    H.route $ H.setExtension ".pdf"
    let context =
          H.listField "experience" H.defaultContext (H.loadAll ("cv/experience/*" .&&. H.hasVersion "tex")) `mappend`
          H.listField "education" H.defaultContext (H.loadAll ("cv/education/*" .&&. H.hasVersion "tex")) `mappend`
          H.listField "skills" H.defaultContext (H.loadAll ("cv/skill/*" .&&. H.hasVersion "tex")) `mappend`
          H.listField "hobbies" H.defaultContext (H.loadAll ("cv/hobby/*" .&&. H.hasVersion "tex")) `mappend`
          H.defaultContext
    H.compile $
      H.getResourceBody
        >>= H.applyAsTemplate context
        >>= H.readPandoc
        >>= writeLaTeX
        >>= H.loadAndApplyTemplate "templates/layout.tex" context
        >>= generatePdf

  H.match "templates/**" $
    H.compile H.templateBodyCompiler

writeLaTeX :: Item Pandoc.Pandoc -> Compiler (Item String)
writeLaTeX = traverse $ \pandoc ->
  case Pandoc.runPure (Pandoc.writeLaTeX Pandoc.def pandoc) of
    Left err -> fail $ show err
    Right x  -> return (T.unpack x)

configuration :: Configuration
configuration = H.defaultConfiguration
  { destinationDirectory = "public"
  , deploySite = const $
      Process.rawSystem "rsync" [ "-avzh", "public/", "joris@guyonvarch.me:/var/www/guyonvarch.me" ]
  }

metadataListField :: Context a
metadataListField = Context $ \k _ i -> do
   values <- getMetadataListField (H.itemIdentifier i) k
   case values of
     Just vs -> do
       listItems <- mapM H.makeItem vs
       return $ ListField (H.field "item" (return . H.itemBody)) listItems
     Nothing ->
       empty

getMetadataListField :: MonadMetadata m => Identifier -> String -> m (Maybe [String])
getMetadataListField identifier key = do
   metadata <- H.getMetadata identifier
   return $ H.lookupStringList key metadata

generatePdf :: Item String -> Compiler (Item TmpFile)
generatePdf item = do
    TmpFile texPath <- H.newTmpFile "file.tex"
    let tmpDir  = FilePath.takeDirectory texPath
        pdfPath = FilePath.replaceExtension texPath "pdf"

    H.unsafeCompiler $ do
        writeFile texPath $ H.itemBody item
        _ <- Process.system $ unwords ["cd resume", "&&", "pdflatex", "-halt-on-error",
            "-output-directory", "../" ++ tmpDir, "../" ++ texPath, ">/dev/null", "2>&1"]
        _ <- Process.system $ unwords ["cd resume", "&&", "pdflatex", "-halt-on-error",
            "-output-directory", "../" ++ tmpDir, "../" ++ texPath, ">/dev/null", "2>&1"]
        return ()

    H.makeItem $ TmpFile pdfPath