use strict;
use XML::DOM;
use Jcode;
use Image::Size;

my $image_dir;

open FH , "./imgdir.pl";
my @content_all = <FH>;
my $content = join('', 	@content_all);
close FH;

eval $content;

my @keywords;  ;# keywords

my $orgfile;
my $outfile;
my $curpara;
my $curtitle;
my $in_para;
my $a_target;
my $c_count;
my $in_bq = 0;
my $in_comment = 0;

sub sjis
{
	my($text);
	$text = $_[0];
	$text = Jcode->new($text, "utf8")->sjis;
	$text =~ s/---yen---/\\/g;
	$text =~ s/---tilde---/\~/g;
	$text =~ s/---nami---/〜/g;
	$text =~ s/---haifun---/−/g;
	$text =~ s/&/&amp;/g;
	$text =~ s/</&lt;/g;
	$text =~ s/>/&gt;/g;
	$text =~ s/\t/&nbsp;&nbsp;&nbsp;&nbsp;/g;
	$text =~ s/\"/&quot;/g;
	if($in_bq)
	{
		$text =~ s/ /&nbsp;/g;
		$text =~ s/\/\*/<span class=\"comment\">\/\*/g;
		$text =~ s/\*\//\*\/<\/span>/g;
		if($text =~ s/^(\/\/.*?)$/<span class=\"comment\">$1/s ||
			$text =~ s/([^:])(\/\/.*?)$/$1<span class=\"comment\">$2/s)
		{
			$in_comment = 1;
		}
	}
	return $text;
}

sub quit_comment
{
	if($in_comment)
	{
		$in_comment = 0;
		return "</span>";
	}
	return "";
}

sub html
{
	my($text);
	$text = $_[0];
	$text =~ s/&/&amp;/g;
	$text =~ s/</&lt;/g;
	$text =~ s/>/&gt;/g;
#	$text =~ s/ /&nbsp;/g;
	$text =~ s/\t/&nbsp;&nbsp;&nbsp;&nbsp;/g;
	$text =~ s/\"/&quot;/g;
	return $text;
}

sub sjis_noquote
{
	my($text);
	$text = $_[0];
	$text = Jcode->new($text, "utf8")->sjis;
	$text =~ s/---yen---/\\/g;
	$text =~ s/---tilde---/\~/g;
	$text =~ s/---nami---/〜/g;
	$text =~ s/---haifun---/−/g;
	return $text;
}


sub get_title
{
	my $file = $_[0];
	my $fn;
	$fn = $file . ".xml";
	if(!open(LFH, $fn))
	{
		$fn = $file . ".tag";
		if(!open(LFH, $fn))
		{
			return "";
		}
	}
	my @all = <LFH>;
	my $all = join('', @all);
	close LFH;
	$all =~ /\<title\>(.*?)\<\/title\>/;
	return $1;
}

sub rr
{
	my($node) = @_;
	my($type);

	$type = $node->getNodeType;
	if($type == ELEMENT_NODE)
	{
		my($name);
		$name = sjis($node->getNodeName);
		if($name eq "doc")
		{
			my $title = sjis(($node->getElementsByTagName("title"))[0]->getFirstChild->getData);
			$curtitle=$title;

#			my $num = $#keywords  + 1;
#			$keywords[$num] = $title . "\t" . "id$num" . "\t" . $outfile . "\t";

			print OH <<EOF;
<?xml version="1.0" encoding="Shift_JIS"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html  xmlns="http://www.w3.org/1999/xhtml" xml:lang="ja" lang="ja">
<!-- generated by to_html.pl from $orgfile -->
<head>
	<meta http-equiv="Content-Type" content="text/html; charset=Shift_JIS" />
	<title>$title</title>
	<meta name="author" content="W.Dee" />
	<meta http-equiv="Content-Style-Type" content="text/css" />
	<meta http-equiv="Content-Script-Type" content="text/javascript" />
	<link href="browser.css" type="text/css" rel="stylesheet" title="吉里吉里関連リファレンス用標準スタイル" />
	<link href="mailto:dee\@kikyou.info" rev="Made" />
	<link href="index.html" target="_top" rel="Start" title="トップページ" />
</head>
<body>
EOF
#			print OH "<a id=\"id$num\" name=\"id$num\"></a>";


			foreach my $child ($node->getChildNodes) { &rr($child); }

			print OH <<EOF;
	<script type="text/javascript" charset="Shift_JIS" src="documentid.js" ></script>
	<script type="text/javascript" charset="Shift_JIS" src="postcontent.js" ></script>
</body>
</html>
EOF

		}
		elsif($name eq "title")
		{
		}
		elsif($name eq "lc")
		{
			$c_count = 0;
		}
		elsif($name eq "l")
		{
			$c_count ++;
			print OH "<span class=\"linenumber\">";
			my $num = sprintf("%3d|", $c_count);
			$num =~ s/ /&nbsp;/g;
			print OH "$num</span>";
		}
		elsif($name eq "para")
		{
			$curpara = '';
			foreach my $child ($node->getChildNodes) { &rr($child); }
			print OH "</div></div>\n";
		}
		elsif($name eq "ptitle")
		{
			my $num = $#keywords  + 1;

			print OH "<h1>";

			print OH "<a id=\"id$num\" name=\"id$num\">";

			$in_para = 1;
			foreach my $child ($node->getChildNodes) { &rr($child); }
			$in_para = 0;

			$keywords[$num] = $curpara . "\t" . "id$num" . "\t" . $outfile . "\t" . $curtitle;


			print OH <<EOF;
</a>
</h1><div class="para"><div>
EOF
		}
		elsif($name eq "note")
		{
			print OH <<EOF;
<br /><div class="note"><div class="notehead"><span class="noteheadspan">Note</span></div>
EOF

			foreach my $child ($node->getChildNodes) { &rr($child); }

			print OH <<EOF;
</div><br />
EOF
		}
		elsif($name eq "descimg")
		{
			my $title = sjis(($node->getElementsByTagName("dititle"))[0]->getFirstChild->getData);
			print OH "<br /><div class=\"descimg\">";
			foreach my $child ($node->getChildNodes) { &rr($child); }
			print OH "<br />$title</div><br />";
		}
		elsif($name eq "kw")
		{
			my $word = $node->getFirstChild->getData;
			my $num = $#keywords  + 1;
			$keywords[$num] = sjis_noquote($word) . "\t" . "id$num" . "\t" . $outfile . "\t";
			if(!$in_para && $curtitle ne $curpara)
			{
				$keywords[$num] .= "$curtitle-$curpara";
			}
			else
			{
				$keywords[$num] .= "$curtitle";
			}
			print OH "<a id=\"id$num\" name=\"id$num\" class=\"targanchor\"><dfn>";
			foreach my $child ($node->getChildNodes) { &rr($child); }
			print OH "</dfn></a>";
		}
		elsif($name eq "img")
		{
			my $filename = $image_dir . $node->getAttribute("src");
			if(-e $filename)
			{
				my $w;
				my $h;
				($w, $h) = imgsize($filename);
				print OH "<img width=\"$w\" height=\"$h\" alt=\"". sjis($node->getAttribute("src"))."\" src=\"". sjis($node->getAttribute("src"))."\" />";
			}
			else
			{
				print OH "<img src=\"".sjis($filename)."\" alt=\"".sjis($filename)."\" />";
			}
		}
		elsif($name eq "colorbox")
		{
			my $color=sjis($node->getAttribute("color"));
			print OH "<span style=\"border:1px solid black; color:$color; background-color:$color;\">";
			print OH "■";
			print OH "</span>\n";
		}
		elsif($name eq "dt")
		{
			print OH "\n<dt>";
			foreach my $child ($node->getChildNodes) { &rr($child); }
			print OH "</dt>\n";
		}
		elsif($name eq "tt")
		{
			print OH "<code class=\"inlinecode\">";
			foreach my $child ($node->getChildNodes) { &rr($child); }
			print OH "</code>";
		}
		elsif($name eq "b")
		{
			print OH "<em>";
			foreach my $child ($node->getChildNodes) { &rr($child); }
			print OH "</em>";
		}
		elsif($name eq "i")
		{
			print OH "<span class=\"i\">";
			foreach my $child ($node->getChildNodes) { &rr($child); }
			print OH "</span>";
		}
		elsif($name eq "bq")
		{
			print OH "\n<br />\n<code class=\"bq\">";
			$in_bq = 1;
			foreach my $child ($node->getChildNodes) { &rr($child); }
			$in_bq = 0;
			print OH &quit_comment;
			print OH "</code>\n<br />\n\n";
		}
		elsif($name eq "example")
		{
			print OH "\n<br />\n<code class=\"bq\"><span class=\"weak\">例:</span><br />";
			$in_bq = 1;
			foreach my $child ($node->getChildNodes) { &rr($child); }
			$in_bq = 0;
			print OH &quit_comment;
			print OH "</code>\n<br />\n\n";
		}
		elsif($name eq "dititle")
		{
		}
		elsif($name eq "a")
		{
			print OH "<a class=\"jump\" href=\"" . sjis($node->getAttribute("href")) . "\">";
			foreach my $child ($node->getChildNodes) { &rr($child); }
			print OH "</a>";
		}
		elsif($name eq "at")
		{
			if($node->getAttribute("target") ne '')
			{
				print OH "<a target=\"" . $node->getAttribute("target") . "\" class=\"jump\" href=\"" . sjis($node->getAttribute("href")) . "\">";
			}
			else
			{
				print OH "<a target=\"$a_target\" class=\"jump\" href=\"" . sjis($node->getAttribute("href")) . "\">";
			}
			foreach my $child ($node->getChildNodes) { &rr($child); }
			print OH "</a>";
		}
		elsif($name eq "r")
		{
#			if(!$in_bq) { print OH "<br />\n"; } else { print OH "\n"; }
			print OH &quit_comment;
			print OH "<br />\n";
		}
		elsif($name eq "link")
		{
			my $href = $node->getAttribute("href");
			my $title = get_title($href);
			if($title ne '')
			{
				print OH "<a target=\"$a_target\" class=\"jump\" href=\"${href}.html\">$title</a>";
			}
		}
		elsif($name eq "comlink")
		{
			my $href = $node->getAttribute("href");
			my $title = get_title($href);
			if($title ne '')
			{
				print OH "<span class=\"comlink\"> ( → <a target=\"$a_target\" class=\"jump\" href=\"${href}.html\">$title</a> ) </span>";
			}
		}
		elsif($name eq "font")
		{
			my $color = $node->getAttribute("color");
			print OH "<font color=\"$color\">";
			foreach my $child ($node->getChildNodes) { &rr($child); }
			print OH "</font>";
		}
		elsif($name eq "anchor")
		{
			$a_target = sjis($node->getAttribute("target"));
		}
		elsif($name eq "keywords")
		{
			print OH "<div><a class=\"jump\" href=\"keywords.html\" target=\"index\">キーワード一覧へ</a></div>\n";
			print OH "<div><a class=\"jump\" href=\"keywords_p.html\" target=\"index\">場所順キーワード一覧へ</a></div><br />\n";
		}
		elsif($name eq "include")
		{
			my $file = $node->getAttribute("file");
			my $content_all;
			open IH, $file or die;
			@content_all = <IH>;
			close IH;
			print OH join("\n", @content_all);
		}
		else
		{
			print OH "<$name>";
			foreach my $child ($node->getChildNodes) { &rr($child); }
			print OH "</$name>";
		}
	}
	elsif($type == TEXT_NODE)
	{
		my $text = $node->getData;
		$text =~ s/^\n//s;
		$text =~ s/\n$//s;
		if($in_para) { $curpara .= sjis_noquote($text); }
		$text = sjis($text);
		print OH $text;
	}
	elsif($type == DOCUMENT_NODE)
	{
		foreach my $child ($node->getChildNodes) { &rr($child); }
	}
}


sub process
{
	my $fn = $_[0];
	my $of = $fn;
	$of =~ s/\.xml/\.html/;

	$orgfile = $fn;
	$outfile = $of;
	$a_target = 'main';
	open FH , $fn;
	my @content_all = <FH>;
	my $content = join('', 	@content_all);
	close FH;

	my $parser = new XML::DOM::Parser;

	$content =~ s/〜/---nami---/g;
	$content =~ s/\x81\x7c/---haifun---/g;
	$content = Jcode->new($content, "sjis")->euc;
	$content =~ s/\\/---yen---/g;
	$content =~ s/\~/---tilde---/g;
	$content = Jcode->new($content, "euc")->sjis;
	$content =~ s/Shift_JIS/x-sjis-unicode/;
	my $doc = $parser->parse($content);

	open OH, ">$of";
	&rr($doc);
}

my @list = <*.xml>;

foreach my $each (@list)
{
	print $each, "\n";
	&process($each);
}


;# keyword

if(open FH, "keys.txt")
{
	my @addt = <FH>;
	close FH;
	foreach my $each ( @addt)
	{
		chop $each;
		push @keywords, $each;
	}
}

sub writekeywords
{
	my $i;

	open OH, ">keywords.html";

	print OH <<EOF;
<?xml version="1.0" encoding="Shift_JIS"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html  xmlns="http://www.w3.org/1999/xhtml" xml:lang="ja" lang="ja">
<head>
	<meta http-equiv="Content-Type" content="text/html; charset=Shift_JIS" />
	<title>キーワード一覧</title>
	<meta name="author" content="W.Dee" />
	<meta http-equiv="Content-Style-Type" content="text/css" />
	<meta http-equiv="Content-Script-Type" content="text/javascript" />
	<link href="keywords.css" type="text/css" rel="stylesheet" title="吉里吉里関連リファレンス用キーワード一覧スタイル" />
	<link href="mailto:dee\@kikyou.info" rev="Made" />
	<link href="index.html" target="_top" rel="Start" title="トップページ" />
</head>
<!-- generated by to_html.pl -->
<body>
<div><a class="jump" href="frame.html" target="index">目次へ</a></div>
<div><a class="jump" href="keywords_p.html">場所順キーワード一覧へ</a></div>

<script type="text/javascript" src="keywordgrep.js"></script>
<form action="#" onsubmit="return false">
<input type="text" value="" size="30" id="grepword" accesskey="/" />
</form>

<table class="keywords" summary="キーワード一覧です"><tbody>
EOF

	@keywords = sort @keywords;

	for($i = 0; $i <= $#keywords; $i++)
	{
		my $word;
		my $place;
		my $name;
		my $placedesc;
		my $title;
		($word, $name, $place, $placedesc) = split(/\t/, $keywords[$i]);
		$title = html($word);
		if($placedesc ne '') { $title .= " ( $placedesc ) "; }
		print OH "<tr><td><div><a class=\"jump\" href=\"$place#$name\" target=\"main\" title=\"". ($title) . "\">" . html($word);
		if($placedesc ne '') { print OH " <span>( ". ($placedesc) ." )</span>"; }
		print OH "</a></div></td></tr>\n";
	}
	print OH "\n";
	print OH <<EOF;
	</tbody>
</table>
</body>
</html>
EOF

	close OH;

	for($i = 0; $i <= $#keywords; $i++)
	{
		my $word;
		my $place;
		my $name;
		my $placedesc;
		($word, $name, $place, $placedesc) = split(/\t/, $keywords[$i]);
		$keywords[$i] = "$place\t$placedesc\t$word\t$name";
	}


	@keywords = sort @keywords;

	open OH, ">keywords_p.html";


	print OH <<EOF;
<?xml version="1.0" encoding="Shift_JIS"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html  xmlns="http://www.w3.org/1999/xhtml" xml:lang="ja" lang="ja">
<head>
	<meta http-equiv="Content-Type" content="text/html; charset=Shift_JIS" />
	<title>場所順キーワード一覧</title>
	<meta name="author" content="W.Dee" />
	<meta http-equiv="Content-Style-Type" content="text/css" />
	<meta http-equiv="Content-Script-Type" content="text/javascript" />
	<link href="keywords.css" type="text/css" rel="stylesheet" title="吉里吉里関連リファレンス用キーワード一覧スタイル" />
	<link href="mailto:dee\@kikyou.info" rev="Made" />
	<link href="index.html" target="_top" rel="Start" title="トップページ" />
</head>
<!-- generated by to_html.pl -->
<body>
<div><a class="jump" href="frame.html" target="index">目次へ</a></div>
<div><a class="jump" href="keywords.html">キーワード一覧へ</a></div>

<script type="text/javascript" src="keywordgrep.js"></script>
<form action="#" onsubmit="return false">
<input type="text" value="" size="30" id="grepword" accesskey="/" />
</form>

<table class="keywords" summary="場所順キーワード一覧です"><tbody>
EOF

	for($i = 0; $i <= $#keywords; $i++)
	{
		my $word;
		my $place;
		my $name;
		my $placedesc;
		my $title;
		($place, $placedesc, $word, $name) = split(/\t/, $keywords[$i]);
		$title = html($word);
		if($placedesc ne '') { $title .= " ( $placedesc ) "; }
		print OH "<tr><td><div><a class=\"jump\" href=\"$place#$name\" target=\"main\" title=\"". ($title) . "\">". html($word);
		if($placedesc ne '') { print OH " <span>( ". ($placedesc) ." )</span>"; }
		print OH "</a></div></td></tr>\n";
	}

	print OH "\n";
	print OH <<EOF;
	</tbody>
</table>
</body>
</html>
EOF
}

&writekeywords;
