def test_to_latex_dict(): bibgloss = BibGlossDB() bibgloss.load_bib(text_str=dedent(bib_str), ignore_nongloss_types=True) latex_dict = bibgloss.to_latex_dict() print(latex_dict) assert latex_dict == { ("glsacronym", "akey1"): [ ("\\newacronym[description={a description}]{" "akey1}{ABRV}{Abbreviation}") ], ("glsacronym", "akey2"): [ ("\\newacronym[plural={OTHERs}]{" "akey2}{OTHER}{Abbrev of other}") ], ("glsterm", "gtkey1"): [ "\\newglossaryentry{gtkey1}{", " description={the description},", " name={name}", "}", ], ("glsterm", "gtkey2"): [ "\\newglossaryentry{gtkey2}{", " description={the description of other},", " name={other name}", "}", ], ("glssymbol", "skey1"): [ "\\newglossaryentry{skey1}{", " description={the description of symbol},", " name={\\pi},", " type={symbols}", "}", ], }
def run_postprocess(self, stream, mimetype, filepath, resources): if "bibglosspath" not in resources: return stream, filepath, resources bibpath = resources["bibglosspath"] if not os.path.exists(str(bibpath)): self.logger.warning( "the bibglossary could not be converted, " "since its path does not exist: {}".format(bibpath)) return stream, filepath, resources bibname, extension = os.path.splitext(os.path.basename(bibpath)) outstr = None outext = None if extension in [".bib"]: if mimetype == "text/restructuredtext": pass elif mimetype == "text/latex": self.logger.info("converting bibglossary to tex") bibdb = BibGlossDB() bibdb.load_bib(path=str(bibpath), encoding=self.encoding) outstr = bibdb.to_latex_string() outext = ".tex" elif extension in [".tex"]: if mimetype == "text/latex": pass elif mimetype == "text/restructuredtext": self.logger.info("converting bibglossary to bibtex") bibdb = BibGlossDB() bibdb.load_tex(path=str(bibpath), encoding=self.encoding) outstr = bibdb.to_bib_string() outext = ".bib" else: self.logger.warning("the bibglossary could not be converted, " "since its file extension was not one of: " "bib, tex") if outstr is None: return stream, filepath, resources if sys.version_info < (3, 0): outstr = unicode(outstr, encoding=self.encoding) # noqa: F821 output_folder = filepath.parent.joinpath(self.files_folder) if not output_folder.exists(): output_folder.mkdir(parents=True) outfile = output_folder.joinpath(bibname + outext) self.logger.info("writing bibglossary: {}".format(outfile)) with outfile.open("w", encoding=self.encoding) as fh: fh.write(outstr) self.logger.debug("finished") return stream, filepath, resources
def test_to_latex_dict(): bibgloss = BibGlossDB() bibgloss.load_bib(text_str=dedent(bib_str), ignore_nongloss_types=True) latex_dict = bibgloss.to_latex_dict() print(latex_dict) assert latex_dict == { ('glsacronym', 'akey1'): [('\\newacronym[description={a description}]{' 'akey1}{ABRV}{Abbreviation}')], ('glsacronym', 'akey2'): [('\\newacronym[plural={OTHERs}]{' 'akey2}{OTHER}{Abbrev of other}')], ('glsterm', 'gtkey1'): [ '\\newglossaryentry{gtkey1}{', ' description={the description},', ' name={name}', '}' ], ('glsterm', 'gtkey2'): [ '\\newglossaryentry{gtkey2}{', ' description={the description of other},', ' name={other name}', '}' ], ('glssymbol', 'skey1'): [ '\\newglossaryentry{skey1}{', ' description={the description of symbol},', ' name={\\pi},', ' type={symbols}', '}' ] }
def test_to_bib_string(): bibgloss = BibGlossDB() bibgloss.load_bib(text_str=dedent(bib_str), ignore_nongloss_types=True) string = bibgloss.to_bib_string() assert re.search( "@glsacronym\\{akey1,.*@glsterm\\{gtkey1,.*@glssymbol\\{skey1.*", string, re.DOTALL)
def test_load_tex(): bibgloss = BibGlossDB() bibgloss.load_tex(text_str=dedent(tex_str)) assert {k: e.type for k, e in bibgloss.items()} == { "gtkey1": "glsterm", "akey1": "glsacronym", "skey1": "glssymbol", }
def test_load_tex(): bibgloss = BibGlossDB() bibgloss.load_tex(text_str=dedent(tex_str)) assert {k: e.type for k, e in bibgloss.items()} == { 'gtkey1': 'glsterm', 'akey1': 'glsacronym', 'skey1': 'glssymbol' }
def update_bibfile_cache(self, bibfile, mtime, encoding): """Parse *bibfile*, and store the parsed data, along with modification time *mtime*, in the bibtex cache. Parameters ---------- bibfile: str The bib file name. mtime: float The bib file's modification time. """ logger.info(bold("parsing bibtex file {0}... ".format(bibfile)), nonl=True) bibglossdb = BibGlossDB() bibglossdb.load(path=bibfile, encoding=encoding) logger.info("parsed {0} entries".format(len(bibglossdb))) env = self.state.document.settings.env env.bibgloss_cache.bibfiles[bibfile] = BibfileCache(mtime=mtime, data=bibglossdb)
def result_nodes(self, document, env, node, is_ref): """Transform reference node into a citation reference, and note that the reference was cited. """ keys = node['reftarget'].split(',') # Note that at this point, usually, env.bibgloss_cache.bibfiles # is still empty because the bibliography directive may not # have been processed yet, so we cannot get the actual entry. # Instead, we simply fake an entry with the desired key, and # fix the label at doctree-resolved time. This happens in # process_citation_references. refnodes = [ docutils_citation_ref_node(BibGlossDB.get_fake_entry_obj(key), document, classes=["bibglossary"]) for key in keys ] for key in keys: env.bibgloss_cache.add_cited(key, env.docname) return refnodes, []
def process_bibfile(self, bibfile, encoding): """Check if ``env.bibgloss_cache.bibfiles[bibfile]`` is still up to date. If not, parse the *bibfile*, and store parsed data in the bibtex cache. Parameters ---------- bibfile: str The bib file name. """ env = self.state.document.settings.env cache = env.bibgloss_cache.bibfiles # get modification time of bibfile try: mtime = os.path.getmtime(bibfile) except OSError: logger.warning( standout("could not open bibtex file {0}.".format(bibfile))) cache[bibfile] = BibfileCache( # dummy cache mtime=-float("inf"), data=BibGlossDB()) return # get cache and check if it is still up to date # if it is not up to date, parse the bibtex file # and store it in the cache logger.info(bold( "checking for {0} in bibtex cache... ".format(bibfile)), nonl=True) try: bibfile_cache = cache[bibfile] except KeyError: logger.info("not found") self.update_bibfile_cache(bibfile, mtime, encoding) else: if mtime != bibfile_cache.mtime: logger.info("out of date") self.update_bibfile_cache(bibfile, mtime, encoding) else: logger.info("up to date")
def test_to_dict(): bibgloss = BibGlossDB() bibgloss.load_bib(text_str=dedent(bib_str), ignore_nongloss_types=True) dct = bibgloss.to_dict() assert set(dct.keys()) == {"gtkey1", "gtkey2", "akey1", "akey2", "skey1"}
def test_load_bib_type_ignore(): bibgloss = BibGlossDB() bibgloss.load_bib(text_str=dedent(bib_str), ignore_nongloss_types=True) assert set(bibgloss.keys()) == {"gtkey1", "gtkey2", "akey1", "akey2", "skey1"}
def test_load_bib_type_error(): bibgloss = BibGlossDB() with pytest.raises(TypeError): bibgloss.load_bib(text_str=dedent(bib_str), ignore_nongloss_types=False)
def run(self): """Process .bib files, set file dependencies, and create a node that is to be transformed to the entries of the glossary. """ env = self.state.document.settings.env # create id and cache for this node # this id will be stored with the node # and is used to look up additional data in env.bibgloss_cache # (implementation note: new_serialno only guarantees unique # ids within a single document, but we need the id to be # unique across all documents, so we also include the docname # in the id) id_ = "bibtex-bibglossary-%s-%s" % (env.docname, env.new_serialno("bibgloss")) # set filter option if "filter" in self.options: if "all" in self.options: logger.warning(standout(":filter: overrides :all:")) if "notcited" in self.options: logger.warning(standout(":filter: overrides :notcited:")) if "cited" in self.options: logger.warning(standout(":filter: overrides :cited:")) try: filter_ = ast.parse(self.options["filter"]) except SyntaxError: logger.warning( standout("syntax error in :filter: expression") + " (" + self.options["filter"] + "); " "the option will be ignored") filter_ = ast.parse("cited") elif "all" in self.options: filter_ = ast.parse("True") elif "notcited" in self.options: filter_ = ast.parse("not cited") else: # the default filter: include only cited entries filter_ = ast.parse("cited") style = self.options.get("style", env.app.config.bibgloss_default_style) if style not in self._allowed_styles: logger.warning( "style '{}' not in allowed styles, defaulting to '{}'".format( style, self._default_style)) style = self._default_style bibcache = BibliographyCache( style=style, unsorted=("unsorted" in self.options), filter_=filter_, encoding=self.options.get( "encoding", self.state.document.settings.input_encoding), keyprefix=self.options.get("keyprefix", ""), labels={}, plurals={}, bibfiles=[], ) for bibfile in self.arguments[0].split(): # convert to normalized absolute path to ensure that the same file # only occurs once in the cache bibfile = os.path.normpath(env.relfn2path(bibfile.strip())[1]) # if the bibfile has been supplied with no extension, guess path bibfile = BibGlossDB().guess_path(bibfile) or bibfile self.process_bibfile(bibfile, bibcache.encoding) env.note_dependency(bibfile) bibcache.bibfiles.append(bibfile) env.bibgloss_cache.set_bibliography_cache(env.docname, id_, bibcache) return [BibGlossaryNode("", ids=[id_])]