MobileRead Forums

MobileRead Forums (https://www.mobileread.com/forums/index.php)
-   Plugins (https://www.mobileread.com/forums/forumdisplay.php?f=268)
-   -   [Plugin] TagMechanic (https://www.mobileread.com/forums/showthread.php?t=270639)

DiapDealer 02-06-2016 02:56 PM

[Plugin] TagMechanic
 
1 Attachment(s)
TagMechanic: conveniently remove, change the attributes of (and/or convert to different html elements) those pesky, nestable spans and divs (as well as a few others for convenience).

** NOTE: this plugin periodically checks for updated versions by connecting to github (where the source is maintained). See the end of this post for more details **

** NOTE 2: this plugin cannot be launched when Sigil is opened via Calibre's Open With feature (or plugin of the same name) if the version of Sigil being used is less than v1.3. If you need that launch-from-calibre functionality, make sure you're running the latest version of Sigil.**

This is a "rebranding" of my "Span/Div Editor" calibre-edit plugin for those who may remember and/or use it.

Minimum Sigil requirement: v0.9.8 or higher
Python Requirements: Python 3.4+ (Bundled or external)
OS Requirements: Windows/Linux/OS X
*** Linux users will have to make sure that the PyQt5 graphical python module is present if it's not already. On Debian-based flavors this can be done with "sudo apt-get install python3-pyqt5". On Arch distributions it can be done with pacman -S python-pyqt5 ***

1) This plugin operates on the Text files selected/highlighted in Sigil's Book Browser, so make sure you highlight the files you want to work with before launching the plugin.

2) Then choose the action you wish to perform (modify/delete), select the tag you want to perform that action on, the attribute you want to match (No attributes means you want to work with those elements that HAVE no attributes at all) and the value that that attribute should have. If the value you entered is a regular expression, check the regex box (capture groups cannot be reinserted later so they will be ignored if present). Otherwise, it is assumed that what you have entered in the New Attribute box is a literal string.

3) If you've chosen to modify a tag, you need to specify whether you want to change the tag to another tag, or change its attributes, or both. If you've chosen to change the tag to something else, then you need to enter all the attributes you want (or check the box to copy the existing attributes) otherwise the tag will be changed to the new tag with no attributes if left empty.

Configuration: From the 'Edit' menu choose 'Config' to customize the plugin. In that dialog, you'll be able to change/add/remove what tags are available to change the various tags to. You can also change/add/remove things from the list of attributes to look for. They're just comma-separated lists of elements/attributes, get cute and you might break something. There's a Reset to Default button if you get in trouble.

After seeing the general report of what was done (or not done), you'll have the opportunity to commit the changes, or to bail out if you get chicken. ;)

TagMechanic's code is hosted/maintained on Github.

This plugin's update checker will only try to check for updates after at least 12 hours has elapsed since its last check. The plugin will only notify you about updates once for each new version released. No nagging, I promise.

Changes
Spoiler:
v0.4.1
- Port from calibre/Qt to Sigil/Tkinter
v0.4.2
- Add 'p' tag to default list of tags the plugin can operate on
v0.4.3
- Skip problematic icon assignment on OS X
v0.4.4
- Made plugin code easier to maintain
- Made config widget dynamically multi-column
v0.4.5
- Added ability to tweak font-sizes
v0.5.0
- Converted the plugin's UI to PyQt5 to mirror Sigil's
v0.5.1
- Fix to work with (stand alone) versions of Sigil[ less than v1.3.0
v0.5.2
- Add PL Translations (thanks @BeckyEbook)
- Various other cosmetic and logical tweaks by @BeckyEbook
- fix reports being output with no linebreaks

theducks 02-06-2016 05:02 PM

Quote:

Originally Posted by DiapDealer (Post 3254777)

1) This plugin operates on the Text files selected/highlighted in Sigil's Book View, so make sure you highlight the files you want to work with before launching the plugin.

Don't you mean Book Browser :smack:

BTW
THANK YOU

I have been jumping over to the Calibre editor to use the toolbag

DiapDealer 02-06-2016 05:18 PM

Quote:

