git.iko.soy/typeable/pandoc
on master help sign in
Added `--strip-comments` option, `readerStripComments` in `ReaderOptions`.

* Options: Added readerStripComments to ReaderOptions. * Added `--strip-comments` command-line option. * Made `htmlTag` from the HTML reader sensitive to this feature. This affects Markdown and Textile input. Closes #2552.

commitb1ee747a249e0c1b1840222ef77607218157f099 authorJohn MacFarlane <jgm@berkeley.edu> dateSun 17 Sep 2017, 12:49 (9 years ago) parentce05814
6 files changed+46−8
MANUAL.txt+8 src/Text/Pandoc/App.hs+8 src/Text/Pandoc/Options.hs+3−1 src/Text/Pandoc/Readers/HTML.hs+10−6 src/Text/Pandoc/Readers/Markdown.hs+3−1 test/command/2552.md+14
MANUAL.txt+8
@@ -655,6 +655,14 @@ General writer options
655655 of contents. The default is 3 (which means that level 1, 2, and 3
656656 headers will be listed in the contents).
657657
658+`--strip-comments`
659+
660+: Strip out HTML comments in the Markdown or Textile source,
661+ rather than passing them on to Markdown, Textile or HTML
662+ output as raw HTML. This does not apply to HTML comments
663+ inside raw HTML blocks when the `markdown_in_html_blocks`
664+ extension is not set.
665+
658666`--no-highlight`
659667
660668: Disables syntax highlighting for code blocks and inlines, even when
src/Text/Pandoc/App.hs+8
@@ -483,6 +483,7 @@ convertWithOpts opts = do
483483 , readerTrackChanges = optTrackChanges opts
484484 , readerAbbreviations = abbrevs
485485 , readerExtensions = readerExts
486+ , readerStripComments = optStripComments opts
486487 }
487488
488489 let transforms = (case optBaseHeaderLevel opts of
@@ -666,6 +667,7 @@ data Opt = Opt
666667 , optIncludeInHeader :: [FilePath] -- ^ Files to include in header
667668 , optResourcePath :: [FilePath] -- ^ Path to search for images etc
668669 , optEol :: LineEnding -- ^ Style of line-endings to use
670+ , optStripComments :: Bool -- ^ Skip HTML comments
669671 } deriving (Generic, Show)
670672
671673instance ToJSON Opt where
@@ -742,6 +744,7 @@ defaultOpts = Opt
742744 , optIncludeInHeader = []
743745 , optResourcePath = ["."]
744746 , optEol = Native
747+ , optStripComments = False
745748 }
746749
747750addMetadata :: (String, String) -> Pandoc -> Pandoc
@@ -1114,6 +1117,11 @@ options =
11141117 "NUMBER")
11151118 "" -- "Length of line in characters"
11161119
1120+ , Option "" ["strip-comments"]
1121+ (NoArg
1122+ (\opt -> return opt { optStripComments = True }))
1123+ "" -- "Strip HTML comments"
1124+
11171125 , Option "" ["toc", "table-of-contents"]
11181126 (NoArg
11191127 (\opt -> return opt { optTableOfContents = True }))
src/Text/Pandoc/Options.hs+3−1
@@ -65,7 +65,8 @@ data ReaderOptions = ReaderOptions{
6565 -- indented code blocks
6666 , readerAbbreviations :: Set.Set String -- ^ Strings to treat as abbreviations
6767 , readerDefaultImageExtension :: String -- ^ Default extension for images
68− , readerTrackChanges :: TrackChanges
68+ , readerTrackChanges :: TrackChanges -- ^ Track changes setting for docx
69+ , readerStripComments :: Bool -- ^ Strip HTML comments instead of parsing as raw HTML
6970} deriving (Show, Read, Data, Typeable, Generic)
7071
7172instance ToJSON ReaderOptions where
@@ -82,6 +83,7 @@ instance Default ReaderOptions
8283 , readerAbbreviations = defaultAbbrevs
8384 , readerDefaultImageExtension = ""
8485 , readerTrackChanges = AcceptChanges
86+ , readerStripComments = False
8587 }
8688
8789defaultAbbrevs :: Set.Set String
src/Text/Pandoc/Readers/HTML.hs+10−6
@@ -46,9 +46,10 @@ import qualified Text.Pandoc.Builder as B
4646import Text.Pandoc.Builder (Blocks, Inlines, trimInlines, HasMeta(..))
4747import Text.Pandoc.Shared ( extractSpaces, addMetaField
4848 , escapeURI, safeRead, crFilter )
49−import Text.Pandoc.Options (ReaderOptions(readerExtensions), extensionEnabled,
50− Extension (Ext_epub_html_exts,
51− Ext_raw_html, Ext_native_divs, Ext_native_spans))
49+import Text.Pandoc.Options (
50+ ReaderOptions(readerExtensions,readerStripComments), extensionEnabled,
51+ Extension (Ext_epub_html_exts,
52+ Ext_raw_html, Ext_native_divs, Ext_native_spans))
5253import Text.Pandoc.Logging
5354import Text.Pandoc.Parsing hiding ((<|>))
5455import Text.Pandoc.Walk
@@ -1070,7 +1071,7 @@ _ `closes` _ = False
10701071--- parsers for use in markdown, textile readers
10711072
10721073-- | Matches a stretch of HTML in balanced tags.
1073−htmlInBalanced :: (Monad m)
1074+htmlInBalanced :: (HasReaderOptions st, Monad m)
10741075 => (Tag String -> Bool)
10751076 -> ParserT String st m String
10761077htmlInBalanced f = try $ do
@@ -1118,7 +1119,7 @@ hasTagWarning (TagWarning _:_) = True
11181119hasTagWarning _ = False
11191120
11201121-- | Matches a tag meeting a certain condition.
1121−htmlTag :: Monad m
1122+htmlTag :: (HasReaderOptions st, Monad m)
11221123 => (Tag String -> Bool)
11231124 -> ParserT [Char] st m (Tag String, String)
11241125htmlTag f = try $ do
@@ -1153,7 +1154,10 @@ htmlTag f = try $ do
11531154 count (length s + 4) anyChar
11541155 skipMany (satisfy (/='>'))
11551156 char '>'
1156− return (next, "<!--" <> s <> "-->")
1157+ stripComments <- getOption readerStripComments
1158+ if stripComments
1159+ then return (next, "")
1160+ else return (next, "<!--" <> s <> "-->")
11571161 | otherwise -> fail "bogus comment mode, HTML5 parse error"
11581162 TagOpen tagname attr -> do
11591163 guard $ all (isName . fst) attr
src/Text/Pandoc/Readers/Markdown.hs+3−1
@@ -1079,7 +1079,9 @@ htmlBlock' = try $ do
10791079 first <- htmlElement
10801080 skipMany spaceChar
10811081 optional blanklines
1082− return $ return $ B.rawBlock "html" first
1082+ return $ if null first
1083+ then mempty
1084+ else return $ B.rawBlock "html" first
10831085
10841086strictHtmlBlock :: PandocMonad m => MarkdownParser m String
10851087strictHtmlBlock = htmlInBalanced (not . isInlineTag)
test/command/2552.mdnew file+14
@@ -0,0 +1,14 @@
1+```
2+% pandoc --strip-comments
3+Foo
4+
5+bar
6+
7+<!-- comment -->
8+
9+baz<!-- bim -->boop
10+^D
11+<p>Foo</p>
12+<p>bar</p>
13+<p>bazboop</p>
14+```