Created
February 24, 2009 17:17
-
-
Save snaka/69667 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// gist.js | |
// | |
// LICENSE: {{{ | |
// Copyright (c) 2009 snaka<[email protected]> | |
// | |
// Distributable under the terms of an MIT-style license. | |
// http://www.opensource.jp/licenses/mit-license.html | |
// }}} | |
// PLUGIN INFO: {{{ | |
var PLUGIN_INFO = | |
<VimperatorPlugin> | |
<name>gist</name> | |
<description>Use Gist via commandline</description> | |
<description lang="ja">Gistをコマンドラインから使う</description> | |
<minVersion>2.0pre</minVersion> | |
<maxVersion>2.0</maxVersion> | |
<updateURL>http://svn.coderepos.org/share/lang/javascript/vimperator-plugins/trunk/gist.js</updateURL> | |
<author mail="[email protected]" homepage="http://vimperator.g.hatena.ne.jp/snaka72/">snaka</author> | |
<license>MIT style license</license> | |
<version>0.1</version> | |
<detail><![CDATA[ | |
== Subject == | |
Use Gist via vimperator commandline. | |
== Commands == | |
Show your all Gists. | |
>|| | |
:gist | |
||< | |
Copy gist to clipboard. | |
>|| | |
:gist id | |
||< | |
== ToDo == | |
Tooooo slow. more speeeeeed. | |
]]></detail> | |
<detail lang="ja"><![CDATA[ | |
== 概要 == | |
GistのVimperatorのコマンドラインから使う。 | |
== コマンド == | |
自分のGistの一覧を表示 | |
>|| | |
:gist | |
||< | |
選択したGistの内容をクリップボードにコピー | |
>|| | |
:gist id | |
||< | |
idは補完できます(遅いけど...) | |
== ToDo == | |
Tooooo slow. more speeeeeed. | |
]]></detail> | |
<detail lang="ja"><![CDATA[ | |
== 概要 == | |
== ToDo == | |
補完リストが遅すぎる。ちゃんとキャッシュとか考えないとダメだ。 | |
]]></detail> | |
</VimperatorPlugin>; | |
// }}} | |
liberator.plugins.gist = (function(){ | |
const GIST_URI = "http://gist.github.com"; | |
// PUBLIC {{{ | |
const self = { | |
// for DEBUG | |
//getList: function(url) getList(url || DEBUG_URL) | |
}; | |
// }}} | |
// COMMAND {{{ | |
commands.addUserCommand( | |
["gist"], | |
"Use gist", | |
function(args) { | |
if (args.length == 0) { | |
// show list | |
liberator.echo( | |
<table> | |
<tr> | |
<th>Id</th> | |
<th>Description or Content</th> | |
</tr> | |
{[ | |
<tr> | |
<td>{i}</td> | |
<td>{gist[i]}</td> | |
</tr> | |
for (i in (gist = getList(GIST_URI + '/mine'))) | |
].reduce(function(prev, curr) prev+curr)} | |
</table>, | |
commandline.FORCE_MULTILINE | |
); | |
} | |
else { | |
// get gist then copy to clipboard | |
var gist = getGistByRawFormat(args[0]); | |
util.copyToClipboard(gist); | |
liberator.echo(gist, commandline.FORCE_MULTILINE); | |
} | |
}, { | |
completer: function(context) { | |
context.title = ["gist", "Description / Content"]; | |
context.completions = [[i.substr(1), list[i]] for (i in (list = getList(GIST_URI + '/mine')))]; | |
}, | |
argCount: "*" | |
}, | |
true // for DEBUG | |
); | |
// }}} | |
// PRIVATE {{{ | |
function getList(url) { | |
var gists = {}; | |
var req = new libly.Request(url, null, {asynchronous: false}); | |
req.addEventListener("onSuccess", function(res) { | |
//liberator.echo(res.responseText, commandline.FORCE_MULTILINE); | |
res.getHTMLDocument(); | |
[ gists[i.href] = /*$LX("../../span[2]", i)*/getDescriptionOrContent(i) | |
for each (i in $LXs("//div[@id='files']//span[1]/a", res.doc)) ] | |
// follow next link | |
var next = $LX("//div[@class='pagination']/a[@hotkey='l']", res.doc); | |
if (next != null) | |
[ gists[i] = nextGists[i] for (i in (nextGists = getList(GIST_URI + next.href)))]; | |
}); | |
req.get(); | |
//liberator.echo(gists); | |
return gists; | |
} | |
function getDescriptionOrContent(ctx) { | |
var desc = $LX("../../span[2]", ctx); | |
if (desc && desc.textContent != "") | |
return desc.textContent; | |
var contents = $LXs("../../../../div[2]//div[@id='LC1'] | ../../../../div[2]//div[@id='LC2'] | ../../../../div[2]//div[@id='LC3']", ctx); | |
if (contents) | |
return [i.textContent for each (i in contents)].join(""); | |
return ""; | |
} | |
function getGistByRawFormat(gistId) { | |
var content = ""; | |
var req = new libly.Request(GIST_URI + '/' + gistId + '.txt', null, {asynchronous: false}); | |
req.addEventListener("onSuccess", function(res) { | |
content = res.responseText; | |
}); | |
req.get(); | |
return content; | |
} | |
var $LX = plugins.libly.$U.getFirstNodeFromXPath; | |
var $LXs = plugins.libly.$U.getNodesFromXPath; | |
// }}} | |
return self; | |
})(); | |
// vim:sw=2 ts=2 et si fdm=marker: |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment