git.iko.soy/typeable/pandoc
on master help sign in
Revamp note citation handling.
ff26af5John MacFarlane
cabal.project+1−1
@@ -10,4 +10,4 @@ source-repository-package
10source-repository-package
10source-repository-package
11 type: git
11 type: git
12 location: https://github.com/jgm/citeproc
12 location: https://github.com/jgm/citeproc
13− tag: 85277c4baa6c0350baf29247583ae0f438f7c9c5
13+ tag: 4ce0501cd6f9c86eee087afcf878c256e49b9615
src/Text/Pandoc/Citeproc.hs+30−14
@@ -27,7 +27,7 @@ import Text.Pandoc.Error (PandocError(..))
27import Text.Pandoc.Extensions (pandocExtensions)
27import Text.Pandoc.Extensions (pandocExtensions)
28import Text.Pandoc.Logging (LogMessage(..))
28import Text.Pandoc.Logging (LogMessage(..))
29import Text.Pandoc.Options (ReaderOptions(..))
29import Text.Pandoc.Options (ReaderOptions(..))
30−import Text.Pandoc.Shared (stringify, ordNub, blocksToInlines, tshow)
30+import Text.Pandoc.Shared (stringify, ordNub, tshow)
31import qualified Text.Pandoc.UTF8 as UTF8
31import qualified Text.Pandoc.UTF8 as UTF8
32import Text.Pandoc.Walk (query, walk, walkM)
32import Text.Pandoc.Walk (query, walk, walkM)
33import Control.Applicative ((<|>))
33import Control.Applicative ((<|>))
@@ -49,6 +49,7 @@ import qualified Data.Text as T
49import System.FilePath (takeExtension)
49import System.FilePath (takeExtension)
50import Safe (lastMay, initSafe)
50import Safe (lastMay, initSafe)
51
51
52+
52processCitations :: PandocMonad m => Pandoc -> m Pandoc
53processCitations :: PandocMonad m => Pandoc -> m Pandoc
53processCitations (Pandoc meta bs) = do
54processCitations (Pandoc meta bs) = do
54 style <- getStyle (Pandoc meta bs)
55 style <- getStyle (Pandoc meta bs)
@@ -99,7 +100,9 @@ processCitations (Pandoc meta bs) = do
99 let Pandoc meta'' bs' =
100 let Pandoc meta'' bs' =
100 maybe id (setMeta "nocite") metanocites .
101 maybe id (setMeta "nocite") metanocites .
101 walk (map capitalizeNoteCitation . mvPunct moveNotes locale) .
102 walk (map capitalizeNoteCitation . mvPunct moveNotes locale) .
103+ (if styleIsNoteStyle sopts
102− walk deNote .
104+ then walk addNote . walk deNote
105+ else id) .
103 evalState (walkM insertResolvedCitations $ Pandoc meta' bs)
106 evalState (walkM insertResolvedCitations $ Pandoc meta' bs)
104 $ cits
107 $ cits
105 return $ Pandoc meta''
108 return $ Pandoc meta''
@@ -568,33 +571,46 @@ capitalizeNoteCitation (Cite cs [Note [Para ils]]) =
568 $ B.fromList ils]]
571 $ B.fromList ils]]
569capitalizeNoteCitation x = x
572capitalizeNoteCitation x = x
570
573
574+addNote :: Inline -> Inline
575+addNote (Span ("",["csl-note"],[]) ils) = Note [Para ils]
576+addNote x = x
577+
571deNote :: [Inline] -> [Inline]
578deNote :: [Inline] -> [Inline]
572deNote [] = []
579deNote [] = []
573deNote (Note bs:rest) =
580deNote (Note bs:rest) =
581+ case bs of
582+ [Para (cit@(Cite (c:_) _) : ils)]
583+ | citationMode c /= AuthorInText ->
584+ -- if citation is first in note, no need to parenthesize.
585+ Note [Para (walk removeNotes $ cit : walk addParens ils)]
586+ : deNote rest
574− Note (walk go bs) : deNote rest
587+ _ -> Note (walk removeNotes . walk addParens $ bs) : deNote rest
575 where
588 where
576− go [] = []
589+ addParens [] = []
577− go (Cite (c:cs) ils : zs)
590+ addParens (Cite (c:cs) ils : zs)
578 | citationMode c == AuthorInText
591 | citationMode c == AuthorInText
579− = Cite (c:cs) (concatMap (noteAfterComma (needsPeriod zs)) ils) : go zs
592+ = Cite (c:cs) (concatMap (noteAfterComma (needsPeriod zs)) ils) :
593+ addParens zs
580 | otherwise
594 | otherwise
581− = Cite (c:cs) (concatMap noteInParens ils) : go zs
595+ = Cite (c:cs) (concatMap noteInParens ils) : addParens zs
582− go (x:xs) = x : go xs
596+ addParens (x:xs) = x : addParens xs
597+ removeNotes (Span ("",["csl-note"],[]) ils) = Span ("",[],[]) ils
598+ removeNotes x = x
583 needsPeriod [] = True
599 needsPeriod [] = True
584 needsPeriod (Str t:_) = case T.uncons t of
600 needsPeriod (Str t:_) = case T.uncons t of
585 Nothing -> False
601 Nothing -> False
586 Just (c,_) -> isUpper c
602 Just (c,_) -> isUpper c
587 needsPeriod (Space:zs) = needsPeriod zs
603 needsPeriod (Space:zs) = needsPeriod zs
588 needsPeriod _ = False
604 needsPeriod _ = False
589− noteInParens (Note bs')
605+ noteInParens (Span ("",["csl-note"],[]) ils)
590 = Space : Str "(" :
606 = Space : Str "(" :
591− removeFinalPeriod (blocksToInlines bs') ++ [Str ")"]
607+ removeFinalPeriod ils ++ [Str ")"]
592 noteInParens x = [x]
608 noteInParens x = [x]
593− noteAfterComma needsPer (Note bs')
609+ noteAfterComma needsPer (Span ("",["csl-note"],[]) ils)
594 = Str "," : Space :
610 = Str "," : Space :
595− (if needsPer
611+ if needsPer
596− then id
612+ then ils
597− else removeFinalPeriod) (blocksToInlines bs')
613+ else removeFinalPeriod ils
598 noteAfterComma _ x = [x]
614 noteAfterComma _ x = [x]
599deNote (x:xs) = x : deNote xs
615deNote (x:xs) = x : deNote xs
600
616
stack.yaml+1−1
@@ -14,7 +14,7 @@ extra-deps:
14- git: https://github.com/jgm/doctemplates
14- git: https://github.com/jgm/doctemplates
15 commit: 428c26d5303cf7a2b1051fe1ffd9aafe9ba71c81
15 commit: 428c26d5303cf7a2b1051fe1ffd9aafe9ba71c81
16- git: https://github.com/jgm/citeproc
16- git: https://github.com/jgm/citeproc
17− commit: 85277c4baa6c0350baf29247583ae0f438f7c9c5
17+ commit: 4ce0501cd6f9c86eee087afcf878c256e49b9615
18ghc-options:
18ghc-options:
19 "$locals": -fhide-source-paths -Wno-missing-home-modules
19 "$locals": -fhide-source-paths -Wno-missing-home-modules
20resolver: lts-18.0
20resolver: lts-18.0