Originally Posted by theducks (Post 3254845)
Don't you mean Book Browser :smack:

Sure do. Thanks! :o

Quote:

Originally Posted by theducks (Post 3254845)
BTW
THANK YOU

I have been jumping over to the Calibre editor to use the toolbag

And you're welcome. :)

IHS 02-11-2016 11:58 AM

Commit changes
 
DiapDealer:

I am new to this forum, thank you for having me. I do epubs (and PDF and mobi ebooks) for a couple of small publishers, starting with indesign and/or xml files.

Thanks for a VERY handy plugin. It makes cleaning up the InDesign epub mess rather a breeze. (On a separate but hopefully not too unrelated note I would be interested in hearing from any forum members about their experience with Indesign epub creation. I am finding that files that are set up with that work well for print layout really need re-tooling if the generated epub files are to be clean and well organized.)

One question I had on your plugin -- my experience is that when I run the plugin the changes are made to the files even though I aborted rather than clicking commit changes. Of course I can close and elect not to save the changes from Sigil's main window, but I assumed that when the plugin offers the opportunity to abort it is the changes to the files that is being aborted...let me know if the question/comment makes no sense.

Thanks again for a great forum.

DiapDealer 02-11-2016 12:16 PM

Quote:

Originally Posted by IHS (Post 3258216)
One question I had on your plugin -- my experience is that when I run the plugin the changes are made to the files even though I aborted rather than clicking commit changes. Of course I can close and elect not to save the changes from Sigil's main window, but I assumed that when the plugin offers the opportunity to abort it is the changes to the files that is being aborted...let me know if the question/comment makes no sense.

I'll certainly look into it, but it's definitely aborting the changes when I use it on my systems (or it was at one point anyway). The changes wrought by any plugin should never be committed to Sigil's working copy until the plugin safely returns a value of zero. Abort returns a value of -1.

That's not to say I didn't make a mistake somewhere! I'll take a look.

DiapDealer 02-11-2016 01:05 PM

Quote:

Originally Posted by DiapDealer (Post 3258235)
That's not to say I didn't make a mistake somewhere! I'll take a look.

I'm definitely not experiencing what you describe on Linux or Windows. No changes are made to Sigil's files unless I click "Commit & Exit". Are you sure you didn't commit changes in a previous run of the plugin and forget?

KevinH 02-11-2016 01:12 PM

FYI:
Up until recently returning -1 was not enough for the plugin to actually abort. Only throwing an exception seemed to do the trick. That bug was fixed in Sigil-0.9.2 or Sigil-0.9.3. I don't remember which right now. So if changes still get applied, please make sure you are using the very latest Sigil-0.9.3 version. If you hit cancel and changes still get applied with that version of Sigil, Sigil itself may have a bug.

KevinH

DiapDealer 02-11-2016 01:15 PM

Quote:

Originally Posted by KevinH (Post 3258274)
FYI:
Up until recently returning -1 was not enough for the plugin to actually abort. Only throwing an exception seemed to do the trick. That bug was fixed in Sigil-0.9.2 or Sigil-0.9.3. I don't remember which right now. So if changes still get applied, please make sure you are using the very latest Sigil-0.9.3 version. If you hit cancel and changes still get applied with that version of Sigil, Sigil itself may have a bug.

KevinH

Ahh... I'd forgotten about that. Perhaps I'll make a version that throws an exception just to be sure. Thanks for reminding me.

EDIT: though I would still recommend users upgrading to the latest version of Sigil. This plugin needs at least Sigil v0.8.900 (bk.selected_iter()), so it's not like people stuck with 0.8.6/7 could use it anyway. There's no good reason I can think of to stay with 0.8.900 - 0.9.2.

tamchec 03-11-2016 12:12 AM

Hi DiapDealer!

Thanks for your useful plugin, I very like this.

I've recently got error when run this plugin. This is my error log:

Code:

Status: failed

