def exportDirPage(self, linkDict, level=0): """Write directory structure with navigation bar and full pages""" title = self.title() lines = [u'<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 '\ 'Transitional//EN">', u'<html>', u'<head>', u'<meta http-equiv="Content-Type" content="text/html; '\ 'charset=utf-8">', u'<link rel="stylesheet" type="text/css" '\ 'href="%sdefault.css" />' % ('../' * level), u'<title>%s</title>' % title, u'</head>', u'<body>', u'<div id="sidebar">'] links = [] for item in self.childList: links.append(u' • '\ '<a href="%s/%s.html">%s</a><br />' % (self.exportDirName(False), item.exportDirName(False), item.title())) uncleList = [] if self.parent and level > 0: siblingList = self.parent.childList if self.parent.parent and level > 1: uncleList = self.parent.parent.childList else: uncleList = [self.parent] else: siblingList = [self] pos = 0 for item in siblingList: if item is self: links.insert(pos, u' • <b>%s</b><br />' % self.title()) pos = len(links) else: links.insert(pos, u' • '\ '<a href="%s.html">%s</a><br />' % (item.exportDirName(False), item.title())) pos += 1 pos = 0 for item in uncleList: links.insert(pos, u'• <a href="../%s.html">%s</a><br />' % (item.exportDirName(False), item.title())) if item is self.parent: pos = len(links) pos += 1 lines.extend(links) lines.append('</div>') textList = [] for line in self.formatText(True, True, True): for match in TreeItem.dirExportLinkRe.finditer(line): anchor = match.group(1) absPath = linkDict.get(anchor, '') if absPath: curPath = unicode(os.getcwd(), sys.getfilesystemencoding()) relPath = treedoc.relativePath(curPath, absPath) if os.sep != '/': relPath = relPath.replace(os.sep, '/') link = '<a href="%s">' % relPath line = TreeItem.dirExportLinkRe.sub(link, line) textList.append(line) sep = globalref.docRef.lineBreaks and u'<br />\n' or u'\n' lines.append(sep.join(textList)) lines.extend([u'</body>', u'</html>']) dirName = self.exportDirName(True) fileName = '%s.html' % dirName try: f = codecs.open(fileName, 'w', 'utf-8') f.writelines([line + '\n' for line in lines]) except (IOError, UnicodeError): print 'Error - could not write file to %s', fileName raise IOError(_('Error - cannot write file to %s') % fileName) f.close() if self.childList: try: if not os.access(dirName, os.R_OK): os.mkdir(dirName, 0755) os.chdir(dirName) except (OSError, ValueError, UnicodeError): print 'Error - cannot create directory', dirName raise IOError(_('Error - cannot create directory %s') % dirName) for child in self.childList: child.exportDirPage(linkDict, level + 1) os.chdir('..')
def exportDirTable(self, linkDict, parentTitle=None, header='', footer=''): """Write dir structure with html tables""" if not self.childList: return try: dirName = self.exportDirName(True) if not os.access(dirName, os.R_OK): os.mkdir(dirName, 0755) os.chdir(dirName) except (OSError, ValueError, UnicodeError): print 'Error - cannot create directory', dirName raise IOError(_('Error - cannot create directory %s') % dirName) title = self.title() lines = [u'<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 '\ 'Transitional//EN">', u'<html>', u'<head>', u'<meta http-equiv="Content-Type" content="text/html; '\ 'charset=utf-8">', u'<title>%s</title>' % title, u'</head>', u'<body>'] if header: lines.append(header) lines.append(u'<h1 align="center">%s</h1>' % title) if parentTitle: label = _('Parent: ') lines.append(u'<p align="center">%s' '<a href="../index.html">%s</a></p>' % (label, parentTitle)) lines.extend([u'<table cellpadding="10">', u'<tr>']) ### headings kludge???? headings = self.childList[0].nodeFormat().lineFields() lines.extend([u'<th><u>%s</u></th>' % cell for cell in headings]) lines.append(u'</tr><tr>') for child in self.childList: textList = [] for line in child.formatText(False): for match in TreeItem.dirExportLinkRe.finditer(line): anchor = match.group(1) absPath = linkDict.get(anchor, '') if absPath: curPath = unicode(dirName, sys.getfilesystemencoding()) relPath = treedoc.relativePath(curPath, absPath) relPath = os.path.join(relPath, 'index.html') if os.sep != '/': relPath = relPath.replace(os.sep, '/') link = '<a href="%s#%s">' % (relPath, anchor) line = TreeItem.dirExportLinkRe.sub(link, line) textList.append(line) childDir = child.exportDirName(False) if child.childList: textList[0] = u'<a href="%s/index.html">%s</a>' % \ (childDir, textList[0]) for anchor in filter(None, child.refFieldText().split('\n')): textList[0] = u'<a id="%s" />%s' % (anchor, textList[0]) lines.extend([u'<td>%s</td>' % cell for cell in textList]) lines.append(u'</tr><tr>') lines.extend([u'</tr>', u'</table>']) if footer: lines.append(footer) lines.extend([u'</body>', u'</html>']) try: f = codecs.open('index.html', 'w', 'utf-8') f.writelines([line + '\n' for line in lines]) except IOError: print 'Error - could not write file to', dirName raise IOError(_('Error - cannot write file to %s') % dirName) f.close() for child in self.childList: child.exportDirTable(linkDict, title, header, footer) os.chdir('..')