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 12-23-2024, 08:38 PM   #556
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: 28,558
Karma: 204127028
Join Date: Jan 2010
Device: Nexus 7, Kindle Fire HD
What build cycle do you need to go through with a Python plugin?
DiapDealer is offline   Reply With Quote
Old 12-24-2024, 03:16 PM   #557
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,749
Karma: 5706256
Join Date: Nov 2009
Device: many
That would work but the Sigil.cfg would have to match the current Sigil, dll locations, etc and unzipped epub (to select html files), but other than that the interface was designed to be run in a subprocess and results passed back in xml and stored updated files.

Not sure about any "build cycle" as this is python which can be run as is.

I personally find that logic errors are better found by studying the source (a follower of Linus's many eyes club) and normally stay far away from debuggers since python generates its own traceback stacks), but to each his or her own.

Glad you have something that works for you.
KevinH is offline   Reply With Quote
Old 01-01-2025, 12:34 AM   #558
jwes
Connoisseur
jwes began at the beginning.
 
Posts: 74
Karma: 10
Join Date: Jul 2023
Device: none
How does Sigil check the validity of the html when editing and can I use it in a plugin? I am trying to figure out what to do if an user edit results in invalid html.
jwes is offline   Reply With Quote
Old 01-01-2025, 03:06 AM   #559
Doitsu
Grand Sorcerer
Doitsu ought to be getting tired of karma fortunes by now.Doitsu ought to be getting tired of karma fortunes by now.Doitsu ought to be getting tired of karma fortunes by now.Doitsu ought to be getting tired of karma fortunes by now.Doitsu ought to be getting tired of karma fortunes by now.Doitsu ought to be getting tired of karma fortunes by now.Doitsu ought to be getting tired of karma fortunes by now.Doitsu ought to be getting tired of karma fortunes by now.Doitsu ought to be getting tired of karma fortunes by now.Doitsu ought to be getting tired of karma fortunes by now.Doitsu ought to be getting tired of karma fortunes by now.
 
Doitsu's Avatar
 
Posts: 5,725
Karma: 24031401
Join Date: Dec 2010
Device: Kindle PW2
Quote:
Originally Posted by jwes View Post
How does Sigil check the validity of the html when editing and can I use it in a plugin? I am trying to figure out what to do if an user edit results in invalid html.
It's automatically handled by the plugin handler code. If your plugin tries to write invalid html code to a file, Sigil will display a warning dialog box.
Doitsu is offline   Reply With Quote
Old 01-01-2025, 08:49 AM   #560
jwes
Connoisseur
jwes began at the beginning.
 
Posts: 74
Karma: 10
Join Date: Jul 2023
Device: none
Quote:
Originally Posted by Doitsu View Post
It's automatically handled by the plugin handler code. If your plugin tries to write invalid html code to a file, Sigil will display a warning dialog box.
I'd like to catch it when it happens like Sigil does and not when exiting the plugin.
jwes is offline   Reply With Quote
Old 01-01-2025, 09:06 AM   #561
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,749
Karma: 5706256
Join Date: Nov 2009
Device: many
If you load your Preview clone correctly and provide the proper xhtml media type, your Preview clone should be showing all xhtml parsing errors.

Otherwise you have to pass the source through an error detecting xml parser.
KevinH is offline   Reply With Quote
Old 01-02-2025, 12:44 AM   #562
jwes
Connoisseur
jwes began at the beginning.
 
Posts: 74
Karma: 10
Join Date: Jul 2023
Device: none
Quote:
Originally Posted by KevinH View Post
If you load your Preview clone correctly and provide the proper xhtml media type, your Preview clone should be showing all xhtml parsing errors.

Otherwise you have to pass the source through an error detecting xml parser.
I'm taking the defaults. If there is some parameter I need to set, I haven't found it. The code is: self.webview.setHtml(self.textview.toPlainText()).
on_load_finished receives OK. What, if anything, do I need to put in baseUrl?
jwes is offline   Reply With Quote
Old 01-02-2025, 08:25 AM   #563
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,749
Karma: 5706256
Join Date: Nov 2009
Device: many
Do *not* use setHtml as you are loading xhtml which is a different specification with very different parsing rules that are much less forgiving than the html spaghetti.

Use:

void setContent(const QByteArray &data, const QString &mimeType = QString(), const QUrl &baseUrl = QUrl())


The baseURL is the path to the file as a QUrl, and data holds the contents of the file in bytes (ie. a bytes buffer that you read the entire file into), and of course the proper mimetype: = "application/xhtml+xml"

See the QtWebEngineView online docs:

https://doc.qt.io/qt-6/qwebengineview.html

And for a python example see the calibre source code here:

https://github.com/kovidgoyal/calibr...lay/webview.py

Code:
    if force_as_html or load_as_html(html):
        view.setHtml(html, loading_url)
    else:
        view.setContent(QByteArray(html.encode(codec)), mime_type,
                loading_url)
        mf = view.page().mainFrame()
        elem = mf.findFirstElement('parsererror')

Last edited by KevinH; 01-02-2025 at 10:54 AM.
KevinH is offline   Reply With Quote
Old 01-03-2025, 03:08 AM   #564
jwes
Connoisseur
jwes began at the beginning.
 
Posts: 74
Karma: 10
Join Date: Jul 2023
Device: none
Quote:
Originally Posted by KevinH View Post
Do *not* use setHtml as you are loading xhtml which is a different specification with very different parsing rules that are much less forgiving than the html spaghetti.

Use:

void setContent(const QByteArray &data, const QString &mimeType = QString(), const QUrl &baseUrl = QUrl())


The baseURL is the path to the file as a QUrl, and data holds the contents of the file in bytes (ie. a bytes buffer that you read the entire file into), and of course the proper mimetype: = "application/xhtml+xml"

See the QtWebEngineView online docs:

https://doc.qt.io/qt-6/qwebengineview.html

And for a python example see the calibre source code here:

https://github.com/kovidgoyal/calibr...lay/webview.py

Code:
    if force_as_html or load_as_html(html):
        view.setHtml(html, loading_url)
    else:
        view.setContent(QByteArray(html.encode(codec)), mime_type,
                loading_url)
        mf = view.page().mainFrame()
        elem = mf.findFirstElement('parsererror')
Thank you. setContent helped and that python code shows how to tell if there is an html error. Unfortunately page().mainFrame() was in Webkit and was reintroduced in Qt 6.8, so I guess I have to use an xml parser to check for parsererror.

I also am having a problem with using the stylesheet and am guessing it is a problem with the QUrl basepath. Which bookpath functions do I use to get the basepath? id_to_bookpath(current_fileid) doesn't seem to work.
jwes is offline   Reply With Quote
Old 01-03-2025, 09:07 AM   #565
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,749
Karma: 5706256
Join Date: Nov 2009
Device: many
A bookpath is a path from the root folder of the epub to that particular file. To link in css from any one xhtml file you need to calculate the relative path from the folder holding the xhtml file to your css file so you can create the proper link. There are a number of utility routines available to python plugins on Sigil to help you do that.

From the Sigil plugin interface code see bookcontainer.py:

Code:
    # returns the href relative path from source bookpath to target bookpath
    def get_relativepath(self, from_bookpath, to_bookpath):
        return self._w.get_relativepath(from_bookpath, to_bookpath)
There are also routines required to uuencode and uudecode path elements properly and many other things.
KevinH is offline   Reply With Quote
Old 01-03-2025, 09:29 AM   #566
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,749
Karma: 5706256
Join Date: Nov 2009
Device: many
And by the way, you can use Qt callbacks to extract the code of the page being viewed in case you want to load exactly what is in your Preview clone into a validator.

That said, once you setContent and specify xhtml if there is a parsing error, the error message should show up in your Preview clone as it does in Sigil. If that is not happening then something is not set propertly. Look up how QtWebEngineProfiles are used to set settings used by QtWebEngineViews. Defaults are rarely what you want.
KevinH is offline   Reply With Quote
Old 01-03-2025, 10:04 AM   #567
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,749
Karma: 5706256
Join Date: Nov 2009
Device: many
As for base url, I prepend the path of the stored epub to the bookpath (properly handling url encode and decode if needed) to create an absolute file path to the xhtml file in question then use Qt's routine to create a url from the absolute file path.

Last edited by KevinH; 01-03-2025 at 11:07 AM.
KevinH is offline   Reply With Quote
Old 01-03-2025, 05:41 PM   #568
jwes
Connoisseur
jwes began at the beginning.
 
Posts: 74
Karma: 10
Join Date: Jul 2023
Device: none
Quote:
Originally Posted by KevinH View Post
As for base url, I prepend the path of the stored epub to the bookpath (properly handling url encode and decode if needed) to create an absolute file path to the xhtml file in question then use Qt's routine to create a url from the absolute file path.
As far as I can tell, I need basedir from wrapper.py, which is ebook_root or outdir if the file has been written. I don't see a function to get that.
jwes is offline   Reply With Quote
Old 01-03-2025, 06:26 PM   #569
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,749
Karma: 5706256
Join Date: Nov 2009
Device: many
If you want full control check out bookcontainer.py for the copy epub to folder routine. You specify the folder full path and can get actual file access to a full copy of all the epub's files.

If you never directly write to it, you can cheat and do something like:

bk._w.ebook_root

to access the actual internal-to-Sigil, ebook root path but if you write directly to there without using the proper plugin interface you will crash Sigil.

Sigil plugins use a "copy and modify on write approach" built into the bookcontainer.py and wrapper.py routines to protect Sigil.

So if a file is modified remember to only write its new contents using the proper plugin interface so that it is properly registered as being modified and stored away.

Last edited by KevinH; 01-03-2025 at 07:34 PM.
KevinH is offline   Reply With Quote
Reply


Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Loading Plugin in development Sladd Development 6 06-17-2014 06:57 PM
Question for plugin development gurus DiapDealer Plugins 2 02-04-2012 11:33 PM
DR800 Plugin development for DR800/DR1000 yuri_b iRex Developer's Corner 0 09-18-2010 09:46 AM
Device plugin development reader42 Plugins 10 03-29-2010 12:39 PM
Calibre plugin development - Newbie problems minstrel Plugins 5 04-12-2009 12:44 PM


All times are GMT -4. The time now is 02:42 AM.


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