Skip to content

Commit 4fffcca

Browse files
committed
Move resource embedding into HTML writer [API change]
This adds a new field `writerEmbedResources` to the `WriterOptions` type. It is set to `False` by default. The field is named `embed_resources` in Lua.
1 parent 790abcf commit 4fffcca

File tree

7 files changed

+23
-8
lines changed

7 files changed

+23
-8
lines changed

doc/lua-filters.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2132,6 +2132,9 @@ Fields:
21322132
: How to obfuscate emails -- one of 'none', 'references', or
21332133
'javascript' (string)
21342134

2135+
`embed_resources`
2136+
: Whether resources should be embedded in HTML output (boolean)
2137+
21352138
`epub_chapter_level`
21362139
: Header level for chapters, i.e., how the document is split
21372140
into separate files (integer)

pandoc-lua-engine/src/Text/Pandoc/Lua/Marshal/WriterOptions.hs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,11 @@ typeWriterOptions = deftype "WriterOptions"
8080
(pushViaJSON, writerEmailObfuscation)
8181
(peekViaJSON, \opts x -> opts{ writerEmailObfuscation = x })
8282

83+
, property "embed_resources"
84+
"Whether resources should be embedded in HTML output"
85+
(pushViaJSON, writerEmbedResources)
86+
(peekViaJSON, \opts x -> opts{ writerEmbedResources = x })
87+
8388
, property "split_level"
8489
"Level at which EPUB or chunked HTML documents are split into files"
8590
(pushIntegral, writerSplitLevel)

pandoc-lua-engine/test/lua/module/globals.lua

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,9 @@ return {
2323
test('email_obfuscation', function ()
2424
assert.are_equal(type(PANDOC_WRITER_OPTIONS.email_obfuscation), 'string')
2525
end),
26+
test('embed_resources', function ()
27+
assert.are_equal(type(PANDOC_WRITER_OPTIONS.embed_resources), 'boolean')
28+
end),
2629
test('split_level', function ()
2730
assert.are_equal(type(PANDOC_WRITER_OPTIONS.split_level), 'number')
2831
end),

src/Text/Pandoc/App.hs

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,6 @@ import Text.Pandoc.Filter (Filter (JSONFilter, LuaFilter), Environment (..),
6565
import qualified Text.Pandoc.Format as Format
6666
import Text.Pandoc.PDF (makePDF)
6767
import Text.Pandoc.Scripting (ScriptingEngine (..), CustomComponents(..))
68-
import Text.Pandoc.SelfContained (makeSelfContained)
6968
import Text.Pandoc.Shared (tshow)
7069
import Text.Pandoc.URI (isURI)
7170
import Text.Pandoc.Writers.Shared (lookupMetaString)
@@ -329,10 +328,7 @@ convertWithOpts' scriptingEngine istty datadir opts = do
329328
| standalone = t
330329
| T.null t || T.last t /= '\n' = t <> T.singleton '\n'
331330
| otherwise = t
332-
textOutput <- ensureNl <$> f writerOptions doc
333-
if (optSelfContained opts || optEmbedResources opts) && htmlFormat format
334-
then TextOutput <$> makeSelfContained textOutput
335-
else return $ TextOutput textOutput
331+
TextOutput . ensureNl <$> f writerOptions doc
336332
reports <- getLog
337333
return (output, reports)
338334

src/Text/Pandoc/App/OutputSettings.hs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -263,6 +263,8 @@ optToOutputSettings scriptingEngine opts = do
263263
, writerSyntaxMap = syntaxMap
264264
, writerPreferAscii = optAscii opts
265265
, writerLinkImages = optLinkImages opts
266+
, writerEmbedResources = optEmbedResources opts ||
267+
optSelfContained opts
266268
}
267269
return $ OutputSettings
268270
{ outputFormat = format

src/Text/Pandoc/Options.hs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -326,6 +326,7 @@ data WriterOptions = WriterOptions
326326
, writerSyntaxMap :: SyntaxMap
327327
, writerPreferAscii :: Bool -- ^ Prefer ASCII representations of characters when possible
328328
, writerLinkImages :: Bool -- ^ Use links rather than embedding ODT images
329+
, writerEmbedResources :: Bool -- ^ Embed resources in HTML
329330
} deriving (Show, Data, Typeable, Generic)
330331

331332
instance Default WriterOptions where
@@ -365,6 +366,7 @@ instance Default WriterOptions where
365366
, writerSyntaxMap = defaultSyntaxMap
366367
, writerPreferAscii = False
367368
, writerLinkImages = False
369+
, writerEmbedResources = False
368370
}
369371

370372
instance HasSyntaxExtensions WriterOptions where

src/Text/Pandoc/Writers/HTML.hs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ import Text.Pandoc.Highlighting (formatHtmlBlock, formatHtml4Block,
5555
formatHtmlInline, highlight, styleToCss)
5656
import Text.Pandoc.ImageSize
5757
import Text.Pandoc.Options
58+
import Text.Pandoc.SelfContained (makeSelfContained)
5859
import Text.Pandoc.Shared
5960
import Text.Pandoc.Slides
6061
import Text.Pandoc.Templates (renderTemplate)
@@ -230,9 +231,12 @@ writeHtmlString' st opts d = do
230231
let colwidth = case writerWrapText opts of
231232
WrapAuto -> Just (writerColumns opts)
232233
_ -> Nothing
233-
(if writerPreferAscii opts
234-
then toEntities
235-
else id) <$>
234+
(if writerEmbedResources opts
235+
then makeSelfContained
236+
else pure) =<<
237+
(if writerPreferAscii opts
238+
then toEntities
239+
else id) <$>
236240
case writerTemplate opts of
237241
Nothing -> return $
238242
case colwidth of

0 commit comments

Comments
 (0)