⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 buildhtb.py

📁 Emdros is a text database middleware-layer aimed at storage and retrieval of "text plus information
💻 PY
📖 第 1 页 / 共 2 页
字号:
# -*- coding: utf-8 -*-import osimport reimport sysfrom config import *LOOKING_FOR_START_OF_PAGE = 1PARSING_PAGE = 2def addFileIfExists(fout, infilename):    if os.access(infilename, os.F_OK):        fin = open(infilename, "r")	for line in fin.readlines():     	    fout.write(line)        fin.close()    else:        # Only add if file exists and is accessible         passclass FilesEntry:    def __init__(self):        self.mylist = []    def readFromLine(self, line):        self.mylist = line.strip().split("\t")    def getID(self):        print self.mylist[0]        return int(self.mylist[0])    def getTitle(self):        return self.mylist[1]    def getNext(self):        return int(self.mylist[2])    def getUp(self):        return int(self.mylist[3])    def getType(self):        return self.mylist[4]    def __cmp__(self, other):        return cmp(self.getID(), other.getID())class Hierarchy:    def __init__(self):        self.entries = {}        self.nextrelation = {}        self.prevrelation = {}        self.docline = []        self.levels = {}    def readFromFile(self, f):        for line in f.readlines():            fe = FilesEntry()            fe.readFromLine(line)            self.entries[fe.getID()] = fe        self.calculateNextPrevRelations()        self.calculateDocLine()        self.calculateLevels()    def calculateDocLine(self):        id = 1000        while id != -1:            self.docline.append(id)            id = self.getNext(id)            print "UP201: id =", id    def calculateLevels(self):        self.levels[1000] = 0        for id in self.docline:            if id != -1 and id != 1000:                self.levels[id] = self.levels[self.entries[id].getUp()] + 1    def calculateNextPrevRelations(self):        for id in self.entries.keys():            fe = self.entries[id]            self.nextrelation[id] = fe.getNext()        for id in self.nextrelation.keys():            next = self.nextrelation[id]            self.prevrelation[next] = id    def getEntry(self, id):        return self.entries[id]    def getNext(self, id):        try:            return self.nextrelation[id]        except:            return -1    def getImmediateChildren(self, id):        result = []        for childid in self.docline:            upid = self.getUp(childid)            if upid == id:                result.append(childid)        return result    def getPrev(self, id):        try:            return self.prevrelation[id]        except Exception:            return -1    def getUp(self, id):        return self.entries[id].getUp()    def getIDs(self):        return self.entries.keys()    def getLinkFromId(self, id, extension):        if id == -1:            return "none"        else:            fe = self.entries[id]            return "<A HREF=\"%s\">%s</A>" % (str(id) + str(extension), fe.getTitle())    def idIsBelowOther(self, other_id, id):        up_id = self.getUp(id)        if up_id == -1:            return False        elif up_id == other_id:            return True        else:            return self.idIsBelowOther(other_id, up_id)class Pages:    def __init__(self):        self.pages = {}        self.myindex = {}    def readFromFile(self, f):        pagestartre = re.compile(r'^<page[ ]+ID="([0-9]+)">')        pageendre = re.compile(r'^</page>')        iState = LOOKING_FOR_START_OF_PAGE        id = 0        for line in f.readlines():            if iState == LOOKING_FOR_START_OF_PAGE:                mo = pagestartre.match(line)                if mo != None:                    id = int(line[mo.start(1):mo.end(1)])                    self.pages[id] = ""                    iState = PARSING_PAGE            elif iState == PARSING_PAGE:                mo = pageendre.match(line)                if mo != None:                    iState = LOOKING_FOR_START_OF_PAGE                else:                    self.pages[id] = self.pages[id] + line    def getPage(self, id):        return self.pages[id]    def writeHTMLPages(self, hierarchy, extension, bWithNavigation):        ids = hierarchy.getIDs()        ids.sort()        for id in ids:            self.writeHTMLFile(id, hierarchy, extension, bWithNavigation)    def writeHTMLFile(self, id, hierarchy, extension, bWithNavigation):        filename = str(id) + str(extension)        print filename        f = open(filename, "w")        if bWithNavigation:            css_header = "    <link href=\"cms.css\" rel=\"stylesheet\" type=\"text/css\">"        else:            css_header = ""        self.writeHTMLHeader(f, id, hierarchy, css_header)        self.writeHTMLPage(f, id, hierarchy, extension, bWithNavigation)        self.writeHTMLFooter(f, id, hierarchy, extension)        f.close()    def writeHTMLHeader(self, f, id, hierarchy, css_header):        print >>f, """<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0//EN"         "http://www.w3.org/TR/REC-html40/strict.dtd">"""        print >>f, "<html>"        print >>f, "  <head>"        print >>f, "     <title>" + hierarchy.getEntry(id).getTitle() + "</title>"        print >>f, css_header        print >>f, "  </head>"        print >>f, "<body>"    def writeHTMLPage(self, f, id, hierarchy, extension, bWithNavigation):        if bWithNavigation:            print >>f, "<TABLE cellspacing=\"0\" cellpadding=\"0\" class=\"page\" width=\"100%\">\n"            print >>f, "<TR>\n"            print >>f, "<TD CLASS=\"menu\">\n"            print >>f, "<DIV CLASS=\"hjem\"><A CLASS=\"menu\" HREF=\"http://emdros.org/\">Home</A></DIV>"            print >>f, "<BR><BR><DIV CLASS=\"PDF\"><A CLASS=\"menu\" HREF=\"%s.pdf\">PDF Version</A></DIV>" % PDF_PREFIX            print >>f, "<br><br><DIV CLASS=\"indhold\">Contents:</DIV>"            self.writeNavigationLeft(f, 1000, id, hierarchy, extension)            print >>f, "<br>"            print >>f, "<DIV CLASS=\"search\">Search:</DIV><br>"            print >>f, "<FORM ACTION=\"search.php\" METHOD=\"GET\">"            print >>f, "<INPUT TYPE=\"HIDDEN\" NAME=\"static_links\" VALUE=\"true\">"            print >>f, "<INPUT TYPE=\"TEXT\" NAME=\"terms\" SIZE=\"23\" ALT=\"Write search terms\">"            print >>f, "<INPUT TYPE=\"SUBMIT\" VALUE=\"Search\">"            print >>f, "</FORM>"            print >>f, ""            addFileIfExists(f, "sidebar.inc")            print >>f, "</TD><TD class=\"main\">"            print >>f, ""        print >>f, "<H1>%s</H1>" % hierarchy.getEntry(id).getTitle()        pagestring = self.pages[id]        pagestring = self.expandTags(pagestring, id, hierarchy, extension)        print >>f, pagestring        if bWithNavigation:            print >>f, "</TD>"            print >>f, "</TR>"            print >>f, "</TABLE>"    def writeNavigationLeft(self, f, main_id, focus_id, hierarchy, extension):        arr_subsections = hierarchy.getImmediateChildren(main_id)        if len(arr_subsections) != 0:            bIsNotTOC = main_id != 1000            if bIsNotTOC:                print >>f, "<DIV class=\"menu\">"            for child_id in arr_subsections:                entry = hierarchy.getEntry(child_id)                child_title = entry.getTitle()                if entry.getType() == "Part":                    bChildIsPart = True                else:                    bChildIsPart = False                if bChildIsPart:                    print >>f, "<br>"                    print >>f, "<DIV class=\"part\">"                else:                    print >>f, "<DIV class=\"menu-item\">"                    print >>f, "<IMG SRC=\"dot.gif\">&nbsp;"                if child_id == focus_id:                    print >>f, "<em>%s</em>" % child_title                else:                    child_link = "<A class=\"menu\" HREF=\"%d%s\">%s</A>" % (child_id, extension, child_title)                    print >>f, child_link                print >>f, "</DIV>"                bFocusDocIsBelow = hierarchy.idIsBelowOther(child_id, focus_id)                if bFocusDocIsBelow or focus_id == child_id:                    self.writeNavigationLeft(f, child_id, focus_id, hierarchy, extension)                                if bIsNotTOC:                print >>f, "</DIV>"    def writeHTMLFooter(self, f, id, hierarchy, extension):        print >>f, "<hr>"        print >>f, "<strong>Previous:</strong>" + hierarchy.getLinkFromId(hierarchy.getPrev(id), extension) + "<br>"        print >>f, "<strong>Up:</strong>" + hierarchy.getLinkFromId(hierarchy.getUp(id), extension) + "<br>"        print >>f, "<strong>Next:</strong>" + hierarchy.getLinkFromId(hierarchy.getNext(id), extension) + "<br>"        print >>f, "</body>"        print >>f, "</html>"    def writeOneBigHTMLPage(self, bookname, title, hierarchy):        filename = "%s.htm" % bookname        f = open(filename, "w")        print >>f, """<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0//EN"         "http://www.w3.org/TR/REC-html40/strict.dtd">"""        print >>f, "<html>"        print >>f, "  <head>"        print >>f, "     <title>" + title + "</title>"        print >>f, "  </head>"        print >>f, "<body>"        for id in hierarchy.docline:            if id != 1000:                print >>f, "<H%d><A NAME=\"%s\">%s</A></H%d>" % (hierarchy.levels[id],str(id),hierarchy.getEntry(id).getTitle(),hierarchy.levels[id])                pagestring = self.pages[id]                pagestring = self.expandTags(pagestring, id, hierarchy, None)                print >>f, pagestring                #print >>f, "<br><br><hr><br><br>"        f.close()    def getSection(self, level):        if level == 1:            return "\section"        elif level == 2:            return "\subsection"        elif level == 3:            return "\subsubsection"        else:            return "\paragraph"    def writeOneBigLaTeXPage(self, bookname, title, hierarchy):        filename = "%s.tex" % bookname        f = open(filename, "w")        print >> f, "\\documentclass[a4paper]{article}"        print >> f, "\\usepackage{graphicx}"        print >> f, "\\usepackage{ucs}"        print >> f, "\\usepackage{html}"        print >> f, "\\title{%s}" % title        print >> f, "\\author{Ulrik Petersen}"	try:	    print >> f, "\\date{}" % DOC_DATE        except:            pass # If DOC_DATE isn't available, print distribution date by default        print >> f, "\\begin{document}"        print >> f, """\\newenvironment{precode}        {\\begin{list}{}{        \\setlength{\\rightmargin}{\\leftmargin}        \\setlength{\\listparindent}{0pt}% needed for AMS classes        \\raggedright        \\setlength{\\itemsep}{0pt}        \\setlength{\\parsep}{0pt}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -