AsciiDoc Writer: escape square brackets at start of line (#4708)

closes #4545
This commit is contained in:
Mauro Bieg 2018-07-12 19:37:37 +02:00 committed by John MacFarlane
parent e56a1a3088
commit 5809d5bef2
2 changed files with 33 additions and 8 deletions

View file

@ -44,7 +44,7 @@ import Data.Aeson (Result (..), Value (String), fromJSON, toJSON)
import Data.Char (isPunctuation, isSpace)
import Data.List (intercalate, intersperse, stripPrefix)
import qualified Data.Map as M
import Data.Maybe (fromMaybe, isJust)
import Data.Maybe (fromMaybe, isJust, listToMaybe)
import qualified Data.Set as Set
import Data.Text (Text)
import qualified Data.Text as T
@ -126,11 +126,16 @@ olMarker = do (start, style', delim) <- anyOrderedListMarker
else spaceChar
-- | True if string begins with an ordered list marker
beginsWithOrderedListMarker :: String -> Bool
beginsWithOrderedListMarker str =
case runParser olMarker defaultParserState "para start" (take 10 str) of
Left _ -> False
Right _ -> True
-- or would be interpreted as an AsciiDoc option command
needsEscaping :: String -> Bool
needsEscaping s = beginsWithOrderedListMarker s || isBracketed s
where
beginsWithOrderedListMarker str =
case runParser olMarker defaultParserState "para start" (take 10 str) of
Left _ -> False
Right _ -> True
isBracketed ('[':cs) = listToMaybe (reverse cs) == Just ']'
isBracketed _ = False
-- | Convert Pandoc block element to asciidoc.
blockToAsciiDoc :: PandocMonad m
@ -146,8 +151,8 @@ blockToAsciiDoc opts (Para [Image attr alt (src,'f':'i':'g':':':tit)]) =
blockToAsciiDoc opts (Para inlines) = do
contents <- inlineListToAsciiDoc opts inlines
-- escape if para starts with ordered list marker
let esc = if beginsWithOrderedListMarker (render Nothing contents)
then text "\\"
let esc = if needsEscaping (render Nothing contents)
then text "{empty}"
else empty
return $ esc <> contents <> blankline
blockToAsciiDoc opts (LineBlock lns) = do

20
test/command/4545.md Normal file
View file

@ -0,0 +1,20 @@
```
% pandoc -t asciidoc
Test 1
[my text]
Test 2
^D
Test 1
{empty}[my text]
Test 2
```
```
% pandoc -t asciidoc
4\. foo
^D
{empty}4. foo
```