def handle_starttag(self, tag, attrs): if tag == "a": for name,value in attrs: if name == "href" and value.startswith("/"): if value.startswith("/"): # manual wants an absolute path, the help manual does not support # absolute path, so make sure that the image exists where the # absolute path indicates, then make the path into a relative path # with the approriate number of updirs test = os.path.join(self.options.indir, value[1:]) if not os.path.exists(test): raise IOError("Cannot find %s in base path" % (value)) # try find a valid relative path subdirdepth = len(self.subdir.split(os.path.sep)) prefix = "../"*subdirdepth relpath = os.path.join(prefix, value[1:]) if not os.path.exists(os.path.join(self.options.indir, self.subdir,relpath)): raise Exception("Cannot relativize path: %s" % (value)) else: attrs = update_attribute(attrs, 'src', relpath) value = relpath if name == "href" and not value.startswith("http://"): # make sure the file being refered to exists in stage3 check_ref = change_filename(value, ".stage3", ".", self.files['stage3'], makedirs=False) if not os.path.exists(check_ref): logging.debug(" File (%s) does not exist to be bundled into archive!", check_ref) fixed_ref = change_filename(value, ".htm", ".", ".", makedirs=False) attrs = update_attribute(attrs, "href", fixed_ref) OutputParser.handle_starttag(self, tag, attrs)
def process_input_stage2(file, options, files, helparray): infile = open(file, mode="r") input = infile.read() infile.close() outname = change_filename(file, ".stage2", files['stage1'], files['stage2']) outfile = open(outname, mode="w") parser = Stage2Parser(file=outfile) parser.feed(input) parser.close() outfile.close() if len(parser.control) > 0: filename_in_archive = change_filename(outname, ".htm", files['stage2'], ".", makedirs=False) for control in parser.control: helparray.append((control, filename_in_archive)) logging.debug(" Control name %s", control) return outname
def process_input_stage1(file, options, files): infile = open(file, mode="r") input = infile.read() infile.close() md = markdown.Markdown( extensions=['toc'] ) output = md.convert(input) outfile_name = change_filename(file, ".stage1", options.indir, files['stage1']) outfile = open(outfile_name, mode="w") outfile.write(output) outfile.close() return outfile_name
def process_input_stage3(file, options, files, extrafiles): infile = open(file, mode="r") input = infile.read() infile.close() outname = change_filename(file, ".stage3", files['stage2'], files['stage3']) outfile = open(outname, mode="w") #figure out what subdirectory of the onlinehelp I am in subdir = string.replace(os.path.dirname(outname), os.path.normpath(files['stage3']), "") if subdir.startswith(os.path.sep): subdir = string.replace(subdir, os.path.sep, "", 1) # I only want to remove the leading sep parser = Stage3Parser(options, files, file=outfile, extrafiles=extrafiles, subdir=subdir) parser.feed(input) parser.close() outfile.close() return outname
def process_input_stage3(file, options, files, extrafiles): infile = open(file, mode="r") input = infile.read() infile.close() outname = change_filename(file, ".stage3", files['stage2'], files['stage3']) outfile = open(outname, mode="w") #figure out what subdirectory of the onlinehelp I am in subdir = os.path.dirname(outname).replace(os.path.normpath(files['stage3']), "") if subdir.startswith(os.path.sep): subdir = subdir.replace(os.path.sep, "", 1) # I only want to remove the leading sep parser = Stage3Parser(options, files, file=outfile, extrafiles=extrafiles, subdir=subdir) parser.feed(input) parser.close() outfile.close() return outname
def process_input_stage6(options, stage_dirs): #make sure that the directories exist before creating file outfile_path = os.path.dirname(options.outfile) if os.path.exists(outfile_path): if os.path.isdir(outfile_path): pass else: log.error(" %s exists but is not a directory", outfile_path) else: os.makedirs(outfile_path) outzip = zipfile.ZipFile(options.outfile, mode="w", compression=zipfile.ZIP_DEFLATED) for path, dirs, files in os.walk(stage_dirs['stage5']): for filename in files: full_filename = os.path.join(path, filename) arcname = change_filename(full_filename, None, stage_dirs['stage5'], ".", False) logging.debug(" Added %s as %s", full_filename, arcname) outzip.write(full_filename, arcname) outzip.close()
def process_input_stage4(file, options, files): """Fix the a tags so that they point to the htm files that will be in the output archive.""" infile = open(file, mode="r") input = infile.read() infile.close() outname = change_filename(file, ".stage4", files['stage3'], files['stage4']) outfile = open(outname, mode="w") #figure out what subdirectory of the onlinehelp I am in subdir = string.replace(os.path.dirname(outname), os.path.normpath(files['stage4']), "") if subdir.startswith(os.path.sep): subdir = string.replace(subdir, os.path.sep, "", 1) # I only want to remove the leading sep parser = Stage4Parser(files=files, file=outfile, options=options, subdir=subdir) parser.feed(input) parser.close() outfile.close() return outname
def process_input_stage4(file, options, files): """Fix the a tags so that they point to the htm files that will be in the output archive.""" infile = open(file, mode="r") input = infile.read() infile.close() outname = change_filename(file, ".stage4", files['stage3'], files['stage4']) outfile = open(outname, mode="w") #figure out what subdirectory of the onlinehelp I am in subdir = os.path.dirname(outname).replace(os.path.normpath(files['stage4']), "") if subdir.startswith(os.path.sep): subdir = subdir.replace(os.path.sep, "", 1) # I only want to remove the leading sep parser = Stage4Parser(files=files, file=outfile, options=options, subdir=subdir) parser.feed(input) parser.close() outfile.close() return outname
def handle_startendtag(self, tag, attrs): """Find the image and copy it to the stage3 folder where it should be in the file output.""" ALT_INDEX = None if tag == "img": # figure out which attribute is src for x in range(0, len(attrs)): if attrs[x][0] == "src": SRC_INDEX = x elif attrs[x][0] == "alt": ALT_INDEX = x if attrs[SRC_INDEX][1].startswith("/"): # manual wants an absolute path, the help manual does not support # absolute path, so make sure that the image exists where the # absolute path indicates, then make the path into a relative path # with the approriate number of updirs test = os.path.join(self.options.indir, attrs[SRC_INDEX][1][1:]) if not os.path.exists(test): raise IOError("Cannot find %s in base path"%(attrs[SRC_INDEX][1])) # try find a valid relative path subdirdepth = len(self.subdir.split(os.path.sep)) prefix = "../"*subdirdepth relpath = os.path.join(prefix, attrs[SRC_INDEX][1][1:]) if not os.path.exists(os.path.join(self.options.indir, self.subdir,relpath)): raise Exception("Cannot relativize path: %s"%(attrs[SRC_INDEX][1])) else: attrs = update_attribute(attrs, 'src', relpath) location1 = os.path.join(self.options.indir, self.subdir, attrs[SRC_INDEX][1]) location = os.path.normpath(location1) # check to make sure that the image I am including was in the onlinehelp # folder, if not change the dst name so that it will be located # correctly in the stage3 directory logging.debug("%s - %s", location, self.options.indir) if location.startswith(self.options.indir): dst1 = os.path.join(self.files['stage3'], self.subdir, attrs[SRC_INDEX][1]) dst = os.path.normpath(dst1) else: # get extention basename = os.path.basename(attrs[SRC_INDEX][1]) (name, ext) = os.path.splitext(basename) (file, outname) = tempfile.mkstemp(ext, name, self.files['stage3']) dst1 = outname.replace(os.getcwd(), ".") # make into a relative path # fix up attrs dst = os.path.normpath(dst1) attrs = update_attribute(attrs, 'src', change_filename(dst, os.path.splitext(dst)[1], self.files['stage3'], ".", makedirs=False)) if ALT_INDEX is None: ALT_INDEX = SRC_INDEX logging.debug(" Image (%s) should be in %s and copying to %s", attrs[ALT_INDEX][1], location, dst) try: if not os.path.exists(os.path.dirname(dst)): os.mkdir(os.path.dirname(dst)) shutil.copy2(location, dst) except: traceback.print_exc() logging.error(" '%s' does not exist", location ) sys.exit(3) self.extrafiles.append(dst) OutputParser.handle_startendtag(self, tag, attrs)
def process_input_stage5(options, files, extrafiles): """Generate the index and table of contents for the output file from the output of stage4.""" # write header file headerfile_name = os.path.join(files['stage5'], "header.hhp") headerfile = open(headerfile_name, mode="w") headerfile.write( """Contents file=%(contents)s\nIndex file=%(index)s\nTitle=%(title)s\nDefault topic=%(default)s\nCharset=UTF-8\n""" % { "contents": "contents.hhc", "index": "index.hhk", "title": "wxLauncher User Guide", "default": "index.htm" }) headerfile.close() # generate both index and table of contents tocfile_name = os.path.join(files['stage5'], "contents.hhc") tocfile = open(tocfile_name, mode="w") tocfile.write("<ul>\n") toclevel = 1 indexfile_name = os.path.join(files['stage5'], "index.hhk") indexfile = open(indexfile_name, mode="w") indexfile.write("<ul>\n") help_files = generate_file_list(files['stage4'], ".stage4") last_path_list = [] for path, dirs, thefiles in os.walk(files['stage4']): """new directory. If the directory is not the base directory then check if there will be an index.htm for this directory. If there is an index.htm then parse it for the title so that the subsection will have the title of the index.htm as the name of the subsection. If there is no index.htm then make a default one and use the name of the directory as the subsection name. Note that this algorithm requires that os.walk generates the names in alphabetical order.""" # relativize directory path for being in the archive path_in_arc = make_path_in_archive(path, files['stage4']) logging.debug("Processing directory '%s'", path_in_arc) path_list = path_in_arc.split(os.path.sep) if len(path_list) == 1 and path_list[0] == '': path_list = [] level = len(path_list) # parse the index.help to get the name of the section index_file_name = os.path.join(path, "index.stage4") # relativize filename for being in the archive index_in_archive = change_filename(index_file_name, ".htm", files['stage4'], ".", False) index_in_archive = index_in_archive.replace(os.path.sep, "/") # make the separators the same so that it doesn't matter what platform the launcher is built on. # find the title outindex_name = change_filename(index_file_name, ".htm", files['stage4'], files['stage5']) outindex = open(outindex_name, mode="w") inindex = open(index_file_name, mode="r") input = inindex.read() inindex.close() parser = Stage5Parser(file=outindex) parser.feed(input) parser.close() outindex.close() tocfile.write(generate_sections(path_list, last_path_list,index_filename=index_in_archive, section_title=parser.title)) last_path_list = path_list if level > 0: # remove index.htm from thefiles because they are used by the section names try: thefiles.remove('index.stage4') except ValueError: logging.warning("Directory %s does not have an index.help", path_in_arc) for file in thefiles: full_filename = os.path.join(path, file) # relativize filename for being in the archive filename_in_archive = change_filename(full_filename, ".htm", files['stage4'], ".", False) # find the title outfile_name = change_filename(full_filename, ".htm", files['stage4'], files['stage5']) outfile = open(outfile_name, mode="w") infile = open(full_filename, mode="r") input = infile.read() infile.close() parser = Stage5Parser(file=outfile) parser.feed(input) parser.close() outfile.close() tocfile.write( """%(tab)s<li> <object type="text/sitemap">\n%(tab)s <param name="Name" value="%(name)s">\n%(tab)s <param name="Local" value="%(file)s">\n%(tab)s </object>\n""" % { "tab": " "*level, "name": parser.title, "file": filename_in_archive, }) indexfile.write( """%(tab)s<li> <object type="text/sitemap">\n%(tab)s\t<param name="Name" value="%(name)s">\n%(tab)s\t<param name="Local" value="%(file)s">\n%(tab)s </object>\n""" % { "tab": "\t", "name": parser.title, "file": filename_in_archive, }) tocfile.write(generate_sections([], last_path_list)) tocfile.write("</ul>\n") tocfile.close() indexfile.close() # copy the extra files (i.e. images) from stage3 for extrafilename in extrafiles: logging.debug(" Copying: %s", extrafilename) dst = change_filename(extrafilename, None, orginaldir=files['stage3'], destdir=files['stage5']) shutil.copy2(extrafilename, dst)
def process_input_stage5(options, files, extrafiles): """Generate the index and table of contents for the output file from the output of stage4.""" # write header file headerfile_name = os.path.join(files['stage5'], "header.hhp") headerfile = open(headerfile_name, mode="w") headerfile.write( """Contents file=%(contents)s\nIndex file=%(index)s\nTitle=%(title)s\nDefault topic=%(default)s\nCharset=UTF-8\n""" % { "contents": "contents.hhc", "index": "index.hhk", "title": "wxLauncher User Guide", "default": "index.htm" }) headerfile.close() # generate both index and table of contents tocfile_name = os.path.join(files['stage5'], "contents.hhc") tocfile = open(tocfile_name, mode="w") tocfile.write("<ul>\n") toclevel = 1 indexfile_name = os.path.join(files['stage5'], "index.hhk") indexfile = open(indexfile_name, mode="w") indexfile.write("<ul>\n") help_files = generate_file_list(files['stage4'], ".stage4") last_path_list = [] for path, dirs, thefiles in os.walk(files['stage4']): """new directory. If the directory is not the base directory then check if there will be an index.htm for this directory. If there is an index.htm then parse it for the title so that the subsection will have the title of the index.htm as the name of the subsection. If there is no index.htm then make a default one and use the name of the directory as the subsection name. Note that this algorithm requires that os.walk generates the names in alphabetical order.""" # relativize directory path for being in the archive path_list = find_path_in_archive(path, files['stage4']) logging.debug("Processing directory '%s'", os.path.sep.join(path_list)) if len(path_list) == 1 and path_list[0] == '': path_list = [] level = len(path_list) # parse the index.help to get the name of the section index_file_name = os.path.join(path, "index.stage4") # relativize filename for being in the archive index_in_archive = change_filename(index_file_name, ".htm", files['stage4'], ".", False) index_in_archive = index_in_archive.replace(os.path.sep, "/") # make the separators the same so that it doesn't matter what platform the launcher is built on. # find the title outindex_name = change_filename(index_file_name, ".htm", files['stage4'], files['stage5']) outindex = open(outindex_name, mode="w") inindex = open(index_file_name, mode="r") input = inindex.read() inindex.close() parser = Stage5Parser(file=outindex) parser.feed(input) parser.close() outindex.close() tocfile.write(generate_sections(path_list, last_path_list,index_filename=index_in_archive, section_title=parser.title)) last_path_list = path_list if level > 0: # remove index.htm from thefiles because they are used by the section names try: thefiles.remove('index.stage4') except ValueError: logging.warning("Directory %s does not have an index.help", os.path.sep.join(path_list)) for file in thefiles: full_filename = os.path.join(path, file) # relativize filename for being in the archive filename_in_archive = change_filename(full_filename, ".htm", files['stage4'], ".", False) # find the title outfile_name = change_filename(full_filename, ".htm", files['stage4'], files['stage5']) outfile = open(outfile_name, mode="w") infile = open(full_filename, mode="r") input = infile.read() infile.close() parser = Stage5Parser(file=outfile) parser.feed(input) parser.close() outfile.close() tocfile.write( """%(tab)s<li> <object type="text/sitemap">\n%(tab)s <param name="Name" value="%(name)s">\n%(tab)s <param name="Local" value="%(file)s">\n%(tab)s </object>\n""" % { "tab": " "*level, "name": parser.title, "file": filename_in_archive, }) indexfile.write( """%(tab)s<li> <object type="text/sitemap">\n%(tab)s\t<param name="Name" value="%(name)s">\n%(tab)s\t<param name="Local" value="%(file)s">\n%(tab)s </object>\n""" % { "tab": "\t", "name": parser.title, "file": filename_in_archive, }) tocfile.write(generate_sections([], last_path_list)) tocfile.write("</ul>\n") tocfile.close() indexfile.close() # copy the extra files (i.e. images) from stage3 for extrafilename in extrafiles: logging.debug(" Copying: %s", extrafilename) dst = change_filename(extrafilename, None, orginaldir=files['stage3'], destdir=files['stage5']) shutil.copy2(extrafilename, dst)