-
-
Save introt/fed672dfbea9df399610084be448326b to your computer and use it in GitHub Desktop.
| # Copyright 2022 introt <[email protected]> | |
| # 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') | |
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:
(base) alobo@pop-os:~$ which zim
(base) alobo@pop-os:~$ zim
Command 'zim' not found, but can be installed with:
sudo apt install zim
(base) alobo@pop-os:~$
As it had not been installed.
But I have been able to locate a lot of zim.log files. The latest in
/home/alobo/.var/app/org.zim_wiki.Zim/cache/tmp/zim-edrucvc6
export-log.txt
zim.log
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:
(base) alobo@pop-os:~/WORK/HSI_Lab/weblabEcodivResponse/Test_files$ ls -l
total 5580
drwxr-xr-x 2 alobo alobo 4096 ene 9 07:47 01_Equipment
-rw-r--r-- 1 alobo alobo 263156 ene 9 07:47 01_Equipment.html
drwxr-xr-x 2 alobo alobo 4096 ene 9 07:47 02_Technical_Notes,_Tests_and_Demos
-rw-r--r-- 1 alobo alobo 126071 ene 9 07:47 02_Technical_Notes,_Tests_and_Demos.html
drwxr-xr-x 2 alobo alobo 4096 ene 9 07:47 03_Protocoles_and_Manuals
-rw-r--r-- 1 alobo alobo 5594 ene 9 07:47 03_Protocoles_and_Manuals.html
-rw-r--r-- 1 alobo alobo 5949 ene 9 07:47 04_In-house_Software.html
-rw-r--r-- 1 alobo alobo 4588 ene 9 07:47 05_DataSets.html
drwxr-xr-x 2 alobo alobo 4096 ene 9 07:47 06_Research_Studies
-rw-r--r-- 1 alobo alobo 366089 ene 9 07:47 06_Research_Studies.html
-rw-rw-r-- 1 alobo alobo 64240 oct 1 2021 figure1.jpg
-rw-rw-r-- 1 alobo alobo 48845 oct 2 2021 figuresmalllab001.jpg
-rw-rw-r-- 1 alobo alobo 46623 oct 1 2021 figuresmalllab.jpg
-rw-r--r-- 1 alobo alobo 3178239 ene 20 2021 LabImagingSpectroscopy_geo3bcn.pdf
-rw-r--r-- 1 alobo alobo 25877 sep 9 2021 pasted_image001.png
-rw-r--r-- 1 alobo alobo 51312 sep 9 2021 pasted_image002.png
-rw-r--r-- 1 alobo alobo 456301 sep 9 2021 pasted_image003.png
-rw-r--r-- 1 alobo alobo 68466 sep 9 2021 pasted_image004.png
-rw-r--r-- 1 alobo alobo 400639 sep 9 2021 pasted_image005.png
-rw-r--r-- 1 alobo alobo 442556 sep 28 19:33 pasted_image006.png
-rw-r--r-- 1 alobo alobo 104586 sep 9 2021 pasted_image.png
drwxrwxr-x 2 alobo alobo 4096 oct 2 2021 _resources
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.
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 gather that you're on a Linux system - what's your distribution and how did you install Zim? If Zim is not running in some sandbox it should have access to
$PATH, sogeditwith or without path should work.Another way to get the log is to execute Zim from the command line:
zim -D > zim.log 2>&1; you'll then find the output file in the current directory, assuming a POSIXy shell.