git.iko.soy/typeable/pandoc
on master help sign in
Citeproc: cleanup and efficiency improvement in deNote.
commit77537b17657e29d047a2f8df1feda2ed540c6e08 authorJohn MacFarlane <jgm@berkeley.edu> dateMon 5 Jul 2021, 13:28 (5 years ago) parentff26af5
1 file changed+21−15
src/Text/Pandoc/Citeproc.hs+21−15
src/Text/Pandoc/Citeproc.hs+21−15
@@ -99,7 +99,7 @@ processCitations (Pandoc meta bs) = do
9999 let metanocites = lookupMeta "nocite" meta
100100 let Pandoc meta'' bs' =
101101 maybe id (setMeta "nocite") metanocites .
102− walk (map capitalizeNoteCitation . mvPunct moveNotes locale) .
102+ walk (mvPunct moveNotes locale) .
103103 (if styleIsNoteStyle sopts
104104 then walk addNote . walk deNote
105105 else id) .
@@ -564,27 +564,28 @@ extractText (FancyVal x) = toText x
564564extractText (NumVal n) = T.pack (show n)
565565extractText _ = mempty
566566
567−capitalizeNoteCitation :: Inline -> Inline
568−capitalizeNoteCitation (Cite cs [Note [Para ils]]) =
569− Cite cs
570− [Note [Para $ B.toList $ addTextCase Nothing CapitalizeFirst
571− $ B.fromList ils]]
572−capitalizeNoteCitation x = x
573−
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.
574570addNote :: Inline -> Inline
575−addNote (Span ("",["csl-note"],[]) ils) = Note [Para ils]
571+addNote (Span ("",["csl-note"],[]) ils) =
572+ Note [Para $
573+ B.toList . addTextCase Nothing CapitalizeFirst . B.fromList $ ils]
576574addNote x = x
577575
578−deNote :: [Inline] -> [Inline]
579−deNote [] = []
580−deNote (Note bs:rest) =
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) =
581582 case bs of
582583 [Para (cit@(Cite (c:_) _) : ils)]
583584 | citationMode c /= AuthorInText ->
584585 -- if citation is first in note, no need to parenthesize.
585586 Note [Para (walk removeNotes $ cit : walk addParens ils)]
586− : deNote rest
587− _ -> Note (walk removeNotes . walk addParens $ bs) : deNote rest
587+ _ -> Note (walk removeNotes . walk addParens $ bs)
588+
588589 where
589590 addParens [] = []
590591 addParens (Cite (c:cs) ils : zs)
@@ -594,25 +595,30 @@ deNote (Note bs:rest) =
594595 | otherwise
595596 = Cite (c:cs) (concatMap noteInParens ils) : addParens zs
596597 addParens (x:xs) = x : addParens xs
598+
597599 removeNotes (Span ("",["csl-note"],[]) ils) = Span ("",[],[]) ils
598600 removeNotes x = x
601+
599602 needsPeriod [] = True
600603 needsPeriod (Str t:_) = case T.uncons t of
601604 Nothing -> False
602605 Just (c,_) -> isUpper c
603606 needsPeriod (Space:zs) = needsPeriod zs
604607 needsPeriod _ = False
608+
605609 noteInParens (Span ("",["csl-note"],[]) ils)
606610 = Space : Str "(" :
607611 removeFinalPeriod ils ++ [Str ")"]
608612 noteInParens x = [x]
613+
609614 noteAfterComma needsPer (Span ("",["csl-note"],[]) ils)
610615 = Str "," : Space :
611616 if needsPer
612617 then ils
613618 else removeFinalPeriod ils
614619 noteAfterComma _ x = [x]
615−deNote (x:xs) = x : deNote xs
620+
621+deNote x = x
616622
617623-- Note: we can't use dropTextWhileEnd indiscriminately,
618624-- because this would remove the final period on abbreviations like Ibid.