git.iko.soy/typeable/pandoc
on master help sign in
Add --strip-empty-paragraphs option.

This works for any input format.

commit7b8c2b6691e3816ba52ee07ee7f63573d4ae7253 authorJohn MacFarlane <jgm@berkeley.edu> dateSat 2 Dec 2017, 15:21 (9 years ago) parente09e6a6
3 files changed+30−5
MANUAL.txt+6 src/Text/Pandoc/App.hs+15−5 src/Text/Pandoc/Shared.hs+9
MANUAL.txt+6
@@ -427,6 +427,12 @@ Reader options
427427
428428: Specify the base level for headers (defaults to 1).
429429
430+`--strip-empty-paragraphs`
431+
432+: Ignore paragraphs with non content. This option is useful
433+ for converting word processing documents where users have
434+ used empty paragraphs to create inter-paragraph space.
435+
430436`--indented-code-classes=`*CLASSES*
431437
432438: Specify classes to use for indented code blocks--for example,
src/Text/Pandoc/App.hs+15−5
@@ -86,8 +86,8 @@ import Text.Pandoc.Lua (LuaException (..), runLuaFilter)
8686import Text.Pandoc.PDF (makePDF)
8787import Text.Pandoc.Process (pipeProcess)
8888import Text.Pandoc.SelfContained (makeDataURI, makeSelfContained)
89−import Text.Pandoc.Shared (eastAsianLineBreakFilter, headerShift, isURI, ordNub,
90− safeRead, tabFilter)
89+import Text.Pandoc.Shared (eastAsianLineBreakFilter, stripEmptyParagraphs,
90+ headerShift, isURI, ordNub, safeRead, tabFilter)
9191import qualified Text.Pandoc.UTF8 as UTF8
9292import Text.Pandoc.Writers.Math (defaultKaTeXURL, defaultMathJaxURL)
9393import Text.Pandoc.XML (toEntities)
@@ -461,14 +461,17 @@ convertWithOpts opts = do
461461
462462 let transforms = (case optBaseHeaderLevel opts of
463463 x | x > 1 -> (headerShift (x - 1) :)
464− | otherwise -> id) $
464+ | otherwise -> id) .
465+ (if optStripEmptyParagraphs opts
466+ then (stripEmptyParagraphs :)
467+ else id) .
465468 (if extensionEnabled Ext_east_asian_line_breaks
466469 readerExts &&
467470 not (extensionEnabled Ext_east_asian_line_breaks
468471 writerExts &&
469472 writerWrapText writerOptions == WrapPreserve)
470473 then (eastAsianLineBreakFilter :)
471− else id)
474+ else id) $
472475 []
473476
474477 let sourceToDoc :: [FilePath] -> PandocIO Pandoc
@@ -622,6 +625,7 @@ data Opt = Opt
622625 , optLuaFilters :: [FilePath] -- ^ Lua filters to apply
623626 , optEmailObfuscation :: ObfuscationMethod
624627 , optIdentifierPrefix :: String
628+ , optStripEmptyParagraphs :: Bool -- ^ Strip empty paragraphs
625629 , optIndentedCodeClasses :: [String] -- ^ Default classes for indented code blocks
626630 , optDataDir :: Maybe FilePath
627631 , optCiteMethod :: CiteMethod -- ^ Method to output cites
@@ -694,6 +698,7 @@ defaultOpts = Opt
694698 , optLuaFilters = []
695699 , optEmailObfuscation = NoObfuscation
696700 , optIdentifierPrefix = ""
701+ , optStripEmptyParagraphs = False
697702 , optIndentedCodeClasses = []
698703 , optDataDir = Nothing
699704 , optCiteMethod = Citeproc
@@ -940,7 +945,12 @@ options =
940945 "NUMBER")
941946 "" -- "Headers base level"
942947
943− , Option "" ["indented-code-classes"]
948+ , Option "" ["strip-empty-paragraphs"]
949+ (NoArg
950+ (\opt -> return opt{ optStripEmptyParagraphs = True }))
951+ "" -- "Strip empty paragraphs"
952+
953+ , Option "" ["indented-code-classes"]
944954 (ReqArg
945955 (\arg opt -> return opt { optIndentedCodeClasses = words $
946956 map (\c -> if c == ',' then ' ' else c) arg })
src/Text/Pandoc/Shared.hs+9
@@ -72,6 +72,7 @@ module Text.Pandoc.Shared (
7272 inlineListToIdentifier,
7373 isHeaderBlock,
7474 headerShift,
75+ stripEmptyParagraphs,
7576 isTightList,
7677 addMetaField,
7778 makeMeta,
@@ -529,6 +530,14 @@ headerShift n = walk shift
529530 shift (Header level attr inner) = Header (level + n) attr inner
530531 shift x = x
531532
533+-- | Remove empty paragraphs.
534+stripEmptyParagraphs :: Pandoc -> Pandoc
535+stripEmptyParagraphs = walk go
536+ where go :: [Block] -> [Block]
537+ go = filter (not . isEmptyParagraph)
538+ isEmptyParagraph (Para []) = True
539+ isEmptyParagraph _ = False
540+
532541-- | Detect if a list is tight.
533542isTightList :: [[Block]] -> Bool
534543isTightList = all firstIsPlain