Skip to content

Instantly share code, notes, and snippets.

@introt
Created December 24, 2022 07:54
Show Gist options
  • Save introt/fed672dfbea9df399610084be448326b to your computer and use it in GitHub Desktop.
Save introt/fed672dfbea9df399610084be448326b to your computer and use it in GitHub Desktop.
Zim plugin to inline images in HTML files
# Copyright 2022 introt <introt@koti.fimnet.fi>
# based on windowtitleeditor.py and Zim Desktop Wiki code released under GPLv2
import base64
from pathlib import PurePath
from zim.formats.html import Dumper, html_encode
from zim.plugins import PluginClass
from zim.gui.mainwindow import MainWindowExtension
import logging
logger = logging.getLogger('zim.plugins.htmlimageinliner')
class HtmlImageInlinerPlugin(PluginClass):
plugin_info = {
'name': _('HTML Image Inliner'), # T: plugin name
'description': _('''\
Enabling this plugin makes the html exporter inline images.
To restore original functionality, disable the plugin.
Limitations:
- Only images attached from the current dir are inlined.
Other images break.
- Inlined images are still copied into the "page_files"
directory on export; the files therein can be safely
deleted.
'''), # T: plugin description
'author': 'introt',
}
class HtmlImageInlinerExtension(MainWindowExtension):
def __init__(self, plugin, window):
MainWindowExtension.__init__(self, plugin, window) # init super
self.old_dumper = Dumper.dump_img
Dumper.dump_img = lambda d, t, a, s=None: self.inline_img(d, t, a, s)
logger.debug('HtmlImageInliner loaded')
def inline_img(self, dumper, tag, attrib, strings=None):
l = dumper.linker
src = l.img(attrib['src'])
try:
# XXX: quick hack; instead of improving,
# please use the time to create a real implementation
a_dir = PurePath(str(l.notebook.get_attachments_dir(l.source)))
path = a_dir / PurePath(str(src)).parts[-1]
with open(path, 'rb') as f:
src = f'''data:image/png;base64,{base64.b64encode(f.read()).decode('utf-8')}'''
except:
pass
opt = ''
if 'alt' in attrib:
opt += ' alt="%s"' % html_encode(attrib['alt']).replace('"', '&quot;')
for o in ('width', 'height'):
if o in attrib and int(float(attrib[o])) > 0:
opt += ' %s="%s"' % (o, attrib[o])
if 'href' in attrib:
href = dumper.linker.link(attrib['href'])
return ['<a href="%s"><img src="%s"%s></a>' % (href, src, opt)]
else:
return ['<img src="%s"%s>' % (src, opt)]
def teardown(self):
Dumper.dump_img = self.old_dumper
logger.debug('Original dump_img method restored')
@introt
Copy link
Author

introt commented Jan 9, 2023

using flatpack

That would explain gedit not being found; see "Flatpak: Could not find executable when trying to open files with external application" zim-desktop-wiki/zim-desktop-wiki#2169 for more information

I cannot find the executable

According to the Flathub page, flatpak run org.zim_wiki.Zim -D > zim.log 2>&1 should work

Anyway, I can load your plugin now, perhaps I needed to reboot.

Great! The plugin does a monkey patch at runtime so that might indeed be required.

But I have generated a Test.html with the plugin activated and still get a Test_files
directory with the images

https://gist.github.com/introt/fed672dfbea9df399610084be448326b#file-htmlimageinliner-py-L31-L33

find $EXPORTDIR -type f -not -name '*.html' -exec rm {} or similar at your own risk

Hope this helps! I haven't tested exporting multiple pages at once so you might need to use the CLI to export them one by one.

@aloboa
Copy link

aloboa commented Jan 9, 2023

Thanks.
I have deleted the directory Test_files and Test.html shows all images.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment