MobileRead Forums

MobileRead Forums (https://www.mobileread.com/forums/index.php)
-   Sigil (https://www.mobileread.com/forums/forumdisplay.php?f=203)
-   -   A regex question (https://www.mobileread.com/forums/showthread.php?t=176705)

PatNY 04-27-2012 09:58 AM

A regex question
 
I have hundreds of subtitles throughout a cookbook, and would like to know if there is a way to do a search and replace to give all these titles an h3 designation:

Quote:

<p class="calibre8" id="filepos170176"><span class="calibre6 bold">Spicy Escarole with Croutons and Eggs</span></p>
Of course, the filepos information changes in every one, as does the actual text between the tags.

I only have a rudimentary understanding of regex and just tried a few things, but nothing would work. Any ideas?

Perkin 04-27-2012 10:38 AM

If all the subtitles are <p class="calibre8" id="filepos######"><span class="calibre6 bold">
With other paragraphs not using those.

If they are you should be good to use this for search (regex mode)

Code:

<p class="calibre8" id="filepos\d+"><span class="calibre6 bold">(.+?)</span></p>
Replace with this

Code:

<h3>\1</h3>
If not we'll need more details of what the defining part is that means that it's the subtitle and if any other paragraphs use that as well.

DiapDealer 04-27-2012 10:52 AM

Quote:

Originally Posted by Perkin (Post 2059650)
If they are you should be good to use this for search (regex mode)

Code:

<p class="calibre8" id="filepos\d+"><span class="calibre6 bold">(.+?)</span></p>
Replace with this

Code:

<h3>\1</h3>

You'll want to be careful there are no nested spans tags inside the <span class="calibre6 bold"> element. Given how those sections are already being used as headers, that would probably be pretty rare, but it could result in a bit of mess if that regex came upon something like:
Code:

<p class="calibre8" id="filepos170176"><span class="calibre6 bold"><span class="italic">Spicy Escarole with Croutons and Eggs</span></span></p>

Perkin 04-27-2012 11:27 AM

Shoud be fairly safe though as I was doing opening p+span, with matching closing span+p

Would result in italic span being inserted in the header tag, while not necessarily wanted, also wouldn't be illegal etc..

PatNY 04-27-2012 03:43 PM

Perkin, thanks a million for your help. Your solution worked perfectly.

Kudos to all you regex experts. I've tried to wrap my head around this but I can never quite grasp it. Or I grasp the basic principles, then after a few months I've completely forgotten it as I use regex so infrequently!

PatNY 04-27-2012 05:45 PM

While there are some regex experts here, two more questions:

1) Is it possible to change all-caps text in a toc.ncx file to initial caps? For example I would like to change: <text>ICE CREAM</text> to <text>Ice Cream</text>

2) Is it possible to have some of the items in the metadata TOC underlined or italicized? I get the feeling this is not possible but I am not sure.

theducks 04-27-2012 06:14 PM

Quote:

Originally Posted by PatNY (Post 2060247)
While there are some regex experts here, two more questions:

1) Is it possible to change all-caps text in a toc.ncx file to initial caps? For example I would like to change: <text>ICE CREAM</text> to <text>Ice Cream</text>

2) Is it possible to have some of the items in the metadata TOC underlined or italicized? I get the feeling this is not possible but I am not sure.

Not your answer, but is your TOC being generated by Sigil from Headers?
<h3>ICE CREAM</h3>

you could use:
<h3 title="Ice Cream">ICE CREAM</h3>

PatNY 04-27-2012 06:45 PM

Quote:

Originally Posted by theducks (Post 2060296)
Not your answer, but is your TOC being generated by Sigil from Headers?
<h3>ICE CREAM</h3>

you could use:
<h3 title="Ice Cream">ICE CREAM</h3>

Hi ducks. Yes, the TOC.ncx is being auto-generated by heads in the epub. I really want to keep the text in the html files the way it is. If the headline in the epub pages is all caps, I'd like to keep it that way. In the metadata TOC, however, the density of all caps entries can be hard to read. I would like to know if one can use regex on the TOC.ncx file to turn the all caps into initial caps words.

