Format author names.

Get rid of commas, add non-breaking spaces where needed, trim spaces.
This commit is contained in:
Justin Hsu 2015-02-05 17:52:55 +01:00
parent f700124ccf
commit 9fc0e0a5fd
1 changed files with 42 additions and 10 deletions

View File

@ -15,6 +15,9 @@ module HakyllBibTex
-------------------------------------------------------------------------------- --------------------------------------------------------------------------------
import Control.Applicative import Control.Applicative
import Data.Binary (Binary (..)) import Data.Binary (Binary (..))
import Data.Char (isSpace)
import Data.List.Split (splitOn)
import Data.List (intercalate)
import Data.Typeable (Typeable) import Data.Typeable (Typeable)
import Hakyll import Hakyll
import qualified Bibtex as B import qualified Bibtex as B
@ -45,16 +48,9 @@ bibEntryContext = Context $ \key _ item ->
"identifier" -> return $ StringField $ B.identifier t "identifier" -> return $ StringField $ B.identifier t
_ -> case lookup key (B.fields t) of _ -> case lookup key (B.fields t) of
Nothing -> empty Nothing -> empty
Just val -> return $ StringField $ latexToHtml val Just val -> case key of
where "author" -> return $ StringField $ formatAuthors val
-- Renders latex to HTML, but don't wrap everything in a <p>... _ -> return $ StringField $ latexToHtml val
latexToHtml tex =
let p = case Pandoc.readLaTeX Pandoc.def tex of
Pandoc.Pandoc meta [Pandoc.Para para] ->
Pandoc.Pandoc meta [Pandoc.Plain para]
x -> x
in Pandoc.writeHtmlString Pandoc.def p
-------------------------------------------------------------------------------- --------------------------------------------------------------------------------
newtype BibFile = BibFile [BibEntry] newtype BibFile = BibFile [BibEntry]
@ -80,3 +76,39 @@ lookupBibEntry name (BibFile es) =
case [BibEntry t | BibEntry t <- es, B.identifier t == name] of case [BibEntry t | BibEntry t <- es, B.identifier t == name] of
[] -> error $ name ++ " not found in BibFile" [] -> error $ name ++ " not found in BibFile"
(x : _) -> x (x : _) -> x
--------------------------------------------------------------------------------
formatAuthors :: String -> String
formatAuthors auth =
let auths = splitOn "and" auth
swapped = fmap formatAuthor auths
finalSep = case (length auths) of
1 -> ""
2 -> " and&nbsp;"
_ -> ", and&nbsp;"
in
(intercalate ", " (init swapped)) ++ finalSep ++ last swapped
--------------------------------------------------------------------------------
formatAuthor :: String -> String
formatAuthor = intercalate "&nbsp;"
. (splitOn " ")
. latexToHtml
. trimSpace
. (intercalate " ")
. reverse
. (splitOn ",")
--------------------------------------------------------------------------------
-- Inefficient...
trimSpace :: String -> String
trimSpace = f . f
where f = reverse . dropWhile isSpace
--------------------------------------------------------------------------------
latexToHtml tex =
let p = case Pandoc.readLaTeX Pandoc.def tex of
Pandoc.Pandoc meta [Pandoc.Para para] ->
Pandoc.Pandoc meta [Pandoc.Plain para]
x -> x
in Pandoc.writeHtmlString Pandoc.def p