aboutsummaryrefslogtreecommitdiffstats
path: root/pluginManager.js
blob: 95c84c6a9b1a9ed134a2c3f172a9b2c681120489 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
var PLUGIN_INFO =
<VimperatorPlugin>
<name>{NAME}</name>
<description>Manage Vimperator Plugin</description>
<description lang="ja">{"Vimpeator\u30D7\u30E9\u30B0\u30A4\u30F3\u7BA1\u7406"}</description>
<version>0.1a</version>
<detail><![CDATA[
これはVimperatorプラグインの詳細情報orヘルプを表示するためのプラグインです
== Command ==
:plugin[help] [pluginName] [-v]:
	{pluginName}を入れるとそのプラグインの詳細を表示します
	省略すると全てのプラグインの詳細を表示します
	オプション -v はより細かなデータを表示します

== Mapping ==
none

== ToDo ==
* hogehoge
]]></detail>
</VimperatorPlugin>;

liberator.plugins.pluginManager = (function(){

var lang = window.navigator.language;
var tags = {
	name: function(info) info.name ? info.name[0] : null,
	description: function(info){
		if (!info.description) return null;
		var desc = "";
		var length = info.description.length();
		if (length > 1){
			desc = info.description[0];
			for (let i=0; i<length; i++){
				if (info.description[i].@lang == lang)
					desc = info.description[i];
			}
		}
		return desc;
	},
	version: function(info) info.version || null,
	maxVersion: function(info) info.maxVersion || null,
	minVersion: function(info) info.minVersion || null,
	detail: function(info){
		if (!info.detail)
			return null;

		if (info.detail.* && info.detail.*[0].nodeKind() == 'element')
			return info.detail.*;

		/*
		function fromUTF8Octets(octets){
			return decodeURIComponent(octets.replace(/[%\x80-\xFF]/g, function(c){
				return "%" + c.charCodeAt(0).toString(16);
			}));
		}
		function escapeUnicode(str){
			return str.replace(/[^\u0020-\u005B\u005D-\u007E]/g, function(m0){
				var code = m0.charCodeAt(0);
				return '\\u'+ ((code < 0x10)? '000' :
								(code < 0x100)? '00':
								(code < 0x100)? '0' : '') + code.toString(16);
			});
		}
		function unescapeUnicode(str){
			return str.replace(/\\u([a-fA-F0-9]{4})/g, function(m0, m1){
				return String.fromCharCode(parseInt(m1, 16));
			});
		}
		*/
		//var text = fromUTF8Octets(info.detail.*.toString());
		var text = info.detail.*.toString();
		/*
		var converter = Cc["@mozilla.org/intl/scriptableunicodeconverter"].createInstance(Ci.nsIScriptableUnicodeConverter);
		converter.charset = "UTF-16";
		text = converter.ConvertFromUnicode(text);
		*/
		var lines = text.split(/\r\n|[\r\n]/);
		var xml = <></>;
		var ite = Iterator(lines);
		var num, line;
		try {
		while ([num, line] = ite.next()){
			if (!line) continue;
			if (/^\s*==(.*)==\s*$/.test(line)){
				line = RegExp.$1;
				xml += <h1 style="font-weight:bold;font-size:medium;">{line}</h1>;
				continue;
			}
			let reg = /^\s*(.*)\s*:\s*$/;
			if (reg.test(line)){
				let dl = <dl><dt>{RegExp.$1}</dt></dl>;
				while ([num, line] = ite.next()){
					if (!line) break;
					if (reg.test(line)){
						dl.* += <dt>{RegExp.$1}</dt>;
					} else {
						dl.* += <dd>{line.replace(/^\s+|\s+$/g, "")}</dd>;
					}
				}
				xml += dl;
				continue;
			}
			xml += <>{line}<br/></>;
		}
		} catch (e){}
		return xml;
	}
};
function getPlugins(){
	var list = [];
	var contexts = liberator.plugins.contexts;
	for (let path in contexts){
		let context = contexts[path];
		let info = context.PLUGIN_INFO || null;
		let plugin = [
			["path", path]
		];
		plugin["name"] = context.NAME;
		if (info){
			for (let tag in tags){
				let value = tags[tag](info);
				if (value.toString().length > 0)
					plugin.push([tag, value]);
			}
		}
		list.push(plugin);
	}
	return list;
}
function itemFormater(plugin, showDetail){
	if (showDetail)
		return template.table(plugin.name, plugin);
	
	var data = plugin.filter(function($_) $_[0] != 'detail');
	return template.table(plugin.name, data);
}
commands.addUserCommand(['plugin[help]'], 'list Vimperator plugin ',
	function(args){
		liberator.plugins.pluginManager.list(args[0], args['-verbose']);
	}, {
		argCount: "*",
		options: [
			[['-verbose', '-v'], commands.OPTION_NOARG],
		],
		completer: function(context){
			var all = getPlugins().map(function(plugin){
				let desc = '-';
				for (let i=0; i<plugin.length; i++){
					if (plugin[i][0]== 'description')
						desc = plugin[i][1];
				}
				return [plugin.name, desc];
			});
			context.title = ['PluginName', 'Description'];
			context.completions = all.filter(function(row) row[0].toLowerCase().indexOf(context.filter.toLowerCase()) >= 0);

		}
	}, true);
var public = {
	list: function(name, showDetail){
		var xml = <></>;
		var plugins = getPlugins();
		if (name){
			let plugin = plugins.filter(function(plugin) plugin.name == name)[0];
			if (plugin){
				xml = itemFormater(plugin, showDetail);
			}
		} else {
			plugins.forEach(function(plugin) xml += itemFormater(plugin, showDetail));
		}
		liberator.echo(xml, true);
	}
};
return public;
})();