diff options
author | John MacFarlane <fiddlosopher@gmail.com> | 2012-10-06 09:40:08 -0700 |
---|---|---|
committer | John MacFarlane <fiddlosopher@gmail.com> | 2012-10-06 09:51:23 -0700 |
commit | b617cce95ba2da682a5c25bf3420a5dcceb0a0b8 (patch) | |
tree | f18a627e08f37945fd756e8af3284611029e15b8 /src/Text | |
parent | 1b68dc340554c6e45e922fb8a59ac37c7cdb5752 (diff) |
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.
Diffstat (limited to 'src/Text')
-rw-r--r-- | src/Text/Pandoc/Biblio.hs | 43 |
1 files changed, 19 insertions, 24 deletions
diff --git a/src/Text/Pandoc/Biblio.hs b/src/Text/Pandoc/Biblio.hs index f1fbb28cb..596032087 100644 --- a/src/Text/Pandoc/Biblio.hs +++ b/src/Text/Pandoc/Biblio.hs @@ -30,7 +30,6 @@ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA module Text.Pandoc.Biblio ( processBiblio ) where import Data.List -import Data.Unique import Data.Char ( isDigit, isPunctuation ) import qualified Data.Map as M import Text.CSL hiding ( Cite(..), Citation(..) ) @@ -38,30 +37,24 @@ import qualified Text.CSL as CSL ( Cite(..) ) import Text.Pandoc.Definition import Text.Pandoc.Generic import Text.Pandoc.Shared (stringify) -import Text.Parsec +import Text.Parsec hiding (State) import Control.Monad +import Control.Monad.State -- | Process a 'Pandoc' document by adding citations formatted -- according to a CSL style, using 'citeproc' from citeproc-hs. -processBiblio :: String -> Maybe FilePath -> [Reference] -> Pandoc - -> IO Pandoc -processBiblio cslStr abrfile r p - = if null r then return p - else do - csl <- parseCSL cslStr - abbrevs <- case abrfile of - Just f -> readJsonAbbrevFile f - Nothing -> return [] - p' <- bottomUpM setHash p - let grps = queryWith getCitation p' - style = csl { styleAbbrevs = abbrevs } - result = citeproc procOpts style r (setNearNote style $ - map (map toCslCite) grps) - cits_map = M.fromList $ zip grps (citations result) - biblioList = map (renderPandoc' style) (bibliography result) - Pandoc m b = bottomUp (processCite style cits_map) p' - b' = bottomUp mvPunct $ deNote b - return $ Pandoc m $ b' ++ biblioList +processBiblio :: Style -> [Reference] -> Pandoc -> Pandoc +processBiblio _ [] p = p +processBiblio style r p = + let p' = evalState (bottomUpM setHash p) 1 + grps = queryWith getCitation p' + result = citeproc procOpts style r (setNearNote style $ + map (map toCslCite) grps) + cits_map = M.fromList $ zip grps (citations result) + biblioList = map (renderPandoc' style) (bibliography result) + Pandoc m b = bottomUp (processCite style cits_map) p' + b' = bottomUp mvPunct $ deNote b + in Pandoc m $ b' ++ biblioList -- | Substitute 'Cite' elements with formatted citations. processCite :: Style -> M.Map [Citation] [FormattedOutput] -> Inline -> Inline @@ -116,9 +109,11 @@ getCitation :: Inline -> [[Citation]] getCitation i | Cite t _ <- i = [t] | otherwise = [] -setHash :: Citation -> IO Citation -setHash (Citation i p s cm nn _) - = hashUnique `fmap` newUnique >>= return . Citation i p s cm nn +setHash :: Citation -> State Int Citation +setHash (Citation i p s cm nn _) = do + ident <- get + put $ ident + 1 + return $ Citation i p s cm nn ident toCslCite :: Citation -> CSL.Cite toCslCite c |