on master help sign in
Fix bibliography handling
88519feJasper Van der Jeugt
src/Hakyll.hs+2
@@ -25,6 +25,7 @@ module Hakyll
25 , module Hakyll.Web.Page.Metadata
25 , module Hakyll.Web.Page.Metadata
26 , module Hakyll.Web.Page.Read
26 , module Hakyll.Web.Page.Read
27 , module Hakyll.Web.Pandoc
27 , module Hakyll.Web.Pandoc
28+ , module Hakyll.Web.Pandoc.Biblio
28 , module Hakyll.Web.Pandoc.FileType
29 , module Hakyll.Web.Pandoc.FileType
29 , module Hakyll.Web.Urls
30 , module Hakyll.Web.Urls
30 , module Hakyll.Web.Urls.Relativize
31 , module Hakyll.Web.Urls.Relativize
@@ -57,6 +58,7 @@ import Hakyll.Web.Page.List
57import Hakyll.Web.Page.Metadata
58import Hakyll.Web.Page.Metadata
58import Hakyll.Web.Page.Read
59import Hakyll.Web.Page.Read
59import Hakyll.Web.Pandoc
60import Hakyll.Web.Pandoc
61+import Hakyll.Web.Pandoc.Biblio
60import Hakyll.Web.Pandoc.FileType
62import Hakyll.Web.Pandoc.FileType
61import Hakyll.Web.Urls
63import Hakyll.Web.Urls
62import Hakyll.Web.Urls.Relativize
64import Hakyll.Web.Urls.Relativize
src/Hakyll/Web/Pandoc.hs+12−4
@@ -10,6 +10,7 @@ module Hakyll.Web.Pandoc
10 -- * Functions working on pages/compilers
10 -- * Functions working on pages/compilers
11 , pageReadPandoc
11 , pageReadPandoc
12 , pageReadPandocWith
12 , pageReadPandocWith
13+ , pageReadPandocWithA
13 , pageRenderPandoc
14 , pageRenderPandoc
14 , pageRenderPandocWith
15 , pageRenderPandocWith
15
16
@@ -20,7 +21,7 @@ module Hakyll.Web.Pandoc
20
21
21import Prelude hiding (id)
22import Prelude hiding (id)
22import Control.Applicative ((<$>))
23import Control.Applicative ((<$>))
23−import Control.Arrow ((>>^), (&&&))
24+import Control.Arrow ((>>>), (>>^), (&&&), (***))
24import Control.Category (id)
25import Control.Category (id)
25import Data.Maybe (fromMaybe)
26import Data.Maybe (fromMaybe)
26
27
@@ -28,6 +29,7 @@ import Text.Pandoc
28
29
29import Hakyll.Core.Compiler
30import Hakyll.Core.Compiler
30import Hakyll.Core.Identifier
31import Hakyll.Core.Identifier
32+import Hakyll.Core.Util.Arrow
31import Hakyll.Web.Pandoc.FileType
33import Hakyll.Web.Pandoc.FileType
32import Hakyll.Web.Page.Internal
34import Hakyll.Web.Page.Internal
33
35
@@ -78,10 +80,16 @@ pageReadPandoc = pageReadPandocWith defaultHakyllParserState
78-- | Read the resource using pandoc
80-- | Read the resource using pandoc
79--
81--
80pageReadPandocWith :: ParserState -> Compiler (Page String) (Page Pandoc)
82pageReadPandocWith :: ParserState -> Compiler (Page String) (Page Pandoc)
81−pageReadPandocWith state =
83+pageReadPandocWith state = constA state &&& id >>> pageReadPandocWithA
84+
85+-- | Read the resource using pandoc. This is a (rarely needed) variant, which
86+-- comes in very useful when the parser state is the result of some arrow.
87+--
88+pageReadPandocWithA :: Compiler (ParserState, Page String) (Page Pandoc)
89+pageReadPandocWithA =
82− id &&& getIdentifier &&& getFileType >>^ pageReadPandocWith'
90+ id *** id &&& getIdentifier &&& getFileType >>^ pageReadPandocWithA'
83 where
91 where
84− pageReadPandocWith' (p, (i, t)) = readPandocWith state t (Just i) <$> p
92+ pageReadPandocWithA' (s, (p, (i, t))) = readPandocWith s t (Just i) <$> p
85
93
86-- | Render the resource using pandoc
94-- | Render the resource using pandoc
87--
95--
src/Hakyll/Web/Pandoc/Biblio.hs+36−20
@@ -1,11 +1,19 @@
1-- | Wraps pandocs bibiliography handling
1-- | Wraps pandocs bibiliography handling
2+--
3+-- In order to add a bibliography, you will need a bibliography file (e.g.
4+-- @.bib@) and a CSL file (@.csl@). Both need to be compiled with their
5+-- respective compilers ('biblioCompiler' and 'cslCompiler'). Then, you can
6+-- refer to these files when you use 'pageReadPandocBiblio'. This function also
7+-- takes a parser state for completeness -- you can use
8+-- 'defaultHakyllParserState' if you're unsure.
9+--
2{-# LANGUAGE Arrows, DeriveDataTypeable, GeneralizedNewtypeDeriving #-}
10{-# LANGUAGE Arrows, DeriveDataTypeable, GeneralizedNewtypeDeriving #-}
3module Hakyll.Web.Pandoc.Biblio
11module Hakyll.Web.Pandoc.Biblio
4 ( CSL
12 ( CSL
5 , cslCompiler
13 , cslCompiler
6− , References (..)
14+ , Biblio (..)
7− , referencesCompiler
15+ , biblioCompiler
8− , processBiblioCompiler
16+ , pageReadPandocBiblio
9 ) where
17 ) where
10
18
11import Control.Applicative ((<$>))
19import Control.Applicative ((<$>))
@@ -13,7 +21,7 @@ import Control.Arrow (arr, returnA)
13import Data.Typeable (Typeable)
21import Data.Typeable (Typeable)
14
22
15import Data.Binary (Binary (..))
23import Data.Binary (Binary (..))
16−import Text.Pandoc (Pandoc)
24+import Text.Pandoc (Pandoc, ParserState (..))
17import Text.Pandoc.Biblio (processBiblio)
25import Text.Pandoc.Biblio (processBiblio)
18import qualified Text.CSL as CSL
26import qualified Text.CSL as CSL
19
27
@@ -22,6 +30,7 @@ import Hakyll.Core.Identifier
22import Hakyll.Core.Resource
30import Hakyll.Core.Resource
23import Hakyll.Core.Writable
31import Hakyll.Core.Writable
24import Hakyll.Web.Page
32import Hakyll.Web.Page
33+import Hakyll.Web.Pandoc
25
34
26newtype CSL = CSL FilePath
35newtype CSL = CSL FilePath
27 deriving (Binary, Show, Typeable, Writable)
36 deriving (Binary, Show, Typeable, Writable)
@@ -29,29 +38,36 @@ newtype CSL = CSL FilePath
29cslCompiler :: Compiler Resource CSL
38cslCompiler :: Compiler Resource CSL
30cslCompiler = arr (CSL . unResource)
39cslCompiler = arr (CSL . unResource)
31
40
32−newtype References = References [CSL.Reference]
41+newtype Biblio = Biblio [CSL.Reference]
33 deriving (Show, Typeable)
42 deriving (Show, Typeable)
34
43
35−instance Binary References where
44+instance Binary Biblio where
36 -- Ugly.
45 -- Ugly.
37− get = References . read <$> get
46+ get = Biblio . read <$> get
38− put (References rs) = put $ show rs
47+ put (Biblio rs) = put $ show rs
39
48
40−instance Writable References where
49+instance Writable Biblio where
41 write _ _ = return ()
50 write _ _ = return ()
42
51
43−referencesCompiler :: Compiler Resource References
52+biblioCompiler :: Compiler Resource Biblio
44−referencesCompiler = unsafeCompiler $
53+biblioCompiler = unsafeCompiler $
45− fmap References . CSL.readBiblioFile . unResource
54+ fmap Biblio . CSL.readBiblioFile . unResource
46
55
56+pageReadPandocBiblio :: ParserState
47−processBiblioCompiler :: Identifier CSL
57+ -> Identifier CSL
48− -> Identifier References
58+ -> Identifier Biblio
49− -> Compiler (Page Pandoc) (Page Pandoc)
59+ -> Compiler (Page String) (Page Pandoc)
50−processBiblioCompiler csl refs = proc page -> do
60+pageReadPandocBiblio state csl refs = proc page -> do
51− let body = pageBody page
52 CSL csl' <- require_ csl -< ()
61 CSL csl' <- require_ csl -< ()
53− References refs' <- require_ refs -< ()
62+ Biblio refs' <- require_ refs -< ()
63+ -- We need to know the citation keys, add then *before* actually parsing the
64+ -- actual page. If we don't do this, pandoc won't even consider them
65+ -- citations!
66+ let cits = map CSL.refId refs'
67+ state' = state {stateCitations = stateCitations state ++ cits}
68+ pandocPage <- pageReadPandocWithA -< (state', page)
69+ let pandoc = pageBody pandocPage
54− body' <- unsafeCompiler (tuc processBiblio) -< (csl', refs', body)
70+ pandoc' <- unsafeCompiler (tuc processBiblio) -< (csl', refs', pandoc)
55− returnA -< page {pageBody = body'}
71+ returnA -< pandocPage {pageBody = pandoc'}
56 where
72 where
57 tuc f (x, y, z) = f x y z
73 tuc f (x, y, z) = f x y z