예제 #1
0
    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)
예제 #2
0
    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)