packages feed

pandoc-plot 1.4.0 → 1.4.1

raw patch · 4 files changed

+13/−33 lines, 4 filesPVP ok

version bump matches the API change (PVP)

API changes (from Hackage documentation)

Files

CHANGELOG.md view
@@ -2,6 +2,10 @@ 
 pandoc-plot uses [Semantic Versioning](http://semver.org/spec/v2.0.0.html)
 
+## Release 1.4.1
+
+* Don't automatically defer javascript scripts when creating interactive figures (#39).
+
 ## Release 1.4.0
 
 * The executable `pandoc-plot` now uses pandoc 2.17. Pandoc 2.11+ are still supported.
pandoc-plot.cabal view
@@ -1,6 +1,6 @@ cabal-version:  2.2
 name:           pandoc-plot
-version:        1.4.0
+version:        1.4.1
 synopsis:       A Pandoc filter to include figures generated from code blocks using your plotting toolkit of choice.
 description:    A Pandoc filter to include figures generated from code blocks. 
                 Keep the document and code in the same location. Output is 
src/Text/Pandoc/Filter/Plot/Embed.hs view
@@ -17,13 +17,11 @@ where
 
 import Data.Default (def)
-import Data.List (nub)
 import Data.Maybe (fromMaybe)
 import Data.Text (Text, pack)
 import qualified Data.Text.IO as T
 import Text.HTML.TagSoup
-  ( Attribute,
-    Tag (TagClose, TagOpen),
+  ( Tag (TagClose, TagOpen),
     canonicalizeTags,
     parseOptionsFast,
     parseTagsOptions,
@@ -174,7 +172,11 @@ extractPlot t =
   let tags = canonicalizeTags $ parseTagsOptions parseOptionsFast t
       extracted = headScripts tags <> [inside "body" tags]
-   in mconcat $ renderTags <$> (deferScripts <$> extracted)
+      -- In the past (e.g. commit 8417b011ccb20263427822c7447840ab4a30a41e), we used to
+      -- make all JS scripts 'deferred'. This turned out to be problematic for plotly 
+      -- specifically (see issue #39). In the future, we may want to defer scripts for
+      -- certain toolkits, but that's a testing nightmare...
+   in mconcat $ renderTags <$> extracted
   where
     headScripts = partitions (~== ("<script>" :: String)) . inside "head"
 
@@ -183,30 +185,4 @@ inside :: Text -> [Tag Text] -> [Tag Text]
 inside t = init . tail . tgs
   where
-    tgs = takeWhile (~/= TagClose t) . dropWhile (~/= TagOpen t [])
-
-data ScriptTag
-  = InlineScript [Attribute Text]
-  | ExternalScript [Attribute Text]
-
-fromTag :: Tag Text -> Maybe ScriptTag
-fromTag (TagOpen "script" attrs) =
-  Just $
-    if "src" `elem` map fst attrs
-      then ExternalScript attrs
-      else InlineScript attrs
-fromTag _ = Nothing
-
-toTag :: ScriptTag -> Tag Text
-toTag (InlineScript t) = TagOpen "script" t
-toTag (ExternalScript t) = TagOpen "script" t
-
-deferScript :: ScriptTag -> ScriptTag
-deferScript (InlineScript attrs) = InlineScript $ nub $ attrs <> [("type", "module")]
-deferScript (ExternalScript attrs) = ExternalScript $ nub $ attrs <> [("defer", mempty)]
-
--- | Replace /<script src=...>/ tags with /<script src=... defer>/,
--- and inline scripts as /<script type="module">/.
--- This makes scripts execute only after HTML parsing has finished.
-deferScripts :: [Tag Text] -> [Tag Text]
-deferScripts = fmap (\t -> maybe t (toTag . deferScript) (fromTag t))
+    tgs = takeWhile (~/= TagClose t) . dropWhile (~/= TagOpen t [])
tests/Main.hs view
@@ -155,6 +155,6 @@               "</html>"
             ]
         extracted = extractPlot html
-        expected = "<script src=\"script.js\" defer></script>\n    <p>Hello</p>"
+        expected = "<script src=\"script.js\"></script>\n    <p>Hello</p>"
 
     assertEqual "" expected extracted