Skip to content

Commit

Permalink
refactor(reference_citation): move stdio to main().
Browse files Browse the repository at this point in the history
fix(filter_reference): prepend ref- to citation reference link to align
with pandoc citation links. Include pandoc processed file up to refs
section in stdio.

test: update expected reference links.
  • Loading branch information
jfishe committed Dec 11, 2023
1 parent 47fcb57 commit f220ffa
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 46 deletions.
2 changes: 1 addition & 1 deletion setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ console_scripts =
delete_bullet_star = panvimwiki.filter.delete_bullet_star:main
delete_task_pending = panvimwiki.filter.delete_task_pending:main
do_nothing = panvimwiki.filter.do_nothing:main
reference_citation = panvimwiki.filter.reference_citation:filter_reference
reference_citation = panvimwiki.filter.reference_citation:main
# script_name = panvimwiki.module:function
# For example:
# console_scripts =
Expand Down
29 changes: 15 additions & 14 deletions src/panvimwiki/filter/reference_citation.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,30 +85,31 @@
import sys


def filter_reference(source: str | None = None) -> str | None:
def filter_reference(source: str) -> str | None:
"""Convert pandoc citeproc CSL references to explicit reference links.
`Reference links <https://pandoc.org/MANUAL.html#reference-links>`_.
"""
if source is None:
source = sys.stdin.read()
isstdio = True
else:
isstdio = False

lines = []
for result in re.findall("::: {#ref-(.*?):::", source, re.S):
entry = result.split("\n")
reference = f"[#{entry[0].split()[0]}]:"
reference = f"[#ref-{entry[0].split()[0]}]:"
citation = "\n".join(entry[1:])
lines.append(f"{reference} {citation}")
lines = "\n".join(lines)
return "\n".join(lines)


if isstdio:
print(lines)
return None
return lines
def main():
"""Echo stdin and append filtered references."""
source = sys.stdin.read()
m = re.search(
r"::: {#refs \.references \.csl-bib-body \.hanging-indent}", string=source
)
if m is not None:
print(source[: m.start()])
print(filter_reference(source))
return None


if __name__ == "__main__":
filter_reference()
main()
41 changes: 18 additions & 23 deletions src/panvimwiki/wiki2pandoc.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,35 +14,30 @@


@bridged
def expand_citeproc(path: str) -> str | None:
"""Pandoc returns expanded citeproc references.
def expand_citeproc(path: str | Path) -> None:
"""Append pandoc expanded citeproc references at EOF.
Parameters
----------
path : TODO
Returns
-------
TODO
path :
Path to markdown input file.
"""
reference = filter_reference(
convert(
inputfile=str(path),
outputfile=None,
format="markdown+wikilinks_title_after_pipe",
to="markdown-citations",
prefilters=None,
filters=None,
extra_args=(
"--citeproc",
"--standalone",
"--wrap",
"none",
),
)
reference = convert(
inputfile=str(path),
outputfile=None,
format="markdown+wikilinks_title_after_pipe",
to="markdown-citations",
prefilters=None,
filters=None,
extra_args=(
"--citeproc",
"--standalone",
"--wrap",
"none",
),
)
vim.command(f"let @x = '{reference}'")
vim.command(f"let @x = '{filter_reference(reference)}'")
vim.command("$put x")


Expand Down
12 changes: 6 additions & 6 deletions tests/unit/prefilter/reference_citation.out.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
\[#bloggs-jones\]: Bloggs, A. J., and X. Y. Jones. 1959. \"Title Title
Title Title Title Title Title Title Title Title.\"
\[#ref-bloggs-jones\]: Bloggs, A. J., and X. Y. Jones. 1959. \"Title
Title Title Title Title Title Title Title Title Title.\"
[]{#Journal Journal Journal}**Journal Journal Journal**.

\[#chomsky-73\]: Chomsky, N. 1973. \"Conditions on Transformations.\" In
[]{#A Festschrift for Morris Halle}**A Festschrift for Morris Halle**,
edited by S. R. Anderson and P. Kiparsky. New York: Holt, Rinehart &
Winston.
\[#ref-chomsky-73\]: Chomsky, N. 1973. \"Conditions on
Transformations.\" In []{#A Festschrift for Morris Halle}**A Festschrift
for Morris Halle**, edited by S. R. Anderson and P. Kiparsky. New York:
Holt, Rinehart & Winston.
4 changes: 2 additions & 2 deletions tests/vim/ExpandCiteproc.vader
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,6 @@ Expect (Append references):

----
House of Dude
[#bloggs-jones]: Bloggs, A. J., and X. Y. Jones. 1959. "Title Title Title Title Title Title Title Title Title Title." *Journal Journal Journal*.
[#ref-bloggs-jones]: Bloggs, A. J., and X. Y. Jones. 1959. "Title Title Title Title Title Title Title Title Title Title." *Journal Journal Journal*.

[#chomsky-73]: Chomsky, N. 1973. "Conditions on Transformations." In *A Festschrift for Morris Halle*, edited by S. R. Anderson and P. Kiparsky. New York: Holt, Rinehart & Winston.
[#ref-chomsky-73]: Chomsky, N. 1973. "Conditions on Transformations." In *A Festschrift for Morris Halle*, edited by S. R. Anderson and P. Kiparsky. New York: Holt, Rinehart & Winston.

0 comments on commit f220ffa

Please sign in to comment.