示例#1
0
def test_duplicate_key():
    text_str = """
    \\newacronym{thekey}{OTHER}{Abbreviation of other}
    \\newglossaryentry{thekey}{
        name={name},
        description={the description},
        type={symbols}
    }
    """
    with pytest.raises(KeyError):
        parse_tex(text_str=text_str)
示例#2
0
def test_parse_gterms():

    text_str = """
    \\newglossaryentry{otherkey}{
        name={other name},
        description={the description of other}
    }
    \\newglossaryentry{thekey}{
        name={name},
        description={the description},
        type={symbols}
    }
    """
    gterms, acronyms = parse_tex(text_str=text_str)
    assert acronyms == {}
    assert gterms == {
        "otherkey": {
            "description": "the description of other",
            "name": "other name"
        },
        "thekey": {
            "description": "the description",
            "name": "name",
            "type": "symbols"
        },
    }
示例#3
0
    def load_tex(self, text_str=None, path=None, encoding='utf8',
                 skip_ioerrors=False, ignore_unknown_types=True):
        """load a tex file

        Parameters
        ----------
        text_str=None: str or None
            string representing the bib file contents
        path=None: str or None
            path to bibfile
        bibdb=None: bibtexparser.bibdatabase.BibDatabase or None
        encoding="utf8": str
            bib file encoding
        skip_ioerrors: bool
            if False, an IOError will be raised if
            newglossaryterm or newacronym is badly formatted
        ignore_unknown_types: bool
            if True, strip unknown types, otherwise raise a ValueError

        Notes
        -----
        the texsoup package is required.

        if a newglossaryterm has field 'type={symbols}', then
        it will be loaded as a symbol

        """
        from ipypublish.bib2glossary.parse_tex import parse_tex
        gterms, acronyms = parse_tex(
            text_str=text_str, path=path, encoding=encoding,
            skip_ioerrors=skip_ioerrors
        )
        entries = {}
        for key, fields in gterms.items():

            fields["ENTRYTYPE"] = ETYPE_GLOSS
            if fields.get("type", None) == "symbols":
                fields["ENTRYTYPE"] = ETYPE_SYMBOL
                fields.pop("type")
            elif "type" in fields:
                if not ignore_unknown_types:
                    raise ValueError("the 'type' is not recognised: "
                                     "{}".format(fields['type']))
                fields.pop("type")

            fields["ID"] = key
            entry = BibGlossEntry(fields)
            entries[entry.key] = entry

        for key, fields in acronyms.items():
            fields["ENTRYTYPE"] = ETYPE_ACRONYM
            fields["ID"] = key
            entry = BibGlossEntry(fields)
            entries[entry.key] = entry

        self._entries = entries

        return True
示例#4
0
def test_ioerror_skip():
    text_str = """
    \\newacronym{thekey}{Abbreviation of other}
    \\newacronym{thekey2}{ABBR}{Abbreviation of other}
    """
    gterms, acronyms = parse_tex(text_str=text_str, skip_ioerrors=True)
    assert gterms == {}
    assert acronyms == {
        "thekey2": {
            "abbreviation": "ABBR",
            "longname": "Abbreviation of other"
        }
    }
示例#5
0
def test_ioerror_skip():
    text_str = """
    \\newacronym{thekey}{Abbreviation of other}
    \\newacronym{thekey2}{ABBR}{Abbreviation of other}
    """
    gterms, acronyms = parse_tex(text_str=text_str, skip_ioerrors=True)
    assert gterms == {}
    assert acronyms == {
        "thekey2": {
            'abbreviation': 'ABBR',
            'longname': 'Abbreviation of other'
        }
    }
示例#6
0
def test_parse_acronyms():

    text_str = """
    \\newacronym{otherkey}{OTHER}{Abbreviation of other}
    \\newacronym{thekey}{ABRV}{Abbreviation}
    """
    gterms, acronyms = parse_tex(text_str=text_str)
    assert gterms == {}
    assert acronyms == {
        'otherkey': {
            'abbreviation': 'OTHER',
            'longname': 'Abbreviation of other'},
        'thekey': {
            'abbreviation': 'ABRV',
            'longname': 'Abbreviation'}
    }
