From bc5cb4482452862f9b9bb85dd70da0327dbc62f7 Mon Sep 17 00:00:00 2001
From: Jake <j.vondoemming@stud.uni-goettingen.de>
Date: Wed, 31 Aug 2022 21:46:23 +0200
Subject: [PATCH] parse tables

---
 fgs/pandoc.py | 90 +++++++++++++++++++++++++++++++++++++++++++++++++--
 1 file changed, 88 insertions(+), 2 deletions(-)

diff --git a/fgs/pandoc.py b/fgs/pandoc.py
index 5126704..9fb7a6e 100644
--- a/fgs/pandoc.py
+++ b/fgs/pandoc.py
@@ -181,6 +181,12 @@ class Element():
     def parse_int(self, raw_num):
         return raw_num # TODO
 
+    def parse_double_as_percentage(self, raw_num):
+        res = {}
+        res["raw"] = raw_num
+        res["percentage"] = raw_num * 100
+        return res
+
     def parse_target(self, raw_target): # For URLs
         res = {}
         rawurl = self.parse_text(raw_target[0])
@@ -368,8 +374,88 @@ class BlockTable(Block): # Attr Caption [ColSpec] TableHead [TableBody] TableFoo
         ]
         """
         self.attr = self.parse_attr(pandocraw[0])
-        # TODO continue
-        raise Exception(pandocraw)
+        self.caption = self.parse_caption(pandocraw[1])
+        self.column_specs = self.parse_column_specs(pandocraw[2])
+        self.head = self.parse_head(pandocraw[3])
+        self.bodies = self.parse_bodies(pandocraw[4])
+        self.foot = self.parse_foot(pandocraw[5])
+
+
+    def parse_caption(self, raw_caption):
+        #print("called parse_caption: ", raw_caption)
+        if raw_caption[0] != None:
+            print("Warning: Found table with unsupported short caption:", raw_caption, file=sys.stderr)
+        return self.parse_blocks(raw_caption[1])
+
+    def parse_column_specs(self, raw_column_specs):
+        #print("called parse_column_specs: ", raw_column_specs)
+        res = []
+        for cur in raw_column_specs:
+            colspec = {}
+            colspec["alignment"] = self.parse_alignment(cur[0])
+            if cur[1]['t'] == "ColWidthDefault":
+                colspec["has_width"] = False
+            else:
+                colspec["has_width"] = True
+                colspec["width"] = self.parse_double_as_percentage(cur[1]['c'])
+            res.append(colspec)
+        return res
+
+    def parse_alignment(self, raw_alignment):
+        return self.parse_enum({
+            "AlignLeft": "left",
+            "AlignRight": "right",
+            "AlignCenter": "center",
+            "AlignDefault": "default"
+        }, raw_alignment)
+
+    def parse_cells(self, raw_cells):
+        cells = []
+        for cur in raw_cells:
+            cell = {}
+            cell["attr"] = self.parse_attr(cur[0])
+            cell["alignment"] = self.parse_alignment(cur[1])
+            cell["row_span"] = self.parse_int(cur[2]) # number of rows occupied by a cell; the height of a cell
+            cell["column_span"] = self.parse_int(cur[3]) # number of columns occupied by a cell; the width of a cell
+            cell["content"] = self.parse_blocks(cur[4])
+            cells.append(cell)
+        return cells
+
+    def parse_rows(self, raw_rows):
+        rows = []
+        for cur in raw_rows:
+            row = {}
+            row["attr"] = self.parse_attr(cur[0])
+            row["cells"] = self.parse_cells(cur[1])
+            rows.append(row)
+        return rows
+
+    def parse_head(self, raw_head):
+        #print("called parse_head: ", raw_head)
+        res = {}
+        res["attr"] = self.parse_attr(raw_head[0])
+        res["rows"] = self.parse_rows(raw_head[1])
+        return res
+
+    def parse_bodies(self, raw_bodies):
+        #print("called parse_bodies: ", raw_bodies)
+        bodies = []
+        for cur in raw_bodies:
+            body = {}
+            body["attr"] = self.parse_attr(cur[0])
+            body["row_head_columns"] = self.parse_int(cur[1]) # number of row header columns in the intermediate body
+            body["intermediate_head"] = self.parse_rows(cur[2])
+            body["intermediate_body"] = self.parse_rows(cur[3])
+            bodies.append(body)
+        return bodies
+
+    def parse_foot(self, raw_foot):
+        #print("called parse_foot: ", raw_foot)
+        res = {}
+        res["attr"] = self.parse_attr(raw_foot[0])
+        res["rows"] = self.parse_rows(raw_foot[1])
+        return res
+
 
 ############################## INLINE #########################################
 
-- 
GitLab