git.iko.soy/typeable/pandoc
on master help sign in
Biblio: Changed type of processBiblio.

* It is no longer in the IO monad. * setHash uses state rather than Data.Unique. * It takes a Style argument rather than parameters for CSL and abbrev filenames. * pandoc.hs now calls the functions to parse the style file and add abbrevs.

commitb617cce95ba2da682a5c25bf3420a5dcceb0a0b8 authorJohn MacFarlane <fiddlosopher@gmail.com> dateSat 6 Oct 2012, 09:40 (14 years ago) parent1b68dc3
2 files changed+24−26
src/Text/Pandoc/Biblio.hs+19−24 src/pandoc.hs+5−2
src/Text/Pandoc/Biblio.hs+19−24
@@ -30,7 +30,6 @@ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
3030module Text.Pandoc.Biblio ( processBiblio ) where
3131
3232import Data.List
33−import Data.Unique
3433import Data.Char ( isDigit, isPunctuation )
3534import qualified Data.Map as M
3635import Text.CSL hiding ( Cite(..), Citation(..) )
@@ -38,30 +37,24 @@ import qualified Text.CSL as CSL ( Cite(..) )
3837import Text.Pandoc.Definition
3938import Text.Pandoc.Generic
4039import Text.Pandoc.Shared (stringify)
41−import Text.Parsec
40+import Text.Parsec hiding (State)
4241import Control.Monad
42+import Control.Monad.State
4343
4444-- | Process a 'Pandoc' document by adding citations formatted
4545-- according to a CSL style, using 'citeproc' from citeproc-hs.
46−processBiblio :: String -> Maybe FilePath -> [Reference] -> Pandoc
47− -> IO Pandoc
48−processBiblio cslStr abrfile r p
49− = if null r then return p
50− else do
51− csl <- parseCSL cslStr
52− abbrevs <- case abrfile of
53− Just f -> readJsonAbbrevFile f
54− Nothing -> return []
55− p' <- bottomUpM setHash p
56− let grps = queryWith getCitation p'
57− style = csl { styleAbbrevs = abbrevs }
58− result = citeproc procOpts style r (setNearNote style $
59− map (map toCslCite) grps)
60− cits_map = M.fromList $ zip grps (citations result)
61− biblioList = map (renderPandoc' style) (bibliography result)
62− Pandoc m b = bottomUp (processCite style cits_map) p'
63− b' = bottomUp mvPunct $ deNote b
64− return $ Pandoc m $ b' ++ biblioList
46+processBiblio :: Style -> [Reference] -> Pandoc -> Pandoc
47+processBiblio _ [] p = p
48+processBiblio style r p =
49+ let p' = evalState (bottomUpM setHash p) 1
50+ grps = queryWith getCitation p'
51+ result = citeproc procOpts style r (setNearNote style $
52+ map (map toCslCite) grps)
53+ cits_map = M.fromList $ zip grps (citations result)
54+ biblioList = map (renderPandoc' style) (bibliography result)
55+ Pandoc m b = bottomUp (processCite style cits_map) p'
56+ b' = bottomUp mvPunct $ deNote b
57+ in Pandoc m $ b' ++ biblioList
6558
6659-- | Substitute 'Cite' elements with formatted citations.
6760processCite :: Style -> M.Map [Citation] [FormattedOutput] -> Inline -> Inline
@@ -116,9 +109,11 @@ getCitation :: Inline -> [[Citation]]
116109getCitation i | Cite t _ <- i = [t]
117110 | otherwise = []
118111
119−setHash :: Citation -> IO Citation
120−setHash (Citation i p s cm nn _)
121− = hashUnique `fmap` newUnique >>= return . Citation i p s cm nn
112+setHash :: Citation -> State Int Citation
113+setHash (Citation i p s cm nn _) = do
114+ ident <- get
115+ put $ ident + 1
116+ return $ Citation i p s cm nn ident
122117
123118toCslCite :: Citation -> CSL.Cite
124119toCslCite c
src/pandoc.hs+5−2
@@ -1007,7 +1007,8 @@ main = do
10071007 doc2 <- do
10081008 if citeMethod == Citeproc && not (null refs)
10091009 then do
1010− csl <- case mbCsl of
1010+ csl <- CSL.parseCSL =<<
1011+ case mbCsl of
10111012 Nothing -> readDataFile datadir "default.csl"
10121013 Just cslfile -> do
10131014 exists <- doesFileExist cslfile
@@ -1018,7 +1019,9 @@ main = do
10181019 print csldir
10191020 readDataFile datadir (replaceDirectory
10201021 (replaceExtension cslfile "csl") csldir)
1021− processBiblio csl cslabbrevs refs doc1
1022+ abbrevs <- maybe (return []) CSL.readJsonAbbrevFile cslabbrevs
1023+ let csl' = csl { CSL.styleAbbrevs = abbrevs }
1024+ return $ processBiblio csl' refs doc1
10221025 else return doc1
10231026
10241027 let writeBinary :: B.ByteString -> IO ()