tr:html-embed-resources html-embed-resources
xproc-util/html-embed-resources/xpl/html-embed-resources.xpl
Import URI: http://transpect.io/xproc-util/html-embed-resources/xpl/html-embed-resources.xpl
tr:html-embed-resources
This step tries to embed external resources such as images, CSS and JavaScript via data URI into the HTML document.
Input Ports
Name | Documentation | Connections |
---|---|---|
source | expects an XHTML document |
Output Ports
Name | Documentation | Connections |
---|---|---|
resultⓅ | provides the XHTML document with embedded resources |
Options
Name | Documentation | Default |
---|---|---|
exclude | Space-separated list of tokens. Available tokens are: image video script style audio object #all. (Question: support font as a category on its own?) | '' |
debug | 'no' | |
fail-on-error | 'true' |
Subpipeline
Step | Inputs | Outputs | Options | ||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
p:variable top-level-base-uri | ( /*/@xml:base, base-uri(/*) )[1] | ||||||||||||||
p:variable suppress-video | tokenize($exclude, '\s+')[. = ('#all', 'video')] | ||||||||||||||
p:variable suppress-audio | tokenize($exclude, '\s+')[. = ('#all', 'audio')] | ||||||||||||||
p:variable suppress-image | tokenize($exclude, '\s+')[. = ('#all', 'image')] | ||||||||||||||
p:variable suppress-script | tokenize($exclude, '\s+')[. = ('#all', 'script')] | ||||||||||||||
p:variable suppress-style | tokenize($exclude, '\s+')[. = ('#all', 'style')] | ||||||||||||||
p:variable suppress-object | tokenize($exclude, '\s+')[. = ('#all', 'object')] | ||||||||||||||
p:viewport viewport | |||||||||||||||
p:variable local-base-uri | (base-uri(.), $top-level-base-uri)[1] | ||||||||||||||
p:variable href-attribute | (*[local-name() = ('img', 'audio', 'video', 'script')]/@src, html:object/@data, html:link/@href, svg:image/@xlink:href)[1] | ||||||||||||||
p:variable href | if(starts-with($href-attribute, 'data:')) (: leave data URIs as-is :) then $href-attribute else resolve-uri(if(matches($href-attribute, '^(http[s]?|file)://?')) (: resolve regular URIs :) then $href-attribute else concat(replace($local-base-uri, '^(.+/).+$', '$1'), $href-attribute), $local-base-uri) | ||||||||||||||
p:choose d172e51 | |||||||||||||||
exists( /*[local-name() = ('img'[$suppress-image], 'audio'[$suppress-audio], 'video'[$suppress-video], 'script'[$suppress-script])][@src] | /html:object[@data][$suppress-object] | /html:link[@rel eq 'stylesheet'][@href][$suppress-style] | /svg:image[@xlink:href][$suppress-image] ) | Suppress embedding for elements meeting these conditions. Unfortunately, the conditions could not be specified in the p:viewport match attribute because options and variables seem to be inaccessible there. | ||||||||||||||
p:identity d172e58 |
| result | |||||||||||||
not(starts-with($href-attribute, 'data:')) | |||||||||||||||
p:try d172e63 | |||||||||||||||
p:group d172e65 | |||||||||||||||
p:choose d172e67 | |||||||||||||||
$debug eq 'yes' | |||||||||||||||
cx:message d172e71 |
| result | message = 'embed: ', $href | ||||||||||||
p:otherwise | |||||||||||||||
p:identity d172e79 |
| result | |||||||||||||
p:add-attribute construct-http-request |
| result | attribute-name = 'href' match = '/c:request' attribute-value = $href | ||||||||||||
p:http-request http-request |
| result | |||||||||||||
p:add-attribute add-xmlbase |
| result | attribute-name = 'xml:base' match = '//c:body' attribute-value = $href | ||||||||||||
p:choose d172e108 | |||||||||||||||
html:img|html:audio|html:video|html:script|html:object|svg:image | |||||||||||||||
p:string-replace d172e121 | result | match = '*[local-name() = ('img', 'audio', 'video', 'script')]/@src|html:object/@data|svg:image/@xlink:href|html:video/html:source|html:audio/@src/html:source/@src' replace = concat('''', 'data:', $content-type, ';', $encoding, ',', //c:body, '''') | |||||||||||||
p:otherwise | |||||||||||||||
p:insert insert-style |
| result | match = 'html:style' position = 'first-child' | ||||||||||||
p:try try-extract-references-from-css | |||||||||||||||
p:group d172e157 | |||||||||||||||
p:xslt extract-references-from-css |
| result | |||||||||||||
p:viewport viewport-data-uri | |||||||||||||||
p:variable data-uri | tr:data-uri/@href | ||||||||||||||
p:variable mime-type | tr:data-uri/@mime-type | ||||||||||||||
p:choose d172e177 | |||||||||||||||
$debug eq 'yes' | |||||||||||||||
cx:message d172e181 |
| result | message = 'embed: ', $data-uri | ||||||||||||
p:otherwise | |||||||||||||||
p:identity d172e189 |
| result | |||||||||||||
p:unwrap d172e223 |
| result | match = 'html:style//tr:data-uri' | ||||||||||||
p:catch d172e226 | error | ||||||||||||||
p:identity d172e228 |
| result | |||||||||||||
p:unwrap d172e238 |
| result | match = 'html:style//c:body' | ||||||||||||
p:catch d172e245 | error | ||||||||||||||
p:choose d172e247 | |||||||||||||||
$fail-on-error eq 'true' | |||||||||||||||
p:error d172e251 |
| result | code = 'html-resource-embed-failed' | ||||||||||||
p:otherwise | |||||||||||||||
p:identity d172e266 | result | ||||||||||||||
p:choose d172e274 | |||||||||||||||
$debug eq 'yes' | |||||||||||||||
cx:message d172e278 | result | message = '[WARNING] failed to embed file: ', $href | |||||||||||||
p:otherwise | |||||||||||||||
p:identity d172e286 | result | ||||||||||||||
p:otherwise | |||||||||||||||
p:identity d172e297 | result |