def add_label(html_path, url, line): """ Add the label "Archive" to the archive after the url in the line Return the line """ integrated_link = re.compile('\[\[' + utils.protect(url) + '(\|.*\]\])') matching = integrated_link.search(line) if matching == None: #It is not an integrated link effective_pattern = url new_url = effective_pattern + " [[" + utils.get_unexpanded_path(html_path) + "|(Archive)]]" else: #It is... effective_pattern = '[[' + url + matching.groups()[0] new_url = effective_pattern + " [[" + utils.get_unexpanded_path(html_path) + "|(Archive)]]" effective_pattern = utils.protect(effective_pattern) line = re.sub(effective_pattern, new_url, line) return line
def process_text(original_text, *bibtex): """ Core function: process the whole text * Track cite{} keys * Replace them * write the modified text :param original_text: String containing the text to process :param bibtex: bibtex file path(s). TODO: does not support multiple bibtex yet! It returns a status (bool == True if something goes wrong) and the updated text. :returns: tuple (bool, string) """ entries_hash = {} for thisbibtex in bibtex: # In case of a relative path thisbibtex = os.path.expanduser(thisbibtex) ########### # Bibtex ########### filedirectory = get_filedirectory(thisbibtex) theseentries_hash = get_entries(thisbibtex) # Append the filedirectory in each entry for el in theseentries_hash: theseentries_hash[el]["filedirectory"] = filedirectory entries_hash.update(theseentries_hash) citecommand = re.compile("cite{([0-9a-zA-Z]+)}") copy_text = original_text keys = citecommand.findall(copy_text) error = False ########### # Edit text ########### for key in keys: logger.debug("key %s", key) try: path = entries_hash[key]["file"] if entries_hash[key]["type"] == "article": pubtype = entries_hash[key]["journal"]["name"] else: pubtype = entries_hash[key]["type"] except KeyError: logger.error("Unknown key: %s", key) error = True continue # next key # Jabref codes path like ":/tmp/file.pdf:PDF" # or ":file.pdf:PDF" or ":file.pdf:label:PDF" # For the second case, jabref write comments with metadata (filepath) # path = re.sub('^:(.*):[A-Z]+', "\\1", path) path = re.sub("^(?:.*):(.*):[A-Z]+", "\\1", path) if path.startswith("/"): path = get_unexpanded_path(path) elif path.startswith("~/"): pass else: if filedirectory is None: # CHECKME: is it really none. The best would be to write a unittest # TODO... log print("filedirectory is none") print("the indication is missing in the bibtex") continue # next key path = os.path.join(entries_hash[key]["filedirectory"], path) path = get_unexpanded_path(path) # Modify the text. Use foo for bibtex data cite = "cite{" + key + "}" internal_link = "[[" + str(path) + "|" + key + ", " + pubtype + "]]" copy_text = re.sub(cite, internal_link, copy_text) return (error, copy_text)