From 2a2de7425580e9080c0076b509821759c8a96861 Mon Sep 17 00:00:00 2001 From: Armaan Bhojwani Date: Sat, 13 Feb 2021 22:19:24 -0500 Subject: [PATCH] Add table and lenient config option --- config.py | 2 ++ lightcards/parse.py | 16 +++++++++++----- lightcards/runner.py | 2 +- 3 files changed, 14 insertions(+), 6 deletions(-) diff --git a/config.py b/config.py index ee974b0..b61e4fd 100644 --- a/config.py +++ b/config.py @@ -10,6 +10,8 @@ alphabetize = False shuffle = False reverse = False cache = True +table = False # set to integer or range +lenient = False default_view = 1 diff --git a/lightcards/parse.py b/lightcards/parse.py index 5d9b402..082b7dc 100644 --- a/lightcards/parse.py +++ b/lightcards/parse.py @@ -1,7 +1,6 @@ # Parse markdown table into tuple of lists # Armaan Bhojwani 2021 -import sys from bs4 import BeautifulSoup import markdown @@ -24,7 +23,7 @@ def md2html(file): return outp -def parse_html(html, args): +def parse_html(html, args, conf): """Use BeautifulSoup to parse the HTML""" def clean_text(inp): @@ -33,12 +32,19 @@ def parse_html(html, args): soup = BeautifulSoup(html, "html.parser") outp, ths = [], [] - for i, table in enumerate(soup.find_all("table")): + if args.table: + table_num = args.table + elif conf["table"]: + table_num = conf["table"] + else: + table_num = False + + for i, table in enumerate(soup.find_all("table"), start=1): ths = table.find_all("th") if len(ths) != 2: - if not args.lenient: + if conf["lenient"] or not args.lenient: raise Exception("lightcards: Headings malformed") - elif args.table == i: + elif (table_num and i == table_num) or not table_num: try: for x in table.find_all("tr"): y = x.find_all("td") diff --git a/lightcards/runner.py b/lightcards/runner.py index b40328d..0bd5600 100644 --- a/lightcards/runner.py +++ b/lightcards/runner.py @@ -124,7 +124,7 @@ def main(args=sys.argv): sys.tracebacklimit = 0 global headers, stack - (headers, stack) = parse.parse_html(parse.md2html(args.inp), args) + (headers, stack) = parse.parse_html(parse.md2html(args.inp), args, conf) show(args, stack, headers, conf) -- 2.39.2