Skip to content

Commit 3a564b3

Browse files
committed
[skip ci] refactoring
1 parent 6c0eea9 commit 3a564b3

File tree

3 files changed

+32
-29
lines changed

3 files changed

+32
-29
lines changed

src/Text/Pandoc/Filter/Plot/Embed.hs

Lines changed: 28 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -69,14 +69,17 @@ figure :: Attr
6969
-- must be "fig:"
7070
-- Janky? yes
7171
figure as fp caption' = return . head . toList . para $
72-
imageWith as (pack fp) "fig:" caption'
72+
imageWith as (pack fp) "fig:" caption'
7373

7474

7575
interactiveBlock :: Attr
7676
-> FilePath
7777
-> Inlines
7878
-> PlotM Block
7979
interactiveBlock _ 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.
102105
extractPlot :: Text -> Text
103106
extractPlot 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>/
109115
inside :: Text -> [Tag Text] -> [Tag Text]
110116
inside 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.
126146
deferScripts :: [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))

src/Text/Pandoc/Filter/Plot/Monad.hs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,9 @@ runCommand command = do
149149
-- Plot state consists of a map of filepaths to hashes
150150
-- This allows multiple plots to depend on the same file/directory, and the file hashes
151151
-- will only be calculated once. This is OK because pandoc-plot will not run for long.
152+
-- We note that because figures are rendered possibly in parallel, access to
153+
-- the state must be synchronized; otherwise, each thread might compute its own
154+
-- hashes.
152155
type FileHash = Int
153156
type PlotState = MVar (Map FilePath FileHash)
154157

src/Text/Pandoc/Filter/Plot/Parse.hs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ parseFigureSpec block@(CodeBlock (id', classes, attrs) content) = do
9292
directory = makeValid $ unpack $ Map.findWithDefault (pack $ defaultDirectory conf) (tshow DirectoryK) attrs'
9393
dpi = fromMaybe defDPI $ (read . unpack) <$> Map.lookup (tshow DpiK) attrs'
9494
extraAttrs = Map.toList extraAttrs'
95-
blockAttrs = (id', classes, filteredAttrs)
95+
blockAttrs = (id', filter (/= cls toolkit) classes, filteredAttrs)
9696

9797
let blockDependencies = parseFileDependencies $ fromMaybe mempty $ Map.lookup (tshow DependenciesK) attrs'
9898
dependencies = (defaultDependencies conf) <> blockDependencies

0 commit comments

Comments
 (0)