Traceback (most recent call last):
  File "C:\Program Files\Sigil\plugin_launchers\python\launcher.py", line 135, in launch
    self.exitcode = target_script.run(container)
  File "C:\Users\username\AppData\Local\sigil-ebook\sigil\plugins\TagMechanic\plugin.py", line 545, in run
    guiMain(root, bk).pack(fill=tkinter_constants.BOTH)
  File "C:\Users\username\AppData\Local\sigil-ebook\sigil\plugins\TagMechanic\plugin.py", line 113, in __init__
    self.initUI()
  File "C:\Users\username\AppData\Local\sigil-ebook\sigil\plugins\TagMechanic\plugin.py", line 150, in initUI
    self.tag_combo.current(self.gui_prefs['tag'])
  File "tkinter\ttk.py", line 713, in current
_tkinter.TclError: Index -1 out of range
Error: Index -1 out of range

Please tell me how to fix. Thanks.
My machine: Windows 10 64bit, Sigil 0.9.3.
I've tried remove and re-install sigil (both 0.9.3 and 0.9.2) but still get that error.

EDIT: Wow, I've removed folder plugins_prefs/TagMechanic and TagMechanic can run normally. <3

DiapDealer 03-11-2016 09:14 AM

Quote:

Originally Posted by tamchec (Post 3278412)
Hi DiapDealer!

Thanks for your useful plugin, I very like this.

I've recently got error when run this plugin. This is my error log:


Please tell me how to fix. Thanks.
My machine: Windows 10 64bit, Sigil 0.9.3.
I've tried remove and re-install sigil (both 0.9.3 and 0.9.2) but still get that error.

EDIT: Wow, I've removed folder plugins_prefs/TagMechanic and TagMechanic can run normally. <3

If you modified the attributes or change-to elements from the default, it's possible that things can get a bit out of sync. I tried to include some basic error checking when customizing the plugin, but I'm quite certain that it doesn't cover all the ways it might possibly get broken. If the plugin still runs, you can use the "Reset to default" button to get back to a fresh install, but if the plugin doesn't run, deleting the json preference file (as you've discovered) will do the same thing.

If you do customize the available parameters, I recommend closing the plugin and restarting it before trying to use them in a new query.

Glad you got it working again (and glad you like it) :)

Auramazda 09-20-2016 08:04 PM

great plug-in that I missed, it solve some of my problem

Crick 12-02-2016 05:10 AM

This plugin is very usefull, I have 2 request:
1 - Can you add p in the tag name list so that I dont have to delete TagMechanic.json every time I change something in p.
2 - the plugin can be used more times in the same session instead of a single process and then close?

theducks 12-02-2016 10:40 AM

Quote:

Originally Posted by Crick (Post 3436688)
This plugin is very usefull, I have 2 request:

2 - the plugin can be used more times in the same session instead of a single process and then close?

I second this.
1) Many times I am doing a 'cleanup' of horrible (inline) code and it is most annoying (time consuming) to have it reload

2) I made some sort of error, so nothing got changed :o. Not only #1, but the terms have been cleared, giving me a second chance to mess up... :rolleyes:

:bulb: Intelligent attribute suggestions

Marry the Sigil Report (results will be cached when TM is first run PER BOOK to help performance. Not absolutely perfect ) results to Tag Mechanic
If I select:
tag name: Span
whose value: Pulldown suggest the available classes found used with Spans (from the report data)

DiapDealer 12-02-2016 11:46 AM

I can handle #1 (even though the need to change a "p" tag to another tag is pretty rare--and altering a p tag's attributes is already easy enough with regex).

But I'm not a fan of #2. The last-minute abort/commit feature is too important to me. And allowing multiple passes per plugin run would require me to keep track of multiple (and potentially conficting) commits to be applied (or cancelled). I won't rule it out completely, but it would take a considerable code refactoring project to accomplish it to my satisfaction. I'll have to do some cyphering on it.

Also, the fact that the plugin only operates on the files that were selected before the plugin launches sort of limits what any subsequent runs could accomplish.

DiapDealer 12-02-2016 12:13 PM

Quote:

Originally Posted by theducks (Post 3436820)
:bulb: Intelligent attribute suggestions

