Formatting works for comments and description

This commit is contained in:
2018-03-07 09:43:21 -08:00
parent 0ae283604a
commit 4aa059d9e6
4 changed files with 61 additions and 22 deletions

View File

@@ -4,6 +4,7 @@ class AsanaService(object):
TASK_FIELDS = [ TASK_FIELDS = [
'name', 'name',
'html_notes', 'html_notes',
'notes',
'assignee.name', 'assignee.name',
'assignee_status', 'assignee_status',
'completed', 'completed',
@@ -26,6 +27,7 @@ class AsanaService(object):
STORY_FIELDS = [ STORY_FIELDS = [
'created_by.name', 'created_by.name',
'html_text', 'html_text',
'text',
'type' 'type'
] ]

View File

@@ -35,10 +35,15 @@ class Task(AsanaObject):
return self.object_dict['assignee_status'] return self.object_dict['assignee_status']
def description(self): def description(self):
if 'notes' in self.object_dict: if 'html_notes' in self.object_dict:
parser = HTMLTextParser()
parser.feed(self.object_dict['html_notes'])
parser.close()
return parser.get_formatted_text()
elif 'notes' in self.object_dict:
return self.object_dict['notes'] return self.object_dict['notes']
else: else:
return None return ""
def due_date(self): def due_date(self):
if 'due_at' in self.object_dict: if 'due_at' in self.object_dict:
@@ -94,22 +99,43 @@ class Strong(object):
def __init__(self, body): def __init__(self, body):
self.body = body self.body = body
def str(self): def text_format(self):
return ('strong', str(body)) return ('strong', self.body.text_format())
class Italic(object): class Italic(object):
def __init__(self, body): def __init__(self, body):
self.body = body self.body = body
def str(self): def text_format(self):
return ('italic', str(body)) return ('italic', self.body.text_format())
class Underline(object):
def __init__(self, body):
self.body = body
def text_format(self):
return ('underline', self.body.text_format())
class Link(object):
def __init__(self, body):
self.body = body
def text_format(self):
return ('link', self.body.text_format())
class Tag(object): class Tag(object):
def __init__(self, body): def __init__(self, body):
self.body = body self.body = body
def str(self): def text_format(self):
return str(self.body) return self.body.text_format()
class Text(object):
def __init__(self, body):
self.body = body
def text_format(self):
return self.body
class HTMLTextParser(HTMLParser): class HTMLTextParser(HTMLParser):
def __init__(self): def __init__(self):
@@ -118,15 +144,19 @@ class HTMLTextParser(HTMLParser):
super().__init__() super().__init__()
def handle_starttag(self, tag, attrs): def handle_starttag(self, tag, attrs):
if tag == 'STRONG': if tag == 'strong':
self.tag_stack.append(Strong) self.tag_stack.append(Strong)
elif tag == 'EM': elif tag == 'em':
self.tag_stack.append(Italic) self.tag_stack.append(Italic)
elif tag == 'u':
self.tag_stack.append(Underline)
elif tag == 'a':
self.tag_stack.append(Link)
else: else:
self.tag_stack.append(Tag) self.tag_stack.append(Tag)
def handle_data(self, data): def handle_data(self, data):
self.text.append(data) self.text.append(Text(data))
def handle_endtag(self, tag): def handle_endtag(self, tag):
data = self.text.pop() data = self.text.pop()
@@ -135,8 +165,9 @@ class HTMLTextParser(HTMLParser):
self.text.append(tag(data)) self.text.append(tag(data))
def get_formatted_text(self): def get_formatted_text(self):
print(self.text) formatted = [t.text_format() for t in self.text]
return self.text print(formatted, file=sys.stderr)
return formatted
class Story(AsanaObject): class Story(AsanaObject):
@@ -147,6 +178,10 @@ class Story(AsanaObject):
return '' return ''
def text(self): def text(self):
parser = HTMLTextParser() if 'html_text' in self.object_dict:
parser.feed(self.object_dict['html_text']) parser = HTMLTextParser()
return parser.get_formatted_text() parser.feed(self.object_dict['html_text'])
parser.close()
return parser.get_formatted_text()
else:
return [self.object_dict['text']]

View File

@@ -8,6 +8,10 @@ palette = [
('selected', 'standout', ''), ('selected', 'standout', ''),
('task', 'light green', ''), ('task', 'light green', ''),
('text', '', ''), ('text', '', ''),
('strong', 'bold', ''),
('underline', 'underline', ''),
('link', 'underline,light blue', ''),
('italic', 'italics', ''),
('workspace', 'white', 'dark blue'), ('workspace', 'white', 'dark blue'),
] ]

View File

@@ -74,11 +74,9 @@ class CustomFields(object):
class Stories(object): class Stories(object):
def __init__(self, stories): def __init__(self, stories):
components = [urwid.Text([ components = [
('author', s.creator()), urwid.Text([('author', s.creator())] + s.text())
s.text(), for s in stories]
'\n'
]) for s in stories]
self.stories = urwid.Pile(components) self.stories = urwid.Pile(components)