git.iko.soy/typeable/pandoc
on master help sign in
HTML writer: export tagWithAttributes.

This is a helper allowing other writers to create single HTML tags.

commit5d0863d19838cc5fab15664bceec103d7b563d35 authorJohn MacFarlane <jgm@berkeley.edu> dateSun 3 Dec 2017, 12:09 (9 years ago) parent0a091f1
1 file changed+19−2
src/Text/Pandoc/Writers/HTML.hs+19−2
src/Text/Pandoc/Writers/HTML.hs+19−2
@@ -41,7 +41,8 @@ module Text.Pandoc.Writers.HTML (
4141 writeSlidy,
4242 writeSlideous,
4343 writeDZSlides,
44− writeRevealJs
44+ writeRevealJs,
45+ tagWithAttributes
4546 ) where
4647import Control.Monad.State.Strict
4748import Data.Char (ord, toLower)
@@ -55,6 +56,7 @@ import qualified Data.Text.Lazy as TL
5556import Network.HTTP (urlEncode)
5657import Network.URI (URI (..), parseURIReference, unEscapeString)
5758import Numeric (showHex)
59+import Text.Blaze.Internal (customLeaf, textTag)
5860import Text.Blaze.Html hiding (contents)
5961import Text.Pandoc.Definition
6062import Text.Pandoc.Highlighting (formatHtmlBlock, formatHtmlInline, highlight,
@@ -83,7 +85,7 @@ import System.FilePath (takeBaseName, takeExtension)
8385import Text.Blaze.Html.Renderer.Text (renderHtml)
8486import qualified Text.Blaze.XHtml1.Transitional as H
8587import qualified Text.Blaze.XHtml1.Transitional.Attributes as A
86−import Text.Pandoc.Class (PandocMonad, report)
88+import Text.Pandoc.Class (PandocMonad, report, runPure)
8789import Text.Pandoc.Error
8890import Text.Pandoc.Logging
8991import Text.TeXMath
@@ -542,6 +544,21 @@ obfuscateChar char =
542544obfuscateString :: String -> String
543545obfuscateString = concatMap obfuscateChar . fromEntities
544546
547+-- | Create HTML tag with attributes.
548+tagWithAttributes :: WriterOptions
549+ -> Bool -- ^ True for HTML5
550+ -> Bool -- ^ True if self-closing tag
551+ -> Text -- ^ Tag text
552+ -> Attr -- ^ Pandoc style tag attributes
553+ -> Text
554+tagWithAttributes opts html5 selfClosing tagname attr =
555+ let mktag = (TL.toStrict . renderHtml <$> evalStateT
556+ (addAttrs opts attr (customLeaf (textTag tagname) selfClosing))
557+ defaultWriterState{ stHtml5 = html5 })
558+ in case runPure mktag of
559+ Left _ -> mempty
560+ Right t -> t
561+
545562addAttrs :: PandocMonad m
546563 => WriterOptions -> Attr -> Html -> StateT WriterState m Html
547564addAttrs opts attr h = foldl (!) h <$> attrsToHtml opts attr