git.iko.soy/typeable/pandoc
on master help sign in
Markdown writer: escape paragraphs that begin with ordered list

markers, so they don't get interpreted as ordered lists. git-svn-id: https://pandoc.googlecode.com/svn/trunk@988 788f1e2b-df1e-0410-8736-df70ead52e1b

commita70c2b261e39efacd5ab2722ecac740f95789c45 authorfiddlosopher <fiddlosopher@788f1e2b-df1e-0410-8736-df70ead52e1b> dateSun 2 Sep 2007, 00:39 (19 years ago) parent0982a67
1 file changed+23−1
src/Text/Pandoc/Writers/Markdown.hs+23−1
src/Text/Pandoc/Writers/Markdown.hs+23−1
@@ -33,6 +33,7 @@ module Text.Pandoc.Writers.Markdown ( writeMarkdown) where
3333import Text.Pandoc.Definition
3434import Text.Pandoc.Shared
3535import Text.Pandoc.Blocks
36+import Text.ParserCombinators.Parsec ( parse, (<|>), GenParser )
3637import Data.List ( group, isPrefixOf, drop, find, intersperse )
3738import Text.PrettyPrint.HughesPJ hiding ( Str )
3839import Control.Monad.State
@@ -139,6 +140,22 @@ elementToListItem (Sec headerText subsecs) = [Plain headerText] ++
139140 then []
140141 else [BulletList $ map elementToListItem subsecs]
141142
143+-- | Ordered list start parser for use in Para below.
144+olMarker :: GenParser Char st Char
145+olMarker = do (start, style, delim) <- anyOrderedListMarker
146+ if delim == Period &&
147+ (style == UpperAlpha || (style == UpperRoman &&
148+ start `elem` [1, 5, 10, 50, 100, 500, 1000]))
149+ then spaceChar >> spaceChar
150+ else spaceChar
151+
152+-- | True if string begins with an ordered list marker
153+beginsWithOrderedListMarker :: String -> Bool
154+beginsWithOrderedListMarker str =
155+ case parse olMarker "para start" str of
156+ Left _ -> False
157+ Right _ -> True
158+
142159-- | Convert Pandoc block element to markdown.
143160blockToMarkdown :: WriterOptions -- ^ Options
144161 -> Block -- ^ Block element
@@ -148,7 +165,12 @@ blockToMarkdown opts (Plain inlines) =
148165 wrapped (inlineListToMarkdown opts) inlines
149166blockToMarkdown opts (Para inlines) = do
150167 contents <- wrapped (inlineListToMarkdown opts) inlines
151− return $ contents <> text "\n"
168+ -- escape if para starts with ordered list marker
169+ let esc = if (not (writerStrictMarkdown opts)) &&
170+ beginsWithOrderedListMarker (render contents)
171+ then char '\\'
172+ else empty
173+ return $ esc <> contents <> text "\n"
152174blockToMarkdown opts (RawHtml str) = return $ text str
153175blockToMarkdown opts HorizontalRule = return $ text "\n* * * * *\n"
154176blockToMarkdown opts (Header level inlines) = do