Skip to content
This repository
Browse code

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
  • Loading branch information...
commit a70c2b261e39efacd5ab2722ecac740f95789c45 1 parent 0982a67
fiddlosopher authored

Showing 1 changed file with 23 additions and 1 deletion. Show diff stats Hide diff stats

  1. +23 1 src/Text/Pandoc/Writers/Markdown.hs
24 src/Text/Pandoc/Writers/Markdown.hs
@@ -33,6 +33,7 @@ module Text.Pandoc.Writers.Markdown ( writeMarkdown) where
33 33 import Text.Pandoc.Definition
34 34 import Text.Pandoc.Shared
35 35 import Text.Pandoc.Blocks
  36 +import Text.ParserCombinators.Parsec ( parse, (<|>), GenParser )
36 37 import Data.List ( group, isPrefixOf, drop, find, intersperse )
37 38 import Text.PrettyPrint.HughesPJ hiding ( Str )
38 39 import Control.Monad.State
@@ -139,6 +140,22 @@ elementToListItem (Sec headerText subsecs) = [Plain headerText] ++
139 140 then []
140 141 else [BulletList $ map elementToListItem subsecs]
141 142
  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 +
142 159 -- | Convert Pandoc block element to markdown.
143 160 blockToMarkdown :: WriterOptions -- ^ Options
144 161 -> Block -- ^ Block element
@@ -148,7 +165,12 @@ blockToMarkdown opts (Plain inlines) =
148 165 wrapped (inlineListToMarkdown opts) inlines
149 166 blockToMarkdown opts (Para inlines) = do
150 167 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"
152 174 blockToMarkdown opts (RawHtml str) = return $ text str
153 175 blockToMarkdown opts HorizontalRule = return $ text "\n* * * * *\n"
154 176 blockToMarkdown opts (Header level inlines) = do

0 comments on commit a70c2b2

Please sign in to comment.
Something went wrong with that request. Please try again.