04-03-2022, 11:57 AM | #1 |
Zealot
Posts: 100
Karma: 10
Join Date: Aug 2019
Device: none
|
[Plugin] regex function replace plugin
Purpose
This plugin is for regex match and replace it with a function. Description You can see the appearance of this plugin in the attachment. This plugin is divided into three parts, the right side is used to display the print function output. The usefulness of the rest should be obvious at a glance. The splitter between the three parts can be pulled, and it is supported to keep only any one of the three parts. This plugin only has a button to replace all. The function of the plugin is still very rudimentary, please use it with caution. After match all files, replace function will be called use match=None Warning: I haven't edited EPUB for a long time since work (and I also prefer Markdown now in my daily life because it works better in version control and file diff). This plugin is just an implementation of one of my suggestions. I had suggested this before and KevinH suggested that I implement it as a plugin. Although KevinH listed this feature as the first item on the to-do list, it can be seen that they think the built-in implementation of this feature is complicated. Some time ago I planned to familiarize myself with Qt, so I decided to combine this plan with developing a Sigil plugin, developing a plugin with PyQt. So I developed this. This plugin is very simple. The editor pane I prepared has no code highlighting and automatic indentation. The principle of code implementation is just a simple exec function. And this plugin only process files in manifest. And, because I haven't done EPUB related work for a long time, this plugin has only done some basic tests, and has not tested the effect of actual editing. All kinds of questions are welcome, but there is no guarantee that they will be handled and resolved. This plugin is released on Github under the GPLv3 license, anyone can develop new plugins based on my plugin. Last edited by The_book; 04-03-2022 at 08:58 PM. |
04-03-2022, 12:29 PM | #2 |
Sigil Developer
Posts: 7,645
Karma: 5433388
Join Date: Nov 2009
Device: many
|
Very Nicely done!
I will add this to the Plugin Index tomorrow when I am back at my Desktop machine unless someone beats me to it. Thank you! KevinH |
Advert | |
|
04-03-2022, 03:16 PM | #3 |
Grand Sorcerer
Posts: 27,552
Karma: 193191846
Join Date: Jan 2010
Device: Nexus 7, Kindle Fire HD
|
I wish I had known someone was working on a Qt plugin. I'd have mentioned the future switch to PySide6. Adjusting it work for both shouldn't prove to difficult, though. Guess it's time to make some prominent announcements concerning the future changes!
|
04-04-2022, 10:29 AM | #4 |
Sigil Developer
Posts: 7,645
Karma: 5433388
Join Date: Nov 2009
Device: many
|
Added this plugin to the Sigil Plugin Index sticky.
|
04-12-2022, 06:08 PM | #5 |
EBook-obsessive gal
Posts: 39
Karma: 10
Join Date: Mar 2011
Location: La ciudad de la eterna primavera
Device: Kobo Clara HD
|
Hey, The_book, great idea of this plugin! Thanks.
Could you please post an example on how to write the regex function so it works with Current File. Please do it for not-so-savvy Python learners. |
Advert | |
|
04-12-2022, 06:36 PM | #6 |
Bibliophagist
Posts: 35,464
Karma: 145525534
Join Date: Jul 2010
Location: Vancouver
Device: Kobo Sage, Forma, Clara HD, Lenovo M8 FHD, Paperwhite 4, Tolino epos
|
Looking at the image in the original post and it's Mode box, you could either select a single HTML or CSS file and then choose the selected HTML files or Selected CSS files option from the Mode dropdown menu.
|
04-13-2022, 06:43 PM | #7 |
EBook-obsessive gal
Posts: 39
Karma: 10
Join Date: Mar 2011
Location: La ciudad de la eterna primavera
Device: Kobo Clara HD
|
What I mean is how to use the regex
Code:
replace With a typical example, I can deduce how to use it. For example, I have this code in my epub: Code:
<div xml:lang="es-MX"> <div> <h3 class="fecha">3 de enero</h3> <p class="capitulo">Muéstrate implacable con las cosas que no importan</p> </div> </div> And I want to remove the divs and substitute them with a Code:
blockquote |
04-13-2022, 07:30 PM | #8 |
Sigil Developer
Posts: 7,645
Karma: 5433388
Join Date: Nov 2009
Device: many
|
Just like normal regex find and replace you need to provide more context.
Which divs? The outer or inner? How else are div's used throughout the document? All of this is important to understanding the regex to find the divs and how to determine which should be replaced with blockquotes and which should not. |
04-14-2022, 06:05 PM | #9 |
EBook-obsessive gal
Posts: 39
Karma: 10
Join Date: Mar 2011
Location: La ciudad de la eterna primavera
Device: Kobo Clara HD
|
Hi, KevinH.
Sorry to be unclear. I want to remove both divs, inner and outer. I want to go from the code you see to that below. I'm trying to understand the utility of the plugin. It is not clear to me. Code:
<blockquote> <h3 class="fecha">3 de enero</h3> <p class="capitulo">Muéstrate implacable con las cosas que no importan</p> </blockquote> For the replace function, what number do I use? The match means the regex, and it should be within single or double quotes? Which metadata do I have to use? Can I use somehow "current_file" so I can make a more general function or do I put capitulo.xhtml as the filename? Which is the data that I have to put in the function? Once the function is defined, then I suppose I have to click on New to save the function. Then, will I copy/paste the code I want to find/replace in the Find field anf finally hit Replace All button? Those are the things that I want to understand with one example. Thanks for taking the time to answer me ;D |
04-14-2022, 06:53 PM | #10 |
Sigil Developer
Posts: 7,645
Karma: 5433388
Join Date: Nov 2009
Device: many
|
Then you will lose the xml:lang attribute by doing so. Are all divs with that spanish language attribute the ones you want to be converted?
Are there other divs in the file you do not want to be converted? Your request for an example makes no sense until you can specify the exact trigger that indicates which divs in the document need to be converted. None of the metadata or other fields matter at all. A regex to find all nested divs and capture the inner div is possible as is a function to determine what the replacement is to be made but without knowing if it it is safe to do that for any set of nested divs in the document, it is all meaningless. |
04-14-2022, 11:40 PM | #11 | ||||
EBook-obsessive gal
Posts: 39
Karma: 10
Join Date: Mar 2011
Location: La ciudad de la eterna primavera
Device: Kobo Clara HD
|
Quote:
Code:
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="es"> Quote:
My exact trigger must be only the outer div with an extra/unnecessary xml:lang attribute, which also has an inner plain div. Quote:
Quote:
Thanks again! |
||||
04-17-2022, 02:15 PM | #12 |
Sigil Developer
Posts: 7,645
Karma: 5433388
Join Date: Nov 2009
Device: many
|
You do not need actual regex "function replace" to handle what you wanted but you could use it.
If you use just normal Sigil regex find replace, then something like the following should handle it: Find: <div\s*xml:lang=\"es\-MX\">\s*<div>(\s*.*)</div>\s*</div> Replace: <blockquote>\1</blockquote> And make sure you have Dot All and Minimal Match options checked. If you wanted to use the function replace capability then using the same Find value and flags, and then in the replace function you could use a xhtml parser to parse the captured snippet and change it to whatever you want. That is the power of function replacement. |
04-19-2022, 12:11 AM | #13 | ||
EBook-obsessive gal
Posts: 39
Karma: 10
Join Date: Mar 2011
Location: La ciudad de la eterna primavera
Device: Kobo Clara HD
|
Quote:
I applied the regex and it worked perfectly. There were 368 hits, so it was very helpful. Quote:
This plugin looks very powerful, but this case was not the optimal example to apply it. Thanks for your patience with a noob in this matter. |
||
09-22-2022, 12:45 PM | #14 |
Junior Member
Posts: 6
Karma: 10
Join Date: Mar 2018
Device: IPad mini
|
So instead of providing an example of how to use it KevinH asked for a specific problem and then provided a solution without using the plugin.
Way to be helpful without being helpful. Now we still don't know how to best use the plugin. Can anyone else help? For example can I use this for an if then else find and replace? |
09-22-2022, 01:09 PM | #15 | |
Grand Sorcerer
Posts: 27,552
Karma: 193191846
Join Date: Jan 2010
Device: Nexus 7, Kindle Fire HD
|
Quote:
They as much as said, "Here you go. Use my code as a basis for other plugins if you like." Besides... KevinH provided both a solution without using the plugin AND a roadmap for doing the same with a replacement function via the plugin. |
|
|
Similar Threads | ||||
Thread | Thread Starter | Forum | Replies | Last Post |
[GUI Plugin] Mass Search/Replace | un_pogaz | Plugins | 34 | 10-08-2023 09:55 AM |
[Plugin] ReworkChapterHeads - Add, replace and reformat chapter headings | slowsmile | Plugins | 87 | 08-03-2023 05:04 AM |
Editor plugin : problem with regex and special characters | EbookMakers | Development | 4 | 11-14-2019 07:52 AM |
[Plugin] QuickPrefsEdit - Edit plugin prefs json files. | slowsmile | Plugins | 3 | 07-25-2018 08:14 PM |
Goodread Perception Expander plugin not shown on plugin list (kobo h2o) | www | KOReader | 4 | 09-28-2017 10:34 AM |