diff options
| -rw-r--r-- | README.md | 10 | ||||
| -rw-r--r-- | doc/hackernews.txt | 10 | ||||
| -rw-r--r-- | ftplugin/hackernews.py | 20 | ||||
| -rw-r--r-- | plugin/hackernews.vim | 18 | ||||
| -rw-r--r-- | syntax/hackernews.vim | 1 | ||||
| -rw-r--r-- | tests.vader | 2 |
6 files changed, 52 insertions, 9 deletions
@@ -17,6 +17,16 @@ Basic Usage ----------- * Open the Hacker News front page in Vim by executing the `:HackerNews` command +* The HackerNews command takes an optional parameter to view items other + than the top stories on the front page: + * `:HackerNews ask` + * `:HackerNews show` + * `:HackerNews shownew` + * `:HackerNews jobs` + * `:HackerNews best` + * `:HackerNews active` + * `:HackerNews newest` + * `:HackerNews noobstories` * Press lowercase `o` to open links in Vim * Press uppercase `O` to open links in default web browser * Numbered lines with story titles on the front page link to the story url diff --git a/doc/hackernews.txt b/doc/hackernews.txt index 72e4f85..dc6ef13 100644 --- a/doc/hackernews.txt +++ b/doc/hackernews.txt @@ -9,6 +9,16 @@ Version: 0.1.1 BASIC USAGE *hackernews-usage* * Open the Hacker News front page in Vim by executing the `:HackerNews` command +* The HackerNews command takes an optional parameter to view items other + than the top stories on the front page: + * `:HackerNews ask` + * `:HackerNews show` + * `:HackerNews shownew` + * `:HackerNews jobs` + * `:HackerNews best` + * `:HackerNews active` + * `:HackerNews newest` + * `:HackerNews noobstories` * Press lowercase `o` to open links in Vim * Press uppercase `O` to open links in default web browser * Numbered lines with story titles on the front page link to the story url diff --git a/ftplugin/hackernews.py b/ftplugin/hackernews.py index 4c84cb7..a57ae9c 100644 --- a/ftplugin/hackernews.py +++ b/ftplugin/hackernews.py @@ -77,10 +77,16 @@ def main(): bwrite("") try: - news1 = json.loads(urlopen(API_URL+"/news", timeout=5) - .read().decode('utf-8')) - news2 = json.loads(urlopen(API_URL+"/news2", timeout=5) - .read().decode('utf-8')) + stories = vim.eval("g:hackernews_stories") or "news" + if stories == "news": + news1 = json.loads(urlopen(API_URL+"/news", timeout=5) + .read().decode('utf-8')) + news2 = json.loads(urlopen(API_URL+"/news2", timeout=5) + .read().decode('utf-8')) + items = news1 + news2 + else: + items = json.loads(urlopen(API_URL+"/"+stories, timeout=5) + .read().decode('utf-8')) except HTTPError: print("HackerNews.vim Error: %s" % str(sys.exc_info()[1].reason)) return @@ -88,7 +94,7 @@ def main(): print("HackerNews.vim Error: HTTP Request Timeout") return - for i, item in enumerate(news1+news2): + for i, item in enumerate(items): if 'title' not in item: continue if 'domain' in item: @@ -100,7 +106,7 @@ def main(): line = "%s%d. %s [%d]" line %= (" " if i+1 < 10 else "", i+1, item['title'], item['id']) bwrite(line) - if item['type'] == "link": + if item['type'] in ("link", "ask"): line = "%s%d points by %s %s | %d comments [%s]" line %= (" "*4, item['points'], item['user'], item['time_ago'], item['comments_count'], str(item['id'])) @@ -172,7 +178,7 @@ def link(external=False): bwrite("%s (%s)" % (item['title'], item['domain'])) else: bwrite(item['title']) - if item.get('comments_count', None): + if item.get('comments_count', None) is not None: bwrite("%d points by %s %s | %d comments" % (item['points'], item['user'], item['time_ago'], item['comments_count'])) diff --git a/plugin/hackernews.vim b/plugin/hackernews.vim index 7c8fab0..bd4e985 100644 --- a/plugin/hackernews.vim +++ b/plugin/hackernews.vim @@ -14,4 +14,20 @@ filetype plugin on " Load ftplugin when opening .hackernews buffer au! BufRead,BufNewFile *.hackernews set filetype=hackernews -command! HackerNews edit .hackernews +function HackerNews(...) + if a:0 > 0 + let available_lists = ['news', 'newest', 'ask', 'show', 'shownew', + \'jobs', 'best', 'active', 'noobstories'] + if index(available_lists, a:1) >= 0 + let g:hackernews_stories = a:1 + else + let g:hackernews_stories = 'news' + end + else + let g:hackernews_stories = 'news' + end + edit .hackernews + normal! gg +endfunction + +command! -nargs=? HackerNews call HackerNews(<q-args>) diff --git a/syntax/hackernews.vim b/syntax/hackernews.vim index 7e9d7ca..390d2a3 100644 --- a/syntax/hackernews.vim +++ b/syntax/hackernews.vim @@ -34,6 +34,7 @@ endtry syn match Comment /^\s*[0-9]\{1,2}\.\s/ syn match Comment /\s([^\[]\S\+\.\S\+)/ syn match Comment /^\s\{4}[0-9an]\+\s.\+\sago/ +syn match Comment /^\s\{4}[0-9an]\+\spoints.\+\s\s|.*comments/ syn match Comment /^.*ago\s|.*comments/ syn match Comment /^[0-9an]\+\s.\+\sago$/ diff --git a/tests.vader b/tests.vader index 8cb9de3..2638593 100644 --- a/tests.vader +++ b/tests.vader @@ -44,7 +44,7 @@ Do (Test HackerNews Defined): :%s/ \{2,}/ /g\<cr> Expect (edit .hackernews): - HackerNews 0 edit .hackernews + HackerNews ? call HackerNews(<q-args>) Execute (Test HackerNews Command): HackerNews |
