06-22-2020, 11:32 AM | #646 |
Grand Sorcerer
Posts: 5,635
Karma: 23191067
Join Date: Dec 2010
Device: Kindle PW2
|
The following expression should do the trick:
Find:<span class="Cap">(.)</span><span class="SmallCap">(.*?)</span> Replace:\1\L\2\E |
06-22-2020, 01:02 PM | #647 | |
Groupie
Posts: 166
Karma: 91148
Join Date: Jun 2010
Device: Sony 350
|
Quote:
Example: <span class="Cap">F</span><span class="SmallCap">IRST WORD OF THE SENTENCE IS ALWAYS CAPITALISED,</span> <span class="Cap">O</span><span class="SmallCap">THER</span> <span class="Cap">W</span><span class="SmallCap">WORDS IN THE SENTENCE MAY OR MAY NOT BE CAPITALISED</span> Desired result : First word of the sentence is always capitalised, Other Words in the sentence may or may not be capitalised. Actual result: First word of the sentence is always capitalised,</span> <span class="cap">o</span><span class="smallcap">ther</span> <span class="cap">w</span><span class="smallcap">words in the sentence may or may not be capitalised So I have lost some capital letters I want to preserve, and also for some reason even if I insert the cursor before the next opening tag of a complete pair, no other matches are found, and the code is broken. I have not found any way to fix this other than doing it by hand, I really don't know if it's possible to do it with regex to be honest*. Edit: *I should say, except by doing it in several passes, first <span class="Cap">(.)</span><span class="SmallCap">(.*?)</span> <span class="Cap">(.)</span><span class="SmallCap">(.*?)</span> <span class="Cap">(.)</span><span class="SmallCap">(.*?)</span> replace \1\L\2\E \3\L\4\E \5\L\6\E Then <span class="Cap">(.)</span><span class="SmallCap">(.*?)</span> <span class="Cap">(.)</span><span class="SmallCap">(.*?)</span> \1\L\2\E \3\L\4\E Then just one pair. But I'm interested to know if there is a way to manage all the cases in just one pass, in case you don't know in advance how many sets of spans there might be. Last edited by Mister L; 06-22-2020 at 01:29 PM. |
|
Advert | |
|
06-22-2020, 01:49 PM | #648 | |
Grand Sorcerer
Posts: 5,635
Karma: 23191067
Join Date: Dec 2010
Device: Kindle PW2
|
Quote:
Find:<span class="Cap">(.)</span><span class="SmallCap">(.*?)</span> Replace:\1\L\2\E I'm getting: First word of the sentence is always capitalised, Other Wwords in the sentence may or may not be capitalised (None of the Regex options are checked.) You'll need to uncheck the Minimal Match option. |
|
06-22-2020, 03:53 PM | #649 | |
Groupie
Posts: 166
Karma: 91148
Join Date: Jun 2010
Device: Sony 350
|
Quote:
Well thanks very much for that tip, in future I will experiment more with minimal match and see when it is helpful and when not. |
|
06-22-2020, 04:54 PM | #650 | ||
Grand Sorcerer
Posts: 5,635
Karma: 23191067
Join Date: Dec 2010
Device: Kindle PW2
|
Quote:
From the PCRE documentation: Quote:
|
||
Advert | |
|
06-22-2020, 07:34 PM | #651 | |
Groupie
Posts: 166
Karma: 91148
Join Date: Jun 2010
Device: Sony 350
|
Quote:
My original question was about fixing chapter headings to make it possible to easily regenerate a TOC, as I recently had a file with chapter headings in this format. To get back to that, is it possible to keep the original text as is, but copy the modified text into a title attribute, bearing in mind there can be a variable number of sets of spans in the title? I know how to do it with only one set, as I said, but except if I do it in multiple passes (3 sets then 2 sets then 1 set) I don't think this pattern works. For instance, a heading (that may or may not have a class and may or may not have an ID), and the text is in fake small-caps. Some titles may have one or more capitalised words in the middle but not all of them. I want to add the title attribute in sentence case. Find: <h1 class="chapter" id="id01"><span class="Cap">F</span><span class="SmallCap">IRST WORD OF THE SENTENCE IS ALWAYS CAPITALISED</span></h1> But also: <h1 class="chapter" id="id01"><span class="Cap">F</span><span class="SmallCap">IRST WORD OF THE SENTENCE IS ALWAYS CAPITALISED,OTHER</span> <span class="Cap">W</span><span class="SmallCap">ORDS IN THE SENTENCE MAY OR MAY NOT BE CAPITALISED</span></h1> And also: <h1 class="chapter" id="id01"><span class="Cap">F</span><span class="SmallCap">IRST WORD OF THE SENTENCE IS ALWAYS CAPITALISED,</span> <span class="Cap">O</span><span class="SmallCap">THER</span> <span class="Cap">W</span><span class="SmallCap">ORDS IN THE SENTENCE MAY OR MAY NOT BE CAPITALISED</span></h1> etc. Replace: <h1 class="chapter" id="id01" title="First word of the sentence is always capitalised, Other Words in the sentence may or may not be capitalised"><span class="Cap">F</span><span class="SmallCap">IRST WORD OF THE SENTENCE IS ALWAYS CAPITALISED,</span> <span class="Cap">O</span><span class="SmallCap">THER</span> <span class="Cap">W</span><span class="SmallCap">ORDS IN THE SENTENCE MAY OR MAY NOT BE CAPITALISED</span></h1> (and other variations with zero or more capitalised words in the middle of the title) I usually use some variation of this: Search: <h1 class="chapter" id="(.*)"><span class="Cap">(.)</span><span class="SmallCap">(.*)</span></h1> Replace: <h1 class="chapter" id="\1" title="\2\L\3\E"><span class="Cap">\2</span><span class="SmallCap">\3</span></h1> It works if there is only one set of spans. If there is more than one set I have to multiply the search variables and the replace variables so there are the same number of sets of each, for iterations of the same search, or correct them by hand one by one as I go along. |
|
07-25-2020, 06:06 AM | #652 |
Enthusiast
Posts: 37
Karma: 10
Join Date: May 2012
Device: android
|
Hi! I have chapters without titles and would like to add, to each one of them, their file names in the body text... is this regexable? Thanks!
L Last edited by luciaisacat; 07-25-2020 at 06:35 AM. |
07-25-2020, 08:20 AM | #653 | |
A Hairy Wizard
Posts: 3,166
Karma: 18843349
Join Date: Dec 2012
Location: Charleston, SC today
Device: iPhone 15/11/X/6/iPad 1,2,Air & Air Pro/Surface Pro/Kindle PW & Fire
|
Quote:
We have to have some kind of example to help you. Can you copy/paste a section of code that we can look at? |
|
07-25-2020, 08:32 AM | #654 |
Enthusiast
Posts: 37
Karma: 10
Join Date: May 2012
Device: android
|
Sure, thanks for helping....
The recurrent code for each chapter/file is the following : <head> My idea would be to Find/Replace in regex mode: <h2>Chapter</h2> with <h2><filename></h2> .... do you know if there is a regex code for <filename>?[....] </head> <body> <h2>Chapter</h2> Thanks Last edited by luciaisacat; 07-25-2020 at 09:33 AM. |
07-25-2020, 09:27 AM | #655 |
Grand Sorcerer
Posts: 27,872
Karma: 198099188
Join Date: Jan 2010
Device: Nexus 7, Kindle Fire HD
|
Unfortunately, I don't think what you want is possible. Not with Sigil's built-in regex Search & Replace feature anyway. Search and Replace doesn't know what the filename is. In this instance, it's merely searching the content given to it. Which is the contents of the file. The regex engine is unaware that what it's searching for (and replacing) is even part of a file. It's immaterial.
You (or someone) may be able to construct a sigil plugin that could accomplish this, but unless it's something you foresee happening a lot, developing a plugin to handle it would likely be more involved than just manually updating the chapter headers. Sorry. |
07-25-2020, 09:50 AM | #656 | |
Enthusiast
Posts: 37
Karma: 10
Join Date: May 2012
Device: android
|
OK... Thanks!
Quote:
|
|
07-25-2020, 10:10 AM | #657 |
Grand Sorcerer
Posts: 24,905
Karma: 47303822
Join Date: Jul 2011
Location: Sydney, Australia
Device: Kobo:Touch,Glo, AuraH2O, GloHD,AuraONE, ClaraHD, Libra H2O; tolinoepos
|
You should be able to do it with the calibre editor. You can create a regex-functions. One of the parameters the file name where the match is found. A really dumb function to do this is:
Code:
def replace(match, number, file_name, metadata, dictionaries, data, functions, *args, **kwargs): return match.group(0) + '<h2>' + file_name + '</h2>' Code:
(<body>) Code:
<body><h2>OEBPS/Text/Section0001.xhtml</h2> And for the record, I have another function called "Number Chapter" which can be used to number chapters across the complete book in one go. I don't remember if it a supplied function, or one mentioned in the forum that I copied. |
07-26-2020, 03:00 AM | #658 |
Enthusiast
Posts: 39
Karma: 59154
Join Date: May 2010
Location: Stuttgart, Germany
Device: Kobo H2O, PocketBook Touch HD, Tolino Vision 4
|
Hi luciaisacat,
I have played with the idea from davidfor and came up with the following solution for the calibre editor: 1. open your file in the calibre editor. 2. call search/replace, change to Mode: "regex-function", insert your search string and then click "create/edit" 3. you will see a basis function. 4. insert a name for your new Function and replace the code with: Code:
import re def replace(match, number, file_name, metadata, dictionaries, data, functions, *args, **kwargs): #call file_name and strip file path and -extension: newName=(re.search(r'.*?\/([^\.\/]*)\.[^\.]*', file_name).group(1)) newName=re.sub(r'_',r' ',newName) #strip _ newName=re.sub(r' 0',r' ',newName) #strip leading zero #replace bracket term with newName: result=re.sub(match.group(1),newName,match.group()) return result Klecks |
09-09-2020, 03:27 AM | #659 |
Wizard
Posts: 1,033
Karma: 11123121
Join Date: Mar 2013
Location: Guben, Brandenburg, Germany
Device: Kobo Clara 2E, Tolino Shine 3
|
Remove Kobo spans
I just want to contribute a bit, as I found a way to remove Kobo spans:
Search: Code:
<span class="koboSpan" id="kobo.[0-9]{1,2}([.][0-9]{1,2})?">([^<>]+)</span> Code:
\2 |
09-09-2020, 06:06 AM | #660 | |
Grand Sorcerer
Posts: 24,905
Karma: 47303822
Join Date: Jul 2011
Location: Sydney, Australia
Device: Kobo:Touch,Glo, AuraH2O, GloHD,AuraONE, ClaraHD, Libra H2O; tolinoepos
|
Quote:
|
|
|
Similar Threads | ||||
Thread | Thread Starter | Forum | Replies | Last Post |
Examples of Subgroups | emonti8384 | Lounge | 32 | 02-26-2011 06:00 PM |
Accessories Pen examples | Gunnerp245 | enTourage Archive | 15 | 02-21-2011 03:23 PM |
Stylesheet examples? | Skitzman69 | Sigil | 15 | 09-24-2010 08:24 PM |
Examples | kafkaesque1978 | iRiver Story | 1 | 07-26-2010 03:49 PM |
Looking for examples of typos in eBooks | Tonycole | General Discussions | 1 | 05-05-2010 04:23 AM |