From 4aa059d9e61859057e6a31d10792efe8e2ddd36d Mon Sep 17 00:00:00 2001 From: Aaron Gutierrez Date: Wed, 7 Mar 2018 09:43:21 -0800 Subject: [PATCH] Formatting works for comments and description --- asana_service.py | 2 ++ models/models.py | 69 ++++++++++++++++++++++++++++++++++------------ ui/constants.py | 4 +++ ui/task_details.py | 8 ++---- 4 files changed, 61 insertions(+), 22 deletions(-) diff --git a/asana_service.py b/asana_service.py index 134fbad..0911f67 100644 --- a/asana_service.py +++ b/asana_service.py @@ -4,6 +4,7 @@ class AsanaService(object): TASK_FIELDS = [ 'name', 'html_notes', + 'notes', 'assignee.name', 'assignee_status', 'completed', @@ -26,6 +27,7 @@ class AsanaService(object): STORY_FIELDS = [ 'created_by.name', 'html_text', + 'text', 'type' ] diff --git a/models/models.py b/models/models.py index 78b127e..1036dda 100644 --- a/models/models.py +++ b/models/models.py @@ -35,10 +35,15 @@ class Task(AsanaObject): return self.object_dict['assignee_status'] 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'] else: - return None + return "" def due_date(self): if 'due_at' in self.object_dict: @@ -94,22 +99,43 @@ class Strong(object): def __init__(self, body): self.body = body - def str(self): - return ('strong', str(body)) + def text_format(self): + return ('strong', self.body.text_format()) class Italic(object): def __init__(self, body): self.body = body - def str(self): - return ('italic', str(body)) + def text_format(self): + 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): def __init__(self, body): self.body = body - def str(self): - return str(self.body) + def text_format(self): + 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): def __init__(self): @@ -118,25 +144,30 @@ class HTMLTextParser(HTMLParser): super().__init__() def handle_starttag(self, tag, attrs): - if tag == 'STRONG': + if tag == 'strong': self.tag_stack.append(Strong) - elif tag == 'EM': + elif tag == 'em': self.tag_stack.append(Italic) + elif tag == 'u': + self.tag_stack.append(Underline) + elif tag == 'a': + self.tag_stack.append(Link) else: self.tag_stack.append(Tag) def handle_data(self, data): - self.text.append(data) + self.text.append(Text(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 + formatted = [t.text_format() for t in self.text] + print(formatted, file=sys.stderr) + return formatted class Story(AsanaObject): @@ -147,6 +178,10 @@ class Story(AsanaObject): return '' def text(self): - parser = HTMLTextParser() - parser.feed(self.object_dict['html_text']) - return parser.get_formatted_text() + if 'html_text' in self.object_dict: + parser = HTMLTextParser() + parser.feed(self.object_dict['html_text']) + parser.close() + return parser.get_formatted_text() + else: + return [self.object_dict['text']] diff --git a/ui/constants.py b/ui/constants.py index 38fd8ad..8d793b5 100644 --- a/ui/constants.py +++ b/ui/constants.py @@ -8,6 +8,10 @@ palette = [ ('selected', 'standout', ''), ('task', 'light green', ''), ('text', '', ''), + ('strong', 'bold', ''), + ('underline', 'underline', ''), + ('link', 'underline,light blue', ''), + ('italic', 'italics', ''), ('workspace', 'white', 'dark blue'), ] diff --git a/ui/task_details.py b/ui/task_details.py index a534399..a7ad9e7 100644 --- a/ui/task_details.py +++ b/ui/task_details.py @@ -74,11 +74,9 @@ class CustomFields(object): class Stories(object): def __init__(self, stories): - components = [urwid.Text([ - ('author', s.creator()), - s.text(), - '\n' - ]) for s in stories] + components = [ + urwid.Text([('author', s.creator())] + s.text()) + for s in stories] self.stories = urwid.Pile(components)