Marry the Sigil Report (results will be cached when TM is first run PER BOOK to help performance. Not absolutely perfect ) results to Tag Mechanic
If I select:
tag name: Span
whose value: Pulldown suggest the available classes found used with Spans (from the report data)

The plugin framework has no interface to Sigil's report data. That would take changes to the plugin framework--and to Sigil itself to accomplish. Not entirely out of the question, but not very likely--at least not for a single plugin. There'd have to be a need for multiple plugins (and an interest from other plugin devs) to justify that kind of expansion of the framework.

theducks 12-02-2016 04:39 PM

Quote:

Originally Posted by DiapDealer (Post 3436859)
The plugin framework has no interface to Sigil's report data. That would take changes to the plugin framework--and to Sigil itself to accomplish. Not entirely out of the question, but not very likely--at least not for a single plugin. There'd have to be a need for multiple plugins (and an interest from other plugin devs) to justify that kind of expansion of the framework.

Absolutely a valid reason. :thumbsup: had no clue..what was or was not available to the developer

DiapDealer 12-03-2016 09:39 AM

Quote:

Originally Posted by Crick (Post 3436688)
1 - Can you add p in the tag name list

Version 0.4.2 has added the p tag to the list of tags it can work with.

https://www.mobileread.com/forums/sho...77&postcount=1

Crick 12-03-2016 05:13 PM

thanks

DiapDealer 12-03-2016 05:55 PM

Quote:

Originally Posted by Crick (Post 3437494)
thanks

No problem. Let me know if I missed something and it doesn't work right. It's been a while since I've been "under the hood" of that plugin. :)

odamizu 01-16-2017 02:06 AM

Hi there,

I'm getting an error when I try to run Tag Mechanic. I'm still new to Sigil, so I suspect I'm doing something stupid. I start by opening a book in Sigil, selecting a file, then go to Plugins > Edit > TagMechanic.

Then this happens:

Code:

Status: failed

Traceback (most recent call last):
  File "/Applications/Sigil.app/Contents/plugin_launchers/python/launcher.py", line 135, in launch
    self.exitcode = target_script.run(container)
  File "/Users/X/Library/Application Support/sigil-ebook/sigil/plugins/TagMechanic/plugin.py", line 565, in run
    img = tkinter.Image('photo', file=os.path.join(bk._w.plugin_dir, bk._w.plugin_name, 'images/icon.png'))
  File "/Applications/Sigil.app/Contents/Frameworks/Python.framework/Versions/3.5/lib/python3.5/tkinter/__init__.py", line 3350, in __init__
    self.tk.call(('image', 'create', imgtype, name,) + options)
_tkinter.TclError: couldn't recognize data in image file "/Users/X/Library/Application Support/sigil-ebook/sigil/plugins/TagMechanic/images/icon.png"
Error: couldn't recognize data in image file "/Users/X/Library/Application Support/sigil-ebook/sigil/plugins/TagMechanic/images/icon.png"

I am on a Mac OS 10.11.6 using Sigil 0.9.7. I deleted the preferences file, but that didn't help.

Thanks for any suggestions!

Also, is TagMechanic the Sigil equivalent of Diaps Editing Toolbag in Calibre?

slowsmile 01-16-2017 06:13 AM

@odamizu...The error reports that it can't recognize the data in the image file. This probably means that your downloaded version of Tag Mechanic has a corrupt image file. I've also just downloaded Tag Mechanic and re-installed it again on my system and it works without problems.

Just try downloading and re-installing Tag Mechanic again to see if this cures your problem.

Doitsu 01-16-2017 06:44 AM

@odamizu: Did you install the special macOS ActiveState Tcl 8.6.4.1 package mentioned on the download page? This package is required for all Sigil plugins that come with a GUI.

DiapDealer 01-16-2017 09:25 AM

Doitsu is correct. The Tcl that comes with OS X is broken. That's why KevinH recommends installing Activestate's Tcl before running/installing Sigil plugins (at least he ones that have tkinter gui components).

And yes, this plugin is the equivalent of the "Span/Div Editor" portion of my "Diaps EditorToolbag" plugin for calibre's editor. There's a separate PunctuationSmarten plugin that provides the rest of the functionality from the "Toolbag".

