]> git.armaanb.net Git - lightcards.git/blob - lightcards/parse.py
Fix sidebar showing wrong side of card
[lightcards.git] / lightcards / parse.py
1 # Parse markdown table into tuple of lists
2 # Armaan Bhojwani 2021
3
4 from bs4 import BeautifulSoup
5 import markdown
6
7 from .deck import Card
8
9
10 def md2html(file):
11     """Use the markdown module to convert input to HTML"""
12     outp = ""
13     for i in file:
14         try:
15             outp += markdown.markdown(
16                 open(i, "r").read(), extensions=["tables"]
17             )
18         except FileNotFoundError:
19             raise Exception(
20                 f'lightcards: "{i}": No such file or directory'
21             ) from None
22
23     return outp
24
25
26 def parse_html(html, args, conf):
27     """Use BeautifulSoup to parse the HTML"""
28
29     def clean_text(inp):
30         return inp.get_text().rstrip()
31
32     soup = BeautifulSoup(html, "html.parser")
33     outp, ths = [], []
34
35     if args.table:
36         table_num = args.table
37     elif conf["table"]:
38         table_num = conf["table"]
39     else:
40         table_num = False
41
42     for i, table in enumerate(soup.find_all("table"), start=1):
43         ths = table.find_all("th")
44         if len(ths) != 2:
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:
48             try:
49                 for x in table.find_all("tr"):
50                     y = x.find_all("td")
51                     if y:
52                         outp.append(Card(tuple([clean_text(z) for z in y])))
53             except AttributeError:
54                 raise Exception("lightcards: No table found") from None
55
56     # Return a tuple of nested lists
57     return ([clean_text(x) for x in ths], outp)