@@ -118,19 +118,46 @@ pandocToAsciiDoc opts (Pandoc meta blocks) = do
118
118
Nothing -> main
119
119
Just tpl -> renderTemplate tpl context
120
120
121
- -- | Escape special characters for AsciiDoc.
122
- escapeString :: PandocMonad m => Text -> ADW m (Doc Text )
123
- escapeString t = do
124
- parentTableLevel <- gets tableNestingLevel
125
- let needsEscape ' {' = True
126
- needsEscape ' |' = parentTableLevel > 0
127
- needsEscape _ = False
128
- let escChar c | needsEscape c = " \\ " <> T. singleton c
129
- | otherwise = T. singleton c
130
- if T. any needsEscape t
131
- then return $ literal $ T. concatMap escChar t
132
- else return $ literal t
121
+ data EscContext = Normal | InTable
122
+ deriving (Show , Eq )
133
123
124
+ -- | Escape special characters for AsciiDoc.
125
+ escapeString :: EscContext -> Text -> Doc Text
126
+ escapeString context t
127
+ | T. any needsEscape t
128
+ = literal $
129
+ case T. foldl' go (False , mempty ) t of
130
+ (True , x) -> x <> " ++" -- close passthrough context
131
+ (False , x) -> x
132
+ | otherwise = literal t
133
+ where
134
+ -- Bool is True when we are in a ++ passthrough context
135
+ go :: (Bool , Text ) -> Char -> (Bool , Text )
136
+ go (True , x) ' +' = (False , x <> " ++" <> " {plus}" ) -- close context
137
+ go (False , x) ' +' = (False , x <> " {plus}" )
138
+ go (True , x) ' |'
139
+ | context == InTable = (False , x <> " ++" <> " {vbar}" ) -- close context
140
+ go (False , x) ' |'
141
+ | context == InTable = (False , x <> " {vbar}" )
142
+ go (True , x) c
143
+ | needsEscape c = (True , T. snoc x c)
144
+ | otherwise = (False , T. snoc (x <> " ++" ) c)
145
+ go (False , x) c
146
+ | needsEscape c = (True , x <> " ++" <> T. singleton c)
147
+ | otherwise = (False , T. snoc x c)
148
+
149
+ needsEscape ' {' = True
150
+ needsEscape ' +' = True
151
+ needsEscape ' `' = True
152
+ needsEscape ' *' = True
153
+ needsEscape ' _' = True
154
+ needsEscape ' <' = True
155
+ needsEscape ' >' = True
156
+ needsEscape ' [' = True
157
+ needsEscape ' ]' = True
158
+ needsEscape ' \\ ' = True
159
+ needsEscape ' |' = True
160
+ needsEscape _ = False
134
161
135
162
-- | Ordered list start parser for use in Para below.
136
163
olMarker :: Parsec Text ParserState Char
@@ -393,11 +420,11 @@ bulletListItemToAsciiDoc opts blocks = do
393
420
-- | Convert a list item containing text starting with @U+2610 BALLOT BOX@
394
421
-- or @U+2612 BALLOT BOX WITH X@ to asciidoctor checkbox syntax (e.g. @[x]@).
395
422
taskListItemToAsciiDoc :: [Block ] -> [Block ]
396
- taskListItemToAsciiDoc = handleTaskListItem toOrg listExt
423
+ taskListItemToAsciiDoc = handleTaskListItem toAd listExt
397
424
where
398
- toOrg (Str " ☐" : Space : is) = Str " [ ]" : Space : is
399
- toOrg (Str " ☒" : Space : is) = Str " [x]" : Space : is
400
- toOrg is = is
425
+ toAd (Str " ☐" : Space : is) = RawInline ( Format " asciidoc " ) " [ ]" : Space : is
426
+ toAd (Str " ☒" : Space : is) = RawInline ( Format " asciidoc " ) " [x]" : Space : is
427
+ toAd is = is
401
428
listExt = extensionsFromList [Ext_task_lists ]
402
429
403
430
addBlock :: PandocMonad m
@@ -543,24 +570,27 @@ inlineToAsciiDoc opts (Subscript lst) = do
543
570
inlineToAsciiDoc opts (SmallCaps lst) = inlineListToAsciiDoc opts lst
544
571
inlineToAsciiDoc opts (Quoted qt lst) = do
545
572
isLegacy <- gets legacy
546
- inlineListToAsciiDoc opts $
547
- case qt of
548
- SingleQuote
549
- | isLegacy -> [ Str " `" ] ++ lst ++ [ Str " '" ]
550
- | otherwise -> [ Str " '`" ] ++ lst ++ [ Str " `'" ]
551
- DoubleQuote
552
- | isLegacy -> [ Str " ``" ] ++ lst ++ [ Str " ''" ]
553
- | otherwise -> [ Str " \" `" ] ++ lst ++ [ Str " `\" " ]
573
+ contents <- inlineListToAsciiDoc opts lst
574
+ pure $ case qt of
575
+ SingleQuote
576
+ | isLegacy -> " `" <> contents <> " '"
577
+ | otherwise -> " '`" <> contents <> " `'"
578
+ DoubleQuote
579
+ | isLegacy -> " ``" <> contents <> " ''"
580
+ | otherwise -> " \" `" <> contents <> " `\" "
554
581
inlineToAsciiDoc _ (Code _ str) = do
555
582
isLegacy <- gets legacy
556
583
let escChar ' `' = " \\ '"
557
584
escChar c = T. singleton c
558
- let contents = literal (T. concatMap escChar str)
559
- return $
560
- if isLegacy
561
- then text " `" <> contents <> " `"
562
- else text " `+" <> contents <> " +`"
563
- inlineToAsciiDoc _ (Str str) = escapeString str
585
+ parentTableLevel <- gets tableNestingLevel
586
+ let content
587
+ | isLegacy = literal (T. concatMap escChar str)
588
+ | otherwise = escapeString
589
+ (if parentTableLevel > 0 then InTable else Normal ) str
590
+ return $ text " `" <> content <> " `"
591
+ inlineToAsciiDoc _ (Str str) = do
592
+ parentTableLevel <- gets tableNestingLevel
593
+ pure $ escapeString (if parentTableLevel > 0 then InTable else Normal ) str
564
594
inlineToAsciiDoc _ (Math InlineMath str) = do
565
595
isLegacy <- gets legacy
566
596
modify $ \ st -> st{ hasMath = True }
0 commit comments