Skip to content
Snippets Groups Projects
Main.hs 8.43 KiB
Newer Older
orestis.malaspin's avatar
orestis.malaspin committed
{-# LANGUAGE OverloadedStrings #-}
--------------------------------------------------------------------
-- |
-- Copyright :  (c) Stephen Diehl 2013
-- License   :  MIT
-- Maintainer:  stephen.m.diehl@gmail.com
-- Stability :  experimental
-- Portability: non-portable
--
--------------------------------------------------------------------

module Main where

{-# LANGUAGE OverloadedStrings #-}
import           Data.Monoid (mappend)
import           Hakyll
import           Text.Pandoc
import           qualified Data.Map as M
import           Data.Maybe (isJust)
import           Text.Pandoc.Highlighting
import           Hakyll.Images ( loadImage
                                , scaleImageCompiler
                               )
orestis.malaspin's avatar
orestis.malaspin committed

--------------------------------------------------------------------
-- Contexts
--------------------------------------------------------------------

postCtx :: Context String
postCtx =
  dateField "date" "%B %e, %Y"
  `mappend` mathCtx
  `mappend` defaultContext

mathCtx :: Context String
mathCtx = field "mathjax" $ \item -> do
  metadata <- getMetadata $ itemIdentifier item
  return ""
  return $ if isJust $ lookupString "mathjax" metadata
           then "<script type=\"text/javascript\" src=\"https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.0/MathJax.js?config=TeX-AMS_HTML\"></script>"
           else ""

orestis.malaspin's avatar
orestis.malaspin committed
courseCtx posts title =
  listField "posts" postCtx (return posts)
  `mappend` constField "title" title
  `mappend` defaultContext
            
researchCtx posts =
orestis.malaspin's avatar
orestis.malaspin committed
  listField "posts" postCtx (return posts)
  `mappend` constField "title" "Research projects"
  `mappend` defaultContext

bachelorCtx posts =
  listField "posts" postCtx (return posts)
  `mappend` constField "title" "Bachelor projects"
orestis.malaspin's avatar
orestis.malaspin committed
  `mappend` defaultContext

indexCtx posts =
  listField "posts" postCtx (return posts)
  `mappend` constField "title" "Home"
  `mappend` defaultContext

--------------------------------------------------------------------
-- Rules
--------------------------------------------------------------------

static :: Rules ()
static = do
  match ("fonts/*"
       .||. "img/*"
       .||. "img/*/**.png"
       .||. "css/*"
       .||. "js/*"
orestis.malaspin's avatar
orestis.malaspin committed
       .||. "cours/math_tech_info/figs/*"
       .||. "cours/isc_physics/figs/*") $ do
orestis.malaspin's avatar
orestis.malaspin committed
    route idRoute
    compile $ copyFileCompiler

orestis.malaspin's avatar
orestis.malaspin committed
resizeThumbnails :: Rules ()
resizeThumbnails = do
orestis.malaspin's avatar
orestis.malaspin committed
  match "img/thumbnails/**.png" $ do
      route idRoute
      compile $ loadImage
        >>= scaleImageCompiler 140 140

orestis.malaspin's avatar
orestis.malaspin committed
resizeHeads :: Rules ()
resizeHeads = do
  match "img/heads/**.png" $ do
      route idRoute
      compile $ loadImage
        >>= scaleImageCompiler 256 256

orestis.malaspin's avatar
orestis.malaspin committed
resizeLarge :: Rules ()
resizeLarge = do
  match "img/large/**.png" $ do
      route idRoute
      compile $ loadImage
        >>= scaleImageCompiler 900 500

orestis.malaspin's avatar
orestis.malaspin committed
pages :: Rules ()
pages = do
  match "pages/*" $ do
    route $ setExtension "html"
    compile $ getResourceBody
      >>= loadAndApplyTemplate "templates/page.html"    postCtx
      >>= relativizeUrls

posts :: Rules ()
posts = do
  match "posts/*" $ do
    route $ setExtension "html"
    -- compile $ myPandocCompiler
    compile $ bibtexCompiler
      >>= loadAndApplyTemplate "templates/post.html"    postCtx
      >>= relativizeUrls

research :: Rules ()
research = do
  create ["research_projects.html"] $ do
orestis.malaspin's avatar
orestis.malaspin committed
    route idRoute
    compile $ do
      posts <- recentFirst =<< loadAll "posts/research/*"
      makeItem ""
        >>= loadAndApplyTemplate "templates/archive.html" (researchCtx posts)
        >>= relativizeUrls

bachelor :: Rules ()
bachelor = do
  create ["bachelor_projects.html"] $ do
    route idRoute
    compile $ do
      posts <- recentFirst =<< loadAll "posts/bachelor/*"
orestis.malaspin's avatar
orestis.malaspin committed
      makeItem ""
        >>= loadAndApplyTemplate "templates/archive.html" (bachelorCtx posts)
orestis.malaspin's avatar
orestis.malaspin committed
        >>= relativizeUrls

orestis.malaspin's avatar
orestis.malaspin committed
cours_conc :: Rules ()
cours_conc = do
orestis.malaspin's avatar
orestis.malaspin committed
  match "cours/*" $ do
    route $ setExtension "html"
    -- compile $ myPandocCompiler
    compile $ bibtexCompiler
      >>= loadAndApplyTemplate "templates/post.html"    postCtx
      >>= relativizeUrls

conc :: Rules ()
conc = do
  create ["prog_conc.html"] $ do
    route idRoute
    compile $ do
      posts <- recentFirst =<< loadAll "cours/*"
      makeItem ""
orestis.malaspin's avatar
orestis.malaspin committed
        >>= loadAndApplyTemplate "templates/archive.html" (courseCtx posts "Programmation concurrente")
orestis.malaspin's avatar
orestis.malaspin committed
        >>= relativizeUrls

orestis.malaspin's avatar
orestis.malaspin committed
cours_mti :: Rules ()
cours_mti = do
  match "cours/math_tech_info/*.markdown" $ do
orestis.malaspin's avatar
orestis.malaspin committed
    route $ setExtension "html"
    -- compile $ myPandocCompiler
    compile $ bibtexCompiler
      >>= loadAndApplyTemplate "templates/class.html"    postCtx
orestis.malaspin's avatar
orestis.malaspin committed
      >>= relativizeUrls

mti :: Rules ()
mti = do
  create ["math_tech_info.html"] $ do
    route idRoute
    compile $ do
      posts <- recentFirst =<< loadAll "cours/math_tech_info/*"
      makeItem ""
        >>= loadAndApplyTemplate "templates/archive.html" (courseCtx posts "Mathématiques en technologie de l'information")
        >>= relativizeUrls

orestis.malaspin's avatar
orestis.malaspin committed
cours_phys_app :: Rules ()
cours_phys_app = do
  match "cours/isc_physics/*.markdown" $ do
    route $ setExtension "html"
    -- compile $ myPandocCompiler
    compile $ bibtexCompiler
      >>= loadAndApplyTemplate "templates/class.html"    postCtx
      >>= relativizeUrls

phys_app :: Rules ()
phys_app = do
  create ["phys_app.html"] $ do
    route idRoute
    compile $ do
      posts <- recentFirst =<< loadAll "cours/isc_physics/*"
      makeItem ""
        >>= loadAndApplyTemplate "templates/archive.html" (courseCtx posts "Physique appliquée")
        >>= relativizeUrls

orestis.malaspin's avatar
orestis.malaspin committed
index :: Rules ()
index = do
  match "index.html" $ do
    route idRoute
    compile $ do
      posts <- recentFirst =<< loadAll "posts/*"
      getResourceBody
        >>= applyAsTemplate (indexCtx posts)
        >>= relativizeUrls

templates :: Rules ()
templates = match "templates/*" $ compile templateCompiler

--------------------------------------------------------------------
-- Configuration
--------------------------------------------------------------------

myPandocCompiler :: Compiler (Item String)
myPandocCompiler = pandocCompilerWith defaultHakyllReaderOptions pandocOptions 

pandocOptions :: WriterOptions
pandocOptions = defaultHakyllWriterOptions
    { 
        writerExtensions = defaultPandocExtensions
orestis.malaspin's avatar
orestis.malaspin committed
        , writerHTMLMathMethod = MathJax ""
        , writerNumberSections = True 
        , writerTableOfContents = True
orestis.malaspin's avatar
orestis.malaspin committed
    }

-- Pandoc extensions used by the myPandocCompiler
defaultPandocExtensions :: Extensions
defaultPandocExtensions = 
    let extensions = [ 
            -- Pandoc Extensions: http://pandoc.org/MANUAL.html#extensions
            -- Math extensions
              Ext_tex_math_dollars
            , Ext_tex_math_double_backslash
            , Ext_latex_macros
                -- Code extensions
            , Ext_fenced_code_blocks
            , Ext_backtick_code_blocks
            , Ext_fenced_code_attributes
            , Ext_inline_code_attributes        -- Inline code attributes (e.g. `<$>`{.haskell})
                -- Markdown extensions
            , Ext_implicit_header_references    -- We also allow implicit header references (instead of inserting <a> tags)
            , Ext_definition_lists              -- Definition lists based on PHP Markdown
            , Ext_yaml_metadata_block           -- Allow metadata to be speficied by YAML syntax
            , Ext_superscript                   -- Superscripts (2^10^ is 1024)
            , Ext_subscript                     -- Subscripts (H~2~O is water)
            , Ext_footnotes                     -- Footnotes ([^1]: Here is a footnote)
            ]
        defaultExtensions = writerExtensions defaultHakyllWriterOptions

    in foldr enableExtension defaultExtensions extensions

bibtexCompiler :: Compiler (Item String)
bibtexCompiler = do 
    getResourceBody 
        >>= withItemBody (unixFilter "pandoc" ["-F"
                                            , "pandoc-numbering"
                                            , "-F"
                                            , "pandoc-crossref"
                                            , "-t"
                                            , "markdown"
                                            ])
        >>= readPandocWith defaultHakyllReaderOptions
        >>= return . writePandocWith pandocOptions

orestis.malaspin's avatar
orestis.malaspin committed
cfg :: Configuration
cfg = defaultConfiguration

main :: IO ()
main = hakyllWith cfg $ do
  pages
  posts
orestis.malaspin's avatar
orestis.malaspin committed
  cours_conc
orestis.malaspin's avatar
orestis.malaspin committed
  conc
orestis.malaspin's avatar
orestis.malaspin committed
  cours_mti
  mti
orestis.malaspin's avatar
orestis.malaspin committed
  cours_phys_app
  phys_app
orestis.malaspin's avatar
orestis.malaspin committed
  index
  templates
orestis.malaspin's avatar
orestis.malaspin committed
  resizeThumbnails
  resizeLarge
orestis.malaspin's avatar
orestis.malaspin committed
  resizeHeads