aboutsummaryrefslogtreecommitdiff
path: root/src/Text/Pandoc/Writers/BibTeX.hs
diff options
context:
space:
mode:
authorJohn MacFarlane <jgm@berkeley.edu>2021-02-01 18:02:17 -0800
committerJohn MacFarlane <jgm@berkeley.edu>2021-02-01 18:05:20 -0800
commit02d3c71e7224853ecabaa9ac4cd947ec2ac1e579 (patch)
treeb311c690e48cd067bfdf236e0357ca73d4d5f6e7 /src/Text/Pandoc/Writers/BibTeX.hs
parentb239c89a82b66abc55bf7c08e37492938c817c56 (diff)
downloadpandoc-02d3c71e7224853ecabaa9ac4cd947ec2ac1e579.tar.gz
BibTeX writer: use doclayout and doctemplate.
This change allows bibtex/biblatex output to wrap as other formats do, depending on the settings of `--wrap` and `--columns`. It also introduces default templates for bibtex and biblatex, which allow for using the variables `header-include`, `include-before` or `include-after` (or alternatively the command line options `--include-in-header`, `--include-before-body`, `--include-after-body`) to insert content into the generated bibtex/biblatex. This change requires a change in the return type of the unexported `T.P.Citeproc.writeBibTeXString` from `Text` to `Doc Text`. Closes #7068.
Diffstat (limited to 'src/Text/Pandoc/Writers/BibTeX.hs')
-rw-r--r--src/Text/Pandoc/Writers/BibTeX.hs19
1 files changed, 16 insertions, 3 deletions
diff --git a/src/Text/Pandoc/Writers/BibTeX.hs b/src/Text/Pandoc/Writers/BibTeX.hs
index e1cb47ca1..b9ae0c13a 100644
--- a/src/Text/Pandoc/Writers/BibTeX.hs
+++ b/src/Text/Pandoc/Writers/BibTeX.hs
@@ -25,7 +25,11 @@ import Citeproc (parseLang)
import Text.Pandoc.Class (PandocMonad)
import Text.Pandoc.Citeproc.BibTeX as BibTeX
import Text.Pandoc.Citeproc.MetaValue (metaValueToReference)
-import Text.Pandoc.Writers.Shared (lookupMetaString)
+import Text.Pandoc.Writers.Shared (lookupMetaString, defField,
+ addVariablesToContext)
+import Text.DocLayout (render, vcat)
+import Text.DocTemplates (Context(..))
+import Text.Pandoc.Templates (renderTemplate)
-- | Write BibTeX based on the references metadata from a Pandoc document.
writeBibTeX :: PandocMonad m => WriterOptions -> Pandoc -> m Text
@@ -43,6 +47,15 @@ writeBibTeX' variant opts (Pandoc meta _) = do
let refs = case lookupMeta "references" meta of
Just (MetaList xs) -> mapMaybe metaValueToReference xs
_ -> []
- return $ mconcat $
- map (BibTeX.writeBibtexString opts variant mblang) refs
+ let main = vcat $ map (BibTeX.writeBibtexString opts variant mblang) refs
+ let context = defField "body" main
+ $ addVariablesToContext opts (mempty :: Context Text)
+ let colwidth = if writerWrapText opts == WrapAuto
+ then Just $ writerColumns opts
+ else Nothing
+ return $ render colwidth $
+ case writerTemplate opts of
+ Nothing -> main
+ Just tpl -> renderTemplate tpl context
+