mirror of
https://github.com/HKUDS/DeepTutor.git
synced 2026-12-02 02:29:28 +08:00
@string blocks were skipped, so a field written as journal=jname kept the bare macro name in the generated Markdown. Collect the definitions while scanning and expand bare words that name one; unknown words and numbers are left unchanged.
171 lines
4.6 KiB
Python
171 lines
4.6 KiB
Python
from __future__ import annotations
|
|
|
|
from deeptutor.utils.bibtex_converter import bibtex_to_markdown
|
|
|
|
SAMPLE_BIBTEX = r"""
|
|
@string{venue = {Advances}}
|
|
|
|
@article{vaswani2017attention,
|
|
author = {Vaswani, Ashish and Shazeer, Noam},
|
|
title = {Attention {Is} All You Need},
|
|
journal = {Advances in Neural Information Processing Systems},
|
|
year = {2017},
|
|
doi = {10.5555/3295222},
|
|
abstract = {The dominant models use recurrent networks.}
|
|
}
|
|
|
|
@inproceedings{devlin2019bert,
|
|
author = {Devlin, Jacob and Chang, Ming-Wei},
|
|
title = {"BERT": Pre-training of Deep Bidirectional Transformers},
|
|
booktitle = {NAACL},
|
|
year = 2019
|
|
}
|
|
"""
|
|
|
|
|
|
def test_bibtex_to_markdown_extracts_structured_entries() -> None:
|
|
result = bibtex_to_markdown(SAMPLE_BIBTEX, "references")
|
|
|
|
assert "Total entries: 2" in result
|
|
assert "## 1. Attention Is All You Need" in result
|
|
assert "**Authors:** Ashish Vaswani, Noam Shazeer" in result
|
|
assert "**Type:** Journal Article" in result
|
|
assert "10.5555/3295222" in result
|
|
assert "The dominant models use recurrent networks." in result
|
|
assert "@string" not in result
|
|
assert '## 2. "BERT": Pre-training' in result
|
|
assert "**Type:** Conference Paper" in result
|
|
|
|
|
|
def test_bibtex_to_markdown_keeps_value_with_nested_braces_intact() -> None:
|
|
text = r"""
|
|
@article{nested,
|
|
title = {A {Nested {Title}} Here},
|
|
year = {2020}
|
|
}
|
|
"""
|
|
|
|
result = bibtex_to_markdown(text)
|
|
|
|
assert "A Nested Title Here" in result
|
|
assert "2020" in result
|
|
|
|
|
|
def test_bibtex_to_markdown_skips_entry_with_unterminated_value() -> None:
|
|
text = """
|
|
@article{valid,
|
|
title = {Valid Entry},
|
|
year = {2020}
|
|
}
|
|
|
|
@article{broken,
|
|
title = {Unterminated
|
|
"""
|
|
|
|
result = bibtex_to_markdown(text)
|
|
|
|
assert "Valid Entry" in result
|
|
assert "broken" not in result
|
|
|
|
|
|
def test_bibtex_to_markdown_passes_non_bibtex_through() -> None:
|
|
assert bibtex_to_markdown("not a bibliography") == "not a bibliography"
|
|
|
|
|
|
def test_bibtex_to_markdown_keeps_entry_with_accent_quote_in_braces() -> None:
|
|
text = r"""
|
|
@article{mueller2020,
|
|
author = {M{\"u}ller, Hans and Smith, Jane},
|
|
title = {Deep Learning},
|
|
year = {2020}
|
|
}
|
|
|
|
@article{second2021,
|
|
title = {Second Paper},
|
|
year = {2021}
|
|
}
|
|
"""
|
|
|
|
result = bibtex_to_markdown(text)
|
|
|
|
assert "Total entries: 2" in result
|
|
assert "## 1. Deep Learning" in result
|
|
assert "mueller2020" in result
|
|
assert "## 2. Second Paper" in result
|
|
|
|
|
|
def test_bibtex_to_markdown_keeps_quoted_value_with_braced_quote() -> None:
|
|
text = r"""
|
|
@inproceedings{q2020,
|
|
title = "The {"}Quoted{"} Paper",
|
|
author = "Doe, Jane",
|
|
year = 2020
|
|
}
|
|
"""
|
|
|
|
result = bibtex_to_markdown(text)
|
|
|
|
assert '## 1. The "Quoted" Paper' in result
|
|
assert "**Authors:** Jane Doe" in result
|
|
assert "**Year:** 2020" in result
|
|
|
|
|
|
def test_bibtex_to_markdown_formats_jr_and_braced_author_names() -> None:
|
|
text = r"""
|
|
@article{king1963,
|
|
author = {King, Jr., Martin Luther and {Food and Agriculture Organization}},
|
|
title = {Letter},
|
|
year = {1963}
|
|
}
|
|
"""
|
|
|
|
result = bibtex_to_markdown(text)
|
|
|
|
assert "**Authors:** Martin Luther King Jr., Food and Agriculture Organization" in result
|
|
|
|
|
|
def test_bibtex_to_markdown_preserves_spaces_in_concatenated_values() -> None:
|
|
text = """
|
|
@article{joined,
|
|
title = "Attention " # {Is All } # "You Need",
|
|
author = "Doe, " # {Jane and } # "Smith, John",
|
|
abstract = {First sentence.} # " " # {Second sentence.},
|
|
year = 20 # 26
|
|
}
|
|
"""
|
|
|
|
result = bibtex_to_markdown(text)
|
|
|
|
assert "## 1. Attention Is All You Need" in result
|
|
assert "**Authors:** Jane Doe, John Smith" in result
|
|
assert "**Abstract:** First sentence. Second sentence." in result
|
|
assert "**Year:** 2026" in result
|
|
|
|
|
|
def test_bibtex_to_markdown_does_not_insert_spaces_between_joined_values() -> None:
|
|
text = """
|
|
@article{joined,
|
|
title = "Sub" # {word},
|
|
doi = "10.1000/" # "example",
|
|
year = 20 # 26
|
|
}
|
|
"""
|
|
|
|
result = bibtex_to_markdown(text)
|
|
|
|
assert "## 1. Subword" in result
|
|
assert "**DOI:** 10.1000/example" in result
|
|
assert "**Year:** 2026" in result
|
|
|
|
|
|
def test_string_macro_expands_in_later_fields() -> None:
|
|
markdown = bibtex_to_markdown(
|
|
'@string{jname = "Journal of Testing"}\n'
|
|
"@string{short = {J. Test.}}\n"
|
|
'@article{key, title={T}, year=2024, journal=jname, publisher=short # " Press"}'
|
|
)
|
|
|
|
assert "**Journal:** Journal of Testing" in markdown
|
|
assert "J. Test. Press" in markdown
|
|
assert "**Year:** 2024" in markdown
|