summaryrefslogtreecommitdiff
path: root/plugin
diff options
context:
space:
mode:
authorryanss2015-01-06 23:59:00 -0500
committerryanss2015-01-06 23:59:00 -0500
commit925b7ca10c0ce7d0636dc85ef5ff063b0f4a66a0 (patch)
treeb9b2d368ebc00910a2fb451c1de611400b7a6135 /plugin
parenta7cff5c6d5f69bf1c262168432e3eb0e1be9369f (diff)
downloadvim-hn-925b7ca10c0ce7d0636dc85ef5ff063b0f4a66a0.tar.gz
Add story function with nested comments
Diffstat (limited to 'plugin')
-rw-r--r--plugin/hackernews.py38
-rw-r--r--plugin/hackernews.vim4
2 files changed, 41 insertions, 1 deletions
diff --git a/plugin/hackernews.py b/plugin/hackernews.py
index f10b794..6efd4eb 100644
--- a/plugin/hackernews.py
+++ b/plugin/hackernews.py
@@ -1,4 +1,6 @@
import json
+import re
+import textwrap
import urllib2
import vim
@@ -9,7 +11,6 @@ API_URL = "http://node-hnapi.herokuapp.com"
def hacker_news():
vim.command("edit -HackerNews-")
vim.command("setlocal noswapfile")
- vim.command("setlocal nobuflisted")
vim.command("setlocal buftype=nofile")
b = vim.current.buffer
@@ -24,3 +25,38 @@ def hacker_news():
line %= (i+1, item['title'], item['comments_count'], item['id'])
b.append(line)
b.append("")
+
+
+def hacker_news_item():
+ line = vim.current.line
+ start, end = line.rfind('['), line.rfind(']')
+ if start < 0 or end < 0:
+ print "HackerNews.vim Error: Could not parse [item id]"
+ return
+ id = line[start+1:end]
+
+ item = json.loads(urllib2.urlopen(API_URL+"/item/"+id).read())
+
+ vim.command("edit -HackerNews-")
+ b = vim.current.buffer
+ b[0] = item['title']
+ b.append("Posted %s by %s" % (item['time_ago'], item['user']))
+ b.append("%d Points / %d Comments" % (item['points'], item['comments_count']))
+ b.append(item['url'])
+ b.append("")
+ b.append("")
+ print_comments(item['comments'], b)
+
+
+def print_comments(comments, b):
+ for comment in comments:
+ level = comment['level']
+ b.append("%sComment by %s %s:" % ("\t"*level, comment['user'], comment['time_ago']))
+ contents = textwrap.wrap(re.sub('<[^<]+?>', '', comment['content']),
+ width=80,
+ initial_indent=" "*4*level,
+ subsequent_indent=" "*4*level)
+ for line in contents:
+ b.append(line)
+ b.append("")
+ print_comments(comment['comments'], b)
diff --git a/plugin/hackernews.vim b/plugin/hackernews.vim
index 1edb19d..90d8b81 100644
--- a/plugin/hackernews.vim
+++ b/plugin/hackernews.vim
@@ -3,8 +3,12 @@ if !has('python')
finish
endif
+
" Import Python code
let s:path = expand("<sfile>:p:h")
execute "pyfile " . s:path . "/hackernews.py"
+
command! HackerNews python hacker_news()
+
+map <return> :python hacker_news_item()<cr>