@@ -27,7 +27,7 @@ import Text.Pandoc.Error (PandocError(..))
2727import Text.Pandoc.Extensions (pandocExtensions)
2828import Text.Pandoc.Logging (LogMessage(..))
2929import Text.Pandoc.Options (ReaderOptions(..))
30−import Text.Pandoc.Shared (stringify, ordNub, blocksToInlines, tshow)
30+import Text.Pandoc.Shared (stringify, ordNub, tshow)
3131import qualified Text.Pandoc.UTF8 as UTF8
3232import Text.Pandoc.Walk (query, walk, walkM)
3333import Control.Applicative ((<|>))
@@ -49,6 +49,7 @@ import qualified Data.Text as T
4949import System.FilePath (takeExtension)
5050import Safe (lastMay, initSafe)
5151
52+
5253processCitations :: PandocMonad m => Pandoc -> m Pandoc
5354processCitations (Pandoc meta bs) = do
5455 style <- getStyle (Pandoc meta bs)
@@ -99,7 +100,9 @@ processCitations (Pandoc meta bs) = do
99100 let Pandoc meta'' bs' =
100101 maybe id (setMeta "nocite") metanocites .
101102 walk (map capitalizeNoteCitation . mvPunct moveNotes locale) .
102− walk deNote .
103+ (if styleIsNoteStyle sopts
104+ then walk addNote . walk deNote
105+ else id) .
103106 evalState (walkM insertResolvedCitations $ Pandoc meta' bs)
104107 $ cits
105108 return $ Pandoc meta''
@@ -568,33 +571,46 @@ capitalizeNoteCitation (Cite cs [Note [Para ils]]) =
568571 $ B.fromList ils]]
569572capitalizeNoteCitation x = x
570573
574+addNote :: Inline -> Inline
575+addNote (Span ("",["csl-note"],[]) ils) = Note [Para ils]
576+addNote x = x
577+
571578deNote :: [Inline] -> [Inline]
572579deNote [] = []
573580deNote (Note bs:rest) =
574− Note (walk go bs) : deNote 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
587+ _ -> Note (walk removeNotes . walk addParens $ bs) : deNote rest
575588 where
576− go [] = []
577− go (Cite (c:cs) ils : zs)
589+ addParens [] = []
590+ addParens (Cite (c:cs) ils : zs)
578591 | 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
580594 | otherwise
581− = Cite (c:cs) (concatMap noteInParens ils) : go zs
582− go (x:xs) = x : go xs
595+ = Cite (c:cs) (concatMap noteInParens ils) : addParens zs
596+ addParens (x:xs) = x : addParens xs
597+ removeNotes (Span ("",["csl-note"],[]) ils) = Span ("",[],[]) ils
598+ removeNotes x = x
583599 needsPeriod [] = True
584600 needsPeriod (Str t:_) = case T.uncons t of
585601 Nothing -> False
586602 Just (c,_) -> isUpper c
587603 needsPeriod (Space:zs) = needsPeriod zs
588604 needsPeriod _ = False
589− noteInParens (Note bs')
605+ noteInParens (Span ("",["csl-note"],[]) ils)
590606 = Space : Str "(" :
591− removeFinalPeriod (blocksToInlines bs') ++ [Str ")"]
607+ removeFinalPeriod ils ++ [Str ")"]
592608 noteInParens x = [x]
593− noteAfterComma needsPer (Note bs')
609+ noteAfterComma needsPer (Span ("",["csl-note"],[]) ils)
594610 = Str "," : Space :
595− (if needsPer
596− then id
597− else removeFinalPeriod) (blocksToInlines bs')
611+ if needsPer
612+ then ils
613+ else removeFinalPeriod ils
598614 noteAfterComma _ x = [x]
599615deNote (x:xs) = x : deNote xs
600616