1 # Parse markdown table into tuple of lists
4 from bs4 import BeautifulSoup
11 """Use the markdown module to convert input to HTML"""
15 outp += markdown.markdown(
16 open(i, "r").read(), extensions=["tables"]
18 except FileNotFoundError:
20 f'lightcards: "{i}": No such file or directory'
26 def parse_html(html, args, conf):
27 """Use BeautifulSoup to parse the HTML"""
30 return inp.get_text().rstrip()
32 soup = BeautifulSoup(html, "html.parser")
36 table_num = args.table
38 table_num = conf["table"]
42 for i, table in enumerate(soup.find_all("table"), start=1):
43 ths = table.find_all("th")
45 if conf["lenient"] or not args.lenient:
46 raise Exception("lightcards: Headings malformed")
47 elif (table_num and i == table_num) or not table_num:
49 for x in table.find_all("tr"):
52 outp.append(Card(tuple([clean_text(z) for z in y])))
53 except AttributeError:
54 raise Exception("lightcards: No table found") from None
56 # Return a tuple of nested lists
57 return ([clean_text(x) for x in ths], outp)