Skip to content

Commit

Permalink
T.P.Shared: add addPandocAttributes function.
Browse files Browse the repository at this point in the history
[API change]

This is meant to simplify addition of attributes to Pandoc
elements; for elements that don't have a slot for attributes,
an enclosing Div or Span is added to hold the attributes.

Ideally this would live in pandoc-types, but for now we
reuse some code from commonmark-pandoc.
  • Loading branch information
jgm committed Sep 20, 2023
1 parent e57953b commit 26d077f
Showing 1 changed file with 12 additions and 0 deletions.
12 changes: 12 additions & 0 deletions src/Text/Pandoc/Shared.hs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ module Text.Pandoc.Shared (
-- * Date/time
normalizeDate,
-- * Pandoc block and inline list processing
addPandocAttributes,
orderedListMarkers,
extractSpaces,
removeFormatting,
Expand Down Expand Up @@ -113,6 +114,9 @@ import Text.Pandoc.Extensions (Extensions, Extension(..), extensionEnabled)
import Text.Pandoc.Generic (bottomUp)
import Text.DocLayout (charWidth)
import Text.Pandoc.Walk
-- for addPandocAttributes:
import Commonmark.Pandoc (Cm(..))
import Commonmark (HasAttributes(..))

--
-- List processing
Expand Down Expand Up @@ -288,6 +292,14 @@ normalizeDate' s = fmap (formatTime defaultTimeLocale "%F")
-- Pandoc block and inline list processing
--

-- | Add key-value attributes to a pandoc element. If the element
-- does not have a slot for attributes, create an enclosing Span
-- (for Inlines) or Div (for Blocks). Note that both 'Cm () Inlines'
-- and 'Cm () Blocks' are instances of 'HasAttributes'.
addPandocAttributes
:: forall b . HasAttributes (Cm () b) => [(T.Text, T.Text)] -> b -> b
addPandocAttributes kvs bs = unCm . addAttributes kvs $ (Cm bs :: Cm () b)

-- | Generate infinite lazy list of markers for an ordered list,
-- depending on list attributes.
orderedListMarkers :: (Int, ListNumberStyle, ListNumberDelim) -> [T.Text]
Expand Down

0 comments on commit 26d077f

Please sign in to comment.