From 9b52648a347427cfb9b27232a12634a073865458 Mon Sep 17 00:00:00 2001 From: ryanss Date: Sun, 18 Jan 2015 23:59:00 -0500 Subject: Improve formatting of comment blocks --- plugin/hackernews.py | 23 ++++++++++++++++++++--- 1 file changed, 20 insertions(+), 3 deletions(-) diff --git a/plugin/hackernews.py b/plugin/hackernews.py index 3d636b6..76870f5 100644 --- a/plugin/hackernews.py +++ b/plugin/hackernews.py @@ -12,7 +12,10 @@ API_URL = "http://node-hnapi.herokuapp.com" def bwrite(s): s = s.encode('utf-8') b = vim.current.buffer - if not b[0]: + if not s.strip() and not b[-1].strip() and not b[-2].strip(): + # Never write more than two blank lines in a row + return + elif not b[0]: b[0] = s else: b.append(s) @@ -99,7 +102,16 @@ def print_comments(comments): level = comment['level'] bwrite("%sComment by %s %s:" % ("\t"*level, comment.get('user','???'), comment['time_ago'])) for p in comment['content'].split("

"): + if not p: + continue p = html.unescape(p) + + code = None + if p.find("") >= 0: + m = re.search("

([\S\s]*?)
\n", p) + code = m.group(1) + p = p.replace(m.group(0), "!CODE!") + # Convert Text tags # to markdown equivalent: (Text)[http://url/] s = p.find("a>") @@ -119,8 +131,13 @@ def print_comments(comments): initial_indent=" "*4*level, subsequent_indent=" "*4*level) for line in contents: - bwrite(line) - if contents: + if line.find("!CODE!") >= 0: + for c in code.split("\n"): + bwrite(" "*4*level + c) + line = " "*4*level + line.replace("!CODE!", "").strip() + if line.strip(): + bwrite(line) + if contents and line.strip(): bwrite("") bwrite("") print_comments(comment['comments']) -- cgit v1.2.3