「テキスト変換」の版間の差分
セクションの修正 |
SyntaxHighlightにcopyの追加 |
||
| (同じ利用者による、間の10版が非表示) | |||
| 1行目: | 1行目: | ||
== 概要 == | == 概要 == | ||
開いているドキュメントのテキストを変換します。 | |||
== 特徴 == | == 特徴 == | ||
* | * 機能追加が簡単。 | ||
== | == 変更履歴 == | ||
* 2013 | * 1.0.2 (2013-01-25) | ||
* 2013 | ** 簡易表記対応 | ||
* 2013 | * 1.0.1 (2013-01-24) | ||
** グルーピング対応。replaceS、replaceL 対応 | |||
* 1.0.0 (2013-01-23) | |||
** 初版 | |||
== 使用方法 == | == 使用方法 == | ||
# 基本コードを拡張子 .js | # 基本コードを拡張子 .js で保存します。 | ||
# | # 必要な拡張コードを追加します。 | ||
#* | #*「拡張機能の追加」とある場所に、コードをコピペすれば拡張が可能です。 | ||
#* | #* 上から順にポップアップに表示されます。 | ||
# | # マクロを実行すると登録されている機能の一覧が表示されるので、選択して実行します。 | ||
#* | #* テキストを選択している場合は、対象を「選択テキスト」「全体」から選べます。 | ||
#* | #* テキストを選択していない場合は「全体」が対象です。 | ||
== 便利な使い方 == | == 便利な使い方 == | ||
# セパレータの挿入<br><code>f[f.length] = "--";</code><br>でセパレータを挿入できます(- の数は任意) | # セパレータの挿入<br><code>f[f.length] = "--";</code><br>でセパレータを挿入できます(- の数は任意)。 | ||
# グルーピング<br> | # グルーピング<br>下のコードで、グルーピングができます。 | ||
<syntaxhighlight lang="javascript"> | <syntaxhighlight lang="javascript" copy> | ||
f[f.length] = [ "グループ名", function(f){ // <= グルーピング開始 | f[f.length] = [ "グループ名", function(f){ // <= グルーピング開始 | ||
f[f.length] = { // <= 以降はグルーピングされる機能を追加 | f[f.length] = { // <= 以降はグルーピングされる機能を追加 | ||
| 35行目: | 38行目: | ||
</syntaxhighlight> | </syntaxhighlight> | ||
== 基本コード == | |||
<syntaxhighlight lang="javascript" copy> | |||
<syntaxhighlight lang="javascript"> | |||
#title = "テキスト変換" | #title = "テキスト変換" | ||
// ----------------------------------------------------------------------------- | // ----------------------------------------------------------------------------- | ||
| 186行目: | 188行目: | ||
</syntaxhighlight> | </syntaxhighlight> | ||
== 拡張コード == | |||
'''ご自由に追加してください!''' | '''ご自由に追加してください!''' | ||
2つ並んでいますが、機能は全く同じです。 | |||
<syntaxhighlight lang="javascript"> | |||
管理のしやすさから、1行にまとめている方をお勧めします。 | |||
=== 空白削除 === | |||
<syntaxhighlight lang="javascript" copy> | |||
f[f.length] = [ "空白削除", function(f){ | 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,"")} ]; | ||
| 201行目: | 205行目: | ||
}]; | }]; | ||
</syntaxhighlight> | </syntaxhighlight> | ||
<syntaxhighlight lang="javascript"> | <syntaxhighlight lang="javascript" copy> | ||
f[f.length] = { | f[f.length] = { | ||
title:"空白削除", | title:"空白削除", | ||
| 229行目: | 233行目: | ||
</syntaxhighlight> | </syntaxhighlight> | ||
=== 文字変換 === | |||
<syntaxhighlight lang="javascript"> | <syntaxhighlight lang="javascript" copy> | ||
f[f.length] = [ "文字変換", function(f){ | f[f.length] = [ "文字変換", function(f){ | ||
f[f.length] = [ "大文字に変換", "", function(a){return a.toUpperCase()} ]; | f[f.length] = [ "大文字に変換", "", function(a){return a.toUpperCase()} ]; | ||
| 247行目: | 251行目: | ||
}]; | }]; | ||
</syntaxhighlight> | </syntaxhighlight> | ||
<syntaxhighlight lang="javascript"> | <syntaxhighlight lang="javascript" copy> | ||
f[f.length] = { | f[f.length] = { | ||
title:"文字変換", | title:"文字変換", | ||
| 303行目: | 307行目: | ||
</syntaxhighlight> | </syntaxhighlight> | ||
=== ソート === | |||
「[[昇順で並び替え]]」「[[降順で並び替え]] | 「[[昇順で並び替え]]」「[[降順で並び替え]]」の移植です。 | ||
<syntaxhighlight lang="javascript"> | <syntaxhighlight lang="javascript" copy> | ||
f[f.length] = [ "ソート", function(f){ | 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().join("\n")} ]; | ||
| 311行目: | 315行目: | ||
}]; | }]; | ||
</syntaxhighlight> | </syntaxhighlight> | ||
<syntaxhighlight lang="javascript"> | <syntaxhighlight lang="javascript" copy> | ||
f[f.length] = { | f[f.length] = { | ||
title:"ソート", | title:"ソート", | ||
| 327行目: | 331行目: | ||
</syntaxhighlight> | </syntaxhighlight> | ||
=== 行操作 === | |||
「[[連続する重複行を削除]] | 「[[連続する重複行を削除]]」の移植を含みます。 | ||
<syntaxhighlight lang="javascript"> | <syntaxhighlight lang="javascript" copy> | ||
f[f.length] = [ "行操作", function(f){ | f[f.length] = [ "行操作", function(f){ | ||
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(" | 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(" | 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] = [ "行削除", function(f){ | ||
| 345行目: | 349行目: | ||
}]; | }]; | ||
</syntaxhighlight> | </syntaxhighlight> | ||
<syntaxhighlight lang="javascript"> | <syntaxhighlight lang="javascript" copy> | ||
f[f.length] = { | f[f.length] = { | ||
title:"行操作", | title:"行操作", | ||
| 357行目: | 361行目: | ||
var input = null; | var input = null; | ||
return function(text){ | return function(text){ | ||
if (!input){ if(!(input=Prompt(" | if (!input){ if(!(input=Prompt("追加する文字列を入力してください。",""))){Quit()} } | ||
return input + text; | return input + text; | ||
} | } | ||
| 367行目: | 371行目: | ||
var input = null; | var input = null; | ||
return function(text){ | return function(text){ | ||
if (!input){ if(!(input=Prompt(" | if (!input){ if(!(input=Prompt("追加する文字列を入力してください。",""))){Quit()} } | ||
return text + input; | return text + input; | ||
} | } | ||
| 450行目: | 454行目: | ||
</syntaxhighlight> | </syntaxhighlight> | ||
=== グルーピング === | |||
選択範囲を簡易的にグルーピングします。 | |||
<syntaxhighlight lang="javascript"> | <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+"}];"} ]; | 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> | ||
<syntaxhighlight lang="javascript"> | <syntaxhighlight lang="javascript" copy> | ||
f[f.length] = { | f[f.length] = { | ||
title:"機能のグルーピング", | title:"機能のグルーピング", | ||
| 462行目: | 466行目: | ||
if (!group) { Quit(); } | if (!group) { Quit(); } | ||
var indent = text.match(/^\s*/); | var indent = text.match(/^\s*/); | ||
var tab = "\t"; // | var tab = "\t"; // インデント方式。タブでない場合はその分の空白を代入しておく。 | ||
return indent + 'f[f.length] = [ "' + group + '", function(f){\n'+ tab + text.split("\n").join("\n" + tab) + "\n" + indent + "}];"; | return indent + 'f[f.length] = [ "' + group + '", function(f){\n'+ tab + text.split("\n").join("\n" + tab) + "\n" + indent + "}];"; | ||
} | } | ||
| 468行目: | 472行目: | ||
</syntaxhighlight> | </syntaxhighlight> | ||
=== BASE64エンコード(Unicode) === | |||
<syntaxhighlight lang="javascript"> | <syntaxhighlight lang="javascript" copy> | ||
f[f.length] = { | f[f.length] = { | ||
title:"BASE64エンコード(Unicode)", | title:"BASE64エンコード(Unicode)", | ||
| 487行目: | 491行目: | ||
== 開発者向け資料 == | == 開発者向け資料 == | ||
これは拡張コードを開発される方向けの仕様です。 | |||
拡張コードは次のいずれかの関数を実装します。 | |||
# replace | # replace | ||
# replaceS | # replaceS | ||
| 494行目: | 499行目: | ||
=== replace === | === replace === | ||
<syntaxhighlight lang="javascript"> | <syntaxhighlight lang="javascript" copy> | ||
function replace( | function replace( | ||
text : string, | text : string, | ||
| 500行目: | 505行目: | ||
) : string | ) : string | ||
</syntaxhighlight> | </syntaxhighlight> | ||
最も基本的な変換関数です。 | |||
選択されている文字列、またはドキュメント全体の文字列を引数として、変換結果を返します。 | |||
selection | |||
Selection | selection 引数は通常必要有りません。 | ||
Selection の持つ関数を利用したい場合などに利用します。 | |||
=== replaceS === | === replaceS === | ||
<syntaxhighlight lang="javascript"> | <syntaxhighlight lang="javascript" copy> | ||
function replaceS( | function replaceS( | ||
text : string, | text : string, | ||
| 513行目: | 520行目: | ||
) : string | ) : string | ||
</syntaxhighlight> | </syntaxhighlight> | ||
行途中の選択がされている場合に、行全体を選択し直してから実行される変換関数です。 | |||
行は論理行で処理しますが、下記のように <code>view:true</code> を定義時に登録することで、表示行で処理することもできます。 | |||
<syntaxhighlight lang="javascript"> | <syntaxhighlight lang="javascript" copy> | ||
f[f.length] = { | f[f.length] = { | ||
title:"replaceL", | title:"replaceL", | ||
| 525行目: | 532行目: | ||
=== replaceL === | === replaceL === | ||
<syntaxhighlight lang="javascript"> | <syntaxhighlight lang="javascript" copy> | ||
function replaceL( | function replaceL( | ||
text : string, | text : string, | ||
| 531行目: | 538行目: | ||
) : string | ) : string | ||
</syntaxhighlight> | </syntaxhighlight> | ||
行単位で処理する場合に利用する変換関数です。 | |||
次の 2 つの定義は同じ処理(空白行削除) | 渡される text は、他の関数と違い'''「1 行分の文字列」'''です。 | ||
<syntaxhighlight lang="javascript"> | |||
<code>String.replace</code> や <code>String.split("\n").join("\n")</code> を利用することで同様の処理が行えますが、より簡易的に処理するために用意されています。 | |||
次の 2 つの定義は同じ処理(空白行削除)をします。 | |||
<syntaxhighlight lang="javascript" copy> | |||
replaceS:function(text){ return text.replace(/^\n/mg, "") } | replaceS:function(text){ return text.replace(/^\n/mg, "") } | ||
replaceL:function(text){ if (text != "") { return text } } | replaceL:function(text){ if (text != "") { return text } } | ||
</syntaxhighlight> | </syntaxhighlight> | ||
<code>replaceL</code> | <code>replaceL</code> 関数の場合、null を返すかあるいは return しない場合に行を削除します。 | ||
<code>replaceL</code> 関数は行毎に呼ばれるため、全体で1回のみの処理を記述するのが面倒です。 | |||
もし必要な場合は、拡張コードの「文字列追加」のようにクロージャを利用する必要があります。 | |||
=== 関数の簡易表記 === | === 関数の簡易表記 === | ||
関数を1行で書きやすくするため、簡易表記を追加しました。 | |||
<syntaxhighlight lang="javascript"> | 配列の形式で記述し、 | ||
<syntaxhighlight lang="javascript" copy> | |||
f[f.length] = [ "機能名", "関数タイプ", function(text){ return text; }, isView | f[f.length] = [ "機能名", "関数タイプ", function(text){ return text; }, isView | ||
</syntaxhighlight> | </syntaxhighlight> | ||
で記述します。 | |||
関数タイプは文字で記述し、それぞれ次のように対応します。 | |||
* "" ⇒ <code>replace</code> | * "" ⇒ <code>replace</code> | ||
* "S" ⇒ <code>replaceS</code> | * "S" ⇒ <code>replaceS</code> | ||
* "L" ⇒ <code>replaceL</code | * "L" ⇒ <code>replaceL</code> | ||
従来通りの記述から、簡易形式に変換するためのマクロを用意しました。 | |||
[http://dean.edwards.name/packer/ packer] | 関数部分はクリップボードにコピーされるため、そのまま貼り付けるか、[http://dean.edwards.name/packer/ packer] などを利用して圧縮して貼り付けてください。 | ||
<syntaxhighlight lang="javascript"> | <syntaxhighlight lang="javascript" copy> | ||
// 関数の簡易表現化 | // 関数の簡易表現化 | ||
// 関数部分はクリップボードにコピーする | // 関数部分はクリップボードにコピーする | ||
// http://dean.edwards.name/packer/ | // http://dean.edwards.name/packer/ などを用いて圧縮して貼り付ける。(別のそのまま貼り付けてもOK) | ||
// view 属性には非対応 | // view 属性には非対応 | ||
f[f.length] = { | f[f.length] = { | ||
| 571行目: | 582行目: | ||
var ar = text.split("\n"); | var ar = text.split("\n"); | ||
if (!/^\s*f\[f\.length\]\s*=\s*\{\s*$/.test(ar[0]) || !/^\s*\};\s*/.test(ar[ar.length-1])) { | if (!/^\s*f\[f\.length\]\s*=\s*\{\s*$/.test(ar[0]) || !/^\s*\};\s*/.test(ar[ar.length-1])) { | ||
Alert(" | Alert("対応していない表記です。選択範囲のミスはありませんか?"); | ||
Quit(); | Quit(); | ||
} | } | ||
| 595行目: | 606行目: | ||
}; | }; | ||
</syntaxhighlight> | </syntaxhighlight> | ||
== ご意見・ご要望 == | == ご意見・ご要望 == | ||
何かありましたら、右の [編集] から追記してください。対応するもかしれません。 | |||
スポンサーリンク