MobileRead Forums

MobileRead Forums (https://www.mobileread.com/forums/index.php)
-   Plugins (https://www.mobileread.com/forums/forumdisplay.php?f=268)
-   -   Calling Sigil menu functions in plugin? (https://www.mobileread.com/forums/showthread.php?t=334217)

TomMelly 10-21-2020 07:17 AM

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

DiapDealer 10-21-2020 08:31 AM

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.

TomMelly 10-21-2020 02:11 PM

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

Doitsu 10-21-2020 05:13 PM

Quote:

Originally Posted by TomMelly (Post 4049787)
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

You might want to download the Sigil Plugin Framework Guide, if you haven't already done so.

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.)

TomMelly 10-22-2020 02:06 AM

Many thanks - and, yes, I read the guide! (believe it or not)

Thasaidon 10-22-2020 02:18 AM

Quote:

Originally Posted by TomMelly (Post 4049945)
Many thanks - and, yes, I read the guide! (believe it or not)

Would a separate macro app like Win Parrot help you? I have had a similar app running a sequence of Sigil menu items/plug ins.

TomMelly 10-22-2020 03:27 AM

Ah - no, I'd read a different, more basic, guide.

Nearly there! (got the metadata working, now just the cover)

TomMelly 10-22-2020 09:06 AM

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())


Doitsu 10-23-2020 05:23 AM

Quote:

Originally Posted by TomMelly (Post 4050030)
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)

The well-formedness check is only a very basic check, you also might want to check your finished ebook with the IDPF Validator (EPUBCheck).

Shameless plug: I created a Sigil EPUBCheck plugin that you could use if you don't mind installing Java.

Thasaidon 10-23-2020 05:36 AM

Quote:

Originally Posted by Doitsu (Post 4050440)
Shameless plug: I created a Sigil EPUBCheck plugin that you could use if you don't mind installing Java.

I recommend it. I find it invaluable and use it heavily.

Thanks Doitsu. It's a winner.


All times are GMT -4. The time now is 08:41 PM.

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