git.iko.soy/typeable/pandoc
on master help sign in
checkpoint.
commit9f6f4ab0436be061470b3c6df7ea49923af4bc6f authorJohn MacFarlane <jgm@berkeley.edu> dateSat 1 Jul 2017, 21:50 (9 years ago) parentb212241
1 file changed+132−66
src/Text/Pandoc/Readers/LaTeX.hs+132−66
src/Text/Pandoc/Readers/LaTeX.hs+132−66
@@ -59,6 +59,8 @@ import Text.Pandoc.Parsing hiding (many, mathDisplay, mathInline, optional,
5959import Text.Pandoc.Shared
6060import Text.Pandoc.Walk
6161
62+import Text.Pandoc.Extensions (getDefaultExtensions)
63+
6264-- | Parse LaTeX from string and return 'Pandoc' document.
6365readLaTeX :: PandocMonad m
6466 => ReaderOptions -- ^ Reader options
@@ -68,7 +70,9 @@ readLaTeX opts ltx = undefined
6870
6971testParser :: LP PandocIO a -> Text -> IO a
7072testParser p t = do
71− res <- runIOorExplode (runParserT p defaultParserState "name" (tokenize t))
73+ res <- runIOorExplode (runParserT p defaultParserState{
74+ stateOptions = def{ readerExtensions =
75+ getDefaultExtensions "latex" }} "name" (tokenize t))
7276 case res of
7377 Left e -> error (show e)
7478 Right r -> return r
@@ -182,6 +186,9 @@ anyControlSeq = satisfyTok isCtrlSeq <* spaces
182186spaces :: PandocMonad m => LP m ()
183187spaces = skipMany (satisfyTok (tokTypeIn [Comment, Spaces, Newline]))
184188
189+spaces1 :: PandocMonad m => LP m ()
190+spaces1 = skipMany1 (satisfyTok (tokTypeIn [Comment, Spaces, Newline]))
191+
185192tokTypeIn :: [TokType] -> Tok -> Bool
186193tokTypeIn toktypes (Tok _ tt _) = tt `elem` toktypes
187194tokTypeIn _ _ = False
@@ -198,6 +205,13 @@ symbol c = satisfyTok isc
198205 _ -> False
199206 isc _ = False
200207
208+symbolIn :: PandocMonad m => [Char] -> LP m Tok
209+symbolIn cs = satisfyTok isInCs
210+ where isInCs (Tok _ Symbol d) = case T.uncons d of
211+ Just (c,_) -> c `elem` cs
212+ _ -> False
213+ isInCs _ = False
214+
201215sp :: PandocMonad m => LP m ()
202216sp = whitespace <|> endline
203217
@@ -208,8 +222,10 @@ whitespace = () <$ satisfyTok isSpaceTok
208222
209223newlineTok :: PandocMonad m => LP m ()
210224newlineTok = () <$ satisfyTok isNewlineTok
211− where isNewlineTok (Tok _ Newline _) = True
212− isNewlineTok _ = False
225+
226+isNewlineTok :: Tok -> Bool
227+isNewlineTok (Tok _ Newline _) = True
228+isNewlineTok _ = False
213229
214230comment :: PandocMonad m => LP m ()
215231comment = () <$ satisfyTok isCommentTok
@@ -271,44 +287,131 @@ bracketed parser = try $ do
271287 symbol '['
272288 mconcat <$> manyTill parser (symbol ']')
273289
290+-- inline elements:
291+
292+inlineText :: PandocMonad m => LP m Inlines
293+inlineText = (str . T.unpack . untoken) <$> satisfyTok isWordTok
294+ where isWordTok (Tok _ Word _) = True
295+ isWordTok _ = False
296+
297+inlineGroup :: PandocMonad m => LP m Inlines
298+inlineGroup = do
299+ ils <- grouped inline
300+ if isNull ils
301+ then return mempty
302+ else return $ spanWith nullAttr ils
303+ -- we need the span so we can detitlecase bibtex entries;
304+ -- we need to know when something is {C}apitalized
305+
306+doLHSverb :: PandocMonad m => LP m Inlines
307+doLHSverb =
308+ (codeWith ("",["haskell"],[]) . T.unpack . untokenize)
309+ <$> manyTill (satisfyTok (not . isNewlineTok)) (symbol '|')
310+
311+lit :: String -> LP m Inlines
312+lit = pure . str
313+
314+doubleQuote :: PandocMonad m => LP m Inlines
315+doubleQuote = do
316+ quoted' doubleQuoted (try $ count 2 $ symbol '`')
317+ (void $ try $ count 2 $ symbol '\'')
318+ <|> quoted' doubleQuoted ((:[]) <$> symbol '“') (void $ symbol '”')
319+ -- the following is used by babel for localized quotes:
320+ <|> quoted' doubleQuoted (try $ sequence [symbol '"', symbol '`'])
321+ (void $ try $ sequence [symbol '"', symbol '\''])
322+ <|> quoted' doubleQuoted ((:[]) <$> symbol '"')
323+ (void $ symbol '"')
324+
325+singleQuote :: PandocMonad m => LP m Inlines
326+singleQuote = do
327+ quoted' singleQuoted ((:[]) <$> symbol '`')
328+ (try $ symbol '\'' >>
329+ notFollowedBy (satisfyTok startsWithLetter))
330+ <|> quoted' singleQuoted ((:[]) <$> symbol '‘')
331+ (try $ symbol '’' >>
332+ notFollowedBy (satisfyTok startsWithLetter))
333+ where startsWithLetter (Tok _ Word t) =
334+ case T.uncons t of
335+ Just (c, _) | isLetter c -> True
336+ _ -> False
337+ startsWithLetter _ = False
338+
339+quoted' :: PandocMonad m
340+ => (Inlines -> Inlines)
341+ -> LP m [Tok]
342+ -> LP m ()
343+ -> LP m Inlines
344+quoted' f starter ender = do
345+ startchs <- (T.unpack . untokenize) <$> starter
346+ smart <- extensionEnabled Ext_smart <$> getOption readerExtensions
347+ if smart
348+ then do
349+ ils <- many (notFollowedBy ender >> inline)
350+ (ender >> return (f (mconcat ils))) <|>
351+ (<> mconcat ils) <$>
352+ lit (case startchs of
353+ "``" -> "“"
354+ "`" -> "‘"
355+ cs -> cs)
356+ else lit startchs
357+
274358inline :: PandocMonad m => LP m Inlines
275359inline = (mempty <$ comment)
276360 <|> (space <$ whitespace)
277361 <|> (softbreak <$ endline)
278− -- <|> inlineText
362+ <|> inlineText
279363 -- <|> inlineCommand
280364 -- <|> inlineEnvironment
281− -- <|> inlineGroup
282− -- <|> (char '-' *> option (str "-")
283− -- (char '-' *> option (str "–") (str "—" <$ char '-')))
284− -- <|> doubleQuote
285− -- <|> singleQuote
286− -- <|> (str "”" <$ try (string "''"))
287− -- <|> (str "”" <$ char '”')
288− -- <|> (str "’" <$ char '\'')
289− -- <|> (str "’" <$ char '’')
290− -- <|> (str "\160" <$ char '~')
365+ <|> inlineGroup
366+ <|> (symbol '-' *>
367+ option (str "-") (symbol '-' *>
368+ option (str "–") (str "—" <$ symbol '-')))
369+ <|> doubleQuote
370+ <|> singleQuote
371+ <|> (str "”" <$ try (symbol '\'' >> symbol '\''))
372+ <|> (str "”" <$ symbol '”')
373+ <|> (str "’" <$ symbol '\'')
374+ <|> (str "’" <$ symbol '’')
375+ <|> (str "\160" <$ symbol '~')
291376 -- <|> mathDisplay (string "$$" *> mathChars <* string "$$")
292377 -- <|> mathInline (char '$' *> mathChars <* char '$')
293− -- <|> (guardEnabled Ext_literate_haskell *> char '|' *> doLHSverb)
294− -- <|> (str . (:[]) <$> primEscape)
295− -- <|> (do res <- oneOf "#&~^'`\"[]"
296− -- pos <- getPosition
297− -- report $ ParsingUnescaped [res] pos
298− -- return $ str [res])
378+ <|> (guardEnabled Ext_literate_haskell *> symbol '|' *> doLHSverb)
379+ <|> (str . (:[]) <$> primEscape)
380+ <|> (do res <- symbolIn "#&~^'`\"[]"
381+ pos <- getPosition
382+ let s = T.unpack (untoken res)
383+ report $ ParsingUnescaped s pos
384+ return $ str s)
299385
300−{-
301386inlines :: PandocMonad m => LP m Inlines
302−inlines = mconcat <$> many (notFollowedBy (char '}') *> inline)
387+inlines = mconcat <$> many inline
303388
304−inlineGroup :: PandocMonad m => LP m Inlines
305−inlineGroup = do
306− ils <- grouped inline
307− if isNull ils
389+-- PREVIOUSLY HAD (notFollowedBy (char '}') *> inline)
390+
391+-- block elements:
392+
393+paragraph :: PandocMonad m => LP m Blocks
394+paragraph = do
395+ x <- trimInlines . mconcat <$> many1 inline
396+ if x == mempty
308397 then return mempty
309− else return $ spanWith nullAttr ils
310− -- we need the span so we can detitlecase bibtex entries;
311− -- we need to know when something is {C}apitalized
398+ else return $ para x
399+
400+block :: PandocMonad m => LP m Blocks
401+block = (mempty <$ spaces1)
402+ -- <|> environment
403+ -- <|> include
404+ -- <|> macro
405+ -- <|> blockCommand
406+ <|> paragraph
407+ <|> grouped block
408+
409+blocks :: PandocMonad m => LP m Blocks
410+blocks = mconcat <$> many block
411+
412+
413+{-
414+
312415-}
313416
314417{-
@@ -454,37 +557,6 @@ mathChars =
454557 isOrdChar '\\' = False
455558 isOrdChar _ = True
456559
457−quoted' :: PandocMonad m => (Inlines -> Inlines) -> LP m String -> LP m () -> LP m Inlines
458−quoted' f starter ender = do
459− startchs <- starter
460− smart <- extensionEnabled Ext_smart <$> getOption readerExtensions
461− if smart
462− then do
463− ils <- many (notFollowedBy ender >> inline)
464− (ender >> return (f (mconcat ils))) <|>
465− (<> mconcat ils) <$>
466− lit (case startchs of
467− "``" -> "“"
468− "`" -> "‘"
469− _ -> startchs)
470− else lit startchs
471−
472−doubleQuote :: PandocMonad m => LP m Inlines
473−doubleQuote = do
474− quoted' doubleQuoted (try $ string "``") (void $ try $ string "''")
475− <|> quoted' doubleQuoted (string "“") (void $ char '”')
476− -- the following is used by babel for localized quotes:
477− <|> quoted' doubleQuoted (try $ string "\"`") (void $ try $ string "\"'")
478− <|> quoted' doubleQuoted (string "\"") (void $ char '"')
479−
480−singleQuote :: PandocMonad m => LP m Inlines
481−singleQuote = do
482− smart <- extensionEnabled Ext_smart <$> getOption readerExtensions
483− if smart
484− then quoted' singleQuoted (string "`") (try $ char '\'' >> notFollowedBy letter)
485− <|> quoted' singleQuoted (string "‘") (try $ char '’' >> notFollowedBy letter)
486− else str <$> many1 (oneOf "`\'‘’")
487−
488560inline :: PandocMonad m => LP m Inlines
489561inline = (mempty <$ comment)
490562 <|> (space <$ whitespace)
@@ -998,9 +1070,6 @@ dolstinline = do
9981070 marker <- char '{' <|> anyChar
9991071 codeWith ("",classes,[]) <$> manyTill (satisfy (/='\n')) (char '}' <|> char marker)
10001072
1001−doLHSverb :: PandocMonad m => LP m Inlines
1002−doLHSverb = codeWith ("",["haskell"],[]) <$> manyTill (satisfy (/='\n')) (char '|')
1003−
10041073-- converts e.g. \SI{1}[\$]{} to "$ 1" or \SI{1}{\euro} to "1 €"
10051074dosiunitx :: PandocMonad m => LP m Inlines
10061075dosiunitx = do
@@ -1016,9 +1085,6 @@ dosiunitx = do
10161085 emptyOr160 unit,
10171086 unit]
10181087
1019−lit :: String -> LP m Inlines
1020−lit = pure . str
1021−
10221088accent :: (Char -> String) -> Inlines -> LP m Inlines
10231089accent f ils =
10241090 case toList ils of