encodeHTML.js
/* kate-script
* author: akirattii, <tanaka.akira.2006@gmail.com>
* license: BSD
* revision: 1
* kate-version: 3.11.2
* type: commands
* functions: encode, decode
*/
require("range.js");
function help(cmd) {
if (cmd == "encode") {
return i18n("encode html/xml");
} else if (cmd == "decode") {
return i18n("decode html/xml");
}
}
function action(cmd) {
var a = new Object();
if (cmd == "encode") {
a.text = i18n("encode");
a.icon = "";
a.category = "HTML/XML";
a.interactive = false;
a.shortcut = "";
} else if (cmd == "decode") {
a.text = i18n("decode");
a.icon = "";
a.category = "HTML/XML";
a.interactive = false;
a.shortcut = "";
}
return a;
}
function encode() {
var selectionRange = view.selection();
if (view.hasSelection()) {
if (selectionRange.isValid()) {
var fromLine = selectionRange.start.line;
var toLine = selectionRange.end.line;
var fromColumn = selectionRange.start.column;
var toColumn = selectionRange.end.column;
// encode html
var newTxt = view.selectedText()
.replace(/&/g, "&")
.replace(/>/g, ">")
.replace(/</g, "<")
.replace(/"/g, """);
document.editBegin();
document.removeText(fromLine, fromColumn, toLine, toColumn);
document.insertText(fromLine, fromColumn, newTxt);
document.editEnd();
}
}
}
function decode() {
var selectionRange = view.selection();
if (view.hasSelection()) {
if (selectionRange.isValid()) {
var fromLine = selectionRange.start.line;
var toLine = selectionRange.end.line;
var fromColumn = selectionRange.start.column;
var toColumn = selectionRange.end.column;
// decode html
var newTxt = view.selectedText()
.replace(/&/g, "&")
.replace(/>/g, ">")
.replace(/</g, "<")
.replace(/"/g, "\"");
document.editBegin();
document.removeText(fromLine, fromColumn, toLine, toColumn);
document.insertText(fromLine, fromColumn, newTxt);
document.editEnd();
}
}
}これを例によって
/usr/share/kde4/apps/katepart/script/commandsにコピーして、Kateを再起動すればOK。
適当なHTML文字列を選択して
Tools > Scripts > HTML/XML > encode
を実行すればHTMLエンコードが、
Tools > Scripts > HTML/XML > decode
を実行すればHTMLデコードできます。
Kateユーザのプログラマなら何気に重宝するのではないかと思います。
0 件のコメント:
コメントを投稿