git.iko.soy/typeable/pandoc
on master help sign in
Moved renderTags' from HTML reader & SelfContained to Shared.

Improved removal of markdown="1" attribute in Markdow reader.

commita6f2b960844caa81d9c8dd4d18f94c3de50bdb49 authorJohn MacFarlane <jgm@berkeley.edu> dateWed 15 Aug 2012, 09:42 (14 years ago) parent3745706
4 files changed+33−28
src/Text/Pandoc/Readers/HTML.hs+1−13 src/Text/Pandoc/Readers/Markdown.hs+9−3 src/Text/Pandoc/SelfContained.hs+1−12 src/Text/Pandoc/Shared.hs+22
src/Text/Pandoc/Readers/HTML.hs+1−13
@@ -45,7 +45,7 @@ import Text.Pandoc.Options
4545import Text.Pandoc.Parsing
4646import Data.Maybe ( fromMaybe, isJust )
4747import Data.List ( intercalate )
48−import Data.Char ( isDigit, toLower )
48+import Data.Char ( isDigit )
4949import Control.Monad ( liftM, guard, when, mzero )
5050
5151isSpace :: Char -> Bool
@@ -95,18 +95,6 @@ block = choice
9595 , pRawHtmlBlock
9696 ]
9797
98−-- repeated in SelfContained -- consolidate eventually
99−renderTags' :: [Tag String] -> String
100−renderTags' = renderTagsOptions
101− renderOptions{ optMinimize = \x ->
102− let y = map toLower x
103− in y == "hr" || y == "br" ||
104− y == "img" || y == "meta" ||
105− y == "link"
106− , optRawTag = \x ->
107− let y = map toLower x
108− in y == "script" || y == "style" }
109−
11098pList :: TagParser [Block]
11199pList = pBulletList <|> pOrderedList <|> pDefinitionList
112100
src/Text/Pandoc/Readers/Markdown.hs+9−3
@@ -776,11 +776,10 @@ rawHtmlBlocks = do
776776 if "markdown" `notElem`
777777 map fst as
778778 then mzero
779− else return $ substitute
780− " markdown=\"1\"" "" raw
779+ else return $
780+ stripMarkdownAttribute raw
781781 | otherwise -> return raw
782782 _ -> return raw )
783− -- TODO remove markdown="1" attribute from raw tags
784783 sps <- do sp1 <- many spaceChar
785784 sp2 <- option "" (blankline >> return "\n")
786785 sp3 <- many spaceChar
@@ -793,6 +792,13 @@ rawHtmlBlocks = do
793792 let combined = concat htmlBlocks
794793 return $ if last combined == '\n' then init combined else combined
795794
795+-- remove markdown="1" attribute
796+stripMarkdownAttribute :: String -> String
797+stripMarkdownAttribute s = renderTags' $ map filterAttrib $ parseTags s
798+ where filterAttrib (TagOpen t as) = TagOpen t
799+ [(k,v) | (k,v) <- as, k /= "markdown"]
800+ filterAttrib x = x
801+
796802--
797803-- Tables
798804--
src/Text/Pandoc/SelfContained.hs+1−12
@@ -42,7 +42,7 @@ import System.FilePath (takeExtension, dropExtension, takeDirectory, (</>))
4242import Data.Char (toLower, isAscii, isAlphaNum)
4343import Codec.Compression.GZip as Gzip
4444import qualified Data.ByteString.Lazy as L
45−import Text.Pandoc.Shared (findDataFile)
45+import Text.Pandoc.Shared (findDataFile, renderTags')
4646import Text.Pandoc.MIME (getMimeType)
4747import System.Directory (doesFileExist)
4848
@@ -163,14 +163,3 @@ makeSelfContained userdata inp = do
163163 out' <- mapM (convertTag userdata) tags
164164 return $ renderTags' out'
165165
166−-- repeated from HTML reader:
167−renderTags' :: [Tag String] -> String
168−renderTags' = renderTagsOptions
169− renderOptions{ optMinimize = \x ->
170− let y = map toLower x
171− in y == "hr" || y == "br" ||
172− y == "img" || y == "meta" ||
173− y == "link"
174− , optRawTag = \x ->
175− let y = map toLower x
176− in y == "script" || y == "style" }
src/Text/Pandoc/Shared.hs+22
@@ -59,6 +59,8 @@ module Text.Pandoc.Shared (
5959 uniqueIdent,
6060 isHeaderBlock,
6161 headerShift,
62+ -- * TagSoup HTML handling
63+ renderTags',
6264 -- * File handling
6365 inDirectory,
6466 findDataFile,
@@ -89,6 +91,8 @@ import Text.Pandoc.Pretty (charWidth)
8991import System.Locale (defaultTimeLocale)
9092import Data.Time
9193import System.IO (stderr)
94+import Text.HTML.TagSoup (renderTagsOptions, RenderOptions(..), Tag(..),
95+ renderOptions)
9296
9397--
9498-- List processing
@@ -448,6 +452,22 @@ headerShift n = bottomUp shift
448452 shift (Header level inner) = Header (level + n) inner
449453 shift x = x
450454
455+--
456+-- TagSoup HTML handling
457+--
458+
459+-- | Render HTML tags.
460+renderTags' :: [Tag String] -> String
461+renderTags' = renderTagsOptions
462+ renderOptions{ optMinimize = \x ->
463+ let y = map toLower x
464+ in y == "hr" || y == "br" ||
465+ y == "img" || y == "meta" ||
466+ y == "link"
467+ , optRawTag = \x ->
468+ let y = map toLower x
469+ in y == "script" || y == "style" }
470+
451471--
452472-- File handling
453473--
@@ -501,3 +521,5 @@ safeRead s = case reads s of
501521 (d,x):_
502522 | all isSpace x -> return d
503523 _ -> fail $ "Could not read `" ++ s ++ "'"
524+
525+