Skip to content

Commit 4d0f58f

Browse files
authored
feat: rewrite Academic CLI for Python 3.11 (#121)
Changes - rewrite Academic CLI for Python v3.11 - based on user feedback, remove dependency on Hugo to be installed on user's PC - hence, the tool is now framework-agnostic again, so can be used with any website generator or even for writing Markdown-formatted books - remove deprecated features such as JS/CSS asset concatenation for offline sites - migrate from pipenv to Poetry for dependency management (latest best practice)
1 parent 0eda13c commit 4d0f58f

File tree

17 files changed

+493
-804
lines changed

17 files changed

+493
-804
lines changed

MANIFEST.in

Lines changed: 0 additions & 5 deletions
This file was deleted.

Makefile

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
.PHONY: black lint test publish
22

33
format:
4-
isort --profile black .
5-
black .
4+
poetry run isort --profile black .
5+
poetry run black .
66

77
lint:
8-
flake8
8+
poetry run flake8
99

1010
test:
11-
python -m pytest
11+
poetry run pytest
1212

1313
publish:
14-
python setup.py publish
14+
poetry publish --build --dry-run

Pipfile

Lines changed: 0 additions & 17 deletions
This file was deleted.

Pipfile.lock

Lines changed: 0 additions & 531 deletions
This file was deleted.

README.md

Lines changed: 10 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,9 @@ Support development of the Academic CLI:
3434

3535
## Prerequisites
3636

37-
1. Install [Python 3.6+](https://realpython.com/installing-python/) if it’s not already installed
37+
1. Install [Python 3.11+](https://realpython.com/installing-python/) if it’s not already installed
3838

39-
### Additional prerequisites only if you are creating a website with Hugo
39+
### For Building a Website with Hugo (Optional)
4040

4141
1. Create a [Hugo](https://gohugo.io) website such as by using the [Hugo Academic Starter](https://github.com/wowchemy/starter-hugo-academic) template for the [Wowchemy](https://wowchemy.com) website builder
4242
1. [Download your site from GitHub, installing Hugo and its dependencies](https://wowchemy.com/docs/getting-started/install-hugo-extended/)
@@ -49,24 +49,18 @@ Support development of the Academic CLI:
4949
Open your Terminal or Command Prompt app and install the Academic CLI tool:
5050

5151
pip3 install -U academic
52-
53-
Alternatively, install Academic CLI v0.5.1 if you do not wish to install Hugo on your computer:
54-
55-
pip3 install academic==0.5.1
5652

57-
Or, help test the lastest development version:
53+
Or, help test the latest development version:
5854

5955
pip3 install -U git+https://github.com/wowchemy/hugo-academic-cli.git
6056

6157
## Usage
6258

63-
Use the `cd` command to navigate to your website folder in the terminal:
64-
65-
cd <MY_WEBSITE_FOLDER>
59+
Download references from your reference manager, such as Zotero, in the Bibtex format.
6660

67-
**Help:**
61+
Use the `cd` command to navigate to the folder containing your Bibtex file:
6862

69-
academic
63+
cd <MY_BIBTEX_FOLDER>
7064

7165
**Import publications:**
7266

@@ -91,10 +85,6 @@ Optional arguments:
9185

9286
After importing publications, [a full text PDF and image can be associated with each item and further details added via extra parameters](https://wowchemy.com/docs/content/publications/).
9387

94-
**Run a Hugo command (pass-through):**
95-
96-
academic server
97-
9888
## Contribute
9989

10090
Interested in contributing to **open source** and **open science**?
@@ -105,10 +95,10 @@ Check out the [open issues](https://github.com/wowchemy/hugo-academic-cli/issues
10595

10696
For local development, clone this repository and use Pipenv to install the tool using the following commands:
10797

108-
git clone https://github.com/wowchemy/hugo-academic-cli.git
109-
cd hugo-academic-cli
110-
pip3 install pipenv
111-
pipenv install -e .
98+
git clone https://github.com/wowchemy/bibtex-to-markdown.git
99+
cd bibtex-to-markdown
100+
poetry install
101+
poetry run academic import --bibtex=tests/data/article.bib --publication-dir=debug --overwrite
112102

113103
Preparing a contribution:
114104

academic/__init__.py

Lines changed: 0 additions & 4 deletions
This file was deleted.

academic/cli.py

Lines changed: 17 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,17 @@
33
import argparse
44
import logging
55
import os
6-
import subprocess
76
import sys
87
from argparse import RawTextHelpFormatter
98

10-
from academic import __version__ as version
11-
from academic import utils
12-
from academic.import_assets import import_assets
139
from academic.import_bibtex import import_bibtex
10+
from academic.version import VERSION
1411

1512
# Initialise logger.
1613
logging.basicConfig(
17-
format="%(asctime)s %(levelname)s: %(message)s", level=logging.WARNING, datefmt="%I:%M:%S%p",
14+
format="%(asctime)s %(levelname)s: %(message)s",
15+
level=logging.WARNING,
16+
datefmt="%I:%M:%S%p",
1817
)
1918
log = logging.getLogger(__name__)
2019

@@ -32,14 +31,17 @@ def parse_args(args):
3231

3332
# Initialise command parser.
3433
parser = argparse.ArgumentParser(
35-
description=f"Hugo Academic CLI v{version}\nhttps://github.com/wowchemy/hugo-academic-cli", formatter_class=RawTextHelpFormatter,
34+
description=f"Hugo Academic CLI v{VERSION}\nhttps://github.com/wowchemy/bibtex-to-markdown",
35+
formatter_class=RawTextHelpFormatter,
3636
)
3737
subparsers = parser.add_subparsers(help="Sub-commands", dest="command")
3838

3939
# Sub-parser for import command.
4040
parser_a = subparsers.add_parser("import", help="Import data into Academic")
4141
parser_a.add_argument(
42-
"--assets", action="store_true", help="Import third-party JS and CSS for generating an offline site",
42+
"--assets",
43+
action="store_true",
44+
help="Import third-party JS and CSS for generating an offline site",
4345
)
4446
parser_a.add_argument("--bibtex", required=False, type=str, help="File path to your BibTeX file")
4547
parser_a.add_argument(
@@ -52,11 +54,17 @@ def parse_args(args):
5254
parser_a.add_argument("--featured", action="store_true", help="Flag publications as featured")
5355
parser_a.add_argument("--overwrite", action="store_true", help="Overwrite existing publications")
5456
parser_a.add_argument(
55-
"--normalize", action="store_true", help="Normalize each keyword to lowercase with uppercase first letter",
57+
"--normalize",
58+
action="store_true",
59+
help="Normalize each keyword to lowercase with uppercase first letter",
5660
)
5761
parser_a.add_argument("-v", "--verbose", action="store_true", required=False, help="Verbose mode")
5862
parser_a.add_argument(
59-
"-dr", "--dry-run", action="store_true", required=False, help="Perform a dry run (Bibtex only)",
63+
"-dr",
64+
"--dry-run",
65+
action="store_true",
66+
required=False,
67+
help="Perform a dry run (Bibtex only)",
6068
)
6169

6270
known_args, unknown = parser.parse_known_args(args)
@@ -65,21 +73,11 @@ def parse_args(args):
6573
if len(args) == 0:
6674
parser.print_help()
6775
parser.exit()
68-
69-
# If no known arguments, wrap Hugo command.
70-
elif known_args is None and unknown:
71-
cmd = utils.hugo_in_docker_or_local()
72-
if args:
73-
cmd = " ".join([cmd, args])
74-
subprocess.call(cmd)
7576
else:
7677
# The command has been recognised, proceed to parse it.
7778
if known_args.command and known_args.verbose:
7879
# Set logging level to debug if verbose mode activated.
7980
logging.getLogger().setLevel(logging.DEBUG)
80-
if known_args.command and known_args.assets:
81-
# Run command to import assets.
82-
import_assets()
8381
elif known_args.command and known_args.bibtex:
8482
# Run command to import bibtex.
8583
import_bibtex(
File renamed without changes.

academic/import_assets.py

Lines changed: 0 additions & 105 deletions
This file was deleted.

academic/import_bibtex.py

Lines changed: 29 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
import calendar
22
import os
33
import re
4-
import subprocess
5-
import time
64
from datetime import datetime
75
from pathlib import Path
86

@@ -12,13 +10,17 @@
1210
from bibtexparser.bwriter import BibTexWriter
1311
from bibtexparser.customization import convert_to_unicode
1412

15-
from academic import utils
16-
from academic.editFM import EditableFM
13+
from academic.generate_markdown import EditableFM
1714
from academic.publication_type import PUB_TYPES, PublicationType
1815

1916

2017
def import_bibtex(
21-
bibtex, pub_dir=os.path.join("content", "publication"), featured=False, overwrite=False, normalize=False, dry_run=False,
18+
bibtex,
19+
pub_dir=os.path.join("content", "publication"),
20+
featured=False,
21+
overwrite=False,
22+
normalize=False,
23+
dry_run=False,
2224
):
2325
"""Import publications from BibTeX file"""
2426
from academic.cli import AcademicError, log
@@ -37,12 +39,22 @@ def import_bibtex(
3739
bib_database = bibtexparser.load(bibtex_file, parser=parser)
3840
for entry in bib_database.entries:
3941
parse_bibtex_entry(
40-
entry, pub_dir=pub_dir, featured=featured, overwrite=overwrite, normalize=normalize, dry_run=dry_run,
42+
entry,
43+
pub_dir=pub_dir,
44+
featured=featured,
45+
overwrite=overwrite,
46+
normalize=normalize,
47+
dry_run=dry_run,
4148
)
4249

4350

4451
def parse_bibtex_entry(
45-
entry, pub_dir=os.path.join("content", "publication"), featured=False, overwrite=False, normalize=False, dry_run=False,
52+
entry,
53+
pub_dir=os.path.join("content", "publication"),
54+
featured=False,
55+
overwrite=False,
56+
normalize=False,
57+
dry_run=False,
4658
):
4759
"""Parse a bibtex entry and generate corresponding publication bundle"""
4860
from academic.cli import log
@@ -75,11 +87,17 @@ def parse_bibtex_entry(
7587
f.write(writer.write(db))
7688

7789
# Prepare YAML front matter for Markdown file.
78-
hugo = utils.hugo_in_docker_or_local()
7990
if not dry_run:
80-
subprocess.call(f"{hugo} new {markdown_path}", shell=True)
81-
if "docker-compose" in hugo:
82-
time.sleep(2)
91+
from importlib import resources as impresources
92+
93+
from academic import templates
94+
95+
inp_file = impresources.files(templates) / "publication.md"
96+
with inp_file.open("rt") as f:
97+
template = f.read()
98+
99+
with open(markdown_path, "w") as f:
100+
f.write(template)
83101

84102
page = EditableFM(Path(bundle_path), dry_run=dry_run)
85103
page.load(Path("index.md"))

0 commit comments

Comments
 (0)