From 8bbca6fa61b644ca183166666934ecaf7f55f4d0 Mon Sep 17 00:00:00 2001 From: Armaan Bhojwani Date: Sat, 13 Feb 2021 22:04:21 -0500 Subject: [PATCH] Add table number option --- lightcards/parse.py | 8 ++++---- lightcards/runner.py | 9 ++++++++- man/lightcards.1 | 3 +++ man/lightcards.1.md | 3 +++ 4 files changed, 18 insertions(+), 5 deletions(-) diff --git a/lightcards/parse.py b/lightcards/parse.py index 95319f4..5d9b402 100644 --- a/lightcards/parse.py +++ b/lightcards/parse.py @@ -24,7 +24,7 @@ def md2html(file): return outp -def parse_html(html, lenient): +def parse_html(html, args): """Use BeautifulSoup to parse the HTML""" def clean_text(inp): @@ -33,12 +33,12 @@ def parse_html(html, lenient): soup = BeautifulSoup(html, "html.parser") outp, ths = [], [] - for table in soup.find_all("table"): + for i, table in enumerate(soup.find_all("table")): ths = table.find_all("th") if len(ths) != 2: - if not lenient: + if not args.lenient: raise Exception("lightcards: Headings malformed") - else: + elif args.table == i: try: for x in table.find_all("tr"): y = x.find_all("td") diff --git a/lightcards/runner.py b/lightcards/runner.py index fdbbf92..b40328d 100644 --- a/lightcards/runner.py +++ b/lightcards/runner.py @@ -40,6 +40,13 @@ def parse_args(): action="store_true", help="don't raise exception if tables are malformed", ) + parser.add_argument( + "-t", + "--table", + metavar="num_table", + type=int, + help="specify which table to use if multiple are given", + ) parser.add_argument( "-a", "--alphabetize", @@ -117,7 +124,7 @@ def main(args=sys.argv): sys.tracebacklimit = 0 global headers, stack - (headers, stack) = parse.parse_html(parse.md2html(args.inp), args.lenient) + (headers, stack) = parse.parse_html(parse.md2html(args.inp), args) show(args, stack, headers, conf) diff --git a/man/lightcards.1 b/man/lightcards.1 index 247b6b1..d12d70b 100644 --- a/man/lightcards.1 +++ b/man/lightcards.1 @@ -43,6 +43,9 @@ Specify a custom config file .TP \f[B]-l\f[R], \f[B]--lenient\f[R] Don\[cq]t raise exception if tables are malformed +.TP +\f[B]-t\f[R] [int], \f[B]--table\f[R] [int] +If multiple tables are provided in the input files, specify which .SH KEYS .TP \f[B]l\f[R], \f[B]right\f[R] diff --git a/man/lightcards.1.md b/man/lightcards.1.md index 64b55db..40d8783 100644 --- a/man/lightcards.1.md +++ b/man/lightcards.1.md @@ -45,6 +45,9 @@ lightcards \[options\] \[input files\] **-l**, **\--lenient** : Don't raise exception if tables are malformed +**-t** \[int\], **\--table** \[int\] +: If multiple tables are provided in the input files, specify which + # KEYS **l**, **right** : Next card -- 2.39.2