summaryrefslogtreecommitdiff
path: root/plugin
diff options
context:
space:
mode:
authorryanss2015-01-15 23:59:00 -0500
committerryanss2015-01-15 23:59:00 -0500
commite2cc7e574015186e550847a47fd21fe255ec7fd3 (patch)
tree4285e63a3af454d8dfae786ea394c14dc7345f02 /plugin
parenteec0cfbcc481846fdcaebdd457e50303b019b514 (diff)
downloadvim-hn-e2cc7e574015186e550847a47fd21fe255ec7fd3.tar.gz
Add ability to handle links spanning multiple lines
Diffstat (limited to 'plugin')
-rw-r--r--plugin/hackernews.py58
1 files changed, 39 insertions, 19 deletions
diff --git a/plugin/hackernews.py b/plugin/hackernews.py
index b27be67..357f6fd 100644
--- a/plugin/hackernews.py
+++ b/plugin/hackernews.py
@@ -30,11 +30,47 @@ def hacker_news():
def hacker_news_link():
line = vim.current.line
- m = re.search(r"\[(http.*)\]", line)
+
+ # Search for Hacker News [item id]
+ m = re.search(r"^[0-9]{1,2}.*\[([0-9]+)\]", line)
if m:
+ id = m.group(1)
+ 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("[%s]" % item['url'])
+ b.append("")
+ b.append("")
+ print_comments(item['comments'], b)
+ return
+
+ # Search for [http] link
+ b = vim.current.buffer
+ i = vim.current.range.start
+ while b[i].find("[http") < 0 and i >= 0:
+ # The line we were on had no part of a link in it
+ if b[i-1].find("]") > 0 and b[i-1].find("]") > b[i-1].find("[http"):
+ return
+ i -= 1
+ start = i
+ if b[i].find("[http") >= 0:
+ if b[i].find("]") >= 0:
+ url = b[i][b[i].find("[http")+1:b[i].find("]")]
+ else:
+ url = b[i][b[i].find("[http")+1:]
+ while b[i].find("]") < 0:
+ if i != start:
+ url += b[i]
+ i += 1
+ if i != start:
+ url += b[i][:b[i].find("]")]
+ url = url.replace(" ", "").replace("\n", "")
vim.command("edit .hackernews")
b = vim.current.buffer
- content = urllib2.urlopen("http://fuckyeahmarkdown.com/go/?read=1&u="+m.group(1)).read()
+ content = urllib2.urlopen("http://fuckyeahmarkdown.com/go/?read=1&u="+url).read()
for i, line in enumerate(content.split('\n')):
if not line:
b.append("")
@@ -47,23 +83,7 @@ def hacker_news_link():
b.append(wrap)
return
- 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("[%s]" % item['url'])
- b.append("")
- b.append("")
- print_comments(item['comments'], b)
+ print "HackerNews.vim Error: Could not parse [item id]"
html = HTMLParser.HTMLParser()