def endElement(self, name): if name == 'chapter': self._endArticle() elif name == 'div': self._endArticle() elif name == 'p': self._endDfn = False OsisHandler.endElement(self, name) elif name == 'seg': if self._inDfn: self._writeHtml('</%s>' % self._keywordTag) if self._keywordTag == 'h4': self._writeHtml('\n<div class="glossary-entry">') self._inDfn = False self._endDfn = True elif name == 'title': if self._inTitle: self._inTitle = False if self._ignoreTitle: self._ignoreTitle = False elif self._headerProcessed: if self._inChapterTitle: self._titleTag = '<h3 toclevel = 2>' self._inChapterTitle = False self._entryTocLevel = 3 else: self._glossTitleWritten = True self._writeTitle() else: OsisHandler.endElement(self, name)
def startDocument(self): OsisHandler.startDocument(self) self._bibleHtmlOpen = False self._bibleIntroWritten = False self._bibleStarting = True self._bookHtmlOpen = False self._bookTitle = '' self._canonicalTitleWritten = False self._chapterTitle = '' self._chFootnoteMarker = '' self._chNumWritten = False self._chHeadingWritten = False self._firstBook = True self._groupEmpty = False self._groupHtmlOpen = False self._groupIndex = 0 self._inCanonicalTitle = False self._inChapterTitle = False self._ignoreChEnd = False self._ignoreDivEnd = False self._inFootnoteRef = False self._inIntro = False self._introDivTextFound = False self._introStyleStarted = False self._introText = '' self._introTextFound = False self._introTitleWritten = False self._inVerse = False self._psDivTitle = False self._psDivTitleFound = False self._readyForSubtitle = False self._verseNumWritten = False self._verseText = '' self._verseTextFound = False self._defaultHeaderLevel = 3
def startDocument(self): OsisHandler.startDocument(self) self._endDfn = False self._glossTitleWritten = False self._headerProcessed = False self._inArticle = False self._inChapterTitle = False self._inDfn = False self._foundGlossaryDiv = False self._inChapter = False self._entryTocLevel = 2 self._defaultHeaderLevel = 3 # Avoid header level 2 as this would appear in table of contents # Select appropriate setting for FB2 for whether individual entries should be in the toc if self._context.outputFmt == 'fb2': self._context.config.glossEntriesInToc = self._context.config.glossEntriesInTocFb2; if self._context.config.glossEntriesInToc: self._keywordTag = 'h4' # If there are multiple glossaries and Testament headers are used, # create an overall title for the glossaries at the Testament level if title is set in config if len(self._context.glossaries) > 1 and self._context.topHeaderLevel == 1 and self._context.config.glossTitleSet: self._htmlWriter.open('glossary.txt') self._htmlWriter.write('<h1>%s</h1>\n' % self._context.config.glossaryTitle) self._htmlWriter.close() self._context.topHeaderLevel = 2
def _writeFootnoteMarker(self, refBook, noteRef): if self._inChapterTitle: refString = self._footnoteMarker(refBook, noteRef) self._chFootnoteMarker += refString else: OsisHandler._writeFootnoteMarker(self, refBook, noteRef) self._footnoteMarkerWritten = True
def __init__(self, htmlWriter, context): OsisHandler.__init__(self, htmlWriter, context) self._bibleHtmlOpen = False self._bibleIntroWritten = False # A Bible introduction has been written self._bibleStarting = True # Not yet found first Bible chapter self._bookTitle = '' # Book title either from config or found in OSIS self._bookHtmlOpen = False self._bookTitleFound = False # Found title which will be used as book title self._bookTitleWritten = False # Title of current book written to output file self._canonicalTitleWritten = False # Last output written was a canonical title self._chFootnoteMarker = '' # Footnote marker in a chapter title self._chNumWritten = False # Chapter number written before current verse self._chHeadingWritten = False # Found or generated a title at the start of a chapter self._chapterTitle = '' # HTML for chapter title ready to be written to output self._chTitleWritten = False # Chapter title has been written to output self._docStructure = DocStructure() self._firstBook = True # First book of testament self._firstTitle = False # Next title found will be first in book self._firstVerse = False # Current verse is first in chapter self._footnoteMarkerWritten = False # Marker for current footnote has been written (and any scripture reference processed) self._groupEmpty = False # No books yet found in current book group self._groupIndex = 0 # Used to check for start of new testament where separate book groups not used self._groupIntroWritten = False # A testament introduction has been written for current testament self._groupHtmlOpen = False self._groupTitle = '' # Title for current testament, obtained from config self._groupTitleWritten = False # The current testament title has been written self._inCanonicalTitle = False # Currently processing a canonical title self._inChapterTitle = False # Currently processing a chapter title self._ignoreChEnd = False # Processing <chapter> tag which is a milestone tag self._ignoreDivEnd = False # Processing <div> tag which is a milestone tag self._inFootnoteRef = False # Processing initial reference in a footnote self._inIntro = False # Processing an introduction rather than scripture self._introDivTextFound = False # Some text has been processed for current introduction <div> self._introStyleStarted = False # Found first occurrence of 'x-introduction' (sub)type in current introduction self._introText = '' # Html generated for current introduction self._introTextFound = False # Some text has been processed for current introduction self._introTitleWritten = False # A title for the current introduction has been written self._inVerse = False # Currently within a scripture verse and not within a canonical title self._lineSpan = False # Current poetic line in first line of chapter, to the right of a chapter number self._psDivTitle = False # Title being processed had been identified as a Psalm division title or subtitle self._psDivTitleFound = False # Last title processed was a Psalm division title, so next may be corresponding subtitle self._readyForSubtitle = False # Last title processed was a book title, so next may be corresponding subtitle self._singleChapterBook = False # Current book should have only one chapter self._startingChapter = False # <chapter> tag processed, but initial <verse> tag not yet found self._verseEmpty = True # Not yet processed any text in this verse (excluding canonical title) self._verseNumWritten = False # Html for the number of the current verse has been generater self._verseText = '' # Text of verse currently beeing processed # For fb2, we need chapter/psalm titles. # If no format has been provided, set up format as the chapter/psalm number only if self._context.outputFmt == 'fb2': if self._context.config.chapterTitle == '': self._context.config.chapterTitle = '%s' if self._context.config.psalmTitle == '': self._context.config.psalmTitle = '%s'
def __init__(self, htmlWriter, context): OsisHandler.__init__(self, htmlWriter, context) self._endDfn = False # Just written </dfn> tag self._glossTitleWritten = False # The title for the glossary has been written out self._inArticle = False # Currently processing a glossary entry self._inChapterTitle = False # The title currently being processed is a chapter title self._inDfn = False # Currently within an OSIS <seg> tag for a keyword self._foundGlossaryDiv = False # A div with type="glossary" has been found self._inChapter = False # Currently between start and end chapter tags self._entryTocLevel = 2 # Level for toc entries for individual entries self._keywordTag = 'dfn' # Tag to use for keywords
def _writeTitle(self): if len(self._titleText) > 0: origInVerse = self._inVerse if self._psDivTitle: self._inVerse = False # causes title to be written before Psalm heading titleWritten = OsisHandler._writeTitle(self) self._inVerse = origInVerse return titleWritten else: return False
def _writeHtml(self, html): if self._inFootnote and not self._writingFootnoteMarker: OsisHandler._writeHtml(self, html) elif not self._inTitle and not self._inCaption: self._suppressBreaks = False if self._inIntro: self._introText += html elif self._inVerse and not self._verseEmpty: self._verseText += html else: OsisHandler._writeHtml(self, html) self._breakCount = 0 # will be overwritten if called from _writeBreak() else: OsisHandler._writeHtml(self, html)
def startElement(self, name, attrs): if not self._inWork: OsisHandler.startElement(self, name, attrs)
def endDocument(self): self._footnotes.writeFootnotes() OsisHandler.endDocument(self)
def _processBodyTag(self, name, attrs): if name == 'chapter': self._endArticle() sId = self._getAttributeValue(attrs, 'sId') if sId is not None: self._inChapter = True else: eId = self._getAttributeValue(attrs, 'sId') if eId is not None: self._inChapter = False self._entryTocLevel = 2 elif name == 'div': divType = self._getAttributeValue(attrs, 'type') if divType == 'glossary': self._htmlWriter.open(self._osisIDWork) self._breakCount = 2 self._foundGlossaryDiv = True elif not self._foundGlossaryDiv: typeStr = '' if divType is not None: typeStr = 'type %s' % divType print 'Unexpected <div> found - type %s' % typeStr elif name == 'reference': # reference are ignored apart from glossary references refType = self._getAttributeValue(attrs, 'type') if refType == "x-glosslink" and self._endDfn: self._writeHtml(u' \u2014 ') self._endDfn = False OsisHandler._processReference(self, attrs) elif name == 'seg': segType = self._getAttributeValue(attrs, 'type') if segType == 'keyword': self._closeParagraph() self._endArticle() articleTag = 'article' if self._context.outputFmt != 'epub': articleTag = 'div' levelAttr = '' if self._context.config.glossEntriesInToc: levelAttr = ' toclevel="%d"' % self._entryTocLevel if self._keywordTag == 'dfn': # fb2 will not generate toc entry if within <div> self._writeHtml('\n<%s class="glossary-entry">' % articleTag) self._writeHtml('\n<%s%s>' % (self._keywordTag, levelAttr)) self._inArticle = True self._inDfn = True elif name == 'title': self._endArticle() titleType = self._getAttributeValue(attrs,'type') if titleType == 'runningHead': self._inTitle = True self._ignoreTitle = True elif titleType == 'x-chapterLabel': if not self._glossTitleWritten: self._writeDefaultTitle() self._inTitle = True self._inChapterTitle = True self._titleText = '' else: if not self._glossTitleWritten: self._titleTag = '<h%d>' % self._context.topHeaderLevel else: level = self._getAttributeValue(attrs,'level') if level is not None: headerLevel = int(level) + self._context.topHeaderLevel else: headerLevel = self._defaultHeaderLevel subType = self._getAttributeValue(attrs,'subType') if subType is not None: self._titleTag = '<h%d class="%s">' % (headerLevel, subType) else: self._titleTag = '<h%d>' % (headerLevel) self._inTitle = True self._titleText = '' else: OsisHandler._processBodyTag(self, name, attrs)
def _processBodyTag(self, name, attrs): if name == 'chapter': osisId = self._getAttributeValue(attrs,'osisID') if osisId is not None: # Start of a chapter # If this is the first chapter of the book, write book title and any intro if self._inIntro: if len(self._introText) > 0: # Remove unwanted breaks at start of intro before writing while self._introText.startswith('<br />'): self._introText = self._introText[6:] if self._bibleStarting and self._context.config.bibleIntro and not self._bibleIntroWritten: self._htmlWriter.open('bible') self._bibleHtmlOpen = True self._closeParagraph() self._writeIntroText() self._bibleIntroWritten = True self._introText = '' elif self._firstBook and self._context.config.testamentIntro and not self._groupIntroWritten: self._openGroupHtml() self._closeParagraph() self._writeIntroText() self._introText = '' self._groupIntroWritten = True # Ensure testament title is written if self._firstBook and not self._groupTitleWritten and self._groupTitle != '': self._openGroupHtml() self._openBookHtml() if self._bookTitleFound or not self._context.config.bookTitlesInOSIS: self._writeBookTitle() if len(self._introText) > 0: if not self._bookTitleWritten: self._writeBookTitle() self._introText += '<br />\n' self._writeIntroText() self._introText = '' self._inIntro = False self._introStyleStarted = False chId = self._getAttributeValue(attrs,'sID') if chId is not None: self._ignoreChEnd = True # This is a milestone tag else: self._ignoreChEnd = False # This is an enclosing tag chId = osisId self._docStructure.newChapter(chId) # If a chapter/psalm heading format is defined, then write the heading self._chapterTitle = '' self._chTitleWritten = False self._chHeadingWritten = False self._startingChapter = True titleFormat = '' bookId = self._docStructure.bookId if bookId == 'Ps': titleFormat = self._context.config.psalmTitle elif not self._singleChapterBook: titleFormat = self._context.config.chapterTitle if titleFormat != '': title = titleFormat % self._docStructure.chapter self._chapterTitle = '<h3 chapter="%s" class="x-chapter-title">%s</h3><br />' % (self._docStructure.chapter, title) self._bibleStarting = False self._firstBook = False # Do not write chapter number yet, in case there is a heading to write else: chId = self._getAttributeValue(attrs,'eID') if chId is not None: self._docStructure.endChapter(chId) self._writeBreak(True) else: print 'Chapter tag does not have expected attributes - ignoring' elif name == 'div': divType = self._getAttributeValue(attrs, 'type') if divType == 'bookGroup': if self._docStructure.startGroup(): groupNumber = self._docStructure.groupNumber self._startGroup(groupNumber) elif divType == 'book': self._groupEmpty = False # Finish off any preceding Bible or testament introduction if self._bibleHtmlOpen or self._groupHtmlOpen: self._footnotes.writeFootnotes() self._htmlWriter.close() self._bibleHtmlOpen = False self._groupHtmlOpen = False # See which book is starting bookRef = self._getAttributeValue(attrs,'osisID') if self._docStructure.startBook(bookRef): if not self._context.config.testamentGroups: groupIndex = self._context.config.bookGroup(bookRef) if not self._firstBook and groupIndex != self._groupIndex: self._docStructure.groupNumber += 1 self._startGroup(self._docStructure.groupNumber) self._groupIndex = groupIndex self._bookTitle = self._context.config.bookTitle(bookRef) self._inIntro = True self._introText = '' self._introTextFound = False self._introTitleWritten = False self._bookTitleFound = False self._bookTitleWritten = False self._firstTitle = True self._verseTextFound = False print 'Processing book ', bookRef if bookRef == 'Phlm' or bookRef == '2John' or bookRef == '3John' or bookRef == 'Jude' or bookRef == 'Obad': self._singleChapterBook = True else: self._singleChapterBook = False # Don't open book HTML yet, in case there is a testament introduction to write elif divType == 'section': secRef = self._getAttributeValue(attrs, 'sID') self._docStructure.startSection(secRef) if secRef is not None: self._ignoreDivEnd = True # Milestone tag elif divType == 'introduction' or divType == 'preface' or divType == 'coverPage' or divType == 'titlePage': if self._bibleStarting and not self._docStructure.inGroup: self._introDivTextFound = False if not self._bibleHtmlOpen: self._htmlWriter.open('bible') self._bibleHtmlOpen = True self._introTextFound = False self._bibleIntroWritten = True self._docStructure.startIntro() elif self._docStructure.inGroup and self._groupEmpty: self._introDivTextFound = False if not self._groupHtmlOpen: self._openGroupHtml() self._introTextFound = False self._docStructure.startIntro() else: self._docStructure.otherDiv() else: secRef = self._getAttributeValue(attrs, 'eID') if secRef is not None: divType = self._docStructure.endDiv(secRef) self._ignoreDivEnd = True else: self._docStructure.otherDiv() elif name == 'foreign': verseEmpty = self._verseEmpty if self._inVerse: # prevents style being applied to verse number self._verseEmpty = False self._writeHtml('<span class="foreign">') self._verseEmpty = verseEmpty elif name == 'hi': verseEmpty = self._verseEmpty if self._inVerse: self._verseEmpty = False # Prevents highlight being applied to verse number OsisHandler._handleHi(self, attrs) self._verseEmpty = verseEmpty elif name == 'l': self._lineSpan = False htmlTag = self._lineHtml(attrs) if self._inVerse and self._verseEmpty: if self._chNumWritten: self._lineSpan = True self._verseText = self._verseText + '<span class="first-line">' else: self._verseText = htmlTag + self._verseText else: self._verseEmpty = False self._writeHtml(htmlTag) if self._introStyleStarted: self._writeHtml('<span class="x-introduction">') elif name == 'lg': if not self._inIntro and not self._bookTitleWritten and self._bookHtmlOpen: self._writeBookTitle() if self._firstVerse and not self._chTitleWritten and not self._singleChapterBook: self._writeChapterTitleOrNumber() OsisHandler._processBodyTag(self, name, attrs) elif name == 'list': listType = self._getAttributeValue(attrs, 'subType') if listType is None and self._inIntro and self._introStyleStarted: htmlTag = '<ul class="x-introduction">\n' self._writeHtml(htmlTag) else: OsisHandler._processBodyTag(self, name, attrs) elif name == 'p': self._endGeneratedPara() if not self._inIntro and not self._bookTitleWritten and self._bookHtmlOpen: self._writeBookTitle() if self._firstVerse and not self._chTitleWritten and not self._singleChapterBook: self._writeChapterTitleOrNumber() overrideSubType = None if self._inVerse and self._verseEmpty and self._chNumWritten: overrideSubType='first-para' elif self._introStyleStarted: overrideSubType = 'x-introduction' paraTag = self._generateParaTag(attrs, overrideSubType) if 'x-introduction' in paraTag and self._inIntro: self._introStyleStarted = True self._inParagraph = True if self._inVerse and self._verseEmpty: self._verseText = paraTag + self._verseText else: self._writeHtml(paraTag) elif name == 'reference': # reference tags are expected but are ignored # apart from glossary references and references in footnotes refType = self._getAttributeValue(attrs, 'type') if refType == "x-glossary": verseEmpty = self._verseEmpty if self._inVerse: # prevents style being applied to verse number self._verseEmpty = False OsisHandler._processReference(self, attrs) self._verseEmpty = verseEmpty elif self._inFootnote and not self._footnoteMarkerWritten: self._inFootnoteRef = True osisRef = self._getAttributeValue(attrs, 'osisRef') if osisRef is None: # reference not supplied as an attribute verseRef = '%s:%s' % (self._docStructure.chapter, self._docStructure.verse) self._startFootnoteAndWriteMarker(verseRef) elif not self._footnoteRef(osisRef): # We already have a reference, so do not process contents of this reference tag self._inFootnoteRef = False self._inFootnote = False self._ignoreText = True elif self._figHtml != '': self._ignoreText = True elif name == 'title': canonical = self._getAttributeValue(attrs,'canonical') if canonical == 'true': # A canonical title has special treatment # Make sure Psalm title or number is written before the cannonical title if not self._bookTitleWritten: self._writeBookTitle() if self._startingChapter and not self._chTitleWritten: self._writeChapterTitleOrNumber() self._startingChapter = False if self._inVerse: # A canonical title is not part of the verse self._inVerse = False self._verseText = '' if self._firstVerse and not self._chTitleWritten: self._writeChapterTitleOrNumber() if self._context.canonicalClassDefined: self._writeHtml('<span class="canonical">') else: self._writeHtml('<i>') self._inCanonicalTitle = True else: titleType = self._getAttributeValue(attrs,'type') if titleType == 'runningHead': self._inTitle = True self._ignoreTitle = True elif titleType == 'x-chapterLabel': if self._singleChapterBook: self._inTitle = True self._ignoreTitle = True else: self._chapterTitle = '' self._inChapterTitle = True else: level = self._getAttributeValue(attrs,'level') if level is not None: # Header levels 1 and 2 are for testaments and books, so titles start at 3 headerLevel = int(level) + 2 else: headerLevel = self._defaultHeaderLevel subType = self._getAttributeValue(attrs,'subType') chapter = '' if (self._context.outputFmt != 'fb2'): if (not self._singleChapterBook) and (self._startingChapter or (self._inVerse and self._firstVerse and self._verseEmpty and not self._canonicalTitleWritten)): if not self._chHeadingWritten: chapter = ' chapter="%s"' % self._docStructure.chapter if self._readyForSubtitle and headerLevel == 4: self._titleTag = '<h4 class="book-subtitle">' elif subType is not None: self._titleTag = '<h%d class="%s"%s>' % (headerLevel, subType, chapter) else: self._titleTag = '<h%d%s>' % (headerLevel, chapter) self._inTitle = True self._titleText = '' self._readyForSubtitle = False elif name == 'transChange': verseEmpty = self._verseEmpty if self._inVerse: # prevents style being applied to verse number self._verseEmpty = False self._writeHtml('<span class="transChange">') self._verseEmpty = verseEmpty elif name == 'verse': verse = self._getAttributeValue(attrs,'sID') if verse is not None: self._startVerse(verse) else: verse = self._getAttributeValue(attrs,'eID') if verse is not None: self._docStructure.endVerse(verse) if not self._lineGroupPara: self._endGeneratedPara() if not self._verseEmpty: if self._chHeadingWritten: # remove chapter attribute from chapter number if a chapter heading has been written self._verseText = re.sub(r'span chapter="\d+"', 'span', self._verseText) # Add a break if immediately following a canonical title if self._canonicalTitleWritten: self._htmlWriter.write('<br />') self._canonicalTitleWritten = False self._htmlWriter.write(self._verseText + '\n') self._firstVerse = False self._chHeadingWritten = False self._inVerse = False self._verseText ='' else: OsisHandler._processBodyTag(self, name, attrs)
def endElement(self, name): if name == 'chapter': if self._ignoreChEnd: self._ignoreChEnd = False else: self._docStructure.endChapter(self._docStructure.chapterRef) self._writeBreak(True) elif name == 'div': if self._ignoreDivEnd: self._ignoreDivEnd = False else: divType = self._docStructure.endDiv(None) if divType == self._docStructure.BOOK: self._footnotes.writeFootnotes() elif divType == self._docStructure.GROUP: if self._groupEmpty: print 'Ignoring empty book group' self._docStructure.groupNumber -= 1 self._htmlWriter.closeAndRemove() self._groupHtmlOpen = False elif divType == self._docStructure.INTRO: if self._bibleHtmlOpen: self._bibleIntroWritten = True elif self._groupHtmlOpen: self._groupIntroWritten = True elif name == 'figure': if self._inIntro: self._introTextFound = True OsisHandler.endElement(self, name) elif name == 'l': if self._lineSpan: self._writeHtml('</span>\n') else: if self._inIntro: self._writeHtml('</span>') OsisHandler.endElement(self, name) elif name == 'note': if self._inFootnote: self._inFootnoteRef = False OsisHandler.endElement(self, name) elif name == 'reference': if self._inFootnoteRef: self._inFootnoteRef = False else: OsisHandler.endElement(self, name) elif name == 'title': if self._inTitle: self._inTitle = False if self._ignoreTitle: self._ignoreTitle = False elif self._headerProcessed: titleWritten = False if not self._docStructure.inBook: titleWritten = self._processIntroTitle() elif self._inIntro: titleWritten = self._processBookIntroTitle() else: titleWritten = self._processScriptureTitle() if titleWritten: self._breakCount = 2 self._psDivTitle = False self._canonicalTitleWritten = False if re.search('chapter', self._titleTag) is not None: self._chHeadingWritten = True elif self._inCanonicalTitle: closingTag = '' if self._context.canonicalClassDefined: closingTag = '</span>' else: closingTag = '</i>' self._inCanonicalTitle = False self._writeHtml(closingTag + '<br />\n') self._breakCount = 1 self._canonicalTitleWritten = True if self._docStructure.verse != '': self._inVerse = True self._verseEmpty = True if self._chTitleWritten or self._docStructure.verse != '1': self._verseText = '<sup>' + self._docStructure.verse + '</sup>' self._verseNumWritten = True elif self._inChapterTitle: chAttribute ='' if not self._chHeadingWritten: chAttribute = 'chapter="%s" ' % self._docStructure.chapter self._chapterTitle = '<h3 %s class="x-chapter-title">%s</h3>' % (chAttribute, self._chapterTitle) if self._chFootnoteMarker != '': self._chapterTitle += self._chFootnoteMarker self._chFootnoteMarker = '' self._chapterTitle += '<br />' self._chHeadingWritten = True self._inChapterTitle = False else: OsisHandler.endElement(self, name)