Mobileread
Post your Useful Plugin Code Fragments Here
#11  DiapDealer 12-18-2016, 05:27 AM
For the record; I wasn't supporting the use of "name" in epubs, I was suggesting that when working with alternative content that is going to be massaged into an epub, it's better to convert any "name" attributes to "id", rather than just delete them. Parsing the content for hrefs that contain the "name" attributes as fragments should be trivial enough to determne which ones can be safely deleted.
Reply 

#12  slowsmile 12-18-2016, 05:32 AM
@Doitsu...Interesting what you say about Kindle. Their's is a proprietary format that is closely related to epub with some peculiar quirks. Similar to iBooks proprietary version of epub. You can do that if you are a mammoth company like those two.

Here's another piece of BS code for html that I've found very useful:

Spoiler Warning below






Code
 # remove all anchors but preserve # all anchors with internet links for m in soup.findAll('a'): if 'href="http:' in str(m) or \ 'href="https:' in str(m) or \ 'mailto:' in str(m) or \ '@' in str(m): pass else: m.replaceWithChildren()


In my conversion plugin, I've also noticed significant differences between ODF html rendered from OO and LO. One problem I had was clearing out all the myriad FONT, FACE and SIZE declarations in these two different ODF html versions.

I used this code to remove all SIZE = 3 attributes from the html because it was causing problems. Notice that OO uses an integer while LO uses a string numeric for the size value.

Spoiler Warning below






Code
 # remove all 'size = 3' font declarations from OO or LO html for x in soup.findAll('font'): if x.has_attr('size'): if x['size'] == "3" or x['size'] == 3: x.replaceWithChildren()


Both Tidy and BS have saved my bacon on many occasions. They are both remarkably useful and easy to use for processing html.
Reply 

#13  Doitsu 12-18-2016, 06:03 AM
Quote slowsmile
In my conversion plugin, I've also noticed significant differences between ODF html rendered from OO and LO. One problem I had was clearing out all the myriad FONT, FACE and SIZE declarations in these two different ODF html versions.
Before you re-invent the wheel, you might want to have a look at Writer2xhtml/Writer2LaTeX and my ODT import plugin.
Reply 

#14  slowsmile 12-18-2016, 08:50 AM
I don't think that I'm re-inventing the wheel really. And even though my plugin converter will give a full conversion(upload ready) I do not regard that as its main purpose. It's just a plugin that will save you alot of time in your conversion workflow by automatically doing all the drudge jobs like re-styling your new epub from scratch, adding metadata, adding images, creating a stylesheet etc. The plugin's main purpose is to quickly bring the plugin user to a point where he or she can just concentrate on finishing-off tasks in Sigil like final epub re-styling, embedding fonts, adding extra images, fixed layout tasks etc.

I'm also guessing that people will probably criticize the plugin and perhaps say, "Why bother when their are already good converters like Calibre, Scrivener, Jutoh etc ?" The main difference between those converters and my plugin converter is that those converters have editors, toc editors, complex settings, stylers, menus, sub-menus and pre-compiler options etc. They are complex apps that take some time to learn. The only editor my plugin app uses to style epubs is LibreOffice or OpenOffice because the plugin ports all styles -- default styles, heading styles, font styles and named styles to the epub stylesheet. It can do this because it also ports all in-tag styling to the CSS as well. So with my plugin all you have to do is style your ebook in LO or OO as you like and then, after filling in the metadata in the dialog window, just push the OK button and your html doc will convert to epub -- whose layout and styling should exactly mimic the layout and styling of the ODT version. The plugin also has a very simple interface which anyone can learn to use quickly.
Reply 

#15  DiapDealer 12-18-2016, 09:50 AM
On a side-note: I sent you an email about testing your plugin on other platforms, @slowsmile. Did you recieve it?
Reply 

#16  slowsmile 12-18-2016, 05:12 PM
@DiapDealer...That's strange. I haven't received your email yet. I've also checked my spam etc.

I'll send you another private email giving you my email address again, just in case I typed it wrong.
Reply 

#17  slowsmile 12-18-2016, 08:49 PM
@DiapDealer...I received your second email without a problem and have just emailed you some info + plugin + test file. Much thanks for your help.
Reply 

#18  slowsmile 12-22-2016, 05:39 AM
Here's another interesting BeautifulSoup snippet that I've just successfully used:

Code
 # convert all html text to block text format for tag in soup.findAll('p'): if tag.has_attr('style') and 'text-align: center' not in tag['style'].lower(): del tag['style'] tag['class'] = 'BlockText'
The above four lines of code will delete all 'style' attributes in p tags and then add the BlockText class - centered text will not be affected.
Reply 

#19  DiapDealer 01-27-2020, 03:50 PM
Fairly self-contained Python code to make your PyQt5-based GUI plugin match Sigil's light/dark theme. It should be compatible with any version of Sigil that supports PyQt5 plugins. The dark theme just won't appear unless you're using Sigil 1.1.0 or higher.

Code
def dark_palette(bk, app): supports_theming = (bk.launcher_version() >= 20200117) if not supports_theming: return if bk.colorMode() != "dark": return try: from PyQt5.QtCore import Qt from PyQt5.QtGui import QColor, QPalette from PyQt5.QtWidgets import QStyleFactory except ImportError: return p = QPalette() sigil_colors = bk.color dark_color = QColor(sigil_colors("Window")) disabled_color = QColor(127,127,127) dark_link_color = QColor(108, 180, 238) text_color = QColor(sigil_colors("Text")) p.setColor(p.Window, dark_color) p.setColor(p.WindowText, text_color) p.setColor(p.Base, QColor(sigil_colors("Base"))) p.setColor(p.AlternateBase, dark_color) p.setColor(p.ToolTipBase, dark_color) p.setColor(p.ToolTipText, text_color) p.setColor(p.Text, text_color) p.setColor(p.Disabled, p.Text, disabled_color) p.setColor(p.Button, dark_color) p.setColor(p.ButtonText, text_color) p.setColor(p.Disabled, p.ButtonText, disabled_color) p.setColor(p.BrightText, Qt.red) p.setColor(p.Link, dark_link_color) p.setColor(p.Highlight, QColor(sigil_colors("Highlight"))) p.setColor(p.HighlightedText, QColor(sigil_colors("HighlightedText"))) p.setColor(p.Disabled, p.HighlightedText, disabled_color) app.setStyle(QStyleFactory.create("Fusion")) app.setPalette(p)
Then after you initialize your QApplication and before you show/exec it:

Code
app = QApplication(sys.argv)
Add the following function call that takes the BookContainer object and QApplication object as parameters:

Code
dark_palette(bk, app)
That's about it. Building your PyQt5 Application Widgets is up to you.

I may add some platform-specific tweaks and bug workarounds from time to time. I know for a fact there's some Mac color issues.
Reply 

 « First  « Prev   (2/2)
Today's Posts | Search this Thread | Login | Register