From: Armaan Bhojwani Date: Sun, 14 Feb 2021 00:57:06 +0000 (-0500) Subject: Allow for multiple input files to be given X-Git-Tag: v0.7.0~13 X-Git-Url: https://git.armaanb.net/?p=lightcards.git;a=commitdiff_plain;h=3ec382bac0913a7268e8059eaf337a54cf1b0f5c Allow for multiple input files to be given This will concatenate the contents --- diff --git a/README.md b/README.md index d51ee12..4c061d0 100644 --- a/README.md +++ b/README.md @@ -26,7 +26,7 @@ pip install git+https://git.sr.ht/~armaan/lightcards@devel See `lightcards --help` or `man lightcards` for usage information. ## Input file -`contrib/example.md` is an example input file. Lightcards takes the first table from a valid Markdown or HTML file. Each row is a card, and the two columns are the front and back. +`contrib/example.md` is an example input file. Lightcards takes all of the tables from a valid Markdown or HTML file. Each row is a card, and the two columns are the front and back. ## Configuration Copy the config file at `/etc/lightcards/config.py` to `~/.config/lightcards/config.py` or `~/${XDG_CONFIG_HOME}/lightcards/config.py` and edit. diff --git a/lightcards/parse.py b/lightcards/parse.py index 3a1306b..8e0d320 100644 --- a/lightcards/parse.py +++ b/lightcards/parse.py @@ -10,12 +10,18 @@ from .deck import Card def md2html(file): """Use the markdown module to convert input to HTML""" - try: - return markdown.markdown(open(file, "r").read(), extensions=["tables"]) - except FileNotFoundError: - raise Exception( - f'lightcards: "{file}": No such file or directory' - ) from None + outp = "" + for i in file: + try: + outp += markdown.markdown( + open(i, "r").read(), extensions=["tables"] + ) + except FileNotFoundError: + raise Exception( + f'lightcards: "{i}": No such file or directory' + ) from None + + return outp def parse_html(html): @@ -24,21 +30,24 @@ def parse_html(html): def clean_text(inp): return inp.get_text().rstrip() - soup = BeautifulSoup(html, "html.parser").find("table") + soup = BeautifulSoup(html, "html.parser").find_all("table") outp = [] - try: - for x in soup.find_all("tr"): - outp.append(Card(tuple([clean_text(y) for y in x.find_all("td")]))) - except AttributeError: - raise Exception("lightcards: No table found") from None + for table in soup: + try: + for x in table.find_all("tr"): + y = x.find_all("td") + if y: + outp.append(Card(tuple([clean_text(z) for z in y]))) + except AttributeError: + raise Exception("lightcards: No table found") from None - ths = soup.find_all("th") - if len(ths) != 2: - raise Exception("lightcards: Headings malformed") + ths = table.find_all("th") + if len(ths) != 2: + raise Exception("lightcards: Headings malformed") # Return a tuple of nested lists - return ([clean_text(x) for x in ths], outp[1:]) + return ([clean_text(x) for x in ths], outp) def main(file): diff --git a/lightcards/runner.py b/lightcards/runner.py index 8ca91f1..2154c95 100644 --- a/lightcards/runner.py +++ b/lightcards/runner.py @@ -33,7 +33,7 @@ def parse_args(): choices=range(1, 4), help="specify which view to start in", ) - parser.add_argument("inp", metavar="input_file", type=str, nargs=1) + parser.add_argument("inp", metavar="input_files", type=str, nargs="+") parser.add_argument( "-a", "--alphabetize", @@ -111,7 +111,7 @@ def main(args=sys.argv): sys.tracebacklimit = 0 global headers, stack - (headers, stack) = parse.parse_html(parse.md2html(args.inp[0])) + (headers, stack) = parse.parse_html(parse.md2html(args.inp)) show(args, stack, headers, conf) diff --git a/man/lightcards.1 b/man/lightcards.1 index 526d82c..a2e421e 100644 --- a/man/lightcards.1 +++ b/man/lightcards.1 @@ -7,12 +7,12 @@ lightcards - terminal flashcards from Markdown .SH SYNOPSIS .PP -lightcards [options] [input file] +lightcards [options] [input files] .SH DESCRIPTION .PP -\f[B]lightcards\f[R] is a Python program that reads data from a two -column Markdown (and by extension, HTML) table in the \f[B]input -file\f[R], and displays flashcards from their contents. +\f[B]lightcards\f[R] is a Python program that reads data from two column +Markdown (and by extension, HTML) tables from the \f[B]input files\f[R], +and displays flashcards from their contents. .SH OPTIONS .SS Generic Program Information .TP diff --git a/man/lightcards.1.md b/man/lightcards.1.md index 24e3cd0..568aa5e 100644 --- a/man/lightcards.1.md +++ b/man/lightcards.1.md @@ -10,10 +10,10 @@ date: February 2021 lightcards - terminal flashcards from Markdown # SYNOPSIS -lightcards \[options\] \[input file\] +lightcards \[options\] \[input files\] # DESCRIPTION -**lightcards** is a Python program that reads data from a two column Markdown (and by extension, HTML) table in the **input file**, and displays flashcards from their contents. +**lightcards** is a Python program that reads data from two column Markdown (and by extension, HTML) tables from the **input files**, and displays flashcards from their contents. # OPTIONS ## Generic Program Information