Skip to content

Commit 9a9109d

Browse files
author
ryanss
committed
Refactor duplicate code in print functions
1 parent 681ad0e commit 9a9109d

File tree

1 file changed

+8
-39
lines changed

1 file changed

+8
-39
lines changed

ftplugin/hackernews.py

Lines changed: 8 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,7 @@ def link(external=False):
188188
bwrite("[http://news.ycombinator.com/item?id=%s]" % item_id)
189189
if 'content' in item:
190190
bwrite("")
191-
print_content(item['content'])
191+
print_comments([dict(content=item['content'])])
192192
if 'poll' in item:
193193
bwrite("")
194194
max_score = max((c['points'] for c in item['poll']))
@@ -249,45 +249,13 @@ def recall_pos():
249249
vim.current.window.cursor = (int(mark[0]), int(mark[1]))
250250

251251

252-
def print_content(content):
253-
for p in content.split("<p>"):
254-
if not p:
255-
continue
256-
p = html.unescape(p)
257-
p = p.replace("<i>", "_").replace("</i>", "_")
258-
259-
# Convert <a href="http://url/">Text</a> tags
260-
# to markdown equivalent: (Text)[http://url/]
261-
s = p.find("a>")
262-
while s > 0:
263-
s += 2
264-
section = p[:s]
265-
m = re.search(r"<a.*href=[\"\']([^\"\']*)[\"\'].*>(.*)</a>",
266-
section)
267-
if m:
268-
# Do not bother with anchor text if it is same as href url
269-
if m.group(1)[:20] == m.group(2)[:20]:
270-
p = p.replace(m.group(0), "[%s]" % m.group(1))
271-
else:
272-
p = p.replace(m.group(0),
273-
"(%s)[%s]" % (m.group(2), m.group(1)))
274-
s = p.find("a>")
275-
else:
276-
s = p.find("a>", s)
277-
278-
contents = textwrap.wrap(p, width=80)
279-
for line in contents:
280-
if line.strip():
281-
bwrite(line)
282-
if contents and line.strip():
283-
bwrite("")
284-
285-
286252
def print_comments(comments, level=0):
287253
for comment in comments:
288-
bwrite("%sComment by %s %s:"
289-
% (" "*level*4, comment.get('user', '???'),
290-
comment['time_ago']))
254+
if 'level' in comment:
255+
# This is a comment (not content) so add comment header
256+
bwrite("%sComment by %s %s:"
257+
% (" "*level*4, comment.get('user', '???'),
258+
comment['time_ago']))
291259
for p in comment['content'].split("<p>"):
292260
if not p:
293261
continue
@@ -336,4 +304,5 @@ def print_comments(comments, level=0):
336304
if contents and line.strip():
337305
bwrite("")
338306
bwrite("")
339-
print_comments(comment['comments'], level+1)
307+
if 'comments' in comment:
308+
print_comments(comment['comments'], level+1)

0 commit comments

Comments
 (0)