2015-01-03 22:23:27 +00:00
|
|
|
-- JH: From hakyll-bibtex by jaspervdj, using the Pandoc parser.
|
|
|
|
--------------------------------------------------------------------------------
|
|
|
|
{-# LANGUAGE DeriveDataTypeable #-}
|
|
|
|
{-# LANGUAGE GeneralizedNewtypeDeriving #-}
|
|
|
|
module HakyllBibTex
|
|
|
|
( BibEntry (..)
|
|
|
|
, bibEntryContext
|
|
|
|
, BibFile (..)
|
|
|
|
, bibFileEntryNames
|
|
|
|
, parseBibFile
|
|
|
|
, lookupBibEntry
|
|
|
|
) where
|
|
|
|
|
|
|
|
|
|
|
|
--------------------------------------------------------------------------------
|
|
|
|
import Control.Applicative
|
|
|
|
import Data.Binary (Binary (..))
|
2015-02-05 16:52:55 +00:00
|
|
|
import Data.Char (isSpace)
|
|
|
|
import Data.List.Split (splitOn)
|
|
|
|
import Data.List (intercalate)
|
2017-09-23 19:37:50 +00:00
|
|
|
import qualified Data.Map as M (empty)
|
2018-02-17 18:18:06 +00:00
|
|
|
import Data.Text (pack, unpack)
|
2015-01-03 22:23:27 +00:00
|
|
|
import Data.Typeable (Typeable)
|
|
|
|
import Hakyll
|
2015-01-05 04:43:45 +00:00
|
|
|
import qualified Bibtex as B
|
2018-02-17 18:18:06 +00:00
|
|
|
import Text.Pandoc
|
2015-01-03 22:23:27 +00:00
|
|
|
import qualified Text.Parsec as Parsec
|
|
|
|
|
|
|
|
--------------------------------------------------------------------------------
|
2017-09-23 19:37:50 +00:00
|
|
|
newtype BibEntry = BE B.Item
|
2015-01-03 22:23:27 +00:00
|
|
|
deriving (Typeable)
|
|
|
|
|
2017-09-23 19:37:50 +00:00
|
|
|
unwrapBE :: BibEntry -> B.Item
|
|
|
|
unwrapBE (BE it) = it
|
|
|
|
|
2015-01-03 22:23:27 +00:00
|
|
|
--------------------------------------------------------------------------------
|
|
|
|
instance Binary BibEntry where
|
2017-09-23 19:37:50 +00:00
|
|
|
put (BE t) = do
|
2015-01-03 22:23:27 +00:00
|
|
|
put $ B.entryType t
|
|
|
|
put $ B.identifier t
|
|
|
|
put $ B.fields t
|
|
|
|
|
2017-09-23 19:37:50 +00:00
|
|
|
get = BE <$> (B.Item <$> get <*> get <*> get)
|
2015-01-03 22:23:27 +00:00
|
|
|
|
|
|
|
--------------------------------------------------------------------------------
|
2017-09-23 19:37:50 +00:00
|
|
|
-- XXX refactor to use Context monoid ? XXX
|
2015-01-03 22:23:27 +00:00
|
|
|
bibEntryContext :: Context BibEntry
|
|
|
|
bibEntryContext = Context $ \key _ item ->
|
2017-09-23 19:37:50 +00:00
|
|
|
let BE t = itemBody item
|
2015-01-03 22:23:27 +00:00
|
|
|
in case key of
|
|
|
|
"identifier" -> return $ StringField $ B.identifier t
|
|
|
|
_ -> case lookup key (B.fields t) of
|
2017-09-23 19:37:50 +00:00
|
|
|
Just val -> return $ StringField $ case key of
|
|
|
|
k
|
|
|
|
| k == "author" -> formatAuthors val
|
2018-07-02 18:13:33 +00:00
|
|
|
| k == "month" -> expandMonth val
|
|
|
|
| otherwise -> latexToHtml val
|
2017-09-23 19:37:50 +00:00
|
|
|
Nothing -> empty
|
2015-01-03 22:23:27 +00:00
|
|
|
|
|
|
|
--------------------------------------------------------------------------------
|
|
|
|
newtype BibFile = BibFile [BibEntry]
|
|
|
|
deriving (Binary, Typeable)
|
|
|
|
|
|
|
|
|
|
|
|
--------------------------------------------------------------------------------
|
|
|
|
bibFileEntryNames :: BibFile -> [String]
|
2017-09-23 19:37:50 +00:00
|
|
|
bibFileEntryNames (BibFile es) = [B.identifier t | BE t <- es]
|
2015-01-03 22:23:27 +00:00
|
|
|
|
|
|
|
|
|
|
|
--------------------------------------------------------------------------------
|
|
|
|
parseBibFile :: String -> BibFile
|
|
|
|
parseBibFile string = case Parsec.runParser (B.bibEntries <* Parsec.eof)
|
|
|
|
[] "<bib file>" string of
|
|
|
|
Left err -> error $ show err
|
2017-09-23 19:37:50 +00:00
|
|
|
Right xs -> BibFile $ map BE (B.resolveCrossRefs True xs)
|
2015-01-03 22:23:27 +00:00
|
|
|
|
|
|
|
|
|
|
|
--------------------------------------------------------------------------------
|
|
|
|
lookupBibEntry :: String -> BibFile -> BibEntry
|
|
|
|
lookupBibEntry name (BibFile es) =
|
2017-09-23 19:37:50 +00:00
|
|
|
case [BE t | BE t <- es, B.identifier t == name] of
|
2015-01-03 22:23:27 +00:00
|
|
|
[] -> error $ name ++ " not found in BibFile"
|
|
|
|
(x : _) -> x
|
2015-02-05 16:52:55 +00:00
|
|
|
|
2017-09-23 19:37:50 +00:00
|
|
|
|
2015-02-05 16:52:55 +00:00
|
|
|
--------------------------------------------------------------------------------
|
|
|
|
formatAuthors :: String -> String
|
|
|
|
formatAuthors auth =
|
2018-02-17 18:18:25 +00:00
|
|
|
let auths = splitOn " and" auth
|
2015-02-05 16:52:55 +00:00
|
|
|
swapped = fmap formatAuthor auths
|
|
|
|
finalSep = case (length auths) of
|
|
|
|
1 -> ""
|
|
|
|
2 -> " and "
|
|
|
|
_ -> ", and "
|
|
|
|
in
|
|
|
|
(intercalate ", " (init swapped)) ++ finalSep ++ last swapped
|
|
|
|
|
|
|
|
--------------------------------------------------------------------------------
|
|
|
|
formatAuthor :: String -> String
|
|
|
|
formatAuthor = intercalate " "
|
|
|
|
. (splitOn " ")
|
|
|
|
. latexToHtml
|
|
|
|
. trimSpace
|
|
|
|
. (intercalate " ")
|
|
|
|
. reverse
|
|
|
|
. (splitOn ",")
|
|
|
|
|
2016-02-19 04:46:23 +00:00
|
|
|
--------------------------------------------------------------------------------
|
|
|
|
expandMonth :: String -> String
|
|
|
|
expandMonth mon = case mon of
|
|
|
|
"jan" -> "January"
|
|
|
|
"feb" -> "February"
|
|
|
|
"mar" -> "March"
|
|
|
|
"apr" -> "April"
|
|
|
|
"may" -> "May"
|
|
|
|
"jun" -> "June"
|
|
|
|
"jul" -> "July"
|
|
|
|
"aug" -> "August"
|
|
|
|
"sep" -> "September"
|
|
|
|
"oct" -> "October"
|
|
|
|
"nov" -> "November"
|
|
|
|
"dec" -> "December"
|
2016-10-14 02:43:11 +00:00
|
|
|
x -> x
|
2016-02-19 04:46:23 +00:00
|
|
|
|
2015-02-05 16:52:55 +00:00
|
|
|
--------------------------------------------------------------------------------
|
|
|
|
-- Inefficient...
|
|
|
|
trimSpace :: String -> String
|
|
|
|
trimSpace = f . f
|
2016-02-19 04:43:24 +00:00
|
|
|
where f = reverse . dropWhile isSpace
|
2015-02-05 16:52:55 +00:00
|
|
|
|
2018-02-17 18:18:06 +00:00
|
|
|
-- Remove <p> and </p> tags...
|
|
|
|
trimPara :: String -> String
|
|
|
|
trimPara str = concat $ splitOn "</p>" $ drop 3 str
|
|
|
|
|
2015-02-05 16:52:55 +00:00
|
|
|
--------------------------------------------------------------------------------
|
2018-02-17 18:18:06 +00:00
|
|
|
latexToHtml :: String -> String
|
2015-02-05 16:52:55 +00:00
|
|
|
latexToHtml tex =
|
2018-02-17 18:18:06 +00:00
|
|
|
let res = runPure $ readLaTeX def (pack tex)
|
|
|
|
>>= writeHtml5String def
|
|
|
|
>>= return . trimPara . unpack
|
|
|
|
in case res of
|
|
|
|
Left _ -> ""
|
|
|
|
Right html -> html
|