aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorJohn MacFarlane <[email protected]>2025-03-17 09:56:15 -0700
committerJohn MacFarlane <[email protected]>2025-03-17 09:56:15 -0700
commit6af1459a2bd9956000341d629672911f23c7b309 (patch)
treebffe0f6a33be9fb5bb6c35ae47f386614357baa4 /src
parent04476c550ffec4665a17e6abf0040117a7d496e2 (diff)
T.P.Writers.Shared: export `surroundInlines`.
[API change]
Diffstat (limited to 'src')
-rw-r--r--src/Text/Pandoc/Writers/Shared.hs23
1 files changed, 23 insertions, 0 deletions
diff --git a/src/Text/Pandoc/Writers/Shared.hs b/src/Text/Pandoc/Writers/Shared.hs
index d31d0fb4c..83e566db5 100644
--- a/src/Text/Pandoc/Writers/Shared.hs
+++ b/src/Text/Pandoc/Writers/Shared.hs
@@ -48,6 +48,7 @@ module Text.Pandoc.Writers.Shared (
, setupTranslations
, isOrderedListMarker
, toTaskListItem
+ , surroundInlines
)
where
import Safe (lastMay)
@@ -662,3 +663,25 @@ toTaskListItem (Plain (Str "☒":Space:ils):xs) = pure (True, Plain ils:xs)
toTaskListItem (Para (Str "☐":Space:ils):xs) = pure (False, Para ils:xs)
toTaskListItem (Para (Str "☒":Space:ils):xs) = pure (True, Para ils:xs)
toTaskListItem _ = mzero
+
+-- | Add an opener and closer to a Doc. If the Doc begins or ends
+-- with whitespace, export this outside the opener or closer.
+-- This is used for formats, like Markdown, which don't allow spaces
+-- after opening or before closing delimiters.
+surroundInlines :: Doc Text -> Doc Text -> Doc Text -> Doc Text
+surroundInlines opener closer content =
+ mconcat initialWS <> opener <> mconcat middle <> closer <> mconcat finalWS
+ where
+ contents = toList content
+ (initialWS, rest) = span isWS contents
+ (reverseFinalWS, reverseMiddle) = span isWS (reverse rest)
+ finalWS = reverse reverseFinalWS
+ middle = reverse reverseMiddle
+ isWS NewLine = True
+ isWS CarriageReturn = True
+ isWS BreakingSpace = True
+ isWS BlankLines{} = True
+ isWS _ = False
+ toList (Concat (Concat a b) c) = toList (Concat a (Concat b c))
+ toList (Concat a b) = a : toList b
+ toList x = [x]