@@ -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, tshow)
30+import Text.Pandoc.Shared (stringify, ordNub, blocksToInlines, tshow)
3131import qualified Text.Pandoc.UTF8 as UTF8
3232import Text.Pandoc.Walk (query, walk, walkM)
3333import Control.Applicative ((<|>))
@@ -49,7 +49,6 @@ import qualified Data.Text as T
4949import System.FilePath (takeExtension)
5050import Safe (lastMay, initSafe)
5151
52−
5352processCitations :: PandocMonad m => Pandoc -> m Pandoc
5453processCitations (Pandoc meta bs) = do
5554 style <- getStyle (Pandoc meta bs)
@@ -91,18 +90,22 @@ processCitations (Pandoc meta bs) = do
9190 walk (convertQuotes locale) .
9291 insertSpace $ out)
9392 (resultBibliography result)
94− let moveNotes = styleIsNoteStyle sopts &&
95− maybe True truish (lookupMeta "notes-after-punctuation" meta)
93+ let moveNotes = maybe True truish $
94+ lookupMeta "notes-after-punctuation" meta
9695 let cits = map (walk (convertQuotes locale)) $
9796 resultCitations result
9897
98+ let fixQuotes = case localePunctuationInQuote locale of
99+ Just True ->
100+ B.toList . movePunctuationInsideQuotes . B.fromList
101+ _ -> id
102+
99103 let metanocites = lookupMeta "nocite" meta
100104 let Pandoc meta'' bs' =
101105 maybe id (setMeta "nocite") metanocites .
102− walk (mvPunct moveNotes locale) .
103− (if styleIsNoteStyle sopts
104− then walk addNote . walk deNote
105− else id) .
106+ walk (map capitalizeNoteCitation .
107+ fixQuotes . mvPunct moveNotes locale) .
108+ walk deNote .
106109 evalState (walkM insertResolvedCitations $ Pandoc meta' bs)
107110 $ cits
108111 return $ Pandoc meta''
@@ -372,6 +375,7 @@ formatFromExtension fp = case dropWhile (== '.') $ takeExtension fp of
372375
373376
374377isNote :: Inline -> Bool
378+isNote (Note _) = True
375379isNote (Cite _ [Note _]) = True
376380 -- the following allows citation styles that are "in-text" but use superscript
377381 -- references to be treated as if they are "notes" for the purposes of moving
@@ -384,12 +388,6 @@ isSpacy Space = True
384388isSpacy SoftBreak = True
385389isSpacy _ = False
386390
387−movePunctInsideQuotes :: Locale -> [Inline] -> [Inline]
388−movePunctInsideQuotes locale
389− | localePunctuationInQuote locale == Just True
390− = B.toList . movePunctuationInsideQuotes . B.fromList
391− | otherwise
392− = id
393391
394392mvPunct :: Bool -> Locale -> [Inline] -> [Inline]
395393mvPunct moveNotes locale (x : xs)
@@ -402,8 +400,7 @@ mvPunct moveNotes locale (q : s : x : ys)
402400 in if moveNotes
403401 then if T.null spunct
404402 then q : x : mvPunct moveNotes locale ys
405− else movePunctInsideQuotes locale
406− [q , Str spunct , x] ++ mvPunct moveNotes locale
403+ else q : Str spunct : x : mvPunct moveNotes locale
407404 (B.toList
408405 (dropTextWhile isPunctuation (B.fromList ys)))
409406 else q : x : mvPunct moveNotes locale ys
@@ -415,10 +412,9 @@ mvPunct moveNotes locale (Cite cs ils : ys)
415412 , moveNotes
416413 = let s = stringify ys
417414 spunct = T.takeWhile isPunctuation s
418− in Cite cs (movePunctInsideQuotes locale $
419− init ils
420− ++ [Str spunct | not (endWithPunct False (init ils))]
421− ++ [last ils]) :
415+ in Cite cs (init ils
416+ ++ [Str spunct | not (endWithPunct False (init ils))]
417+ ++ [last ils]) :
422418 mvPunct moveNotes locale
423419 (B.toList (dropTextWhile isPunctuation (B.fromList ys)))
424420mvPunct moveNotes locale (s : x : ys) | isSpacy s, isNote x =
@@ -564,62 +560,42 @@ extractText (FancyVal x) = toText x
564560extractText (NumVal n) = T.pack (show n)
565561extractText _ = mempty
566562
567−-- Here we take the Spans with class csl-note that are left
568−-- after deNote has removed nested ones, and convert them
569−-- into real notes.
570−addNote :: Inline -> Inline
571−addNote (Span ("",["csl-note"],[]) ils) =
572− Note [Para $
573− B.toList . addTextCase Nothing CapitalizeFirst . B.fromList $ ils]
574−addNote x = x
575−
576−-- Here we handle citation notes that occur inside footnotes
577−-- or other citation notes, in a note style. We don't want
578−-- notes inside notes, so we convert these to parenthesized
579−-- or comma-separated citations.
580−deNote :: Inline -> Inline
581−deNote (Note bs) =
582− case bs of
583− [Para (cit@(Cite (c:_) _) : ils)]
584− | citationMode c /= AuthorInText ->
585− -- if citation is first in note, no need to parenthesize.
586− Note [Para (walk removeNotes $ cit : walk addParens ils)]
587− _ -> Note (walk removeNotes . walk addParens $ bs)
588−
563+capitalizeNoteCitation :: Inline -> Inline
564+capitalizeNoteCitation (Cite cs [Note [Para ils]]) =
565+ Cite cs
566+ [Note [Para $ B.toList $ addTextCase Nothing CapitalizeFirst
567+ $ B.fromList ils]]
568+capitalizeNoteCitation x = x
569+
570+deNote :: [Inline] -> [Inline]
571+deNote [] = []
572+deNote (Note bs:rest) =
573+ Note (walk go bs) : deNote rest
589574 where
590− addParens [] = []
591− addParens (Cite (c:cs) ils : zs)
575+ go [] = []
576+ go (Cite (c:cs) ils : zs)
592577 | citationMode c == AuthorInText
593− = Cite (c:cs) (concatMap (noteAfterComma (needsPeriod zs)) ils) :
594− addParens zs
578+ = Cite (c:cs) (concatMap (noteAfterComma (needsPeriod zs)) ils) : go zs
595579 | otherwise
596− = Cite (c:cs) (concatMap noteInParens ils) : addParens zs
597− addParens (x:xs) = x : addParens xs
598−
599− removeNotes (Span ("",["csl-note"],[]) ils) = Span ("",[],[]) ils
600− removeNotes x = x
601−
580+ = Cite (c:cs) (concatMap noteInParens ils) : go zs
581+ go (x:xs) = x : go xs
602582 needsPeriod [] = True
603583 needsPeriod (Str t:_) = case T.uncons t of
604584 Nothing -> False
605585 Just (c,_) -> isUpper c
606586 needsPeriod (Space:zs) = needsPeriod zs
607587 needsPeriod _ = False
608−
609− noteInParens (Span ("",["csl-note"],[]) ils)
588+ noteInParens (Note bs')
610589 = Space : Str "(" :
611− removeFinalPeriod ils ++ [Str ")"]
590+ removeFinalPeriod (blocksToInlines bs') ++ [Str ")"]
612591 noteInParens x = [x]
613−
614− noteAfterComma needsPer (Span ("",["csl-note"],[]) ils)
615− | not (null ils)
592+ noteAfterComma needsPer (Note bs')
616593 = Str "," : Space :
617− if needsPer
618− then ils
619− else removeFinalPeriod ils
594+ (if needsPer
595+ then id
596+ else removeFinalPeriod) (blocksToInlines bs')
620597 noteAfterComma _ x = [x]
621−
622−deNote x = x
598+deNote (x:xs) = x : deNote xs
623599
624600-- Note: we can't use dropTextWhileEnd indiscriminately,
625601-- because this would remove the final period on abbreviations like Ibid.