def main(conf): bibfmt_module = conf.bibfmt_module AVAIL_INDICES = [bibfmt_module.KEYWORDS, bibfmt_module.CITEKEY] if not conf.args.index in AVAIL_INDICES: logging.critical("Not a valid choice: {}. Available options are: {}".format( conf.args.index, ",".join(AVAIL_INDICES))) return 1 try: bibfile = open(conf.args.bibfile, 'r') except Exception as e: logging.critical("Could not open file: {}".format(e)) return 1 try: bibfmt = bibfmt_module.BibFmt(bibfile) bibfmt.build_index(conf.args.index) if conf.args.value[0] != "-": values = (x for x in conf.args.value) else: values = (x.strip() for x in sys.stdin) # Perform query query_result = andor_query(bibfmt, conf.args.index, values) # Show results if len(query_result) == 0: logging.info("No matches.") else: for filepos in sorted(query_result): if conf.args.copy is not None: if not os.path.isdir(conf.args.copy): logging.critical("Not a valid path: {}".format(conf.args.copy)) return 1 querydict = bibfmt.read_entry_dict(filepos) filepath = querydict["file"] if conf.args.rename: destpath = os.path.join(conf.args.copy, gen_filename_from_bib(querydict)) else: destpath = conf.args.copy logging.info("Copying: '{}' to '{}'".format(filepath, destpath)) shutil.copy(os.path.expanduser(filepath), destpath) else: print(bibfmt.read_entry_raw(filepos)) finally: bibfile.close()
def process_filepos(filepos): querydict = bibfmt.read_entry_dict(filepos) raw = bibfmt.read_entry_raw(filepos) lines = [] def re_cite_replace(m): links = [] for ck in m.group(1).split(","): ck = ck.strip() links.append("<a href=\"/citekey/{citekey}\">{citekey}</a>".format(citekey=ck)) return "\\cite{{{}}}".format(", ".join(links)) # FIXME: Modifying URLs into links only works with BibTeX bibliographies # right now. for line in raw.split("\n"): # Get indenting spaces html_whitespace = [] for i in range(len(line)): if line[i] != " ": break html_whitespace.append(" ") html_whitespace = "".join(html_whitespace) line = line.strip() if line.startswith("@"): if 'file' in querydict: html = bottle.template("<a href=\"/file/{{citekey}}/{{dlname}}\">{{line}}</a>", citekey=querydict['citekey'], dlname=gen_filename_from_bib(querydict), line=line) else: html = bottle.template("{{line}}", line=line) elif line.startswith("file "): continue elif line == "}": html = bottle.template("{{line}}", line=line) elif "http" in line: html = bottle.template("{{line}}", line=line) html = re.sub("(http[^} ]*)([} ]|$)", "<a href=\"\\1\">\\1</a>\\2", html) elif "\\cite{" in line: html = bottle.template("{{line}}", line=line) html = re.sub("\\\\cite{([^}]*)}", re_cite_replace, html) else: html = bottle.template("{{line}}", line=line) lines.append(html_whitespace + html) return lines
def __call__(self): for path in self.walk_path(): # Check existing entries if self.query_exists(bibfmt_module.FILE, path) is not None: continue # Generate new entry new_entry_args = dict( reftype="misc", citekey="TODO:{}".format(os.path.basename(path)), author="", title="", year="", keywords="", file=path, annotation="", date_added=datetime.date.today().strftime("%Y-%m-%d")) if self.conf.args.hash: new_entry_args["md5"] = gen_hash_md5( os.path.expanduser(path)).hexdigest() # Before we proceed, check if this file is a duplicate of an # already existing file, and if so, check existing entry is # still valid; if not valid replace file, otherwise warn user. if self.check_hash(new_entry_args["md5"], path): continue if self.conf.args.remote: logging.info("Attempting to fetch bibliography information remotely: {}".format( path)) new_entry_args.update(self.conf.bibfetch(filename=path)) if self.conf.args.interactive: new_entry_args = self.interactive_corrections(new_entry_args) if self.conf.args.interactive and self.conf.args.rename: newpath = os.path.join(os.path.dirname(path), gen_filename_from_bib(new_entry_args)) logging.info("Rename: {} to {}".format(path, newpath)) shutil.move(os.path.expanduser(path), os.path.expanduser(newpath)) new_entry_args["file"] = newpath path = newpath # Before we add the new entry, check for duplicate cite-keys citekey_exists_in = self.query_exists(bibfmt_module.CITEKEY, new_entry_args["citekey"]) if citekey_exists_in is not None: logging.debug("Cite-key already exists in '{}': {}".format( citekey_exists_in.bibfile.name, new_entry_args["citekey"])) for c in string.ascii_letters: newcitekey = new_entry_args["citekey"] + c if self.query_exists(bibfmt_module.CITEKEY, newcitekey) is None: break new_entry_args["citekey"] = newcitekey # Finally, generate new entry if self.conf.args.append: logging.info("Appending new entry for: {}".format(path)) self.bibfmt_main.append_new_entry(**new_entry_args) else: self.bibfmt_main.print_new_entry(**new_entry_args)