Register Guidelines E-Books Today's Posts Search

Go Back   MobileRead Forums > E-Book Software > Sigil > Plugins

Notices

Reply
 
Thread Tools Search this Thread
Old 04-03-2022, 11:57 AM   #1
The_book
Zealot
The_book began at the beginning.
 
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.
Attached Thumbnails
Click image for larger version

Name:	image.png
Views:	1538
Size:	15.2 KB
ID:	193066  
Attached Files
File Type: zip RegexFunctionReplace_v0.1.0.zip (13.8 KB, 471 views)

Last edited by The_book; 04-03-2022 at 08:58 PM.
The_book is offline   Reply With Quote
Old 04-03-2022, 12:29 PM   #2
KevinH
Sigil Developer
KevinH ought to be getting tired of karma fortunes by now.KevinH ought to be getting tired of karma fortunes by now.KevinH ought to be getting tired of karma fortunes by now.KevinH ought to be getting tired of karma fortunes by now.KevinH ought to be getting tired of karma fortunes by now.KevinH ought to be getting tired of karma fortunes by now.KevinH ought to be getting tired of karma fortunes by now.KevinH ought to be getting tired of karma fortunes by now.KevinH ought to be getting tired of karma fortunes by now.KevinH ought to be getting tired of karma fortunes by now.KevinH ought to be getting tired of karma fortunes by now.
 
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
KevinH is offline   Reply With Quote
Advert
Old 04-03-2022, 03:16 PM   #3
DiapDealer
Grand Sorcerer
DiapDealer ought to be getting tired of karma fortunes by now.DiapDealer ought to be getting tired of karma fortunes by now.DiapDealer ought to be getting tired of karma fortunes by now.DiapDealer ought to be getting tired of karma fortunes by now.DiapDealer ought to be getting tired of karma fortunes by now.DiapDealer ought to be getting tired of karma fortunes by now.DiapDealer ought to be getting tired of karma fortunes by now.DiapDealer ought to be getting tired of karma fortunes by now.DiapDealer ought to be getting tired of karma fortunes by now.DiapDealer ought to be getting tired of karma fortunes by now.DiapDealer ought to be getting tired of karma fortunes by now.
 
DiapDealer's Avatar
 
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!
DiapDealer is offline   Reply With Quote
Old 04-04-2022, 10:29 AM   #4
KevinH
Sigil Developer
KevinH ought to be getting tired of karma fortunes by now.KevinH ought to be getting tired of karma fortunes by now.KevinH ought to be getting tired of karma fortunes by now.KevinH ought to be getting tired of karma fortunes by now.KevinH ought to be getting tired of karma fortunes by now.KevinH ought to be getting tired of karma fortunes by now.KevinH ought to be getting tired of karma fortunes by now.KevinH ought to be getting tired of karma fortunes by now.KevinH ought to be getting tired of karma fortunes by now.KevinH ought to be getting tired of karma fortunes by now.KevinH ought to be getting tired of karma fortunes by now.
 
Posts: 7,645
Karma: 5433388
Join Date: Nov 2009
Device: many
Added this plugin to the Sigil Plugin Index sticky.
KevinH is offline   Reply With Quote
Old 04-12-2022, 06:08 PM   #5
celiapgt
EBook-obsessive gal
celiapgt began at the beginning.
 
celiapgt's Avatar
 
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.
celiapgt is offline   Reply With Quote
Advert
Old 04-12-2022, 06:36 PM   #6
DNSB
Bibliophagist
DNSB ought to be getting tired of karma fortunes by now.DNSB ought to be getting tired of karma fortunes by now.DNSB ought to be getting tired of karma fortunes by now.DNSB ought to be getting tired of karma fortunes by now.DNSB ought to be getting tired of karma fortunes by now.DNSB ought to be getting tired of karma fortunes by now.DNSB ought to be getting tired of karma fortunes by now.DNSB ought to be getting tired of karma fortunes by now.DNSB ought to be getting tired of karma fortunes by now.DNSB ought to be getting tired of karma fortunes by now.DNSB ought to be getting tired of karma fortunes by now.
 
DNSB's Avatar
 
Posts: 35,464
Karma: 145525534
Join Date: Jul 2010
Location: Vancouver
Device: Kobo Sage, Forma, Clara HD, Lenovo M8 FHD, Paperwhite 4, Tolino epos
Quote:
Originally Posted by celiapgt View Post
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.
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.
DNSB is offline   Reply With Quote
Old 04-13-2022, 06:43 PM   #7
celiapgt
EBook-obsessive gal
celiapgt began at the beginning.
 
celiapgt's Avatar
 
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
function.


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
. How can I achieve this with your plugin/regex function?
celiapgt is offline   Reply With Quote
Old 04-13-2022, 07:30 PM   #8
KevinH
Sigil Developer
KevinH ought to be getting tired of karma fortunes by now.KevinH ought to be getting tired of karma fortunes by now.KevinH ought to be getting tired of karma fortunes by now.KevinH ought to be getting tired of karma fortunes by now.KevinH ought to be getting tired of karma fortunes by now.KevinH ought to be getting tired of karma fortunes by now.KevinH ought to be getting tired of karma fortunes by now.KevinH ought to be getting tired of karma fortunes by now.KevinH ought to be getting tired of karma fortunes by now.KevinH ought to be getting tired of karma fortunes by now.KevinH ought to be getting tired of karma fortunes by now.
 
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.
KevinH is offline   Reply With Quote
Old 04-14-2022, 06:05 PM   #9
celiapgt
EBook-obsessive gal
celiapgt began at the beginning.
 
