-
-
Save introt/fed672dfbea9df399610084be448326b to your computer and use it in GitHub Desktop.
# 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('"', '"') | |
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') | |
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.
Thanks.
I have deleted the directory Test_files and Test.html shows all images.
I'm running linux Pop!_OS 21.04, which is based on Ubuntu 21.04
and have Zim 0.74.3 installed using flatpack.
I cannot find the executable, I get:
As it had not been installed.
But I have been able to locate a lot of zim.log files. The latest in
Anyway, I can load your plugin now, perhaps I needed to reboot.
But I have generated a Test.html with the plugin activated and still get a Test_files
directory with the images:
I link both log files:
https://www.dropbox.com/s/kar18g0qyesdi6h/zim.log?dl=0
https://www.dropbox.com/s/m0kjs8qwsm2iq71/export-log.txt?dl=0
and the html file:
https://www.dropbox.com/s/cxbguh3l4w7yy1h/Test.html?dl=0
Thanks a lot for your support.