#!/usr/bin/env perl
eval 'exec /usr/bin/perl -S $0 ${1+"$@"}'
        if $running_under_some_shell;
use Time::Local;

$BASE="on-o.com/page/diary";

sub ptori() {
    $_ =~ s/<p>//g;
    $_ =~ s!</p>!!g;
}
sub crlftori() {
    $_ =~ s/[\n\r]//g;
}
sub headcheck() {
    $_ =~ s/^</ </;
}
sub cuttitle() {
    $_ =~ s@^<a href=.+</span></a> *@@;
    $_ =~ s/\[<a href=[^>]+( class="vt-p"){0,1}>/\[/g;
    $_ =~ s@</a>\] *@\]@g;
    $_ =~ s/ class="vt-p"//g;
}
sub body {
    while (<>) {
	$_ =~ s/\r//g;
	if ( $_ =~ /^<h3>/ ) {
	    #タイトル行の処理
	    $_ =~ s/^<h3>//;
	    $_ =~ s@</h3>$@@;
	    $URL = $_;
	    cuttitle();
	    $URL =~ s@^<a href="((http|https)://$BASE|\.)/([0-9]+)\.html(#p[0-9]+)".+$@\3\4@;
	    $DATE = $URL;
	    $DATE =~ s/#p[0-9]+//g;
	    if ( $LASTDATE ne $DATE ){
		$LASTDATE = $DATE;
		if ( $DATE =~ /^([0-9]{4})([0-9]{2})([0-9]{2})/ ) {
		    $year=$1; $mon=$2; $mday=$3;
		    $time = timelocal(0, 0, 0, $mday, $mon - 1, $year);	    
		} else {
		    $time = time(); #今の時間
		}
		$TITLE = $_;
		$TITLE =~ s/\[.+\]//g;
		$TITLE =~ s/<a href=".+">//gi;
		$TITLE =~ s!</a>!!gi;
		print "Date: $DATE";
		print "Title: $TITLE";
		print "Last-Modified: $time\n";
		print "Visible: true\n";
		print "Format: tDiary\n\n";
	    }else{
		print;
	    }
	    print $_;
	}elsif ( $_ =~ /^<p>/ || $_ =~ /<\/p>$/ ) {
	    #本文
	    ptori();
            $_ =~ s!<code></code>!!g;
	    #bq読み替え
	    if ( $_ =~ /<blockquote (cite|title)=/ ){
		print " <%=bq '";
		while(<>) {
		    if ( $_ =~ /^<\/blockquote>/ ) { last; }
		    ptori();
		    headcheck();
		    print $_;
		}
		print "', '";
		$_ = <>;
		crlftori();
		if ( $_ =~ s!<p class="source">\[<cite><a href="(.+)" title=.+( class="vt-p"){0,1}>(.+)</a></cite>より引用]</p>!\2! ) {
		    $URL = $1;
		    print $_."', '".$URL."' %>\n";
		}else{
		    $_ =~ s!<p class="source">\[<cite>(.+)</cite>より引用]</p>!\1!;
		    print $_."' %>\n";
		}

		$_ = <>;
		if (/^(<p>){0,1}</p>$/) {
		    next;
		}
	    }
	    #my読み替え
	    $_ =~ s!<a href="(http|https)://$BASE/([0-9]{8}).html(#[pc][0-9]{2}){0,1}"( title=".+" ){0,1}( class="vt-p"){0,1}>([^<]+)</a>!<%=my '\2\3', '\6' %>!g;
	    #image読み替え
	    #<%=image 0, '画像の説明', nil, [235,142]%>
	    $_ =~ s/<img class="photo" src=".+\/[0-9]{8}_([0-9])\.(png|jpg)" alt="(.+)" title=".+" width="([0-9]+)" height="([0-9]+)">/<%=image \1, '\3', nil, [\4,\5] %>/g;
	    $_ =~ s/<img class="photo" src=".+\/[0-9]{8}_([0-9])\.(png|jpg)" alt="(.+)" title=".+">/<%=image \1, '\3' %>/g;
	    #prefblog読み替え
	    $_ =~ s!<img src="./.+_files/page_watch.aspx" width="1" height="1">!<IMG SRC="http://www.pressblog.jp/watch/page_watch.aspx?watchid=FFFFFFFFFFEFFCFAEFF97F7F8" width="1" height="1">!;
	    #isbn アマゾン読み替え
            $bef = $_;
	    $_ =~ s!<a href="http://www.amazon.co.jp/[^/]+/dp/([0-9A-Z]+)%.+(alt=""){0,1}>!<%=isbn_image "\1" %>!g;
	    if ( $_ ne $bef ) {
		crlftori();
		headcheck();
		print $_;
		$_ = <>;
		if ( /^height="[0-9]+" width="[0-9]+" alt="">$/ ) {
		    $_ = <>;
		}
		$_ =~ s!^.+</a>!!;
		$_ =~ s!</p>$!!;
	    }
	    headcheck();
	    print $_;
	}elsif ( $_ =~ /^<\/div>/ ) {
	    if ( $_ = /<div class="section">$/ ) {#第二パラグラフあり
		while (<>) {
		    if ( $_ !~ /^$/ ) { last; }
		}
		print "\n";
		#タイトル行の処理
		$_ =~ s/^<h3>//;
		$_ =~ s@</h3>$@@;
		$_ =~ s/^<p>//;
		$_ =~ s@</p>$@@;
		cuttitle();
		print $_;
	    }else{
		last;
	    }
	}elsif ( $_ =~ /^<div class="sequel">つづき:/ ) {
            #後日談のリンクは無視
	    next;
	}elsif ( $_ =~ /^$/ ) {
	    #NOP
	}else {
	    print EEEEEEE.$_;
	}
    }
    print "\n.\n";
}


print "TDIARY2.01.00\n";
while (<>) {
    if ( $_ =~ /^<div class="body">/ ) {
	while (<>) {
	    if  ( $_ =~ /<div class="section">/ ) {
		body();
	    }
	}
    }
}
