Skip to content

Commit

Permalink
Move resource embedding into HTML writer [API change]
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
tarleb committed Jul 14, 2024
1 parent 790abcf commit 2edf83e
Show file tree
Hide file tree
Showing 7 changed files with 23 additions and 7 deletions.
3 changes: 3 additions & 0 deletions doc/lua-filters.md
Original file line number Diff line number Diff line change
Expand Up @@ -2132,6 +2132,9 @@ Fields:
: How to obfuscate emails -- one of 'none', 'references', or
'javascript' (string)

`embed_resources`
: Whether resources should be embedded in HTML output (boolean)

`epub_chapter_level`
: Header level for chapters, i.e., how the document is split
into separate files (integer)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,11 @@ typeWriterOptions = deftype "WriterOptions"
(pushViaJSON, writerEmailObfuscation)
(peekViaJSON, \opts x -> opts{ writerEmailObfuscation = x })

, property "embed_resources"
"Whether resources should be embedded in HTML output"
(pushViaJSON, writerEmbedResources)
(peekViaJSON, \opts x -> opts{ writerEmbedResources = x })

, property "split_level"
"Level at which EPUB or chunked HTML documents are split into files"
(pushIntegral, writerSplitLevel)
Expand Down
3 changes: 3 additions & 0 deletions pandoc-lua-engine/test/lua/module/globals.lua
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ return {
test('email_obfuscation', function ()
assert.are_equal(type(PANDOC_WRITER_OPTIONS.email_obfuscation), 'string')
end),
test('embed_resources', function ()
assert.are_equal(type(PANDOC_WRITER_OPTIONS.embed_resources), 'boolean')
end),
test('split_level', function ()
assert.are_equal(type(PANDOC_WRITER_OPTIONS.split_level), 'number')
end),
Expand Down
5 changes: 1 addition & 4 deletions src/Text/Pandoc/App.hs
Original file line number Diff line number Diff line change
Expand Up @@ -329,10 +329,7 @@ convertWithOpts' scriptingEngine istty datadir opts = do
| standalone = t
| T.null t || T.last t /= '\n' = t <> T.singleton '\n'
| otherwise = t
textOutput <- ensureNl <$> f writerOptions doc
if (optSelfContained opts || optEmbedResources opts) && htmlFormat format
then TextOutput <$> makeSelfContained textOutput
else return $ TextOutput textOutput
TextOutput . ensureNl <$> f writerOptions doc
reports <- getLog
return (output, reports)

Expand Down
2 changes: 2 additions & 0 deletions src/Text/Pandoc/App/OutputSettings.hs
Original file line number Diff line number Diff line change
Expand Up @@ -263,6 +263,8 @@ optToOutputSettings scriptingEngine opts = do
, writerSyntaxMap = syntaxMap
, writerPreferAscii = optAscii opts
, writerLinkImages = optLinkImages opts
, writerEmbedResources = optEmbedResources opts ||
optSelfContained opts
}
return $ OutputSettings
{ outputFormat = format
Expand Down
2 changes: 2 additions & 0 deletions src/Text/Pandoc/Options.hs
Original file line number Diff line number Diff line change
Expand Up @@ -326,6 +326,7 @@ data WriterOptions = WriterOptions
, writerSyntaxMap :: SyntaxMap
, writerPreferAscii :: Bool -- ^ Prefer ASCII representations of characters when possible
, writerLinkImages :: Bool -- ^ Use links rather than embedding ODT images
, writerEmbedResources :: Bool -- ^ Embed resources in HTML
} deriving (Show, Data, Typeable, Generic)

instance Default WriterOptions where
Expand Down Expand Up @@ -365,6 +366,7 @@ instance Default WriterOptions where
, writerSyntaxMap = defaultSyntaxMap
, writerPreferAscii = False
, writerLinkImages = False
, writerEmbedResources = False
}

instance HasSyntaxExtensions WriterOptions where
Expand Down
10 changes: 7 additions & 3 deletions src/Text/Pandoc/Writers/HTML.hs
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ import Text.Pandoc.Highlighting (formatHtmlBlock, formatHtml4Block,
formatHtmlInline, highlight, styleToCss)
import Text.Pandoc.ImageSize
import Text.Pandoc.Options
import Text.Pandoc.SelfContained (makeSelfContained)
import Text.Pandoc.Shared
import Text.Pandoc.Slides
import Text.Pandoc.Templates (renderTemplate)
Expand Down Expand Up @@ -230,9 +231,12 @@ writeHtmlString' st opts d = do
let colwidth = case writerWrapText opts of
WrapAuto -> Just (writerColumns opts)
_ -> Nothing
(if writerPreferAscii opts
then toEntities
else id) <$>
(if writerEmbedResources opts
then makeSelfContained
else pure) =<<
(if writerPreferAscii opts
then toEntities
else id) <$>
case writerTemplate opts of
Nothing -> return $
case colwidth of
Expand Down

0 comments on commit 2edf83e

Please sign in to comment.