Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 34 additions & 0 deletions test2.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
from tomd import Tomd

table='''
<table>
<thead>
<tr>
<th>head1</th>
<th>head2</th>
<th>head3</th>
</tr>
</thead>
<tbody>
<tr>
<td>content1</td>
<td>content2</td>
<td>content3</td>
</tr>
</tbody>
</table>
'''


md = Tomd(table).markdown
print("~~~~~~~~~~~~~")
print(md)
print("~~~~~~~~~~~~~")

'''
output
|head1|head2|head3
|------
|content1|content2|content3|
'''

24 changes: 1 addition & 23 deletions tomd.py
Original file line number Diff line number Diff line change
Expand Up @@ -134,8 +134,6 @@ def parse_inline(self):
self.content = self.content.replace('<hr/>', '\n---\n')
self.content = self.content.replace('<br/>', '')

if self.tag == "table": # for removing tbody
self.content = re.sub(INLINE_ELEMENTS['tbody'], '\g<1>', self.content)

INLINE_ELEMENTS_LIST_KEYS = list(INLINE_ELEMENTS.keys())
INLINE_ELEMENTS_LIST_KEYS.sort()
Expand All @@ -161,11 +159,6 @@ def parse_inline(self):
elif self.tag == 'tr' and tag == 'td':
self.content = re.sub(pattern, '|\g<1>|', self.content.replace('\n', ''))
self.content = self.content.replace("||", "|") # end of column also needs a pipe
elif self.tag == 'table' and tag == 'td':
self.content = re.sub(pattern, '|\g<1>|', self.content)
self.content = self.content.replace("||", "|") # end of column also needs a pipe
self.content = self.content.replace('|\n\n', '|\n') # replace double new line
self.construct_table()
else:
wrapper = MARKDOWN.get(tag)
if tag == "strong":
Expand All @@ -177,22 +170,6 @@ def parse_inline(self):
# focusing on div, add new line if not there (and if content is long enough)
self.content += '\n'

def construct_table(self):
# this function, after self.content has gained | for table entries,
# adds the |---| in markdown to create a proper table

temp = self.content.split('\n', 3)
for elt in temp:
if elt != "":
count = elt.count("|") # count number of pipes
break
pipe = "\n|" # beginning \n for safety
for i in range(count - 1):
pipe += "---|"
pipe += "\n"
self.content = pipe + pipe + self.content + "\n" # TODO: column titles?
self.content = self.content.replace('|\n\n', '|\n') # replace double new line
self.content = self.content.replace("<br/>\n", "<br/>") # end of column also needs a pipe


class Tomd:
Expand All @@ -207,6 +184,7 @@ def convert(self, html="", options=None):
if html == "":
html = self.html
# main function here
html = re.sub("\s+",'', html) #del \n\t
elements = []
for tag, pattern in BlOCK_ELEMENTS.items():
for m in re.finditer(pattern, html, re.I | re.S | re.M):
Expand Down