From 9f089aa2863afdbfacd8a43dc85f0b8c53463bb8 Mon Sep 17 00:00:00 2001 From: John MacFarlane Date: Mon, 13 Dec 2021 12:10:13 -0800 Subject: Org writer: add tests for org-cite citations, and improve support. --- src/Text/Pandoc/Writers/Org.hs | 32 ++++++++++++++++++++++++++---- test/command/7329.md | 44 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 72 insertions(+), 4 deletions(-) create mode 100644 test/command/7329.md diff --git a/src/Text/Pandoc/Writers/Org.hs b/src/Text/Pandoc/Writers/Org.hs index bf7c3f551..ce406567b 100644 --- a/src/Text/Pandoc/Writers/Org.hs +++ b/src/Text/Pandoc/Writers/Org.hs @@ -29,22 +29,32 @@ import Text.Pandoc.Options import Text.DocLayout import Text.Pandoc.Shared import Text.Pandoc.Templates (renderTemplate) +import Text.Pandoc.Citeproc.Locator (parseLocator, toLocatorMap, LocatorMap, + LocatorInfo(..)) +import Text.Pandoc.Citeproc (getCiteprocLang, getStyle) +import qualified Citeproc as Citeproc import Text.Pandoc.Writers.Shared data WriterState = WriterState { stNotes :: [[Block]] , stHasMath :: Bool , stOptions :: WriterOptions + , stLocatorMap :: LocatorMap } type Org = StateT WriterState -- | Convert Pandoc to Org. writeOrg :: PandocMonad m => WriterOptions -> Pandoc -> m Text -writeOrg opts document = do +writeOrg opts document@(Pandoc meta _) = do + style <- getStyle document + mblang <- getCiteprocLang meta + let locmap = toLocatorMap $ Citeproc.mergeLocales mblang style + let st = WriterState { stNotes = [], stHasMath = False, - stOptions = opts } + stOptions = opts, + stLocatorMap = locmap } evalStateT (pandocToOrg document) st -- | Return Org representation of document. @@ -103,7 +113,12 @@ blockToOrg :: PandocMonad m => Block -- ^ Block element -> Org m (Doc Text) blockToOrg Null = return empty -blockToOrg (Div attr bs) = divToOrg attr bs +blockToOrg (Div attr@(ident,_,_) bs) = do + opts <- gets stOptions + -- Strip off bibliography if citations enabled + if ident == "refs" && isEnabled Ext_citations opts + then return mempty + else divToOrg attr bs blockToOrg (Plain inlines) = inlineListToOrg inlines blockToOrg (SimpleFigure attr txt (src, tit)) = do capt <- if null txt @@ -402,9 +417,18 @@ inlineToOrg (Cite cs lst) = do then do let renderCiteItem c = do citePref <- inlineListToOrg (citationPrefix c) - citeSuff <- inlineListToOrg (citationSuffix c) + locmap <- gets stLocatorMap + let (locinfo, suffix) = parseLocator locmap (citationSuffix c) + citeSuff <- inlineListToOrg suffix + let locator = case locinfo of + Just info -> literal $ + T.replace "\160" " " $ + T.replace "{" "" $ + T.replace "}" "" $ locatorRaw info + Nothing -> mempty return $ hsep [ citePref , ("@" <> literal (citationId c)) + , locator , citeSuff ] citeItems <- mconcat . intersperse "; " <$> mapM renderCiteItem cs let sty = case cs of diff --git a/test/command/7329.md b/test/command/7329.md new file mode 100644 index 000000000..565241db8 --- /dev/null +++ b/test/command/7329.md @@ -0,0 +1,44 @@ +``` +% pandoc -f markdown -t org +- @item1 +- @item1 [p. 12] +- @item1 [p.12; see also @item2] +- [@item1] +- [-@item1] +- [see @item1 p. 12] +- [see @item1, p. 12] +- [see @item1, p. 12 and *passim*] +- [@item1;@item2] +- [see @item1; @item2] +^D +- [cite/t:@item1] +- [cite/t:@item1 p. 12] +- [cite/t:@item1 p.12; see also @item2] +- [cite:@item1] +- [cite/na:@item1] +- [cite:see @item1 p. 12] +- [cite:see @item1 p. 12] +- [cite:see @item1 p. 12 and /passim/] +- [cite:@item1; @item2] +- [cite:see @item1; @item2] +``` + +``` +% pandoc -f markdown -t org -C --bibliography command/biblio.bib +- [@item1] +^D +- [cite:@item1] +``` + + +``` +% pandoc -f markdown -t org-citations -C --bibliography command/biblio.bib +[@item1] +^D +(Doe 2005) + +<> + +<> +Doe, John. 2005. /First Book/. Cambridge: Cambridge University Press. +``` -- cgit v1.2.3