View Single Post
Old 10-22-2020, 08:06 AM   #8
TomMelly
Junior Member
TomMelly began at the beginning.
 
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())
TomMelly is offline   Reply With Quote