このページでは、ウィキペディア日本語版で役に立つスクリプトについて紹介します。

注意 編集

KISSの原則 編集

できるだけ読みやすくするよう、KISSの原則に従ってスクリプトを簡潔で単純にすることを検討してください。

ライセンス 編集

ウィキペディアへの投稿(このページの編集を含む)はCC BY-SAライセンス(ほとんどの場合、GFDLライセンスも)に基づき公開されます。しかし、CC BY-SAライセンスは美術や文学向けに作成され、ソフトウェア向けではありません。そのため、このページで公開したスクリプトが最大限で活用されるために、GNU GPLのようなフリーソフトウェアライセンスでも提供することを検討してください。スクリプトのはじめに明記することで、ほかのライセンスでも提供していることを明示できます。

Unicode数値参照変換スクリプト 編集

JavaScript 編集

原作者:

動作確認済ブラウザ:

注釈:

You may not need a script for converting CJK characters if you have a Mac running Mac OS X 10.2 and have Mozilla as your browser. Just do the editing from within Mozilla. Mozilla automatically does the conversion. For example, in adding this edit, I type in the Japanese characters for "edit," which are 編集. Mozilla automatically converted these characters to the proper romanized Unicode format. Just look at the above lines in the editing box to see for yourself. -User: IppikiOokami 5 September 2003

JavaScript
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
  <head>
    <title>Unicode変換ツール</title>
  </head>
  <body>
   <form name="charform">
     <p>下記のボックスに文字を入力してくださいコードが127以上の文字は&amp;#1234;のように変換されます</p>
     <p>入力:</p>
     <textarea name="input" cols="80" rows="25" onKeyUp="revtxt()">
       JavaScriptが無効です
     </textarea>
     <p>出力:</p>
     <textarea name="output" cols="80" rows="25">
       JavaScriptが無効です
     </textarea>
    </form>
    <script type="text/javascript">
    <!--
      document.charform.input.value="";
      document.charform.output.value="ここに入力しないでください。";
      function revtxt() {
        var s=document.charform.input.value;
        o="";
        for( m=0;s.charAt(m);++m )
          if ( (c=s.charCodeAt(m))<128&&c!=38) o+=s.charAt(m);
          else if (c==38) o+="&amp;";
          else o+="&#"+c+";";
          document.charform.output.value=o;
      }
    -->
    </script>
</html>

Perl 編集

下記はPerlord()関数を利用して、標準入力キャラクタをキャラクタセット内の数字に変換するスクリプトです。

#!/usr/bin/perl
# Code is in the public domain.
use strict;

my @input  = split (//, <>);
foreach my $c ( @input ) {
       if ( ord($c) >= 255 ) {
               print '&amp;#' . ord($c) . ';';
       } else {
               print $c;
       }
}

EmEditor Professional for Windows 編集

Microsoft Windows用のテキストエディタとして、EmEditorがあります。これは、Unicodeをネイティブで扱えるため、ウィキペディアの編集に適しています。ここでは、EmEditor Professional版で使用できるWindows Scripting Hostを用いたマクロを紹介します。一部(document.selection.Text など)を除いてECMAScript互換のJScriptなので流用もしやすいでしょう。ただし、Standard版ではJScript形式のマクロは使用できないのでご注意ください。

単純に数値参照に変える 編集

範囲選択されている部分を数値参照に変えます。

  // EmEditor Professinal用 Unicode数値参照変換スクリプト "num.jsee"
  var str = document.selection.Text; 
  var res = "";
  for (var i = 0, l = str.length; i < l; i++) {
    var c = str.charCodeAt(i);
    if ( c < 0x7f) {
      res += String.fromCharCode(c);
    } else {
      res += "&#" + c + ";";
    }
  }
 
  document.selection.Text = res;

日本語版で扱えない文字のみ変える 編集

このスクリプトは、全角半角変換も行いますが、漏れがかなりあります。実用上は問題ないでしょう。

  // Wikipedia日本語版用 Unicode数値参照変換スクリプト "wjnum.jsee"
  var str = document.selection.Text; 
  var res = "";
 
  for (var i = 0; i < str.length; i++) {
    var c = str.charCodeAt(i);
    // 全角・半角変換
    if ( 0xff10 <= c && c <= 0xff19 || // 全角数字
         0xff21 <= c && c <= 0xff3a || // 全角英大文字
         0xff40 <= c && c <= 0xff5a // 全角英小文字
       ) { 
      c -= 0xfee0;
    } else if (c == 0xff0c) { // 全角カンマ
      c = 0x2c;
    } else if (c == 0xff0e) { // 全角ピリオド
      c = 0x2e;
    } else if (c == 0xff61) { // 半角カナ句点
      c = 0x3002;
    } else if (c == 0xff62) { // 半角カナ鉤括弧
      c = 0x300c;
    } else if (c == 0xff63) { // 半角カナ鉤括弧
      c = 0x300d;
    } else if (c == 0xff64) { // 半角カナ読点
      c = 0x3001;
    } else if (c == 0xff65) { // 半角カナ中黒
      c = 0x30fb;
    }
    // 問題がでやすい文字を数値参照化
    if ( c < 0x7f || // 1byte英数
         0x3041 <= c && c <= 0x3094 || // ひらがな
         0x309b <= c && c <= 0x309e || // かな記号
         0x30a1 <= c && c <= 0x30fe || // カタカナ
         0x3000 <= c && c <= 0x3015 || // CJK記号
         0x4e00 <= c && c <= 0x9f90 || // CJK統合漢字
         0xff01 <= c && c <= 0xff0f || // !"#$%&'()**+,-./
         0xff1a <= c && c <= 0xff20 || // :;<=>?@
         0xff3b <= c && c <= 0xff3f || // [\]^_
         0xff5b <= c && c <= 0xff5e    // {|}~
       ){ 
      res += String.fromCharCode(c);
    } else {
      res += "&#" + c + ";";
    }
  }
 
  document.selection.Text = res; //選択範囲 置換

関連項目 編集