odamizu 01-16-2017 02:19 PM

Thanks. I do have ActiveState Tcl 8.6.4.1 installed. For good measure, I re-installed it. Unfortunately, that didn't help. I still get the error message.

I also re-downloaded a fresh copy of the plug-in, uninstalled and re-installed it a few times. That didn't help either.

Any thing else I can try?

Doitsu 01-16-2017 03:08 PM

Quote:

Originally Posted by odamizu (Post 3459191)
Any thing else I can try?

This appears to be a strange macOS bug. Hopefully, KevinH, who has a Mac, will see this post and know what to do.

In the meantime, you can try the following:

1. Download TextWrangler or another programmer's text editor.
2. Start Sigil and select Edit >Preferences > Open Preferences Location.
3. Click Plugins > TagMechanic and open plugin.py with TextWrangler.
4. Comment out lines 565 and 566 by inserting a # sign:

Code:

    #img = tkinter.Image('photo', file=os.path.join(bk._w.plugin_dir, bk._w.plugin_name, 'images/icon.png'))
    #root.tk.call('wm','iconphoto',root._w,img)

and save plugin.py.

This workaround should allow you to run TagMechanic until KevinH has figured out what additional image library is required to run the commented-out code.

DiapDealer 01-16-2017 03:29 PM

Kevin's tested this plugin for me on OS X, I know. But it's been a while. I don't believe that icon is even visible on a Mac, so I could easily exclude it for that platform if it's found to be causing issues.

@odamizu: in fact ... here's a version that does just that (in case you don't want to manually edit the plugin yourself). See if it makes a difference. If so, I'll commit the change and release a new version. I'm not in love with the icon or anything. ;)

EDIT: test attachment removed since the first post was updated to that version

odamizu 01-16-2017 03:43 PM

Quote:

Originally Posted by Doitsu (Post 3459208)
This appears to be a strange macOS bug. ... Comment out lines 565 and 566 by inserting a # sign ...

This workaround should allow you to run TagMechanic until KevinH has figured what additional image library is required to run the commented-out code.

Excellent! That worked! Thank you!

Quote:

Originally Posted by DiapDealer (Post 3459218)
Kevin's tested this plugin for me on OS X, I know. But it's been a while. I don't believe that icon is even visible on a Mac, so I could easily exclude it for that platform if it's found to be causing issues.

What's weird is, I'm pretty sure I ran this plug-in successfully a few months ago. Not sure what's changed. If there's anything else I can do or info I can provide to help troubleshoot, let me know.

In the meantime, thanks everyone for your help!

odamizu 01-16-2017 03:48 PM

Quote:

Originally Posted by DiapDealer (Post 3459218)
@odamizu: in fact ... here's a version that does just that (in case you don't want to manually edit the plugin yourself). See if it makes a difference. If so, I'll commit the change and release a new version. I'm not in love with the icon or anything. ;)

Thanks! Just installed 0.4.3 and it works, too!

(You're too fast for me. I was trying Doitsu's manual edit, and when I came back to the forum, you had 0.4.3 up)

DiapDealer 01-16-2017 03:52 PM

I'll wait to hear if Kevin has any ideas before updating the first post with the new version. Thanks for making sure it works! :)

KevinH 01-16-2017 04:19 PM

Hi DiapDealer,
Just saw this. I will see if I can reproduce it on my Mac and get back to you.

KevinH

DiapDealer 01-16-2017 04:47 PM

Quote:

Originally Posted by KevinH (Post 3459242)
Hi DiapDealer,
Just saw this. I will see if I can reproduce it on my Mac and get back to you.

KevinH

Thanks! No hurry. Everything I've seen/read say's that that particular code basically does nothing on a Mac anyway. It doesn't show the tkinter widget icon in the dock. But this is the first I'd heard of it actively breaking something.

tk.call() documentation is pretty thin.

KevinH 01-16-2017 05:51 PM

The problem is that tcl/tk 8.5 and lower does not support png icons only gif ones when used in this way. That said, tcl/tk 8.6 and later support png for icons in this way.