celiapgt's Avatar
 
Posts: 39
Karma: 10
Join Date: Mar 2011
Location: La ciudad de la eterna primavera
Device: Kobo Clara HD
Quote:
Originally Posted by KevinH View Post
Which divs? The outer or inner?
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>
Just one tiny example will help me.

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
celiapgt is offline   Reply With Quote
Old 04-14-2022, 06:53 PM   #10
KevinH
Sigil Developer
KevinH ought to be getting tired of karma fortunes by now.KevinH ought to be getting tired of karma fortunes by now.KevinH ought to be getting tired of karma fortunes by now.KevinH ought to be getting tired of karma fortunes by now.KevinH ought to be getting tired of karma fortunes by now.KevinH ought to be getting tired of karma fortunes by now.KevinH ought to be getting tired of karma fortunes by now.KevinH ought to be getting tired of karma fortunes by now.KevinH ought to be getting tired of karma fortunes by now.KevinH ought to be getting tired of karma fortunes by now.KevinH ought to be getting tired of karma fortunes by now.
 
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.
KevinH is offline   Reply With Quote
Old 04-14-2022, 11:40 PM   #11
celiapgt
EBook-obsessive gal
celiapgt began at the beginning.
 
celiapgt's Avatar
 
Posts: 39
Karma: 10
Join Date: Mar 2011
Location: La ciudad de la eterna primavera
Device: Kobo Clara HD
Quote:
Originally Posted by KevinH View Post
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?
Yes, they are. It's OK to remove them. The epub is in Spanish, so the xml:lang attributes in those divs are unnecessary because the head section already has the appropiate tag:
Code:
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="es">
Quote:
Are there other divs in the file you do not want to be converted?
Yes, there are other divs which will remain as div.

My exact trigger must be only the outer div with an extra/unnecessary xml:lang attribute, which also has an inner plain div.

Quote:
None of the metadata or other fields matter at all.
OK


Quote:
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.
OK. I hope I explain it clearer.


Thanks again!
celiapgt is offline   Reply With Quote
Old 04-17-2022, 02:15 PM   #12
KevinH
Sigil Developer
KevinH ought to be getting tired of karma fortunes by now.KevinH ought to be getting tired of karma fortunes by now.KevinH ought to be getting tired of karma fortunes by now.KevinH ought to be getting tired of karma fortunes by now.KevinH ought to be getting tired of karma fortunes by now.KevinH ought to be getting tired of karma fortunes by now.KevinH ought to be getting tired of karma fortunes by now.KevinH ought to be getting tired of karma fortunes by now.KevinH ought to be getting tired of karma fortunes by now.KevinH ought to be getting tired of karma fortunes by now.KevinH ought to be getting tired of karma fortunes by now.
 
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.
KevinH is offline   Reply With Quote
Old 04-19-2022, 12:11 AM   #13
celiapgt
EBook-obsessive gal
celiapgt began at the beginning.
 
celiapgt's Avatar
 
Posts: 39
Karma: 10
Join Date: Mar 2011
Location: La ciudad de la eterna primavera
Device: Kobo Clara HD
Quote:
Originally Posted by KevinH View Post
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.
Wow, thank you! Really appreciated.

I applied the regex and it worked perfectly. There were 368 hits, so it was very helpful.

Quote:
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.
I was trying to understand how to use it in a useful manner. I see now that I was trying to kill a fly with a dremmel

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.
celiapgt is offline   Reply With Quote
Old 09-22-2022, 12:45 PM   #14
Coldturkey
Junior Member
Coldturkey began at the beginning.
 
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?
Coldturkey is offline   Reply With Quote
Old 09-22-2022, 01:09 PM   #15
DiapDealer
Grand Sorcerer
DiapDealer ought to be getting tired of karma fortunes by now.DiapDealer ought to be getting tired of karma fortunes by now.DiapDealer ought to be getting tired of karma fortunes by now.DiapDealer ought to be getting tired of karma fortunes by now.DiapDealer ought to be getting tired of karma fortunes by now.DiapDealer ought to be getting tired of karma fortunes by now.DiapDealer ought to be getting tired of karma fortunes by now.DiapDealer ought to be getting tired of karma fortunes by now.DiapDealer ought to be getting tired of karma fortunes by now.DiapDealer ought to be getting tired of karma fortunes by now.DiapDealer ought to be getting tired of karma fortunes by now.
 
DiapDealer's Avatar
 
Posts: 27,552
Karma: 193191846
Join Date: Jan 2010
Device: Nexus 7, Kindle Fire HD
Quote:
Originally Posted by Coldturkey View Post
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.
Can anyone else help?
For example can I use this for an if then else find and replace?
Being pissy about previous posts isn't likely to bring you much joy either. It's the plugin's creator who's going to need to answer your questions here. It's not the rest of the community's fault that that's not happening.

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.
DiapDealer is offline   Reply With Quote
Reply


Forum Jump

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


All times are GMT -4. The time now is 09:16 AM.


MobileRead.com is a privately owned, operated and funded community.