View Single Post
Old 02-26-2018, 11:38 AM   #358
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: 8,911
Karma: 6120478
Join Date: Nov 2009
Device: many
Sample edit plugin to work with epubcfis

Hi Doitsu,

I have thrown together a static epubcfi conversion to line, col, and utf-8 file offset edit plugin.

See the attached: cfitest_v010.zip

It has two main support files: cficonvert.py and cfiparse.py

The cfiparse.py is basically a modified version of Kovid's cfi parsing code, changed to work with Python 3 and to add some features to make my life easier.

The cficonvert.py is the conversion routine that takes in a text epubcfi and book container object bk and returns the target filename, line number, column number and file offset into a utf-8 encoded version of the file. It uses the gumbo/bs4 parser to do the work.

Since epubcfis are specific to an ebook and its contents, I have created a test epub to work with a specific set of test epubcfis.

See the attached cfitest.epub

It is based on the following idpf example (loosely):
http://www.idpf.org/epub/linking/cfi/epub-cfi.html

In order to exercise the plugin it needs a list of valid epubcfis to work from. When the plugin is launched with that ebook, it will ask you to navigate to select an epubcfi text file with the epubcfis to test one per line:

Here is the attached cfis.txt to use for testing cfitest.epub with:
Code:
epubcfi(/6/4[chap01ref]!/4[body01]/10[para05]/3:8)
epubcfi(/6/4[chap01ref]!/4[body01]/16[svgimg])
epubcfi(/6/4[chap01ref]!/4[body01]/10[para05]/1:0)
epubcfi(/6/4[chap01ref]!/4[body01]/10[para05]/2/1:0)
epubcfi(/6/4[chap01ref]!/4[body01]/10[para05]/2/1:3)
epubcfi(/6/4[chap01ref]!/4[body01]/20/5:5)
And here the attached cfis_answers.txt you can use to check the results of the cfi conversion when it is run:
Code:
epubcfi(/6/4[chap01ref]!/4[body01]/10[para05]/3:8)
refers to the position right before the digit 8 in the paragraph with the ID para05. When producing CFIs for text locations, unless the text is defined by an img element's alt tag, one should always start with the reference to the (possibly-empty) chunk of XML character data that corresponds to the location and then trace the ancestor and reference chain to the Package Document root.

The following examples show how EPUB CFIs can be constructed to reference additional content locations.

Reference to the img element.
epubcfi(/6/4[chap01ref]!/4[body01]/16[svgimg])

Reference to the location just before xxx.
epubcfi(/6/4[chap01ref]!/4[body01]/10[para05]/1:0)

Reference to the location just before yyy.
epubcfi(/6/4[chap01ref]!/4[body01]/10[para05]/2/1:0)

Reference to the location just after yyy.
epubcfi(/6/4[chap01ref]!/4[body01]/10[para05]/2/1:3)

Reference to the text node with "text here", tests text node step numbering without empty text nodes.
epubcfi(/6/4[chap01ref]!/4[body01]/20/5:5)
And here is what chapter01.xhtml looks like inside the test epub:
Code:
<?xml version="1.0"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
  "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    	<title>…</title>
    </head>
    
    <body id="body01">
    	<p>…</p>
    	<p>…</p>
    	<p>…</p>
    	<p>…</p>
        <p id="para05">xxx<em>yyy</em>0123456789</p>
    	<p>…</p>
    	<p>…</p>
    	<img id="svgimg" src="foo.svg" alt="…"/>
    	<p>…</p>
    	<p><span>hello</span><span>goodbye</span>text here<em>adieu</em>text there</p>
    </body>
</html>
So please give that a try and see if you can use it to adapt your ACE Checker which outputs cfis to work with Sigil validation plugins.

FYI: This is very rough work thrown together quickly so there will most likely be problems that will have to be addressed!

Please let me know what troubles you run into.

Hope this helps.

KevinH

Update: I have attached an updated version of this plugin called cfitest_v020.zip that runs with Sigil 2.0.0 or later and replaces the tk gui interface with a Qt PySide6 gui.
Attached Files
File Type: zip cfitest_v010.zip (7.3 KB, 850 views)
File Type: txt cfis.txt (298 Bytes, 659 views)
File Type: txt cfis_answers.txt (1.0 KB, 649 views)
File Type: epub cfitest.epub (2.8 KB, 792 views)
File Type: zip cfitest_v020.zip (10.3 KB, 9 views)

Last edited by KevinH; 05-28-2025 at 06:24 PM.
KevinH is offline   Reply With Quote