Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions tests/cases.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,12 @@ class ReferenceCase:
pmid: int | None = None
arxiv: str | None = None

@property
def expected_text(self) -> str:
"""Expected text of the reference."""
# Currently, all texts can be infered from markdown.
return (self.expected_markdown.split(", DOI:")[0] + ".").replace("*", "")


TEST_CASES = [
# mainly from pubmed
Expand Down
67 changes: 65 additions & 2 deletions tests/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ def test_cli_from_to_stdout_markdown():
assert out.strip() == case.expected_markdown.strip()


def test_cli_from_to_file_markdow():
def test_cli_from_to_file_markdown():
"""Test wenxian from DOI to a certain file."""
case = TEST_CASES[0]
with tempfile.NamedTemporaryFile("w+") as f:
Expand All @@ -85,7 +85,7 @@ def test_cli_from_to_file_markdow():
assert f.read().strip() == case.expected_markdown.strip()


def test_cli_from_to_default_file_markdow():
def test_cli_from_to_default_file_markdown():
"""Test wenxian from DOI to the default file."""
case = TEST_CASES[0]
with tempfile.TemporaryDirectory() as tmpdirname:
Expand All @@ -108,6 +108,69 @@ def test_cli_from_to_default_file_markdow():
assert f.read().strip() == case.expected_markdown.strip()


def test_cli_from_to_stdout_text():
"""Test wenxian from DOI to stdout."""
case = TEST_CASES[0]
out = subprocess.check_output(
[
sys.executable,
"-m",
"wenxian",
"from",
case.reference.doi,
"--type",
"text",
],
text=True,
)
assert out.strip() == case.expected_text.strip()


def test_cli_from_to_file_text():
"""Test wenxian from DOI to a certain file."""
case = TEST_CASES[0]
with tempfile.NamedTemporaryFile("w+") as f:
subprocess.check_call(
[
sys.executable,
"-m",
"wenxian",
"from",
case.reference.doi,
"-o",
f.name,
"--type",
"text",
],
text=True,
)
f.seek(0)
assert f.read().strip() == case.expected_text.strip()


def test_cli_from_to_default_file_text():
"""Test wenxian from DOI to the default file."""
case = TEST_CASES[0]
with tempfile.TemporaryDirectory() as tmpdirname:
subprocess.check_call(
[
sys.executable,
"-m",
"wenxian",
"from",
case.reference.doi,
"-o",
"--type",
"text",
],
text=True,
cwd=tmpdirname,
)
with open(Path(tmpdirname) / (case.reference.key + ".txt")) as f:
# The default file is references.txt
assert f.read().strip() == case.expected_text.strip()


def test_cli_ignore_errors():
"""Test wenxian from DOI to stdout."""
# expected success
Expand Down
8 changes: 8 additions & 0 deletions tests/test_output.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,11 @@ def test_bibtex(reference: Reference, expected):
def test_markdown(reference: Reference, expected):
"""Test generating Markdown from references."""
assert reference.markdown.strip() == expected


@pytest.mark.parametrize(
"reference, expected", [(cc.reference, cc.expected_text) for cc in TEST_CASES]
)
def test_text(reference: Reference, expected):
"""Test generating text from references."""
assert reference.text.strip() == expected
4 changes: 4 additions & 0 deletions wenxian/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ def cmd_from(
buff.append(ref.bibtex)
elif output_type == "markdown":
buff.append(ref.markdown)
elif output_type == "text":
buff.append(ref.text)
else:
raise ValueError(f"Unknown output type: {output_type}")
if output is None:
Expand All @@ -51,6 +53,7 @@ def cmd_from(
extension = {
"bibtex": ".bib",
"markdown": ".md",
"text": ".txt",
}.get(output_type)
if extension is None:
raise ValueError(f"Unknown output type: {output_type}")
Expand Down Expand Up @@ -102,6 +105,7 @@ def main_parser() -> argparse.ArgumentParser:
choices=[
"bibtex",
"markdown",
"text",
],
default="bibtex",
help="Output type.",
Expand Down
16 changes: 13 additions & 3 deletions wenxian/reference.py
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,14 @@ def bibtex(self) -> str:
@property
def markdown(self) -> str:
"""Generate a Markdown for this reference."""
return self._markdown_or_text(markdown=True)

@property
def text(self) -> str:
"""Generate a plain text for this reference."""
return self._markdown_or_text(markdown=False)

def _markdown_or_text(self, markdown: bool) -> str:
if self.author is None:
author_string = "Unknown Author"
else:
Expand All @@ -244,20 +252,22 @@ def markdown(self) -> str:
for ss in (
author_string,
self.title,
f"*{self.journal_abbr}*" if self.journal_abbr is not None else None,
(f"*{self.journal_abbr}*" if markdown else self.journal_abbr)
if self.journal_abbr is not None
else None,
self.year,
self.volume,
page_string,
f"DOI: [{self.doi}](https://doi.org/{self.doi})"
if self.doi is not None
if self.doi is not None and markdown
else None,
)
if ss is not None
)
+ "."
+ (
f" [![Citations](https://citations.njzjz.win/{self.doi})](https://badge.dimensions.ai/details/doi/{self.doi})"
if self.doi is not None
if self.doi is not None and markdown
else ""
)
+ "\n"
Expand Down