feature: html table implemented
as envisioned by mistltoe developers
This commit is contained in:
+31
-26
@@ -1,4 +1,5 @@
|
||||
|
||||
from mistletoe import span_token
|
||||
from mistletoe.block_token import BlockToken
|
||||
|
||||
|
||||
@@ -8,20 +9,40 @@ class Table(BlockToken):
|
||||
return '|' in line
|
||||
|
||||
@classmethod
|
||||
def parseHeader(self, line):
|
||||
def parseHeader(cls, line):
|
||||
# Parse header, extract alignment and make an ordinary row out of it
|
||||
line = line.strip().strip('|') # Remove leading/trailing whitespace and pipes
|
||||
columns = [col.strip() for col in line.split('|')] # Split by pipe |
|
||||
|
||||
alignment = []
|
||||
data = []
|
||||
header = "| "
|
||||
|
||||
for column in columns:
|
||||
attr, val = column.split(':')
|
||||
alignment.append(attr)
|
||||
data.append(val)
|
||||
header += val + " | "
|
||||
|
||||
return data, alignment
|
||||
return header, alignment
|
||||
|
||||
@classmethod
|
||||
def read(cls, lines):
|
||||
header = next(lines)
|
||||
header, alignment = cls.parseHeader(header)
|
||||
rows = [TableRow(header)]
|
||||
|
||||
for line in lines:
|
||||
if "|" not in line :
|
||||
break
|
||||
else :
|
||||
rows.append(TableRow(line))
|
||||
|
||||
return alignment, rows
|
||||
|
||||
def __init__(self, match):
|
||||
self.alignment, self.children = match
|
||||
|
||||
|
||||
class TableRow(BlockToken):
|
||||
@classmethod
|
||||
def parseRow(self, line):
|
||||
line = line.strip().strip('|')
|
||||
@@ -30,29 +51,13 @@ class Table(BlockToken):
|
||||
data = []
|
||||
|
||||
for column in columns:
|
||||
data.append(column)
|
||||
data.append(TableCell(column))
|
||||
|
||||
return data
|
||||
|
||||
@classmethod
|
||||
def read(self, lines):
|
||||
|
||||
table = []
|
||||
|
||||
header = next(lines)
|
||||
|
||||
data, alignment = self.parseHeader(header)
|
||||
table.append([data])
|
||||
|
||||
for line in lines:
|
||||
if "|" not in line:
|
||||
break
|
||||
data = self.parseRow(line)
|
||||
table.append([data])
|
||||
|
||||
return [table, alignment]
|
||||
|
||||
def __init__(self, match):
|
||||
self.table = match[0]
|
||||
self.alignment = match[1]
|
||||
print("init")
|
||||
self.children = self.parseRow(match)
|
||||
|
||||
class TableCell(BlockToken) :
|
||||
def __init__(self, match):
|
||||
super().__init__(match, span_token.tokenize_inner)
|
||||
|
||||
Reference in New Issue
Block a user