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 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)