The problem is that Python 3.5 building from source on Mac OS X seems to now be hard coded to link to tck/tk 8.5 when previously they linked to either 8.5 or 8.6.

See https://www.python.org/download/mac/tcltk/

So the default build will no longer dynamically link to tck/tk 8.6 which has the fix.

This is so sad! I am going to have to rebuild the bundled Python and somehow path the build to actually look for and require 8.6 and not the stupid 8.5.

I will look to see what changed from building Python 3.4 to 3.5 and see if I can back out that change.

What a mess!

And why farting with this I have broken my portable Python 3.5 build completely so no more Sigil builds until I get it fixed!

KevinH

DiapDealer 01-16-2017 06:02 PM

Ouch! Sorry to have been the one to help you discover the mess. :o

KevinH 01-16-2017 08:18 PM

The change was not in python, it was that the default search path for Frameworks used to be /Library/Frameworks followed by /System/Library/Frameworks when using gcc/clang in the linking stage to link to Tk or Tcl frameworks. More recent Mac OSX versions have locked down the /System path so nothing can write their anymore.

So I have to patch the Python 3.5.2 setup.py code to include the -F/Library/Frameworks to force it to look there for linking first. I will do this so that things will work again for our next release at least for the bundled Python.

KevinH

odamizu 01-16-2017 09:29 PM

Yikes! All I can say is, thank you so much for developing and sharing Sigil, and sorry for opening the door to this mess with my question :o

KevinH 01-16-2017 09:56 PM

No Problems.
I was able to fix the bundled Python interpreter on my Mac OSX machine to look in /Library/Frameworks/Tk.framework and its cousin Tcl.framework and for the ActiveState Active TclTk if it exists and if so use it.

So DiapDealer's current version of Tag Mechanic will now run with no changes needed.

The next build for Mac OSX will bring the fix to Sigil, in the meantime, commenting out those two lines is probably for the best on Mac OS X.

Thanks for the bug report,

KevinH

DiapDealer 01-17-2017 12:12 PM

Can anyone verify that the png "wrench" icon is visible anywhere on a Mac (assuming python is linking to the version of tcl/tk where my code doesn't cause an error) when the plugin is running? If the icon doesn't appear in the dock (or anywhere else) regardless, I'd just as soon "if" that code out on the Mac platform. That way the plugin will work as is on Sigil 0.9.7 on a Mac.

If the icon DOES appear, though, I'll leave the code in and point to this stop-gap version of the plugin until the next Sigil release.

odamizu 01-17-2017 03:38 PM

Quote:

Originally Posted by DiapDealer (Post 3459645)
Can anyone verify that the png "wrench" icon is visible anywhere on a Mac (assuming python is linking to the version of tcl/tk where my code doesn't cause an error) when the plugin is running? If the icon doesn't appear in the dock (or anywhere else) regardless, I'd just as soon "if" that code out on the Mac platform. That way the plugin will work as is on Sigil 0.9.7 on a Mac.

If the icon DOES appear, though, I'll leave the code in and point to this stop-gap version of the plugin until the next Sigil release.

Not sure if this helps, but I re-installed the full plug-in (nothing commented out). I do not see the wrench anywhere on my Mac and the plug-in fails with the aforementioned error message. Thanks!

DiapDealer 01-17-2017 05:18 PM

Quote:

Originally Posted by odamizu (Post 3459764)
Not sure if this helps, but I re-installed the full plug-in (nothing commented out). I do not see the wrench anywhere on my Mac and the plug-in fails with the aforementioned error message. Thanks!

Thanks, Though at this time, someone would probably have to try it on an older version of Sigil (or a newer beta version) to see if the icon was ever visible or not on the Mac. :)

KevinH 01-17-2017 05:40 PM

I can do it and report back. The window menu bar on Macs do not allow/show the icons, but it may show up in the panel if the Window is minimized, but typically these just map to a generic Python app symbol.


All times are GMT -4. The time now is 07:08 PM.

Powered by: vBulletin
Copyright ©2000 - 3.8.5, Jelsoft Enterprises Ltd.
MobileRead.com is a privately owned, operated and funded community.