diff options
| author | ryanss | 2015-01-18 23:59:00 -0500 |
|---|---|---|
| committer | ryanss | 2015-01-18 23:59:00 -0500 |
| commit | 9b52648a347427cfb9b27232a12634a073865458 (patch) | |
| tree | 3f6dc3797d31b5020e35bbab54b0e2a9fc327947 | |
| parent | d52dcffa6ca6861393619ea230692804d8863b79 (diff) | |
| download | vim-hn-9b52648a347427cfb9b27232a12634a073865458.tar.gz | |
Improve formatting of comment <code> blocks
| -rw-r--r-- | plugin/hackernews.py | 23 |
1 files 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("<p>"): + if not p: + continue p = html.unescape(p) + + code = None + if p.find("<code>") >= 0: + m = re.search("<pre><code>([\S\s]*?)</code></pre>\n", p) + code = m.group(1) + p = p.replace(m.group(0), "!CODE!") + # Convert <a href="http://url/">Text</a> 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']) |
