いつものストップモーションアニメとは関係ないエントリですが、タグリムーバーを作りました。といっても jericho html parser のキッカーです。
タグの削除ってエディタの正規表現「<[^>]*>」(こんな感じ?)での置換とかで出来るのですが、複数ファイルまとめてやると、手作業も入ってきてちょっと時間がかかりすぎます。ので、作りました。主に自分用なのですが公開したいと思います。
このタグリムーバはjavaで作られています。つまりjavaがインストールされていないと動きません。
アーカイブに起動バッチを用意しましたのでそれを使っていただければと思います。引数にhtml文書が格納されたディレクトリ(再帰的にテキストに落とします)か、html文書そのものを与えます。
なんでいまさらタグリムーバーなの?ってのもありますが、ググってみてもタグリムーバーって自分にあったものがなかなか見つからなかったからです。調べてる最中に出てきた、このjericho html parserライブラリは非常によかったです。
jspiderなんかを使って落としたhtml文書を(※クローラは時としてwebサーバに対してDos攻撃となりますので注意して使ったほうがいいんじゃないかしらと思います)テキストにしてサクサク読めます。オフラインでローカルのhtml文書を読むとき――主に英文の技術的な仕様書やTIPSなどを読むとき(や保存するとき)などに使ったりしています。
ちなみにリムーバーでディレクトリに対して再帰的に*.htmlから*.txtを作成した後、アーカイブ内の以下のバッチを実行すると*.txtを収集してallDoc.txtっていう1つのファイルにしてくれます。ソート順を適宜考えて変えてあげるなどすれば、それなりに使えるかもしれません。
1行目のdirコマンドは*.txtファイルを/sスイッチでディレクトリ内を再帰的に、/bスイッチでファイル名だけをリストしてdir.txtに落とします。
2行目の%cd%であらわしているのがunixでいうpwdです。allDoc.txtにリダイレクトしています。
3行目、forコマンドの"delims="って言うのは、半角スペースを勝手にデリミタにしちゃうのでそれを防いでいます。これを指定しないと、"C:\Documents and Settings"のパスに含まれる半角スペースがデリミタになって変数iにわたるのが"C:\Documents"になってしまうんです。
4行目、typeコマンドはcatと同じですね。allDoc.txtへ追記しています。
01:>dir /s /b *.txt > dir.txt 02:>echo [ %cd% 以下すべてのテキスト文書 ]>allDoc.txt 03:>for /f "delims=" %%i in ( dir.txt ) do ( 04:> type "%%i">>allDoc.txt 05:>) 06:>del dir.txt
javaのソースは以下になります。うわー。はずかしいけどまぁいいや。たいしたもんじゃないし。 ※アーカイブに格納されています。
import au.id.jericho.lib.html.*;
import java.util.*;
import java.io.*;
import java.net.*;
/*
http://jerichohtml.sourceforge.net/doc/index.html
download -> jericho-html-2.4.zip
jar -> jericho-html-2.4.jar
jar cmf MANIFEST.MF TagDel.jar *.class au\*
MANIFEST.MF
Main-Class: TagDel
Manifest-Version: 1.0
Created-By: 1.4.2_14 (Sun Microsystems Inc.)
*/
public class TagDel {
public static void main( String[] args ) throws Exception {
try {
// ファイルパスのパラメータを得る。なければ終了
if( args.length <= 0 ){
return;
}
// 引数を直接使っちゃう。
File filePath = new File( args[ 0 ] );
// 引数が存在しないパスの場合、終了
if( !filePath.exists() ){
System.out.println( "error - inValid filePath." );
return;
}
// インスタンスの用意
Vector allFileList = new Vector();
FileSetUtil fsuInst = new FileSetUtil();
// ディレクトリの場合は再帰したい。
if( filePath.isDirectory() ){
System.out.println( filePath.getAbsolutePath() );
fsuInst.createFileList( allFileList, filePath );
}
// ファイルは直接セット
else {
System.out.println( filePath.getAbsolutePath() );
allFileList.add( (String)filePath.getAbsolutePath() );
}
for( int idx = 0; idx < allFileList.size(); idx++ ){
// パスごとにプレーンテキストファイルを作成する。
fsuInst.createPlaneText( (String)allFileList.get( idx ) );
}
}
catch( Exception e ){
System.out.println( e.toString() );
}
}
};
class FileSetUtil {
public void createFileList( Vector allFileList, File filePath )
{
for( int idx = 0; idx < filePath.list().length; idx++ ){
File curPath = new File( filePath.getPath() + "\\" + filePath.list()[ idx ] );
boolean isDir = false;
isDir = curPath.isDirectory();
if( isDir ){
createFileList( allFileList, curPath );
}
else {
// 拡張子の切り出し。拡張子がない場合はスキップします。
String fileAtrCheckStr = curPath.getPath();
if( fileAtrCheckStr.lastIndexOf( "." ) >= 0 ){
fileAtrCheckStr = fileAtrCheckStr.substring( fileAtrCheckStr.lastIndexOf( "." ) + 1 );
fileAtrCheckStr = fileAtrCheckStr.toLowerCase();
// htmlとか(xhtml / shtml...)じゃなければリストにパスを加えない
if( fileAtrCheckStr.indexOf( "htm" ) >= 0 ){
allFileList.add( (String)curPath.getAbsolutePath() );
}
}
}
}
}
public void createPlaneText( String filePath ) throws Exception
{
try {
// 引数のfilePathは信用しちゃう。そのまま使う。
String renderFilePath = "";
renderFilePath = "file:" + filePath;
Source source = new Source( new URL( renderFilePath ) );
String renderedText = source.getRenderer().toString();
BufferedWriter bw = new BufferedWriter(
new OutputStreamWriter(
new FileOutputStream( filePath + ".txt" ), "MS932" ) );
bw.write( renderedText );
bw.close();
}
catch( Exception e ){
System.out.println( e.toString() );
}
}
};
ほいでわ。



