SpidermonkeyでE4X
rhinoでE4Xの続き。コメントでspidermonkey-binは? とid:takano32さんに言われたのでインストールしてみた。
$ apt-cache search spidermonkey libmozjs-dev - Development files for the Mozilla SpiderMonkey JavaScript library libmozjs1d - The Mozilla SpiderMonkey JavaScript library libmozjs1d-dbg - Development files for the Mozilla SpiderMonkey JavaScript library spidermonkey-bin - standalone JavaScript/ECMAScript (ECMA-262) interpreter js2-mode - Emacs mode for editing Javascript programs
うむ、spidermonkey-binがありますね。んじゃ、インストール。
$ sudo apt-get install spidermonkey-bin パッケージリストを読み込んでいます... 完了 依存関係ツリーを作成しています 状態情報を読み取っています... 完了 以下のパッケージが新たにインストールされます: spidermonkey-bin アップグレード: 0 個、新規インストール: 1 個、削除: 0 個、保留: 0 個。 67.0kB のアーカイブを取得する必要があります。 この操作後に追加で 135kB のディスク容量が消費されます。 取得:1 http://ftp.jp.debian.org lenny/main spidermonkey-bin 1.9.0.5-1 [67.0kB] 67.0kB を 0s で取得しました (87.9kB/s) 未選択パッケージ spidermonkey-bin を選択しています。 (データベースを読み込んでいます ... 現在 166714 個のファイルとディレクトリがインストールされています。) (.../spidermonkey-bin_1.9.0.5-1_i386.deb から) spidermonkey-bin を展開しています... menu のトリガを処理しています ... man-db のトリガを処理しています ... spidermonkey-bin (1.9.0.5-1) を設定しています ... menu のトリガを処理しています ...
パッケージの中身はどうなっているの?
$ dpkg -L spidermonkey-bin /. /usr /usr/bin /usr/bin/smjs /usr/share /usr/share/doc /usr/share/doc/spidermonkey-bin /usr/share/doc/spidermonkey-bin/copyright /usr/share/doc/spidermonkey-bin/changelog.Debian.gz /usr/share/doc/spidermonkey-bin/MPL.gz /usr/share/menu /usr/share/menu/spidermonkey-bin /usr/share/man /usr/share/man/man1 /usr/share/man/man1/smjs.1.gz
ふむふむ、/usr/bin/smjsが実行ファイルっぽいね。んじゃ、実行
$ smjs js>
おぉ、インタープリタが立ち上がった。
js> print("ok")
ok
js> var xml = <root>hoge</root>;
js> xml
hoge
js> xml.* += <b>abc</b>;
hoge
<b>abc</b>
js> xml
<root>
hoge
<b>abc</b>
</root>
js> quit()E4Xも問題なく動く。letやArray.reduceとかもあるかな?
$ smjs js> let a = [1,2,3,4,5]; js> a.reduce(function(p,c) c+p) 15 js> quit()
うん、動く動く。
Vimperatorプラグインのcopy.jsを読み込んでみる。
$ smjs
js> var NAME = "copy";
js> try { load("copy.js"); } catch(e) {}
js> PLUGIN_INFO
js> PLUGIN_INFO.detail.toString()
== Command ==
:copy {copyString}:
copy the argument replaced some certain string
:copy! {expr}:
evaluate the argument and copy the result
=== Example ===
:copy %TITLE%:
copied the title of the current page
:copy title:
some as `:copy %TITLE%' by default
:copy! liberator.version:
copy the value of `liberator.version'
(略)すばらしい。
スクリプトを作ってみる。
wiki.js
var NAME = arguments[0]; try { load(NAME); } catch(e) {} load("jwiki.js"); var str = PLUGIN_INFO.detail.toString() var parser = new WikiParser(str); var xml = parser.parse() print(xml.toXMLString());
第一引数をファイルとしてロードし、さらに、wikiパーサのjswiki.jsをロードする(wiki formtterで使っているもの)。そして、パーサで文字列をパースしてHTML文字列を出力するスクリプトである。
以下の様に使う。
$ smjs wiki.js copy.js | w3m -dump -T text/html
Command
:copy {copyString}
copy the argument replaced some certain string
:copy! {expr}
evaluate the argument and copy the result
Example
:copy %TITLE%
copied the title of the current page
:copy title
some as `:copy %TITLE%' by default
:copy! liberator.version
copy the value of `liberator.version'
(略)素晴らしい。これでコンソールからVimperatorプラグインのヘルプを見ることが可能になるぜぃ。