「テキスト変換」の版間の差分
細編集の要約なし |
SyntaxHighlightにcopyの追加 |
||
| (2人の利用者による、間の14版が非表示) | |||
| 1行目: | 1行目: | ||
== 概要 == | == 概要 == | ||
開いているドキュメントのテキストを変換します。 | |||
== 特徴 == | == 特徴 == | ||
* | * 機能追加が簡単。 | ||
== 変更履歴 == | |||
* 1.0.2 (2013-01-25) | |||
** 簡易表記対応 | |||
* 1.0.1 (2013-01-24) | |||
** グルーピング対応。replaceS、replaceL 対応 | |||
* 1.0.0 (2013-01-23) | |||
** 初版 | |||
== 使用方法 == | == 使用方法 == | ||
# 基本コードを拡張子 .js | # 基本コードを拡張子 .js で保存します。 | ||
# | # 必要な拡張コードを追加します。 | ||
#* | #*「拡張機能の追加」とある場所に、コードをコピペすれば拡張が可能です。 | ||
#* | #* 上から順にポップアップに表示されます。 | ||
#* <code>f[f.length] = "--";</code><br>でセパレータを挿入できます(- の数は任意) | # マクロを実行すると登録されている機能の一覧が表示されるので、選択して実行します。 | ||
# | #* テキストを選択している場合は、対象を「選択テキスト」「全体」から選べます。 | ||
# | #* テキストを選択していない場合は「全体」が対象です。 | ||
== 便利な使い方 == | |||
# セパレータの挿入<br><code>f[f.length] = "--";</code><br>でセパレータを挿入できます(- の数は任意)。 | |||
# グルーピング<br>下のコードで、グルーピングができます。 | |||
<syntaxhighlight lang="javascript" copy> | |||
f[f.length] = [ "グループ名", function(f){ // <= グルーピング開始 | |||
f[f.length] = { // <= 以降はグルーピングされる機能を追加 | |||
title:"大文字に変換", | |||
replace:function(text){ return text.toUpperCase() } | |||
}; | |||
f[f.length] = { | |||
title:"小文字に変換", | |||
replace:function(text){ return text.toLowerCase() } | |||
}; | |||
]}; // <= グルーピングの終了 | |||
</syntaxhighlight> | |||
== 基本コード == | |||
<syntaxhighlight lang="javascript" copy> | |||
#title = "テキスト変換" | |||
// ----------------------------------------------------------------------------- | |||
// 手軽にテキスト変換を行うためのマクロ | |||
// ----------------------------------------------------------------------------- | |||
var f = []; | |||
// 拡張機能の追加 | |||
// ↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓ | |||
f[f.length] = [ "行頭の空白文字を削除", "", function(a){return a.replace(/^[ \t ]+/mg,"")} ]; | |||
f[f.length] = [ "行末の空白文字を削除", "", function(a){return a.replace(/[ \t ]+$/mg,"")} ]; | |||
f[f.length] = [ "行頭・行末の空白文字を削除", "", function(a){return a.replace(/^[ \t ]+|[ \t ]+$/mg,"")} ]; | |||
f[f.length] = [ "空白行を削除", "L", function(text){ return text==""?null:text} ]; | |||
// ↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑ | |||
// 基本機能 | |||
(function(){ | |||
var topMenu = CreatePopupMenu(); | |||
var menus = null; | |||
var selMenu = null; | |||
var docMenu = topMenu; | |||
var DIFF_ID = 0x10000; // 適用対象を判別するための ID の最低値 | |||
var SELECTION_ID = 0x10000; // Selection を対象 | |||
var DOCUMENT_ID = 0x20000; // Document を対象 | |||
var EDITOR_ID = 0x40000; // Editor を対象(未対応) | |||
// サブメニュー登録 | |||
var AddSubMenu = function(menu, text) { | |||
var subMenu = null; | |||
if (menu){ | |||
subMenu = CreatePopupMenu(); | |||
menu.AddPopup(text, subMenu); | |||
} | |||
return subMenu; | |||
}; | |||
// コマンド登録 | |||
var commands = {}, id = 0; | |||
var AddCommand = function(menus, text, cmd, flag) { | |||
for (var i=0; i<menus.length; i++) { | |||
if (menus[i]) { | |||
menus[i].Add(text, (DIFF_ID << i) + id, flag); | |||
commands[id] = cmd; | |||
id++; | |||
} | |||
} | |||
}; | |||
// 選択領域を行全体に変更してその文字列を返す | |||
SelectLine = function(selection, isPosView) { | |||
var posFlag = isPosView ? mePosView : mePosLogical; | |||
var ty = selection.GetTopPointY(posFlag); | |||
var x = ScrollX, y = ScrollY; | |||
selection.SetActivePoint(posFlag, 1, selection.GetBottomPointY(posFlag)); | |||
selection.EndOfLine(false, posFlag); | |||
selection.SetAnchorPoint(posFlag, 1, ty); | |||
ScrollX = x; ScrollY = y; | |||
return selection.Text; | |||
}; | |||
// 行単位処理 | |||
EnumLine = function(selection, isPosView, replace) { | |||
var lines = SelectLine(selection, isPosView).split("\n"); | |||
var result = []; | |||
for (var i=0; i<lines.length; i++) { | |||
var text = replace(lines[i]); | |||
if (text != null) { | |||
result.push(text); | |||
} | |||
} | |||
return result.join("\n"); | |||
} | |||
== | // 選択文字列があるときは選択肢追加 | ||
=== 拡張コード | var sel = document.Selection; | ||
if (sel.Text) { | |||
selMenu = AddSubMenu(topMenu, "選択文字列"); | |||
docMenu = AddSubMenu(topMenu, "全体"); | |||
} | |||
// 選択肢追加 | |||
// 定義リストからメニューを作成 | |||
var ListToMenu = function(menus, f) { | |||
for (var i=0; i<f.length; i++) { | |||
// グループ簡易設定の変換 | |||
if (f[i] instanceof Array && f[i].length >= 2 && typeof f[i][0] == "string" && typeof f[i][1] == "function") { | |||
f[i] = { title:f[i][0], group:f[i][1] }; | |||
} | |||
// 関数簡易設定の変換 | |||
if (f[i] instanceof Array && f[i].length >= 3 && typeof f[i][0] == "string" && typeof f[i][1] == "string" && typeof f[i][2] == "function") { | |||
var ar = f[i]; | |||
f[i] = { title:ar[0] }; | |||
switch (ar[1]) { | |||
case "": f[i]["replace"] = ar[2]; break; | |||
case "S": f[i]["replaceS"] = ar[2]; break; | |||
case "L": f[i]["replaceL"] = ar[2]; break; | |||
} | |||
if (ar.length >= 4 && typeof ar[3] == "boolean") { | |||
f[i]["view"] = ar[3]; | |||
} | |||
} | |||
if (typeof f[i]["title"] == "string") { | |||
// グループ登録またはコマンド登録 | |||
if (typeof f[i]["group"] == "function") { | |||
var group = []; | |||
f[i].group(group); | |||
var subMenus = []; | |||
for (var j=0; j<menus.length; j++) { | |||
subMenus.push(AddSubMenu(menus[j], f[i].title)); | |||
} | |||
ListToMenu(subMenus, group); | |||
} else if (typeof f[i]["replace"] == "function") { | |||
// 通常コマンド | |||
var exec = function(replace, text, isViewMode, selection){ return replace(text, selection) }; | |||
AddCommand(menus, f[i].title, {replace:f[i].replace, exec:exec}); | |||
} else if (typeof f[i]["replaceS"] == "function") { | |||
// 行全体選択 | |||
var exec = function(replace, text, isViewMode, selection){ return replace(SelectLine(selection, isViewMode), selection) }; | |||
AddCommand(menus, f[i].title, {replace:f[i].replaceS, isViewMode:f[i]["view"], exec:exec}); | |||
} else if (typeof f[i]["replaceL"] == "function") { | |||
// 行単位処理 | |||
var exec = function(replace, text, isViewMode, selection){ return EnumLine(selection, isViewMode, replace) }; | |||
AddCommand(menus, f[i].title, {replace:f[i].replaceL, isViewMode:f[i]["view"], exec:exec}); | |||
} | |||
} else if (typeof f[i] == "string" && /^-+$/.test(f[i])) { | |||
// セパレータ処理 | |||
AddCommand(menus, "", null, meMenuSeparator); | |||
} | |||
} | |||
}; | |||
ListToMenu([selMenu, docMenu], f); | |||
// ポップアップを表示して選択されたコマンドを実行 | |||
var mePosCaret = 0; | |||
var select = topMenu.Track(mePosMouse); | |||
if (select > 0) { | |||
var command = commands[select&(DIFF_ID-1)]; | |||
if (select & SELECTION_ID) { | |||
sel.Text = command.exec(command.replace, sel.Text, command["isViewMode"], sel); | |||
} else if (select & DOCUMENT_ID) { | |||
var x = ScrollX, y = ScrollY; | |||
sel.SelectAll(); | |||
ScrollX = x; ScrollY = y; | |||
Document.Text = command.exec(command.replace, Document.Text, command["isViewMode"], sel); | |||
} | |||
} | |||
})(); | |||
</syntaxhighlight> | |||
== 拡張コード == | |||
'''ご自由に追加してください!''' | '''ご自由に追加してください!''' | ||
* | 2つ並んでいますが、機能は全く同じです。 | ||
< | |||
管理のしやすさから、1行にまとめている方をお勧めします。 | |||
=== 空白削除 === | |||
<syntaxhighlight lang="javascript" copy> | |||
f[f.length] = [ "空白削除", function(f){ | |||
f[f.length] = [ "行頭の空白文字を削除", "", function(a){return a.replace(/^[ \t ]+/mg,"")} ]; | |||
f[f.length] = [ "行末の空白文字を削除", "", function(a){return a.replace(/[ \t ]+$/mg,"")} ]; | |||
f[f.length] = [ "行頭・行末の空白文字を削除", "", function(a){return a.replace(/^[ \t ]+|[ \t ]+$/mg,"")} ]; | |||
f[f.length] = [ "空白行を削除", "L", function(text){ return text==""?null:text} ]; | |||
f[f.length] = [ "カンマ前後の空白文字を削除", "", function(a){return a.replace(/^[ \t ]+|[ \t ]+$/mg,"").replace(/[ \t ]*,[ \t ]*/mg,",")} ]; | |||
}]; | |||
</syntaxhighlight> | |||
<syntaxhighlight lang="javascript" copy> | |||
f[f.length] = { | f[f.length] = { | ||
title:" | title:"空白削除", | ||
replace:function(text){ return text. | group:function(f){ | ||
f[f.length] = { | |||
title:"行頭の空白文字を削除", | |||
replace:function(text){ return text.replace(/^[ \t ]+/mg, "") } | |||
}; | |||
f[f.length] = { | |||
title:"行末の空白文字を削除", | |||
replace:function(text){ return text.replace(/[ \t ]+$/mg, "") } | |||
}; | |||
f[f.length] = { | |||
title:"行頭・行末の空白文字を削除", | |||
replace:function(text){ return text.replace(/^[ \t ]+|[ \t ]+$/mg, "") } | |||
}; | |||
f[f.length] = { | |||
title:"空白行を削除", | |||
replaceL:function(text){ return text=="" ? null : text } | |||
}; | |||
f[f.length] = { | |||
title:"カンマ前後の空白文字を削除", | |||
replace:function(text){ return text.replace(/^[ \t ]+|[ \t ]+$/mg, "").replace(/[ \t ]*,[ \t ]*/mg, ","); } | |||
}; | |||
} | |||
}; | }; | ||
</ | </syntaxhighlight> | ||
< | === 文字変換 === | ||
<syntaxhighlight lang="javascript" copy> | |||
f[f.length] = [ "文字変換", function(f){ | |||
f[f.length] = [ "大文字に変換", "", function(a){return a.toUpperCase()} ]; | |||
f[f.length] = [ "小文字に変換", "", function(a){return a.toLowerCase()} ]; | |||
f[f.length] = [ "全角⇒半角", function(f){ | |||
f[f.length] = [ "英字", "", function(a){return a.replace(/[A-Za-z]/mg,function(s){return String.fromCharCode(s.charCodeAt(0)-0xFEE0)})} ]; | |||
f[f.length] = [ "数字", "", function(a){return a.replace(/[0-9]/mg,function(s){return String.fromCharCode(s.charCodeAt(0)-0xFEE0)})} ]; | |||
f[f.length] = [ "英数字", "", function(a){return a.replace(/[A-Za-z0-9]/mg,function(s){return String.fromCharCode(s.charCodeAt(0)-0xFEE0)})} ]; | |||
}]; | |||
f[f.length] = [ "半角⇒全角", function(f){ | |||
f[f.length] = [ "英字", "", function(a){return a.replace(/[A-Za-z]/mg,function(s){return String.fromCharCode(s.charCodeAt(0)+0xFEE0)})} ]; | |||
f[f.length] = [ "数字", "", function(a){return a.replace(/[0-9]/mg,function(s){return String.fromCharCode(s.charCodeAt(0)+0xFEE0)})} ]; | |||
f[f.length] = [ "英数字", "", function(a){return a.replace(/[A-Za-z0-9]/mg,function(s){return String.fromCharCode(s.charCodeAt(0)+0xFEE0)})} ]; | |||
}]; | |||
f[f.length] = [ "アラビア数字⇒漢数字", "", function(a){return a.replace(/[0-9]/mg,function(s){return String.fromCharCode(s.charCodeAt(0)-0xFEE0)}).replace(/[0-9]/mg,function(s){return"〇一二三四五六七八九".substr(Number(s),1)})} ]; | |||
}]; | |||
</syntaxhighlight> | |||
<syntaxhighlight lang="javascript" copy> | |||
f[f.length] = { | f[f.length] = { | ||
title:"小文字に変換", | title:"文字変換", | ||
group:function(f){ | |||
f[f.length] = { | |||
title:"大文字に変換", | |||
replace:function(text){ return text.toUpperCase() } | |||
}; | |||
f[f.length] = { | |||
title:"小文字に変換", | |||
replace:function(text){ return text.toLowerCase() } | |||
}; | |||
f[f.length] = { | |||
title:"全角⇒半角", | |||
group:function(f){ | |||
f[f.length] = { | |||
title:"英字", | |||
replace:function(text){ return text.replace(/[A-Za-z]/mg, function(s){ return String.fromCharCode(s.charCodeAt(0) - 0xFEE0) }) } | |||
}; | |||
f[f.length] = { | |||
title:"数字", | |||
replace:function(text){ return text.replace(/[0-9]/mg, function(s){ return String.fromCharCode(s.charCodeAt(0) - 0xFEE0) }) } | |||
}; | |||
f[f.length] = { | |||
title:"英数字", | |||
replace:function(text){ return text.replace(/[A-Za-z0-9]/mg, function(s){ return String.fromCharCode(s.charCodeAt(0) - 0xFEE0) }) } | |||
}; | |||
} | |||
}; | |||
f[f.length] = { | |||
title:"半角⇒全角", | |||
group:function(f){ | |||
f[f.length] = { | |||
title:"英字", | |||
replace:function(text){ return text.replace(/[A-Za-z]/mg, function(s){ return String.fromCharCode(s.charCodeAt(0) + 0xFEE0) }) } | |||
}; | |||
f[f.length] = { | |||
title:"数字", | |||
replace:function(text){ return text.replace(/[0-9]/mg, function(s){ return String.fromCharCode(s.charCodeAt(0) + 0xFEE0) }) } | |||
}; | |||
f[f.length] = { | |||
title:"英数字", | |||
replace:function(text){ return text.replace(/[A-Za-z0-9]/mg, function(s){ return String.fromCharCode(s.charCodeAt(0) + 0xFEE0) }) } | |||
}; | |||
} | |||
}; | |||
f[f.length] = { | |||
title:"アラビア数字⇒漢数字", | |||
replace:function(text){ | |||
return text.replace(/[0-9]/mg, function(s){ return String.fromCharCode(s.charCodeAt(0) - 0xFEE0) }).replace(/[0-9]/mg, function(s){ return "〇一二三四五六七八九".substr(Number(s), 1); }); | |||
} | |||
}; | |||
} | |||
}; | }; | ||
</ | </syntaxhighlight> | ||
< | === ソート === | ||
「[[昇順で並び替え]]」「[[降順で並び替え]]」の移植です。 | |||
<syntaxhighlight lang="javascript" copy> | |||
f[f.length] = [ "ソート", function(f){ | |||
f[f.length] = [ "昇順ソート(文字列)", "S", function(a){return a.split("\n").sort().join("\n")} ]; | |||
f[f.length] = [ "降順ソート(文字列)", "S", function(a){return a.split("\n").sort().reverse().join("\n")} ]; | |||
}]; | |||
</syntaxhighlight> | |||
<syntaxhighlight lang="javascript" copy> | |||
f[f.length] = { | f[f.length] = { | ||
title:" | title:"ソート", | ||
group:function(f){ | |||
f[f.length] = { | |||
title:"昇順ソート(文字列)", | |||
replaceS:function(text){ return text.split("\n").sort().join("\n") } | |||
}; | |||
f[f.length] = { | |||
title:"降順ソート(文字列)", | |||
replaceS:function(text){ return text.split("\n").sort().reverse().join("\n") } | |||
}; | |||
} | |||
}; | }; | ||
</ | </syntaxhighlight> | ||
* BASE64エンコード(Unicode) | |||
< | === 行操作 === | ||
「[[連続する重複行を削除]]」の移植を含みます。 | |||
<syntaxhighlight lang="javascript" copy> | |||
f[f.length] = [ "行操作", function(f){ | |||
f[f.length] = [ "文字列追加", function(f){ | |||
f[f.length] = [ "先頭", "L", (function(b){var c=null;return function(a){if(!c){if(!(c=Prompt("追加する文字列を入力してください。",""))){Quit()}}return c+a}})() ]; | |||
f[f.length] = [ "末尾", "L", (function(b){var c=null;return function(a){if(!c){if(!(c=Prompt("追加する文字列を入力してください。",""))){Quit()}}return a+c}})() ]; | |||
}]; | |||
f[f.length] = [ "行削除", function(f){ | |||
f[f.length] = [ "含む行(文字列)", "L", (function(b){var c=null;return function(a){if(!c){if(!(c=Prompt("検索文字列",""))){Quit()}}return a.indexOf(c)>=0?null:a}})() ]; | |||
f[f.length] = [ "含む行(正規表現)", "L", (function(b){var c=null;return function(a){if(!c){if(!(c=Prompt("正規表現",""))){Quit()}else{c=new RegExp(c)}}return c.test(a)?null:a}})() ]; | |||
f[f.length] = [ "含まない行(文字列)", "L", (function(b){var c=null;return function(a){if(!c){if(!(c=Prompt("検索文字列",""))){Quit()}}return a.indexOf(c)>=0?a:null}})() ]; | |||
f[f.length] = [ "含まない行(正規表現)", "L", (function(b){var c=null;return function(a){if(!c){if(!(c=Prompt("正規表現",""))){Quit()}else{c=new RegExp(c)}}return c.test(a)?a:null}})() ]; | |||
f[f.length] = [ "連続する重複を削除", "S", function(a){a=a.split("\n");var b=[a[0]];for(var i=1;i<a.length;i++){if(a[i]!==a[i-1]){b.push(a[i])}}return b.join("\n")} ]; | |||
f[f.length] = [ "重複行を削除", "S", function(a){a=a.split("\n");var b=[],map={};for(var i=0;i<a.length;i++){if(!map[a[i]]){map[a[i]]=true;b.push(a[i])}}return b.join("\n")} ]; | |||
}]; | |||
}]; | |||
</syntaxhighlight> | |||
<syntaxhighlight lang="javascript" copy> | |||
f[f.length] = { | |||
title:"行操作", | |||
group:function(f){ | |||
f[f.length] = { | |||
title:"文字列追加", | |||
group:function(f){ | |||
f[f.length] = { | |||
title:"先頭", | |||
replaceL:(function(text){ | |||
var input = null; | |||
return function(text){ | |||
if (!input){ if(!(input=Prompt("追加する文字列を入力してください。",""))){Quit()} } | |||
return input + text; | |||
} | |||
})() | |||
}; | |||
f[f.length] = { | |||
title:"末尾", | |||
replaceL:(function(text){ | |||
var input = null; | |||
return function(text){ | |||
if (!input){ if(!(input=Prompt("追加する文字列を入力してください。",""))){Quit()} } | |||
return text + input; | |||
} | |||
})() | |||
}; | |||
} | |||
}; | |||
f[f.length] = { | |||
title:"行削除", | |||
group:function(f){ | |||
f[f.length] = { | |||
title:"含む行(文字列)", | |||
replaceL:(function(text){ | |||
var input = null; | |||
return function(text){ | |||
if (!input){ if(!(input=Prompt("検索文字列", ""))){Quit()} } | |||
return text.indexOf(input) >= 0 ? null : text; | |||
} | |||
})() | |||
}; | |||
f[f.length] = { | |||
title:"含む行(正規表現)", | |||
replaceL:(function(text){ | |||
var input = null; | |||
return function(text){ | |||
if (!input){ if(!(input=Prompt("正規表現", ""))){Quit()}else{input=new RegExp(input)} } | |||
return input.test(text) ? null : text; | |||
} | |||
})() | |||
}; | |||
f[f.length] = { | |||
title:"含まない行(文字列)", | |||
replaceL:(function(text){ | |||
var input = null; | |||
return function(text){ | |||
if (!input){ if(!(input=Prompt("検索文字列", ""))){Quit()} } | |||
return text.indexOf(input) >= 0 ? text : null; | |||
} | |||
})() | |||
}; | |||
f[f.length] = { | |||
title:"含まない行(正規表現)", | |||
replaceL:(function(text){ | |||
var input = null; | |||
return function(text){ | |||
if (!input){ if(!(input=Prompt("正規表現", ""))){Quit()}else{input=new RegExp(input)} } | |||
return input.test(text) ? text : null; | |||
} | |||
})() | |||
}; | |||
f[f.length] = { | |||
title:"連続する重複を削除", | |||
replaceS:function(text){ | |||
text = text.split("\n"); | |||
var result = [text[0]]; | |||
for (var i=1; i<text.length; i++) { | |||
if (text[i] !== text[i-1]) { | |||
result.push(text[i]); | |||
} | |||
} | |||
return result.join("\n"); | |||
} | |||
}; | |||
f[f.length] = { | |||
title:"重複行を削除", | |||
replaceS:function(text){ | |||
text = text.split("\n"); | |||
var result = [], map = {}; | |||
for (var i=0; i<text.length; i++) { | |||
if (!map[text[i]]) { | |||
map[text[i]] = true; | |||
result.push(text[i]); | |||
} | |||
} | |||
return result.join("\n"); | |||
} | |||
}; | |||
} | |||
}; | |||
} | |||
}; | |||
</syntaxhighlight> | |||
=== グルーピング === | |||
選択範囲を簡易的にグルーピングします。 | |||
<syntaxhighlight lang="javascript" copy> | |||
f[f.length] = [ "機能のグルーピング", "S", function(a){var b=Prompt("グループ名","");if(!b){Quit()}var c=a.match(/^\s*/);var d="\t";return c+'f[f.length] = [ "'+b+'", function(f){\n'+d+a.split("\n").join("\n"+d)+"\n"+c+"}];"} ]; | |||
</syntaxhighlight> | |||
<syntaxhighlight lang="javascript" copy> | |||
f[f.length] = { | |||
title:"機能のグルーピング", | |||
replaceS:function(text){ | |||
var group = Prompt("グループ名", ""); | |||
if (!group) { Quit(); } | |||
var indent = text.match(/^\s*/); | |||
var tab = "\t"; // インデント方式。タブでない場合はその分の空白を代入しておく。 | |||
return indent + 'f[f.length] = [ "' + group + '", function(f){\n'+ tab + text.split("\n").join("\n" + tab) + "\n" + indent + "}];"; | |||
} | |||
}; | |||
</syntaxhighlight> | |||
=== BASE64エンコード(Unicode) === | |||
<syntaxhighlight lang="javascript" copy> | |||
f[f.length] = { | f[f.length] = { | ||
title:"BASE64エンコード(Unicode)", | title:"BASE64エンコード(Unicode)", | ||
| 56行目: | 488行目: | ||
} | } | ||
}; | }; | ||
</ | </syntaxhighlight> | ||
=== | == 開発者向け資料 == | ||
< | これは拡張コードを開発される方向けの仕様です。 | ||
/ | 拡張コードは次のいずれかの関数を実装します。 | ||
# replace | |||
# replaceS | |||
# replaceL | |||
=== replace === | |||
<syntaxhighlight lang="javascript" copy> | |||
function replace( | |||
text : string, | |||
selection : Selection | |||
) : string | |||
</syntaxhighlight> | |||
最も基本的な変換関数です。 | |||
選択されている文字列、またはドキュメント全体の文字列を引数として、変換結果を返します。 | |||
selection 引数は通常必要有りません。 | |||
Selection の持つ関数を利用したい場合などに利用します。 | |||
/ | === replaceS === | ||
<syntaxhighlight lang="javascript" copy> | |||
function replaceS( | |||
text : string, | |||
selection : Selection | |||
) : string | |||
</syntaxhighlight> | |||
行途中の選択がされている場合に、行全体を選択し直してから実行される変換関数です。 | |||
行は論理行で処理しますが、下記のように <code>view:true</code> を定義時に登録することで、表示行で処理することもできます。 | |||
<syntaxhighlight lang="javascript" copy> | |||
f[f.length] = { | f[f.length] = { | ||
title:" | title:"replaceL", | ||
view:true, | |||
replaceS:function(text){ return text } | |||
}; | }; | ||
</syntaxhighlight> | |||
=== replaceL === | |||
<syntaxhighlight lang="javascript" copy> | |||
function replaceL( | |||
text : string, | |||
selection : Selection | |||
) : string | |||
</syntaxhighlight> | |||
行単位で処理する場合に利用する変換関数です。 | |||
渡される text は、他の関数と違い'''「1 行分の文字列」'''です。 | |||
<code>String.replace</code> や <code>String.split("\n").join("\n")</code> を利用することで同様の処理が行えますが、より簡易的に処理するために用意されています。 | |||
次の 2 つの定義は同じ処理(空白行削除)をします。 | |||
<syntaxhighlight lang="javascript" copy> | |||
replaceS:function(text){ return text.replace(/^\n/mg, "") } | |||
replaceL:function(text){ if (text != "") { return text } } | |||
</syntaxhighlight> | |||
<code>replaceL</code> 関数の場合、null を返すかあるいは return しない場合に行を削除します。 | |||
<code>replaceL</code> 関数は行毎に呼ばれるため、全体で1回のみの処理を記述するのが面倒です。 | |||
もし必要な場合は、拡張コードの「文字列追加」のようにクロージャを利用する必要があります。 | |||
=== 関数の簡易表記 === | |||
関数を1行で書きやすくするため、簡易表記を追加しました。 | |||
配列の形式で記述し、 | |||
<syntaxhighlight lang="javascript" copy> | |||
f[f.length] = [ "機能名", "関数タイプ", function(text){ return text; }, isView | |||
</syntaxhighlight> | |||
で記述します。 | |||
関数タイプは文字で記述し、それぞれ次のように対応します。 | |||
* "" ⇒ <code>replace</code> | |||
* "S" ⇒ <code>replaceS</code> | |||
* "L" ⇒ <code>replaceL</code> | |||
従来通りの記述から、簡易形式に変換するためのマクロを用意しました。 | |||
関数部分はクリップボードにコピーされるため、そのまま貼り付けるか、[http://dean.edwards.name/packer/ packer] などを利用して圧縮して貼り付けてください。 | |||
<syntaxhighlight lang="javascript" copy> | |||
if ( | // 関数の簡易表現化 | ||
// 関数部分はクリップボードにコピーする | |||
// http://dean.edwards.name/packer/ などを用いて圧縮して貼り付ける。(別のそのまま貼り付けてもOK) | |||
// view 属性には非対応 | |||
f[f.length] = { | |||
title:"関数の簡易表現化", | |||
replaceS:function(text){ | |||
var ar = text.split("\n"); | |||
if (!/^\s*f\[f\.length\]\s*=\s*\{\s*$/.test(ar[0]) || !/^\s*\};\s*/.test(ar[ar.length-1])) { | |||
Alert("対応していない表記です。選択範囲のミスはありませんか?"); | |||
Quit(); | |||
} | |||
var indent = text.match(/^\s*/); | |||
var result = indent + 'f[f.length] = ['; | |||
var func = ""; | |||
for (var i=1; i<ar.length-1; i++) { | |||
if (ar[i].match(/^\s*title:(.*)$/)) { | |||
result += " " + RegExp.$1; | |||
} else if (ar[i].match(/^\s*replace([A-Z]?):(.*)$/)) { | |||
result += ' "' + RegExp.$1 + '", '; | |||
func += RegExp.$2 + "\n"; | |||
} else { | |||
//func += ar[i].replace(/^\s*/, ""); | |||
func += ar[i] + "\n"; | |||
} | } | ||
} | } | ||
ClipboardData.SetData(func); | |||
//result += " " + func + "];" | |||
result += " ];"; | |||
return result; | |||
} | } | ||
}; | |||
</syntaxhighlight> | |||
} | |||
</ | |||
== ご意見・ご要望 == | == ご意見・ご要望 == | ||
何かありましたら、右の [編集] から追記してください。対応するもかしれません。 | |||
スポンサーリンク