goo blog サービス終了のお知らせ 

テーマ:未分類(甘党)

日々、思いついたことを記録します。

[秀丸エディタ TIPS] HTMLタグ削除マクロ

2007年05月04日 23時16分22秒 | 秀丸エディタ Tips
以前から、HTMLタグを削除するちょっとしたマクロを使っていましたが、今回、OutlookからHTMLメールを開くと、HTMLタグが入っているので、かなり、多用することになりました。
で、いろいろと、拡張をする必要があったので、以下のようなものになってしまいました。(いやはや、10,000文字を越えてしまったので、コメントアウト部分の008~249を省略)

-----------------

// HTMLタグの削除
// copyright (C) by 甘党のプログラマ
// 基本的には、レイアウトが崩れて見づらいが、とりあえず、テキストに変換したい場合に便利マクロ

// <style> ~ </style>の削除
// 今後も、削除漏れがあれば、この手法で削除できるはず
    gofiletop;
  while(1) {
    // まず、<style>を検索
    searchdown "<style>" , nocasesense;
    if( result )  {
      // 現在のカーソルの保持
      #top_x = x;
      #top_y = y;
      // </style>を検索して
      searchdown "</style>" , nocasesense;
      if( result )  {
        // 範囲選択後、削除
        searchdown ">";
        right;
        if( result )  {
          beginsel;
          moveto #top_x, #top_y;
          delete;
        }
      }
      else {
        break;
      }
    }
    else {
      break;
    }
  }

// <div...>と</div...>に、区切りを入れる。
    gofiletop;
  while (1) {
    searchdown "^</?div" , regular, nocasesense;
    if( result ) {
      insertline;
      insert "------------------------------------------------------------------[[br]]";
      down;
    }
    else {
      break;
    }
  }

// 改行コードの保持 ([todo]本当は、適当なコントロールコードが良いはず)
    replaceallfast "<br>" , "[[br]]" , nocasesense;

// <...>タグの削除
    replaceallfast "(<(n|[^>])+>)" , "" , regular, nocasesense;

// 空白行の削除の削除
//    replaceallfast "^ *n" , "" , regular, nocasesense;
    replaceallfast "n" , "" , regular, nocasesense;

// 元が<br>(現[[br]])を改行 ([todo]本当は、適当なコントロールコードが良いはず)
    replaceallfast "[[br]]" , "n" , nocasesense;
    if( ! result )  beep;

// 特殊文字の変換 (遅くなるので、最小限のものを有効にしたいので、変換しそこなったものに気が付いたら、コメントアウトを外すようにすると良い
  //001   &quot;  &#34;   &#x22;  """  クォーテーション  quotation mark = APL quote
  replaceallfast "&quot;" , """ ; //クォーテーション quotation mark = APL quote
//  replaceallfast "&#34;" , """ ; //クォーテーション  quotation mark = APL quote
//  replaceallfast "&#x22;" , """ ; //クォーテーション quotation mark = APL quote
  //002   &amp;   &#38;   &#x26;  "&"   アンパサンド  ampersand
  replaceallfast "&amp;" , "&" ; //アンパサンド ampersand
//  replaceallfast "&#38;" , "&" ; //アンパサンド ampersand
//  replaceallfast "&#x26;" , "&" ; //アンパサンド  ampersand
  //003   &lt;  &#60;   &#x3C;  "<"   小なり  less-than sign
  replaceallfast "&lt;" , "<" ; //小なり  less-than sign
//  replaceallfast "&#60;" , "<" ; //小なり   less-than sign
//  replaceallfast "&#x3C;" , "<" ; //小なり  less-than sign
  //004   &gt;  &#62;   &#x3E;  ">"   大なり  greater-than sign
  replaceallfast "&gt;" , ">" ; //大なり  greater-than sign
//  replaceallfast "&#62;" , ">" ; //大なり   greater-than sign
//  replaceallfast "&#x3E;" , ">" ; //大なり  greater-than sign
  //005   &nbsp;  &#160;  &#xA0;  " " 空白  no-break space = non-breaking space
  replaceallfast "&nbsp;" , " "; //空白   no-break space = non-breaking space
//  replaceallfast "&#160;", " "; //空白  no-break space = non-breaking space
//  replaceallfast "&#xA0;" , " "; //空白   no-break space = non-breaking space
//  //006   &iexcl;   &#161;  &#xA1;  "!"   inverted exclamation mark
//  replaceallfast "&iexcl;" , "!"; //  inverted exclamation mark
//  replaceallfast "&#161;", "!"; //  inverted exclamation mark
//  replaceallfast "&#xA1;" , "!"; // inverted exclamation mark
//  //007   &cent;  &#162;  &#xA2;  "¢"  セント  cent sign
//  replaceallfast "&cent;" , "¢"; //セント  cent sign
//  replaceallfast "&#162;", "¢"; //セント   cent sign
//  replaceallfast "&#xA2;" , "¢"; //セント  cent sign
// ....(省略)
//  //250   &lsaquo;  &#8249; &#x2039;  "?"   single left-pointing angle quotation mark
//  replaceallfast "&lsaquo;" , "?"; // single left-pointing angle quotation mark
//  replaceallfast "&#8249;", "?"; // single left-pointing angle quotation mark
//  replaceallfast "&#x2039;" , "?"; // single left-pointing angle quotation mark

    gofiletop;
  endmacro;



最新の画像もっと見る

コメントを投稿