git.iko.soy/typeable/pandoc
on master help sign in
Split locator and suffix in Biblio rather than Markdown parser.
0871a51John MacFarlane
src/Text/Pandoc/Biblio.hs+34−4
@@ -31,6 +31,7 @@ module Text.Pandoc.Biblio ( processBiblio ) where
31
31
32import Data.List
32import Data.List
33import Data.Unique
33import Data.Unique
34+import Data.Char ( isDigit )
34import qualified Data.Map as M
35import qualified Data.Map as M
35import Text.CSL hiding ( Cite(..), Citation(..) )
36import Text.CSL hiding ( Cite(..), Citation(..) )
36import qualified Text.CSL as CSL ( Cite(..) )
37import qualified Text.CSL as CSL ( Cite(..) )
@@ -97,8 +98,8 @@ getNoteCitations needNote
97 in queryWith getCitation . getCits
98 in queryWith getCitation . getCits
98
99
99setHash :: Citation -> IO Citation
100setHash :: Citation -> IO Citation
100−setHash (Citation i p s l cm nn _)
101+setHash (Citation i p s cm nn _)
101− = hashUnique `fmap` newUnique >>= return . Citation i p s l cm nn
102+ = hashUnique `fmap` newUnique >>= return . Citation i p s cm nn
102
103
103generateNotes :: [Inline] -> Pandoc -> Pandoc
104generateNotes :: [Inline] -> Pandoc -> Pandoc
104generateNotes needNote = processWith (mvCiteInNote needNote)
105generateNotes needNote = processWith (mvCiteInNote needNote)
@@ -150,14 +151,15 @@ setCitationNoteNum i = map $ \c -> c { citationNoteNum = i}
150
151
151toCslCite :: Citation -> CSL.Cite
152toCslCite :: Citation -> CSL.Cite
152toCslCite c
153toCslCite c
154+ = let (l, s) = locatorWords $ citationSuffix c
153− = let (la,lo) = parseLocator $ citationLocator c
155+ (la,lo) = parseLocator $ unwords l
154 citMode = case citationMode c of
156 citMode = case citationMode c of
155 AuthorInText -> (True, False)
157 AuthorInText -> (True, False)
156 SuppressAuthor -> (False,True )
158 SuppressAuthor -> (False,True )
157 NormalCitation -> (False,False)
159 NormalCitation -> (False,False)
158 in emptyCite { CSL.citeId = citationId c
160 in emptyCite { CSL.citeId = citationId c
159 , CSL.citePrefix = PandocText $ citationPrefix c
161 , CSL.citePrefix = PandocText $ citationPrefix c
160− , CSL.citeSuffix = PandocText $ citationSuffix c
162+ , CSL.citeSuffix = PandocText $ s
161 , CSL.citeLabel = la
163 , CSL.citeLabel = la
162 , CSL.citeLocator = lo
164 , CSL.citeLocator = lo
163 , CSL.citeNoteNumber = show $ citationNoteNum c
165 , CSL.citeNoteNumber = show $ citationNoteNum c
@@ -165,3 +167,31 @@ toCslCite c
165 , CSL.suppressAuthor = snd citMode
167 , CSL.suppressAuthor = snd citMode
166 , CSL.citeHash = citationHash c
168 , CSL.citeHash = citationHash c
167 }
169 }
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 (
34import Data.List ( transpose, isSuffixOf, sortBy, findIndex, intercalate )
34import Data.List ( transpose, isSuffixOf, sortBy, findIndex, intercalate )
35import qualified Data.Map as M
35import qualified Data.Map as M
36import Data.Ord ( comparing )
36import Data.Ord ( comparing )
37−import Data.Char ( isAlphaNum, isDigit )
37+import Data.Char ( isAlphaNum )
38import Data.Maybe
38import Data.Maybe
39import Text.Pandoc.Definition
39import Text.Pandoc.Definition
40import Text.Pandoc.Shared
40import Text.Pandoc.Shared
@@ -1319,23 +1319,12 @@ spnl = try $ do
1319 skipSpaces
1319 skipSpaces
1320 notFollowedBy (char '\n')
1320 notFollowedBy (char '\n')
1321
1321
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−
1332textualCite :: GenParser Char ParserState [Citation]
1322textualCite :: GenParser Char ParserState [Citation]
1333textualCite = try $ do
1323textualCite = try $ do
1334 (_, key) <- citeKey
1324 (_, key) <- citeKey
1335 let first = Citation{ citationId = key
1325 let first = Citation{ citationId = key
1336 , citationPrefix = []
1326 , citationPrefix = []
1337 , citationSuffix = []
1327 , citationSuffix = []
1338− , citationLocator = ""
1339 , citationMode = AuthorInText
1328 , citationMode = AuthorInText
1340 , citationNoteNum = 0
1329 , citationNoteNum = 0
1341 , citationHash = 0
1330 , citationHash = 0
@@ -1349,12 +1338,11 @@ bareloc :: Citation -> GenParser Char ParserState [Citation]
1349bareloc c = try $ do
1338bareloc c = try $ do
1350 spnl
1339 spnl
1351 char '['
1340 char '['
1352− loc <- locator
1353 suff <- suffix
1341 suff <- suffix
1354 rest <- option [] $ try $ char ';' >> citeList
1342 rest <- option [] $ try $ char ';' >> citeList
1355 spnl
1343 spnl
1356 char ']'
1344 char ']'
1357− return $ c{ citationLocator = loc, citationSuffix = suff } : rest
1345+ return $ c{ citationSuffix = suff } : rest
1358
1346
1359normalCite :: GenParser Char ParserState [Citation]
1347normalCite :: GenParser Char ParserState [Citation]
1360normalCite = try $ do
1348normalCite = try $ do
@@ -1376,26 +1364,6 @@ citeKey = try $ do
1376 guard $ key `elem` stateCitations st
1364 guard $ key `elem` stateCitations st
1377 return (suppress_author, key)
1365 return (suppress_author, key)
1378
1366
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−
1399suffix :: GenParser Char ParserState [Inline]
1367suffix :: GenParser Char ParserState [Inline]
1400suffix = try $ do
1368suffix = try $ do
1401 spnl
1369 spnl
@@ -1412,12 +1380,10 @@ citation :: GenParser Char ParserState Citation
1412citation = try $ do
1380citation = try $ do
1413 pref <- prefix
1381 pref <- prefix
1414 (suppress_author, key) <- citeKey
1382 (suppress_author, key) <- citeKey
1415− loc <- option "" $ try $ blankSpace >> locator
1416 suff <- suffix
1383 suff <- suffix
1417 return $ Citation{ citationId = key
1384 return $ Citation{ citationId = key
1418 , citationPrefix = pref
1385 , citationPrefix = pref
1419 , citationSuffix = suff
1386 , citationSuffix = suff
1420− , citationLocator = loc
1421 , citationMode = if suppress_author
1387 , citationMode = if suppress_author
1422 then SuppressAuthor
1388 then SuppressAuthor
1423 else NormalCitation
1389 else NormalCitation