@@ -69,14 +69,17 @@ figure :: Attr
6969-- must be "fig:"
7070-- Janky? yes
7171figure as fp caption' = return . head . toList . para $
72- imageWith as (pack fp) " fig:" caption'
72+ imageWith as (pack fp) " fig:" caption'
7373
7474
7575interactiveBlock :: Attr
7676 -> FilePath
7777 -> Inlines
7878 -> PlotM Block
7979interactiveBlock _ fp caption' = do
80+ -- TODO: should we instead include the scripts in the "include-after"
81+ -- template variable?
82+ -- See https://github.com/jgm/pandoc/issues/6582
8083 htmlpage <- liftIO $ T. readFile fp
8184 renderedCaption <- writeHtml caption'
8285 return $ RawBlock " html5" [st |
@@ -101,47 +104,44 @@ writeHtml is = liftIO $ handleError $ runPure $ writeHtml5String def document
101104-- <body> tag.
102105extractPlot :: Text -> Text
103106extractPlot t = let tags = canonicalizeTags $ parseTagsOptions parseOptionsFast t
104- extracted = headScripts tags <> [htmlBody tags]
107+ extracted = headScripts tags <> [inside " body " $ tags]
105108 in mconcat $ renderTags <$> (deferScripts <$> extracted)
106109 where
107- headScripts = partitions (~== (" <script>" :: String )) . htmlHead
110+ headScripts = partitions (~== (" <script>" :: String )) . inside " head "
108111
112+
113+ -- | Get content inside a tag, e.g. /inside "body"/ returns all tags
114+ -- between /<body>/ and /</body>/
109115inside :: Text -> [Tag Text ] -> [Tag Text ]
110116inside t = init . tail . tgs
111117 where
112118 tgs = takeWhile (~/= TagClose t) . dropWhile (~/= TagOpen t [] )
113119
114120
115- htmlHead :: [Tag Text ] -> [Tag Text ]
116- htmlHead = inside " head"
121+ data ScriptTag = InlineScript [Attribute Text ]
122+ | ExternalScript [Attribute Text ]
123+
124+
125+ fromTag :: Tag Text -> Maybe ScriptTag
126+ fromTag (TagOpen " script" attrs) =
127+ Just $ if " src" `elem` (fst . unzip $ attrs)
128+ then ExternalScript attrs
129+ else InlineScript attrs
130+ fromTag _ = Nothing
131+
132+
133+ toTag :: ScriptTag -> Tag Text
134+ toTag (InlineScript t) = TagOpen " script" t
135+ toTag (ExternalScript t) = TagOpen " script" t
117136
118137
119- htmlBody :: [Tag Text ] -> [Tag Text ]
120- htmlBody = inside " body"
138+ deferScript :: ScriptTag -> ScriptTag
139+ deferScript (InlineScript attrs) = InlineScript $ nub $ attrs <> [(" type" , " module" )]
140+ deferScript (ExternalScript attrs) = ExternalScript $ nub $ attrs <> [(" defer" , mempty )]
121141
122142
123143-- | Replace /<script src=...>/ tags with /<script src=... defer>/,
124144-- and inline scripts as /<script type="module">/.
125145-- This makes scripts execute only after HTML parsing has finished.
126146deferScripts :: [Tag Text ] -> [Tag Text ]
127- deferScripts = fmap (\ tag -> if isExternalScript tag
128- then defer tag
129- else if isInlineScript tag
130- then modularize tag
131- else tag)
132- where
133- isExternalScript :: Tag Text -> Bool
134- isExternalScript (TagOpen " script" attrs) = " src" `elem` (fst . unzip $ attrs)
135- isExternalScript _ = False
136-
137- isInlineScript :: Tag Text -> Bool
138- isInlineScript (TagOpen " script" attrs) = " src" `notElem` (fst . unzip $ attrs)
139- isInlineScript _ = False
140-
141- defer :: Tag Text -> Tag Text
142- defer (TagOpen " script" attrs) = TagOpen " script" . nub $ attrs <> [(" defer" , mempty )]
143- defer t = t
144-
145- modularize :: Tag Text -> Tag Text
146- modularize (TagOpen " script" attrs) = TagOpen " script" . nub $ attrs <> [(" type" , " module" )]
147- modularize t = t
147+ deferScripts = fmap (\ t -> maybe t (toTag . deferScript) (fromTag t))
0 commit comments