logo

inliner filter

← Back to Filter List

inliner


Imports any referenced images as data URIs.

Aliases for this filter

  • inliner

Converts from file formats:

  • .*

To file formats:

  • .*

Available settings:

SettingDescriptionDefault
add-new-filesBoolean or list of extensions/patterns to match.False
added-in-versionDexy version when this filter was first available.
additional-doc-filtersFilters to apply to additional documents created as side effects.{}
additional-doc-settingsSettings to apply to additional documents created as side effects.{}
data-typeAlias of custom data class to use to store filter output.generic
examplesTemplates which should be used as examples for this filter.[]
exclude-add-new-filesList of patterns to skip even if they match add-new-files.[]
exclude-new-files-from-dirList of directories to skip when adding new files.[]
extFile extension to output.None
extension-mapDictionary mapping input extensions to default output extensions.None
helpHelpstring for plugin.Imports any referenced images as data URIs.
html-parserName of html parser BeautifulSoup should use.html.parser
inline-imagesWhether to inline images using the data uri scheme.True
inline-stylesWhether to embed referenced CSS in the page header.True
input-extensionsList of extensions which this filter can accept as input.['.*']
keep-originalsWhether, if additional-doc-filters are specified, the original unmodified docs should also be added.False
mkdirA directory which should be created in working dir.None
mkdirsA list of directories which should be created in working dir.[]
nodocWhether filter should be excluded from documentation.False
outputWhether to output results of this filter by default by reporters such as 'output' or 'website'.False
output-extensionsList of extensions which this filter can produce as output.['.*']
override-workspace-exclude-filtersIf True, document will be populated to other workspaces ignoring workspace-exclude-filters.False
preserve-prior-data-classWhether output data class should be set to match the input data class.False
require-outputShould dexy raise an exception if no output is produced by this filter?True
tagsTags which describe the filter.[]
variablesA dictionary of variable names and values to make available to this filter.{}
varsA dictionary of variable names and values to make available to this filter.{}
workspace-exclude-filtersFilters whose output should be excluded from workspace.['pyg']
workspace-includesIf set to a list of filenames or extensions, only these will be populated to working dir.None
Filter Source Code
class InlineAssets(DexyFilter):
    """
    Imports any referenced images as data URIs.
    """
    aliases = ['inliner']

    _settings = {
            'html-parser' : ("Name of html parser BeautifulSoup should use.", 'html.parser'),
            'inline-images' : ("Whether to inline images using the data uri scheme.", True),
            'inline-styles' : ("Whether to embed referenced CSS in the page header.", True)
            }

    def inline_images(self, soup):
        for tag in soup.find_all("img"):
            path = tag.get('src')

            f = urllib.urlopen(path)
            data = f.read()
            f.close()

            mime, _ = mimetypes.guess_type(path)
            data64 = base64.encodestring(data)
            dataURI = 'data:%s;base64,%s' % (mime, data64)
            tag['src'] = dataURI

    def inline_styles(self, soup):
        for tag in soup.find_all("link"):
            path = tag.get('href')

            f = urllib.urlopen(path)
            data = f.read()
            f.close()

            style = soup.new_tag('style')
            style.string = data

            tag.replace_with(style)

    def process(self):
        soup = BeautifulSoup(str(self.input_data), self.setting('html-parser'))
        self.populate_workspace()

        with chdir(self.parent_work_dir()):
            if self.setting('inline-images'):
                self.inline_images(soup)
    
            if self.setting('inline-styles'):
                self.inline_styles(soup)
    
        self.output_data.set_data(str(soup))

Content © 2020 Dr. Ana Nelson | Site Design © Copyright 2011 Andre Gagnon | All Rights Reserved.