示例#7
0
def test_parse_acronyms():

    text_str = """
    \\newacronym{otherkey}{OTHER}{Abbreviation of other}
    \\newacronym{thekey}{ABRV}{Abbreviation}
    """
    gterms, acronyms = parse_tex(text_str=text_str)
    assert gterms == {}
    assert acronyms == {
        "otherkey": {
            "abbreviation": "OTHER",
            "longname": "Abbreviation of other"
        },
        "thekey": {
            "abbreviation": "ABRV",
            "longname": "Abbreviation"
        },
    }
示例#8
0
def test_parse_acronyms_with_options():

    text_str = """
    \\newacronym[description={a description}]{otherkey}{OTHER}{Abbreviation of other}
    \\newacronym[plural={ABRVs},longplural={Abbreviations}]{thekey}{ABRV}{Abbreviation}
    """  # noqa: E501
    gterms, acronyms = parse_tex(text_str=text_str)
    assert gterms == {}
    assert acronyms == {
        'otherkey': {
            'abbreviation': 'OTHER',
            'longname': 'Abbreviation of other',
            'description': 'a description'},
        'thekey': {
            'abbreviation': 'ABRV',
            'longname': 'Abbreviation',
            'longplural': 'Abbreviations',
            'plural': 'ABRVs'}
    }
示例#9
0
def test_parse_acronyms_with_options():

    text_str = """
    \\newacronym[description={a description}]{otherkey}{OTHER}{Abbreviation of other}
    \\newacronym[plural={ABRVs},longplural={Abbreviations}]{thekey}{ABRV}{Abbreviation}
    """  # noqa: E501
    gterms, acronyms = parse_tex(text_str=text_str)
    assert gterms == {}
    assert acronyms == {
        "otherkey": {
            "abbreviation": "OTHER",
            "longname": "Abbreviation of other",
            "description": "a description",
        },
        "thekey": {
            "abbreviation": "ABRV",
            "longname": "Abbreviation",
            "longplural": "Abbreviations",
            "plural": "ABRVs",
        },
    }
示例#10
0
def test_parse_mixed():
    text_str = """
    \\newacronym{otherkey}{OTHER}{Abbreviation of other}
    \\newglossaryentry{thekey}{
        name={name},
        description={the description},
        type={symbols}
    }
    """
    gterms, acronyms = parse_tex(text_str=text_str)
    assert acronyms == {
        'otherkey': {
            'abbreviation': 'OTHER',
            'longname': 'Abbreviation of other'}
    }
    assert gterms == {
        'thekey': {
            'description': 'the description',
            'name': 'name',
            'type': 'symbols'}
    }
示例#11
0
def test_parse_mixed():
    text_str = """
    \\newacronym{otherkey}{OTHER}{Abbreviation of other}
    \\newglossaryentry{thekey}{
        name={name},
        description={the description},
        type={symbols}
    }
    """
    gterms, acronyms = parse_tex(text_str=text_str)
    assert acronyms == {
        "otherkey": {
            "abbreviation": "OTHER",
            "longname": "Abbreviation of other"
        }
    }
    assert gterms == {
        "thekey": {
            "description": "the description",
            "name": "name",
            "type": "symbols"
        }
    }
示例#12
0
def test_parse_gterms():

    text_str = """
    \\newglossaryentry{otherkey}{
        name={other name},
        description={the description of other}
    }
    \\newglossaryentry{thekey}{
        name={name},
        description={the description},
        type={symbols}
    }
    """
    gterms, acronyms = parse_tex(text_str=text_str)
    assert acronyms == {}
    assert gterms == {
        'otherkey': {
            'description': 'the description of other',
            'name': 'other name'},
        'thekey': {
            'description': 'the description',
            'name': 'name',
            'type': 'symbols'}
    }
示例#13
0
def test_gterm_ioerror():
    text_str = """
    \\newglossaryentry{}
    """
    with pytest.raises(IOError):
        parse_tex(text_str=text_str)
示例#14
0
def test_acronym_ioerror():
    text_str = """
    \\newacronym{thekey}{Abbreviation of other}
    """
    with pytest.raises(IOError):
        parse_tex(text_str=text_str)