git.iko.soy/typeable/pandoc
on master help sign in
Use walk, walkM in place of bottomUp, bottomUpM when possible.

They are significantly faster.

commit02a125d0aa8becd258c99b27c5e30116f0cbacb4 authorJohn MacFarlane <fiddlosopher@gmail.com> dateSat 10 Aug 2013, 18:45 (13 years ago) parent9152fa1
11 files changed+31−27
src/Text/Pandoc/PDF.hs+2−2 src/Text/Pandoc/Readers/LaTeX.hs+2−2 src/Text/Pandoc/Readers/MediaWiki.hs+2−2 src/Text/Pandoc/Shared.hs+1−1 src/Text/Pandoc/Writers/Docx.hs+6−5 src/Text/Pandoc/Writers/EPUB.hs+3−3 src/Text/Pandoc/Writers/FB2.hs+6−2 src/Text/Pandoc/Writers/LaTeX.hs+1−2 src/Text/Pandoc/Writers/Markdown.hs+4−4 src/Text/Pandoc/Writers/ODT.hs+2−2 src/Text/Pandoc/Writers/RTF.hs+2−2
src/Text/Pandoc/PDF.hs+2−2
@@ -44,7 +44,7 @@ import Data.List (isInfixOf)
4444import qualified Data.ByteString.Base64 as B64
4545import qualified Text.Pandoc.UTF8 as UTF8
4646import Text.Pandoc.Definition
47−import Text.Pandoc.Generic (bottomUpM)
47+import Text.Pandoc.Walk (walkM)
4848import Text.Pandoc.Shared (fetchItem, warn)
4949import Text.Pandoc.Options (WriterOptions(..))
5050import Text.Pandoc.MIME (extensionFromMimeType)
@@ -73,7 +73,7 @@ handleImages :: String -- ^ source directory/base URL
7373 -> FilePath -- ^ temp dir to store images
7474 -> Pandoc -- ^ document
7575 -> IO Pandoc
76−handleImages baseURL tmpdir = bottomUpM (handleImage' baseURL tmpdir)
76+handleImages baseURL tmpdir = walkM (handleImage' baseURL tmpdir)
7777
7878handleImage' :: String
7979 -> FilePath
src/Text/Pandoc/Readers/LaTeX.hs+2−2
@@ -35,7 +35,7 @@ module Text.Pandoc.Readers.LaTeX ( readLaTeX,
3535 ) where
3636
3737import Text.Pandoc.Definition
38−import Text.Pandoc.Generic
38+import Text.Pandoc.Walk
3939import Text.Pandoc.Shared
4040import Text.Pandoc.Options
4141import Text.Pandoc.Biblio (processBiblio)
@@ -815,7 +815,7 @@ keyvals :: LP [(String, String)]
815815keyvals = try $ char '[' *> manyTill keyval (char ']')
816816
817817alltt :: String -> LP Blocks
818−alltt t = bottomUp strToCode <$> parseFromString blocks
818+alltt t = walk strToCode <$> parseFromString blocks
819819 (substitute " " "\\ " $ substitute "%" "\\%" $
820820 concat $ intersperse "\\\\\n" $ lines t)
821821 where strToCode (Str s) = Code nullAttr s
src/Text/Pandoc/Readers/MediaWiki.hs+2−2
@@ -42,7 +42,7 @@ import Text.Pandoc.Options
4242import Text.Pandoc.Readers.HTML ( htmlTag, isBlockTag, isCommentTag )
4343import Text.Pandoc.XML ( fromEntities )
4444import Text.Pandoc.Parsing hiding ( nested )
45−import Text.Pandoc.Generic ( bottomUp )
45+import Text.Pandoc.Walk ( walk )
4646import Text.Pandoc.Shared ( stripTrailingNewlines, safeRead )
4747import Data.Monoid (mconcat, mempty)
4848import Control.Applicative ((<$>), (<*), (*>), (<$))
@@ -342,7 +342,7 @@ preformatted = try $ do
342342 spacesStr _ = False
343343 if F.all spacesStr contents
344344 then return mempty
345− else return $ B.para $ bottomUp strToCode contents
345+ else return $ B.para $ walk strToCode contents
346346
347347header :: MWParser Blocks
348348header = try $ do
src/Text/Pandoc/Shared.hs+1−1
@@ -518,7 +518,7 @@ isHeaderBlock _ = False
518518
519519-- | Shift header levels up or down.
520520headerShift :: Int -> Pandoc -> Pandoc
521−headerShift n = bottomUp shift
521+headerShift n = walk shift
522522 where shift :: Block -> Block
523523 shift (Header level attr inner) = Header (level + n) attr inner
524524 shift x = x
src/Text/Pandoc/Writers/Docx.hs+6−5
@@ -45,6 +45,7 @@ import Text.Pandoc.Shared hiding (Element)
4545import Text.Pandoc.Options
4646import Text.Pandoc.Readers.TeXMath
4747import Text.Pandoc.Highlighting ( highlight )
48+import Text.Pandoc.Walk
4849import Text.Highlighting.Kate.Types ()
4950import Text.XML.Light
5051import Text.TeXMath
@@ -108,7 +109,7 @@ writeDocx :: WriterOptions -- ^ Writer options
108109 -> IO BL.ByteString
109110writeDocx opts doc@(Pandoc meta _) = do
110111 let datadir = writerUserDataDir opts
111− let doc' = bottomUp (concatMap fixDisplayMath) doc
112+ let doc' = walk fixDisplayMath doc
112113 refArchive <- liftM (toArchive . toLazy) $
113114 case writerReferenceDocx opts of
114115 Just f -> B.readFile f
@@ -810,17 +811,17 @@ stripLeadingTrailingSpace = go . reverse . go . reverse
810811 where go (Space:xs) = xs
811812 go xs = xs
812813
813−fixDisplayMath :: Block -> [Block]
814+fixDisplayMath :: Block -> Block
814815fixDisplayMath (Plain lst)
815816 | any isDisplayMath lst && not (all isDisplayMath lst) =
816817 -- chop into several paragraphs so each displaymath is its own
817− map (Plain . stripLeadingTrailingSpace) $
818+ Div ("",["math"],[]) $ map (Plain . stripLeadingTrailingSpace) $
818819 groupBy (\x y -> (isDisplayMath x && isDisplayMath y) ||
819820 not (isDisplayMath x || isDisplayMath y)) lst
820821fixDisplayMath (Para lst)
821822 | any isDisplayMath lst && not (all isDisplayMath lst) =
822823 -- chop into several paragraphs so each displaymath is its own
823− map (Para . stripLeadingTrailingSpace) $
824+ Div ("",["math"],[]) $ map (Para . stripLeadingTrailingSpace) $
824825 groupBy (\x y -> (isDisplayMath x && isDisplayMath y) ||
825826 not (isDisplayMath x || isDisplayMath y)) lst
826−fixDisplayMath x = [x]
827+fixDisplayMath x = x
src/Text/Pandoc/Writers/EPUB.hs+3−3
@@ -48,7 +48,7 @@ import qualified Text.Pandoc.Shared as Shared
4848import Text.Pandoc.Builder (fromList, setMeta)
4949import Text.Pandoc.Options
5050import Text.Pandoc.Definition
51−import Text.Pandoc.Generic
51+import Text.Pandoc.Walk
5252import Control.Monad.State
5353import Text.XML.Light hiding (ppTopElement)
5454import Text.Pandoc.UUID
@@ -116,7 +116,7 @@ writeEPUB opts doc@(Pandoc meta _) = do
116116
117117 -- handle pictures
118118 picsRef <- newIORef []
119− Pandoc _ blocks <- bottomUpM
119+ Pandoc _ blocks <- walkM
120120 (transformInline opts' sourceDir picsRef) doc
121121 pics <- readIORef picsRef
122122 let readPicEntry entries (oldsrc, newsrc) = do
@@ -520,7 +520,7 @@ correlateRefs chapterHeaderLevel bs =
520520-- Replace internal link references using the table produced
521521-- by correlateRefs.
522522replaceRefs :: [(String,String)] -> [Block] -> [Block]
523−replaceRefs refTable = bottomUp replaceOneRef
523+replaceRefs refTable = walk replaceOneRef
524524 where replaceOneRef x@(Link lab ('#':xs,tit)) =
525525 case lookup xs refTable of
526526 Just url -> Link lab (url,tit)
src/Text/Pandoc/Writers/FB2.hs+6−2
@@ -45,7 +45,7 @@ import qualified Text.XML.Light.Cursor as XC
4545import Text.Pandoc.Definition
4646import Text.Pandoc.Options (WriterOptions(..), HTMLMathMethod(..), def)
4747import Text.Pandoc.Shared (orderedListMarkers)
48−import Text.Pandoc.Generic (bottomUp)
48+import Text.Pandoc.Walk
4949
5050-- | Data to be written at the end of the document:
5151-- (foot)notes, URLs, references, images.
@@ -423,6 +423,10 @@ indent = indentBlock
423423 indentLines ins = let lns = split isLineBreak ins :: [[Inline]]
424424 in intercalate [LineBreak] $ map ((Str spacer):) lns
425425
426+capitalize :: Inline -> Inline
427+capitalize (Str xs) = Str $ map toUpper xs
428+capitalize x = x
429+
426430-- | Convert a Pandoc's Inline element to FictionBook XML representation.
427431toXml :: Inline -> FBM [Content]
428432toXml (Str s) = return [txt s]
@@ -432,7 +436,7 @@ toXml (Strong ss) = list `liftM` wrap "strong" ss
432436toXml (Strikeout ss) = list `liftM` wrap "strikethrough" ss
433437toXml (Superscript ss) = list `liftM` wrap "sup" ss
434438toXml (Subscript ss) = list `liftM` wrap "sub" ss
435−toXml (SmallCaps ss) = cMapM toXml $ bottomUp (map toUpper) ss
439+toXml (SmallCaps ss) = cMapM toXml $ walk capitalize ss
436440toXml (Quoted SingleQuote ss) = do -- FIXME: should be language-specific
437441 inner <- cMapM toXml ss
438442 return $ [txt "‘"] ++ inner ++ [txt "’"]
src/Text/Pandoc/Writers/LaTeX.hs+1−2
@@ -31,7 +31,6 @@ Conversion of 'Pandoc' format into LaTeX.
3131module Text.Pandoc.Writers.LaTeX ( writeLaTeX ) where
3232import Text.Pandoc.Definition
3333import Text.Pandoc.Walk
34−import Text.Pandoc.Generic
3534import Text.Pandoc.Shared
3635import Text.Pandoc.Writers.Shared
3736import Text.Pandoc.Options
@@ -498,7 +497,7 @@ sectionHeader unnumbered ref level lst = do
498497 txt <- inlineListToLaTeX lst
499498 let noNote (Note _) = Str ""
500499 noNote x = x
501− let lstNoNotes = bottomUp noNote lst
500+ let lstNoNotes = walk noNote lst
502501 let star = if unnumbered then text "*" else empty
503502 -- footnotes in sections don't work unless you specify an optional
504503 -- argument: \section[mysec]{mysec\footnote{blah}}
src/Text/Pandoc/Writers/Markdown.hs+4−4
@@ -32,7 +32,7 @@ Markdown: <http://daringfireball.net/projects/markdown/>
3232-}
3333module Text.Pandoc.Writers.Markdown (writeMarkdown, writePlain) where
3434import Text.Pandoc.Definition
35−import Text.Pandoc.Generic
35+import Text.Pandoc.Walk
3636import Text.Pandoc.Templates (renderTemplate')
3737import Text.Pandoc.Shared
3838import Text.Pandoc.Writers.Shared
@@ -82,7 +82,7 @@ writePlain opts document =
8282 where document' = plainify document
8383
8484plainify :: Pandoc -> Pandoc
85−plainify = bottomUp go
85+plainify = walk go
8686 where go :: Inline -> Inline
8787 go (Emph xs) = SmallCaps xs
8888 go (Strong xs) = SmallCaps xs
@@ -643,13 +643,13 @@ inlineToMarkdown opts (Strikeout lst) = do
643643 then "~~" <> contents <> "~~"
644644 else "<s>" <> contents <> "</s>"
645645inlineToMarkdown opts (Superscript lst) = do
646− let lst' = bottomUp escapeSpaces lst
646+ let lst' = walk escapeSpaces lst
647647 contents <- inlineListToMarkdown opts lst'
648648 return $ if isEnabled Ext_superscript opts
649649 then "^" <> contents <> "^"
650650 else "<sup>" <> contents <> "</sup>"
651651inlineToMarkdown opts (Subscript lst) = do
652− let lst' = bottomUp escapeSpaces lst
652+ let lst' = walk escapeSpaces lst
653653 contents <- inlineListToMarkdown opts lst'
654654 return $ if isEnabled Ext_subscript opts
655655 then "~" <> contents <> "~"
src/Text/Pandoc/Writers/ODT.hs+2−2
@@ -39,7 +39,7 @@ import Text.Pandoc.Shared ( stringify, readDataFile, fetchItem, warn )
3939import Text.Pandoc.ImageSize ( imageSize, sizeInPoints )
4040import Text.Pandoc.MIME ( getMimeType )
4141import Text.Pandoc.Definition
42−import Text.Pandoc.Generic
42+import Text.Pandoc.Walk
4343import Text.Pandoc.Writers.OpenDocument ( writeOpenDocument )
4444import Control.Monad (liftM)
4545import Text.Pandoc.XML
@@ -63,7 +63,7 @@ writeODT opts doc@(Pandoc meta _) = do
6363 -- handle pictures
6464 picEntriesRef <- newIORef ([] :: [Entry])
6565 let sourceDir = writerSourceDirectory opts
66− doc' <- bottomUpM (transformPic sourceDir picEntriesRef) doc
66+ doc' <- walkM (transformPic sourceDir picEntriesRef) doc
6767 let newContents = writeOpenDocument opts{writerWrapText = False} doc'
6868 epochtime <- floor `fmap` getPOSIXTime
6969 let contentEntry = toEntry "content.xml" epochtime $ fromStringLazy newContents
src/Text/Pandoc/Writers/RTF.hs+2−2
@@ -34,7 +34,7 @@ import Text.Pandoc.Shared
3434import Text.Pandoc.Writers.Shared
3535import Text.Pandoc.Readers.TeXMath
3636import Text.Pandoc.Templates (renderTemplate')
37−import Text.Pandoc.Generic (bottomUpM)
37+import Text.Pandoc.Walk
3838import Data.List ( isSuffixOf, intercalate )
3939import Data.Char ( ord, chr, isDigit, toLower )
4040import System.FilePath ( takeExtension )
@@ -70,7 +70,7 @@ rtfEmbedImage x = return x
7070-- images embedded as encoded binary data.
7171writeRTFWithEmbeddedImages :: WriterOptions -> Pandoc -> IO String
7272writeRTFWithEmbeddedImages options doc =
73− writeRTF options `fmap` bottomUpM rtfEmbedImage doc
73+ writeRTF options `fmap` walkM rtfEmbedImage doc
7474
7575-- | Convert Pandoc to a string in rich text format.
7676writeRTF :: WriterOptions -> Pandoc -> String