WIP html formatting
This commit is contained in:
@@ -1,4 +1,5 @@
|
||||
import dateutil
|
||||
from html.parser import HTMLParser
|
||||
import sys
|
||||
|
||||
class AsanaObject(object):
|
||||
@@ -89,6 +90,55 @@ class CustomField(AsanaObject):
|
||||
|
||||
return ''
|
||||
|
||||
class Strong(object):
|
||||
def __init__(self, body):
|
||||
self.body = body
|
||||
|
||||
def str(self):
|
||||
return ('strong', str(body))
|
||||
|
||||
class Italic(object):
|
||||
def __init__(self, body):
|
||||
self.body = body
|
||||
|
||||
def str(self):
|
||||
return ('italic', str(body))
|
||||
|
||||
class Tag(object):
|
||||
def __init__(self, body):
|
||||
self.body = body
|
||||
|
||||
def str(self):
|
||||
return str(self.body)
|
||||
|
||||
class HTMLTextParser(HTMLParser):
|
||||
def __init__(self):
|
||||
self.text = []
|
||||
self.tag_stack = []
|
||||
super().__init__()
|
||||
|
||||
def handle_starttag(self, tag, attrs):
|
||||
if tag == 'STRONG':
|
||||
self.tag_stack.append(Strong)
|
||||
elif tag == 'EM':
|
||||
self.tag_stack.append(Italic)
|
||||
else:
|
||||
self.tag_stack.append(Tag)
|
||||
|
||||
def handle_data(self, data):
|
||||
self.text.append(data)
|
||||
|
||||
def handle_endtag(self, tag):
|
||||
data = self.text.pop()
|
||||
tag = self.tag_stack.pop()
|
||||
|
||||
self.text.append(tag(data))
|
||||
|
||||
def get_formatted_text(self):
|
||||
print(self.text)
|
||||
return self.text
|
||||
|
||||
|
||||
class Story(AsanaObject):
|
||||
def creator(self):
|
||||
if 'created_by' in self.object_dict:
|
||||
@@ -97,4 +147,6 @@ class Story(AsanaObject):
|
||||
return ''
|
||||
|
||||
def text(self):
|
||||
return self.object_dict['text']
|
||||
parser = HTMLTextParser()
|
||||
parser.feed(self.object_dict['html_text'])
|
||||
return parser.get_formatted_text()
|
||||
|
||||
Reference in New Issue
Block a user