summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorryanss2015-03-17 23:59:00 -0400
committerryanss2015-03-17 23:59:00 -0400
commit9a9109d189d2fe00d707cbf39d0ec32f016a5a27 (patch)
treedcf6d53b0cc009b311119243dfee549f6f21bd94
parent681ad0ef1049ed4f6b45f4e8522a3f7af937ce93 (diff)
downloadvim-hn-9a9109d189d2fe00d707cbf39d0ec32f016a5a27.tar.gz
Refactor duplicate code in print functions
-rw-r--r--ftplugin/hackernews.py47
1 files changed, 8 insertions, 39 deletions
diff --git a/ftplugin/hackernews.py b/ftplugin/hackernews.py
index a57ae9c..37f1e05 100644
--- a/ftplugin/hackernews.py
+++ b/ftplugin/hackernews.py
@@ -188,7 +188,7 @@ def link(external=False):
bwrite("[http://news.ycombinator.com/item?id=%s]" % item_id)
if 'content' in item:
bwrite("")
- print_content(item['content'])
+ print_comments([dict(content=item['content'])])
if 'poll' in item:
bwrite("")
max_score = max((c['points'] for c in item['poll']))
@@ -249,45 +249,13 @@ def recall_pos():
vim.current.window.cursor = (int(mark[0]), int(mark[1]))
-def print_content(content):
- for p in content.split("<p>"):
- if not p:
- continue
- p = html.unescape(p)
- p = p.replace("<i>", "_").replace("</i>", "_")
-
- # Convert <a href="http://url/">Text</a> tags
- # to markdown equivalent: (Text)[http://url/]
- s = p.find("a>")
- while s > 0:
- s += 2
- section = p[:s]
- m = re.search(r"<a.*href=[\"\']([^\"\']*)[\"\'].*>(.*)</a>",
- section)
- if m:
- # Do not bother with anchor text if it is same as href url
- if m.group(1)[:20] == m.group(2)[:20]:
- p = p.replace(m.group(0), "[%s]" % m.group(1))
- else:
- p = p.replace(m.group(0),
- "(%s)[%s]" % (m.group(2), m.group(1)))
- s = p.find("a>")
- else:
- s = p.find("a>", s)
-
- contents = textwrap.wrap(p, width=80)
- for line in contents:
- if line.strip():
- bwrite(line)
- if contents and line.strip():
- bwrite("")
-
-
def print_comments(comments, level=0):
for comment in comments:
- bwrite("%sComment by %s %s:"
- % (" "*level*4, comment.get('user', '???'),
- comment['time_ago']))
+ if 'level' in comment:
+ # This is a comment (not content) so add comment header
+ bwrite("%sComment by %s %s:"
+ % (" "*level*4, comment.get('user', '???'),
+ comment['time_ago']))
for p in comment['content'].split("<p>"):
if not p:
continue
@@ -336,4 +304,5 @@ def print_comments(comments, level=0):
if contents and line.strip():
bwrite("")
bwrite("")
- print_comments(comment['comments'], level+1)
+ if 'comments' in comment:
+ print_comments(comment['comments'], level+1)