git.iko.soy/typeable/pandoc
on master help sign in
Split locator and suffix in Biblio rather than Markdown parser.

Patch from Nathan Gass.

commit0871a512d7f5cc71f130a7ac56dbe8dfa9d75051 authorJohn MacFarlane <jgm@berkeley.edu> dateFri 26 Nov 2010, 12:06 (16 years ago) parente8516ff
2 files changed+36−40
src/Text/Pandoc/Biblio.hs+34−4 src/Text/Pandoc/Readers/Markdown.hs+2−36
src/Text/Pandoc/Biblio.hs+34−4
@@ -31,6 +31,7 @@ module Text.Pandoc.Biblio ( processBiblio ) where
3131
3232import Data.List
3333import Data.Unique
34+import Data.Char ( isDigit )
3435import qualified Data.Map as M
3536import Text.CSL hiding ( Cite(..), Citation(..) )
3637import qualified Text.CSL as CSL ( Cite(..) )
@@ -97,8 +98,8 @@ getNoteCitations needNote
9798 in queryWith getCitation . getCits
9899
99100setHash :: Citation -> IO Citation
100−setHash (Citation i p s l cm nn _)
101− = hashUnique `fmap` newUnique >>= return . Citation i p s l cm nn
101+setHash (Citation i p s cm nn _)
102+ = hashUnique `fmap` newUnique >>= return . Citation i p s cm nn
102103
103104generateNotes :: [Inline] -> Pandoc -> Pandoc
104105generateNotes needNote = processWith (mvCiteInNote needNote)
@@ -150,14 +151,15 @@ setCitationNoteNum i = map $ \c -> c { citationNoteNum = i}
150151
151152toCslCite :: Citation -> CSL.Cite
152153toCslCite c
153− = let (la,lo) = parseLocator $ citationLocator c
154+ = let (l, s) = locatorWords $ citationSuffix c
155+ (la,lo) = parseLocator $ unwords l
154156 citMode = case citationMode c of
155157 AuthorInText -> (True, False)
156158 SuppressAuthor -> (False,True )
157159 NormalCitation -> (False,False)
158160 in emptyCite { CSL.citeId = citationId c
159161 , CSL.citePrefix = PandocText $ citationPrefix c
160− , CSL.citeSuffix = PandocText $ citationSuffix c
162+ , CSL.citeSuffix = PandocText $ s
161163 , CSL.citeLabel = la
162164 , CSL.citeLocator = lo
163165 , CSL.citeNoteNumber = show $ citationNoteNum c
@@ -165,3 +167,31 @@ toCslCite c
165167 , CSL.suppressAuthor = snd citMode
166168 , CSL.citeHash = citationHash c
167169 }
170+
171+locatorWords :: [Inline] -> ([String], [Inline])
172+locatorWords (Space : t) = locatorWords t
173+locatorWords (Str "" : t) = locatorWords t
174+locatorWords a@(Str (',' : s) : t)
175+ = if ws /= [] then (ws, t') else ([], a)
176+ where
177+ (ws, t') = locatorWords (Str s:t)
178+locatorWords i
179+ = if any isDigit w then (w':ws, s'') else ([], i)
180+ where
181+ (w, s') = locatorWord i
182+ (ws, s'') = locatorWords s'
183+ w' = if ws == [] then w else w ++ ","
184+
185+locatorWord :: [Inline] -> (String, [Inline])
186+locatorWord (Space : r) = (" " ++ ts, r')
187+ where
188+ (ts, r') = locatorWord r
189+locatorWord (Str t : r)
190+ | t' /= "" = (w , Str t' : r)
191+ | otherwise = (t ++ ts, r' )
192+ where
193+ w = takeWhile (/= ',') t
194+ t' = dropWhile (/= ',') t
195+ (ts, r') = locatorWord r
196+locatorWord i = ("", i)
197+
src/Text/Pandoc/Readers/Markdown.hs+2−36
@@ -34,7 +34,7 @@ module Text.Pandoc.Readers.Markdown (
3434import Data.List ( transpose, isSuffixOf, sortBy, findIndex, intercalate )
3535import qualified Data.Map as M
3636import Data.Ord ( comparing )
37−import Data.Char ( isAlphaNum, isDigit )
37+import Data.Char ( isAlphaNum )
3838import Data.Maybe
3939import Text.Pandoc.Definition
4040import Text.Pandoc.Shared
@@ -1319,23 +1319,12 @@ spnl = try $ do
13191319 skipSpaces
13201320 notFollowedBy (char '\n')
13211321
1322−blankSpace :: GenParser Char st ()
1323−blankSpace = try $ do
1324− res <- many1 $ oneOf " \t\n"
1325− guard $ length res > 0
1326− guard $ length (filter (=='\n') res) <= 1
1327−
1328−noneOfUnlessEscaped :: [Char] -> GenParser Char st Char
1329−noneOfUnlessEscaped cs =
1330− try (char '\\' >> oneOf cs) <|> noneOf cs
1331−
13321322textualCite :: GenParser Char ParserState [Citation]
13331323textualCite = try $ do
13341324 (_, key) <- citeKey
13351325 let first = Citation{ citationId = key
13361326 , citationPrefix = []
13371327 , citationSuffix = []
1338− , citationLocator = ""
13391328 , citationMode = AuthorInText
13401329 , citationNoteNum = 0
13411330 , citationHash = 0
@@ -1349,12 +1338,11 @@ bareloc :: Citation -> GenParser Char ParserState [Citation]
13491338bareloc c = try $ do
13501339 spnl
13511340 char '['
1352− loc <- locator
13531341 suff <- suffix
13541342 rest <- option [] $ try $ char ';' >> citeList
13551343 spnl
13561344 char ']'
1357− return $ c{ citationLocator = loc, citationSuffix = suff } : rest
1345+ return $ c{ citationSuffix = suff } : rest
13581346
13591347normalCite :: GenParser Char ParserState [Citation]
13601348normalCite = try $ do
@@ -1376,26 +1364,6 @@ citeKey = try $ do
13761364 guard $ key `elem` stateCitations st
13771365 return (suppress_author, key)
13781366
1379−locator :: GenParser Char st String
1380−locator = try $ do
1381− spnl
1382− w <- many1 (noneOf " \t\n;,]")
1383− ws <- many (locatorWord <|> locatorComma)
1384− return $ unwords $ w:ws
1385−
1386−locatorWord :: GenParser Char st String
1387−locatorWord = try $ do
1388− spnl
1389− wd <- many1 $ noneOfUnlessEscaped "];, \t\n"
1390− guard $ any isDigit wd
1391− return wd
1392−
1393−locatorComma :: GenParser Char st String
1394−locatorComma = try $ do
1395− char ','
1396− lookAhead $ locatorWord
1397− return ","
1398−
13991367suffix :: GenParser Char ParserState [Inline]
14001368suffix = try $ do
14011369 spnl
@@ -1412,12 +1380,10 @@ citation :: GenParser Char ParserState Citation
14121380citation = try $ do
14131381 pref <- prefix
14141382 (suppress_author, key) <- citeKey
1415− loc <- option "" $ try $ blankSpace >> locator
14161383 suff <- suffix
14171384 return $ Citation{ citationId = key
14181385 , citationPrefix = pref
14191386 , citationSuffix = suff
1420− , citationLocator = loc
14211387 , citationMode = if suppress_author
14221388 then SuppressAuthor
14231389 else NormalCitation