def getHeader(self, activeTab="Main", headTag=None, bodyTag=None): if headTag is None: headTag = ET.Element('head') if bodyTag is None: bodyTag = ET.Element('body') # make sure each component has elements to make formatting consistent headTag.append(ET.Comment('')) bodyTag.append(ET.Comment('')) fileStr = StringIO() ET.ElementTree(headTag).write(fileStr, encoding='utf-8') headTagStr = unicodeUtf8(fileStr.getvalue()) # remove the tag closer # </head> headTagStr = headTagStr[:headTagStr.rindex('<')] # fill in the prefab body tag content titleStr = LandingPageHTML.title landing = ET.Element('body') LandingPageHTML.addBodyHeaderAndContent(landing, titleStr, self.getMenuTags(activeTab)) fileStr = StringIO() ET.ElementTree(landing).write(fileStr, encoding='utf-8') landingStr = unicodeUtf8(fileStr.getvalue()) # remove <body> landingStr = landingStr[landingStr.index('>') + 1:] # remove tag closers for i in xrange(3): # </body> # contents </div> # </center> landingStr = landingStr[:landingStr.rindex('<')] fileStr = StringIO() ET.ElementTree(bodyTag).write(fileStr, encoding='utf-8') bodyTagStr = unicodeUtf8(fileStr.getvalue()) # extract <body> bodyStr = bodyTagStr[bodyTagStr.index('>') + 1:] bodyTagStr = bodyTagStr[:bodyTagStr.index('>') + 1] bodyStr = bodyTagStr + '\n' + landingStr + '\n' + bodyStr s = self.headerTemplate % { 'titlestring': titleStr, 'headTag': headTagStr, 'bodyTag': bodyStr, } return s
def addBodyHeaderAndContent(bodyTag, titleString, menuTags): SE = ET.SubElement bodyTag.append(ET.Comment('HEADER')) header = SE(bodyTag, 'div', id='header') h2 = SE(header, 'h2') h2.text = titleString navContainer = SE(header, 'div', id='navcontainer') navList = SE(navContainer, 'ul', id='navlist') for menuTag in menuTags: navList.append(menuTag) bodyTag.append(ET.Comment('CONTENT')) contents = SE(bodyTag, 'div', id='contents') center = SE(contents, 'center') # for ease of removal of center tag closer center.append(ET.Comment(''))