theducks 04-27-2012 06:56 PM

Quote:

Originally Posted by PatNY (Post 2060335)
Hi ducks. Yes, the TOC.ncx is being auto-generated by heads in the epub. I really want to keep the text in the html files the way it is. If the headline in the epub pages is all caps, I'd like to keep it that way. In the metadata TOC, however, the density of all caps entries can be hard to read. I would like to know if one can use regex on the TOC.ncx file to turn the all caps into initial caps words.


I don't know about the Regex, but if you look close at my example you will see that title= overrides what is between the H3 tags, but leaves what is on the Page alone.
<h3 title="Ice Cream with topping">ICE CREAM</h3>
in this version Sigil would create a TOC entry: Ice Cream with topping

But the page would show plain Vanilla :p

theducks 04-27-2012 07:01 PM

@ PatNY
Take a look at this page for Ideas: http://vim.wikia.com/wiki/Changing_c...ar_expressions

theducks 04-27-2012 07:25 PM

This works in the Body for 2 words

Code:

<h3>(([A-Z])(.+) ([A-Z])(.+))</h3>

<h3 title="\2\L\3\E \4\L\5\E">\2\3 \4\5 </h3>

The Result is: <h3 title="Ice Cream">ICE CREAM </h3>

:thumbsup:

PatNY 04-27-2012 07:59 PM

Quote:

Originally Posted by theducks (Post 2060380)
This works in the Body for 2 words

Code:

<h3>(([A-Z])(.+) ([A-Z])(.+))</h3>

<h3 title="\2\L\3\E \4\L\5\E">\2\3 \4\5 </h3>

The Result is: <h3 title="Ice Cream">ICE CREAM </h3>

:thumbsup:

So you seem to have hit upon an answer, but how do I apply it to the TOC.ncx while I am editing it?

IOW, how do I turn "<text>ICE CREAM</text>" into "<text>Ice Cream</text>" :blink:

PatNY 04-27-2012 08:12 PM

OK, ducks, I tried to adapt your formula to the specific issue I had by using this:

Find:
<text>(([A-Z])(.+) ([A-Z])(.+))</text>

Replace:
<text>\2\L\3\E \4\L\5\E</text>

And it's mostly working. However If I have more than two words in the title, then only the first and last words get the initial cap. The words in the middle are all lower case.

So, for example, your solution will result in:

<text>Ice cream Rocks</text> instead of <text>Ice Cream Rocks</text>

So, do you know how to get every word in the title to be initial caps?

capidamonte 04-27-2012 08:17 PM

He's telling you to change the code in the book body, in the HTML, not in the toc.ncx. Sigil will generate the toc.ncx entries for you, using the "title" attribute instead of the content between the header tags.

PatNY 04-27-2012 09:14 PM

Quote:

Originally Posted by capidamonte (Post 2060419)
He's telling you to change the code in the book body, in the HTML, not in the toc.ncx. Sigil will generate the toc.ncx entries for you, using the "title" attribute instead of the content between the header tags.

Yes, I know he's doing that. However, there are various reasons why I want to do it in the TOC.ncx.

First off, I only need to change the case of words in the TOC.ncx, so I don't see the need to alter other files. I want to make as few changes as possible in the html files. That way if anything goes wrong, it's easier to identify the problem and correct it if it's limited to just the one toc.ncx file rather than having to examine and sift through potentially hundreds of pages in html files.

Second, going forward, there could be literally dozens of permutations in the variety of the tags surrounding a title in the html files. That would mean the exact formula might have to change every time, depending on what the set of tags was. I would have a hard time adjusting that formula every time.

However, the "<text>WORDS GO HERE</text>" tag construction in the TOC.ncx file is the same all the time. So one formula should solve the problem, every time no matter what.

Ducks got me halfway there. I can change all-caps titles into initial-caps words in the TOC, but it doesn't work for all the words in a title -- just the first and last!


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

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