From 173c0fb7f54da37238655d25ae7b50211c7ab434 Mon Sep 17 00:00:00 2001 From: Shyam D Date: Thu, 1 Jul 2021 15:38:45 -0700 Subject: [PATCH 01/10] test multiple pandoc versions --- .github/workflows/testing.yml | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/.github/workflows/testing.yml b/.github/workflows/testing.yml index c112e06..1ab05f7 100644 --- a/.github/workflows/testing.yml +++ b/.github/workflows/testing.yml @@ -21,9 +21,6 @@ jobs: with: python-version: 3.7 - - name: Install Pandoc - run: sudo apt-get install pandoc - - name: Install dependencies run: | python -m pip install --upgrade pip @@ -53,7 +50,7 @@ jobs: matrix: os: [ubuntu-latest] python-version: [3.6, 3.7, 3.8] - + pandoc-verison: [2.9.2, 2.14.0.3] runs-on: ${{ matrix.os }} steps: @@ -64,8 +61,9 @@ jobs: with: python-version: ${{ matrix.python-version }} - - name: Install Pandoc - run: sudo apt-get install pandoc pandoc-citeproc + - uses: r-lib/actions/setup-pandoc@v1 + with: + pandoc-version: ${{ matrix.pandoc-version }} - name: Install Python dependencies run: | From 63b6061265ec33b7a9c288948b7daae4f66807ca Mon Sep 17 00:00:00 2001 From: Shyam D Date: Thu, 1 Jul 2021 15:39:17 -0700 Subject: [PATCH 02/10] replace os.path with PathLib and validation --- mkdocs_bibtex/plugin.py | 36 +++++++++++++----------------------- 1 file changed, 13 insertions(+), 23 deletions(-) diff --git a/mkdocs_bibtex/plugin.py b/mkdocs_bibtex/plugin.py index c4585c8..28f46e8 100644 --- a/mkdocs_bibtex/plugin.py +++ b/mkdocs_bibtex/plugin.py @@ -1,6 +1,5 @@ -import glob -import os.path import re +from pathlib import Path import tempfile from collections import OrderedDict @@ -30,12 +29,15 @@ class BibTexPlugin(BasePlugin): """ config_scheme = [ - ("bib_file", config_options.Type(str, required=False)), - ("bib_dir", config_options.Type(str, required=False)), - ("cite_style", config_options.Type(str, default="plain")), + ("bib_file", config_options.File(exists=True, required=False)), + ("bib_dir", config_options.Dir(exists=True, required=False)), + ( + "cite_style", + config_options.Choice(choices=["plain", "pandoc"], default="plain"), + ), ("bib_command", config_options.Type(str, default="\\bibliography")), ("full_bib_command", config_options.Type(str, default="\\full_bibliography")), - ("csl_file", config_options.Type(str, required=False)), + ("csl_file", config_options.File(exists=True, required=False)), ("unescape_for_arithmatex", config_options.Type(bool, required=False)), ] @@ -48,25 +50,19 @@ def on_config(self, config): """ Loads bibliography on load of config """ - config_path = os.path.dirname(config.config_file_path) bibfiles = [] if self.config.get("bib_file", None) is not None: - bibfiles.append(get_path(self.config["bib_file"], config_path)) + bibfiles.append(self.config["bib_file"]) elif self.config.get("bib_dir", None) is not None: - bibfiles.extend( - glob.glob( - get_path(os.path.join(self.config["bib_dir"], "*.bib"), config_path) - ) - ) + bibfiles.extend(Path(self.config["bib_dir"]).glob("*.bib")) else: raise Exception("Must supply a bibtex file or directory for bibtex files") # load bibliography data refs = {} for bibfile in bibfiles: - bibfile = get_path(bibfile, config_path) bibdata = parse_file(bibfile) refs.update(bibdata.entries) @@ -158,7 +154,7 @@ def format_citations(self, citations): entry_text = formatted_entry.text.render(backend) entry_text = entry_text.replace("\n", " ") if self.unescape_for_arithmatex: - entry_text = entry_text.replace('\(', '(').replace('\)', ')') + entry_text = entry_text.replace("\(", "(").replace("\)", ")") # Local reference list for this file references[key] = entry_text # Global reference list for all files @@ -179,13 +175,7 @@ def full_bibliography(self): return "\n".join(full_bibliography) -def get_path(path, base_path): - if path is None: - return None - elif os.path.isabs(path): - return path - else: - return os.path.abspath(os.path.join(base_path, path)) + def to_markdown_pandoc(entry, csl_path): @@ -200,7 +190,7 @@ def to_markdown_pandoc(entry, csl_path): """ with tempfile.TemporaryDirectory() as tmpdir: - bib_path = os.path.join(tmpdir, "temp.bib") + bib_path = Path(tmpdir).joinpath("temp.bib") with open(bib_path, "w") as bibfile: bibfile.write(bibtex_string) From cefa825f090eb653e35e8d3a872a90d0774af0e2 Mon Sep 17 00:00:00 2001 From: Shyam D Date: Thu, 1 Jul 2021 21:41:51 -0700 Subject: [PATCH 03/10] Convert to version dependent pandoc conversion --- mkdocs_bibtex/plugin.py | 56 +++++++++++++++++++++++++---------------- 1 file changed, 35 insertions(+), 21 deletions(-) diff --git a/mkdocs_bibtex/plugin.py b/mkdocs_bibtex/plugin.py index 28f46e8..8365f7e 100644 --- a/mkdocs_bibtex/plugin.py +++ b/mkdocs_bibtex/plugin.py @@ -175,35 +175,49 @@ def full_bibliography(self): return "\n".join(full_bibliography) - - - def to_markdown_pandoc(entry, csl_path): """ Converts the PyBtex entry into formatted markdown citation text """ bibtex_string = BibliographyData(entries={entry.key: entry}).to_string("bibtex") - citation_text = """ + if tuple(int(ver) for ver in pypandoc.get_pandoc_version().split(".")) >= ( + 2, + 11, + ): + markdown = pypandoc.convert_text( + source=bibtex_string, + to="markdown-citations", + format="bibtex", + extra_args=[ + "--citeproc", + "--csl", + csl_path, + ], + ) + + citation_regex = re.compile( + r"\{\.csl-left-margin\}\[(.*)\]\{\.csl-right-inline\}" + ) + citation = citation_regex.findall(" ".join(markdown.split("\n")))[0] + else: + # Older citeproc-filter version of pandoc + with tempfile.TemporaryDirectory() as tmpdir: + bib_path = Path(tmpdir).joinpath("temp.bib") + with open(bib_path, "w") as bibfile: + bibfile.write(bibtex_string) + citation_text = """ --- nocite: '@*' --- """ - with tempfile.TemporaryDirectory() as tmpdir: - bib_path = Path(tmpdir).joinpath("temp.bib") - with open(bib_path, "w") as bibfile: - bibfile.write(bibtex_string) - - # Call Pandoc. - markdown = pypandoc.convert_text( - source=citation_text, - to="markdown_strict-citations", - format="md", - extra_args=["--csl", csl_path, "--bibliography", bib_path], - filters=["pandoc-citeproc"], - ) - - # TODO: Perform this extraction better - markdown = markdown.split("\n")[0][2:] + markdown = pypandoc.convert_text( + source=citation_text, + to="markdown_strict-citations", + format="md", + extra_args=["--csl", csl_path, "--bibliography", bib_path], + filters=["pandoc-citeproc"], + ) - return str(markdown) + citation = " ".join(markdown[4:].split("\n")) + return citation From 40474b8b178aa510a0b1daa15ef3f97d920d419a Mon Sep 17 00:00:00 2001 From: Shyam D Date: Thu, 1 Jul 2021 21:50:53 -0700 Subject: [PATCH 04/10] test long citation --- mkdocs_bibtex/test_plugin.py | 14 ++++++++++++++ test_files/long_cite.bib | 13 +++++++++++++ 2 files changed, 27 insertions(+) create mode 100644 test_files/long_cite.bib diff --git a/mkdocs_bibtex/test_plugin.py b/mkdocs_bibtex/test_plugin.py index 86b8da3..7a6d07d 100644 --- a/mkdocs_bibtex/test_plugin.py +++ b/mkdocs_bibtex/test_plugin.py @@ -62,6 +62,20 @@ def test_format_citations(self): ) # TODO: Check CSL + def test_long_citation(self): + test_data = parse_file(os.path.join(test_files_dir, "long_cite.bib")) + self.plugin.csl_file = None + self.assertIn( + "Benjamin L\\. De Bivort and Bruno Van Swinderen", + self.plugin.format_citations(test_data.entries.items())["Bivort2016"], + ) + + self.plugin.csl_file = os.path.join(test_files_dir, "nature.csl") + self.assertIn( + "De Bivort, B. L. & Van Swinderen", + self.plugin.format_citations(test_data.entries.items())["Bivort2016"], + ) + def test_full_bibliography(self): test_data = parse_file(os.path.join(test_files_dir, "single.bib")) self.plugin.csl_file = None diff --git a/test_files/long_cite.bib b/test_files/long_cite.bib new file mode 100644 index 0000000..77a5503 --- /dev/null +++ b/test_files/long_cite.bib @@ -0,0 +1,13 @@ +@article{Bivort2016, + title = {Evidence for Selective Attention in the Insect Brain}, + author = {De Bivort, Benjamin L. and Van Swinderen, Bruno}, + year = {2016}, + volume = {15}, + pages = {1--7}, + issn = {22145753}, + doi = {10.1016/j.cois.2016.02.007}, + abstract = {The capacity for selective attention appears to be required by any animal responding to an environment containing multiple objects, although this has been difficult to study in smaller animals such as insects. Clear operational characteristics of attention however make study of this crucial brain function accessible to any animal model. Whereas earlier approaches have relied on freely behaving paradigms placed in an ecologically relevant context, recent tethered preparations have focused on brain imaging and electrophysiology in virtual reality environments. Insight into brain activity during attention-like behavior has revealed key elements of attention in the insect brain. Surprisingly, a variety of brain structures appear to be involved, suggesting that even in the smallest brains attention might involve widespread coordination of neural activity.}, + journal = {Current Opinion in Insect Science}, + keywords = {attention,bees,drosophila,insects}, + pmid = {27436727} +} \ No newline at end of file From b7235565d394166040ecbf8dd487b8a6cc70edba Mon Sep 17 00:00:00 2001 From: Shyam D Date: Thu, 1 Jul 2021 21:51:11 -0700 Subject: [PATCH 05/10] use markdown_strict --- mkdocs_bibtex/plugin.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/mkdocs_bibtex/plugin.py b/mkdocs_bibtex/plugin.py index 8365f7e..a5a90d8 100644 --- a/mkdocs_bibtex/plugin.py +++ b/mkdocs_bibtex/plugin.py @@ -213,7 +213,7 @@ def to_markdown_pandoc(entry, csl_path): markdown = pypandoc.convert_text( source=citation_text, - to="markdown_strict-citations", + to="markdown_strict", format="md", extra_args=["--csl", csl_path, "--bibliography", bib_path], filters=["pandoc-citeproc"], From fcce337009cfc9d9866c11eabce1f5676353b570 Mon Sep 17 00:00:00 2001 From: Shyam D Date: Thu, 1 Jul 2021 21:51:21 -0700 Subject: [PATCH 06/10] only cut off first numeral --- mkdocs_bibtex/plugin.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/mkdocs_bibtex/plugin.py b/mkdocs_bibtex/plugin.py index a5a90d8..a655a8f 100644 --- a/mkdocs_bibtex/plugin.py +++ b/mkdocs_bibtex/plugin.py @@ -219,5 +219,5 @@ def to_markdown_pandoc(entry, csl_path): filters=["pandoc-citeproc"], ) - citation = " ".join(markdown[4:].split("\n")) + citation = markdown[4:] return citation From a2300fb3d72d7d8a97734a050e6c0f3468bc5188 Mon Sep 17 00:00:00 2001 From: Shyam D Date: Thu, 1 Jul 2021 21:52:56 -0700 Subject: [PATCH 07/10] squash citation to single line --- mkdocs_bibtex/plugin.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/mkdocs_bibtex/plugin.py b/mkdocs_bibtex/plugin.py index a655a8f..defd7ea 100644 --- a/mkdocs_bibtex/plugin.py +++ b/mkdocs_bibtex/plugin.py @@ -219,5 +219,5 @@ def to_markdown_pandoc(entry, csl_path): filters=["pandoc-citeproc"], ) - citation = markdown[4:] + citation = markdown.replace("\n", " ")[4:] return citation From 7796540ac9fece192421dceb7269802fa66c56fb Mon Sep 17 00:00:00 2001 From: Shyam D Date: Thu, 1 Jul 2021 21:53:16 -0700 Subject: [PATCH 08/10] pre-commit --- .github/workflows/testing.yml | 4 ++-- README.md | 2 +- mkdocs_bibtex/test_plugin.py | 4 ++-- test_files/long_cite.bib | 2 +- 4 files changed, 6 insertions(+), 6 deletions(-) diff --git a/.github/workflows/testing.yml b/.github/workflows/testing.yml index 1ab05f7..082e9fb 100644 --- a/.github/workflows/testing.yml +++ b/.github/workflows/testing.yml @@ -63,7 +63,7 @@ jobs: - uses: r-lib/actions/setup-pandoc@v1 with: - pandoc-version: ${{ matrix.pandoc-version }} + pandoc-version: ${{ matrix.pandoc-version }} - name: Install Python dependencies run: | @@ -79,7 +79,7 @@ jobs: with: token: ${{ secrets.CODECOV_TOKEN }} file: ./coverage.xml - + merge-deps: needs: - lint diff --git a/README.md b/README.md index 75ee357..5a4c86e 100644 --- a/README.md +++ b/README.md @@ -40,7 +40,7 @@ The footnotes extension is how citations are linked for now. - `bib_command` - The command for your bibliography, defaults to `\bibliography` - `full_bib_command` - The command for your bibliography, defaults to `\full_bibliography` - `csl_file` - Bibtex CSL file to format the citation with, defaults to None, using a built in plain format instead -- `unescape_for_arithmatex` - Optional; set to `True` to avoid the `\(` `\)` [issue](https://github.com/shyamd/mkdocs-bibtex/issues/3) with [pymdownx.arithmatex](https://facelessuser.github.io/pymdown-extensions/extensions/arithmatex/) +- `unescape_for_arithmatex` - Optional; set to `True` to avoid the `\(` `\)` [issue](https://github.com/shyamd/mkdocs-bibtex/issues/3) with [pymdownx.arithmatex](https://facelessuser.github.io/pymdown-extensions/extensions/arithmatex/) ## Usage diff --git a/mkdocs_bibtex/test_plugin.py b/mkdocs_bibtex/test_plugin.py index 7a6d07d..4c53c24 100644 --- a/mkdocs_bibtex/test_plugin.py +++ b/mkdocs_bibtex/test_plugin.py @@ -24,13 +24,13 @@ def test_unescape_for_arithmatex(self): self.plugin.unescape_for_arithmatex = True self.assertIn( "First Author and Second Author\. Test Title (TT)\. *Testing Journal (TJ)*, 2019", - self.plugin.format_citations(test_data.entries.items())["test"] + self.plugin.format_citations(test_data.entries.items())["test"], ) self.plugin.unescape_for_arithmatex = False self.assertIn( "First Author and Second Author\. Test Title \(TT\)\. *Testing Journal \(TJ\)*, 2019", - self.plugin.format_citations(test_data.entries.items())["test"] + self.plugin.format_citations(test_data.entries.items())["test"], ) def test_config_one_bibtex_file(self): diff --git a/test_files/long_cite.bib b/test_files/long_cite.bib index 77a5503..2a7fa9e 100644 --- a/test_files/long_cite.bib +++ b/test_files/long_cite.bib @@ -10,4 +10,4 @@ @article{Bivort2016 journal = {Current Opinion in Insect Science}, keywords = {attention,bees,drosophila,insects}, pmid = {27436727} -} \ No newline at end of file +} From 88e16def71b44c9580ca9a61e489f6b1f13d4757 Mon Sep 17 00:00:00 2001 From: Shyam D Date: Thu, 1 Jul 2021 22:02:57 -0700 Subject: [PATCH 09/10] use main branch? --- .github/workflows/testing.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/testing.yml b/.github/workflows/testing.yml index 082e9fb..67d3c9c 100644 --- a/.github/workflows/testing.yml +++ b/.github/workflows/testing.yml @@ -61,7 +61,7 @@ jobs: with: python-version: ${{ matrix.python-version }} - - uses: r-lib/actions/setup-pandoc@v1 + - uses: r-lib/actions/setup-pandoc@master with: pandoc-version: ${{ matrix.pandoc-version }} From dddd8e3a398eb5de09c09b230be95e107fef4bfc Mon Sep 17 00:00:00 2001 From: Shyam D Date: Thu, 1 Jul 2021 22:04:18 -0700 Subject: [PATCH 10/10] fix name typo --- .github/workflows/testing.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/testing.yml b/.github/workflows/testing.yml index 67d3c9c..a5f9468 100644 --- a/.github/workflows/testing.yml +++ b/.github/workflows/testing.yml @@ -50,7 +50,7 @@ jobs: matrix: os: [ubuntu-latest] python-version: [3.6, 3.7, 3.8] - pandoc-verison: [2.9.2, 2.14.0.3] + pandoc-version: [2.9.2, 2.14.0.3] runs-on: ${{ matrix.os }} steps: