10-21-2020, 06:17 AM | #1 |
Junior Member
Posts: 5
Karma: 10
Join Date: Oct 2020
Device: none
|
Calling Sigil menu functions in plugin?
Hi,
I've written a small, personal-use, plug-in to perform some find-and-replace, called mara01. That works fine, but the actual sequence of events when importing my book to Sigil is as follows: 1. Import docx + css via plugin (1) 2. Run plugin, shadycharacters (2) 3. Restructure epub to Sigil norm (tools) 4. Add cover, cover_01.jpg (tools) 5. Add metadata – title, author (tools, F8) 6. Run mara01 plugin (3) 7. Spit at markers (edit, F6) 8. Create TOC for h1 (tools, Ctrl-T) Step 6 is my plugin, but ideally I'd like to add as many of the other steps as I can. Any advice and/or suggestions as to how to go about this? P.S. not really a python-person |
10-21-2020, 07:31 AM | #2 |
Grand Sorcerer
Posts: 27,549
Karma: 193191846
Join Date: Jan 2010
Device: Nexus 7, Kindle Fire HD
|
You can't call Sigil Menu functions from a plugin. You could probably manage to call another plugin, but it would be a precarious, fragile thing at best.
|
Advert | |
|
10-21-2020, 01:11 PM | #3 |
Junior Member
Posts: 5
Karma: 10
Join Date: Oct 2020
Device: none
|
So, I guess the follow-up is can anyone recommend any existing plugins I can cannibalise that do any of the following?
1. Add meta-data 2. Import covers |
10-21-2020, 04:13 PM | #4 | |
Grand Sorcerer
Posts: 5,584
Karma: 22735033
Join Date: Dec 2010
Device: Kindle PW2
|
Quote:
For the first task you'll need: bk.getmetadataxml() bk.setmetadataxml(new_metadata) For the second task you'll need: bk.addfile() bk.spine_insert_before() (if it's an HTML cover file) (You'll find some code examples in the test plugin.) |
|
10-22-2020, 01:06 AM | #5 |
Junior Member
Posts: 5
Karma: 10
Join Date: Oct 2020
Device: none
|
Many thanks - and, yes, I read the guide! (believe it or not)
|
Advert | |
|
10-22-2020, 01:18 AM | #6 |
Hedge Wizard
Posts: 800
Karma: 19999999
Join Date: May 2011
Location: UK/Philippines
Device: Kobo Touch, Nook Simple
|
|
10-22-2020, 02:27 AM | #7 |
Junior Member
Posts: 5
Karma: 10
Join Date: Oct 2020
Device: none
|
Ah - no, I'd read a different, more basic, guide.
Nearly there! (got the metadata working, now just the cover) |
10-22-2020, 08:06 AM | #8 |
Junior Member
Posts: 5
Karma: 10
Join Date: Oct 2020
Device: none
|
Okay, so below is my final code, which does what I want.
The Sigil well-formed check doesn't give any errors, but is there anything obviously wrong with it? (I know very little about how an epub should be structured) Code:
# -*- coding: utf-8 -*- import sys import re def run(bk): # sort-of check that we haven't run already gde = bk.getguide() if len(gde) != 1: print('Guide-count wrong - already run or wrong book?') return 0 # add title, classes and split-markers for (id, href) in bk.text_iter(): html = bk.readfile(id) html = re.sub(r'<title></title>','<title>Mara’s Tale</title>', html) html = re.sub(r'<p>Mara’s Tale</p>','<p class="Title">Mara’s Tale</p>', html) html = re.sub(r'<p>By Tom Melly</p>','<p class="SubTitle">By Tom Melly</p><hr class="sigil_split_marker"/>', html) html = re.sub(r'<p>For India</p>','<p class="Quote">For India</p><hr class="sigil_split_marker"/>', html) html = re.sub(r'<p>Fifty-thousand years ago, central France','<p class="Quote">Fifty-thousand years ago, central France', html) html = re.sub(r'<h1>','<hr class="sigil_split_marker"/><h1>', html) bk.writefile(id, html) # add metadata title and author xml = bk.getmetadataxml() xml = re.sub(r'\[No data\]','Mara’s Tale', xml) xml = re.sub(r'<dc:language','<dc:creator opf:role="aut">Tom Melly</dc:creator>\n<dc:language', xml) bk.setmetadataxml(xml) # create cover html, plus spine and guide entries cxml = '<?xml version="1.0" encoding="UTF-8" standalone="no" ?>\n' cxml += '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">\n' cxml += '<html xmlns="http://www.w3.org/1999/xhtml">\n' cxml += '<head>\n' cxml += ' <title>Cover</title>\n' cxml += '</head>\n' cxml += '<body>\n' cxml += ' <div style="text-align: center; padding: 0pt; margin: 0pt;">\n' cxml += ' <svg xmlns="http://www.w3.org/2000/svg" height="100%" preserveAspectRatio="xMidYMid meet" version="1.1" viewBox="0 0 797 1057" width="100%" xmlns:xlink="http://www.w3.org/1999/xlink">\n' cxml += ' <image width="797" height="1057" xlink:href="../Images/cover_01.jpg"/>\n' cxml += ' </svg>\n' cxml += ' </div>\n' cxml += '</body>\n' cxml += '</html>' bk.addfile('cover.xhtml', 'cover.xhtml', cxml, mime='application/xhtml+xml') bk.spine_insert_before(0, 'cover.xhtml', None, None) bk.setguide([gde[0], ('cover', 'Cover', 'Text/cover.xhtml')]) # import cover jpeg cover=open('C:\\Users\\tom\\Documents\\Personal\\Mara\\Sigil Build\\cover_01.jpg', 'rb') bkCover=cover.read() #Load image into memory... cover.close() bk.addfile('cover_01.jpg', 'cover_01.jpg', bkCover, 'image/jpeg') return 0 def main(): print ("I reached main when I should not have\n") return -1 if __name__ == "__main__": sys.exit(main()) |
10-23-2020, 04:23 AM | #9 | |
Grand Sorcerer
Posts: 5,584
Karma: 22735033
Join Date: Dec 2010
Device: Kindle PW2
|
Quote:
Shameless plug: I created a Sigil EPUBCheck plugin that you could use if you don't mind installing Java. |
|
10-23-2020, 04:36 AM | #10 | |
Hedge Wizard
Posts: 800
Karma: 19999999
Join Date: May 2011
Location: UK/Philippines
Device: Kobo Touch, Nook Simple
|
Quote:
Thanks Doitsu. It's a winner. |
|
|
Similar Threads | ||||
Thread | Thread Starter | Forum | Replies | Last Post |
[GUI Plugin] Favourites Menu | kiwidude | Plugins | 119 | 03-30-2024 07:10 PM |
[Plugin] KindleImport Sigil plugin | DiapDealer | Plugins | 187 | 07-04-2022 10:11 AM |
plugin menu case | AlanHK | Sigil | 21 | 09-19-2019 12:18 AM |
plugin api call to do editmetadata->merge functions? | minektur | Development | 3 | 01-28-2019 09:45 AM |
Plugin Menu Layout | DoctorOhh | Plugins | 6 | 03-30-2011 03:51 AM |