[[FrontPage]]

 #!/usr/bin/perl -w
 #Usage: cat input.tex | perl l2x.pl > output.pkwk
 
 &print_html_header ();
 
 %env_trans = (
     "center"      => "center",
     "description" => "dl",
     "enumerate"   => "ol",
     "itemize"     => "ul",
     "quote"       => "blockquote",
     "quotation"   => "blockquote",
     "verbatim"    => "pre",
     "shadebox"    => "quote",####
     "hinto"       => "quote",####
     "keikoku"     => "quote",####
     "chui"        => "quote",####
     "tabular"     => "center",###
     "wrapfigure"  => "center",###
     "figure"      => "quote",####
     "small"       => "font",
     "gaiyo"       => "",###
 );
 
 @env_name = ();
 $now_list_item = "\-";
 $base_url = 'http://10.24.195.42/pkwk1/funnist/image';
 $tabular_mode = 0;
 
 while (<>){ 
     if (m/\\begin\{document\}/){
        $cur_env_name = "document";
        push (@env_name, $cur_env_name);
        last;
     }
     
     if (m/\\author\{(.+?)\}/) {$doc_author = $1;}
     if (m/\\title\{(.+?)\}/) {$doc_title = $1;} 
     if (m/\\subtitle\{(.+?)\}/) {$doc_subtitle = $1;}
     if (m/\\date\{(.+?)\}/) {$doc_date = $1;}
 
 }
 
 print "<pre>\n";
 
 while ($line = <>){
     next if ($line =~ m/^\%/);
 
     if ($line =~ m/\\end\{document\}/){
        if ($cur_env_name ne "document"){
            print "Error: environment between $coll_env_name and document env.\n";
        }
        last;
     }
     
     if ($line =~ m/\\end\{$cur_env_name\}/){ 
        if ($cur_env_name eq "tabular"){
            $tabular_mode = 0;
        } elsif ($cur_env_name eq "figure"){
            print "\n\n";
        }
        #print "<" . $env_trans{$cur_env_name} . ">\n";
        #print "this is end of $cur_env_name\n";
        $coll_env_name = pop (@env_name);
        if ($coll_env_name ne $cur_env_name){
            print "Error: environment between $coll_env_name and $cur_env_name.\n";
        }
        $cur_env_name = pop (@env_name);
        push (@env_name, $cur_env_name);
 
        next; last;
     }
     
     if ($line =~ m/\\begin\{(.+?)\}/){
        $cur_env_name = $1;
        push (@env_name, $cur_env_name);
        if ($cur_env_name eq "gaiyo"){
            print "\*\* 概要\n";
        } elsif ($cur_env_name eq "itemize"){
            $now_list_item = "\-";
        } elsif ($cur_env_name eq "enumerate"){
            $now_list_item = "\+";
        } elsif ($cur_env_name eq "tabular"){
            $tabular_mode = 1;
        } elsif ($cur_env_name eq "figure"){
            print "\n\n";
        }
        #print "<\/" . $env_trans{$cur_env_name} . ">\n";
        #print "this is begin of $cur_env_name\n";
        next;
     }
     
     if ($line =~ m/\\maketitle/){
        &print_maketitle ();
        next;
     }
     
     # normalization
     $line =~ s/^ +//;
     
     # tabular mode
     if ($tabular_mode){
        $line = "\| $line";
        $line =~ s/\&/\|/g;
        $line =~ s/\\\\/\|/;
     }
 
     # LaTeX commands
     $line =~ s/\\LaTeX/LaTeX/g;
     $line =~ s/\\tableofcontents/\#contents/;
     $line =~ s/\\..family//g;
     $line =~ s/\\text..\{(.+?)\}/$1/g;
     $line =~ s/\\omidasi\{(.+?)\}/\*$1/g;
     $line =~ s/~/ /g;
     $line =~ s/\\ / /g;
     $line =~ s/^\}//;
     $line =~ s/\%$//;
     $line =~ s/\\\%/\%/g;
     $line =~ s/\\\#/\#/g;
     $line =~ s/\\\\/\&br;/;
     $line =~ s/\\par(.*)/\n\n/;
     $line =~ s/\\vfill//g;
     $line =~ s/\\hfil//g;
     $line =~ s/\\hfill//g;
     $line =~ s/\\hss//g;
     $line =~ s/\\hbox to .+//;
     $line =~ s/\\ldots/\.\.\./g;
     $line =~ s/\\index\{.+?\}//;
     $line =~ s/\\caption\{(.+?)\}//;
     $line =~ s/\\raggedleft/LEFT:/;
     $line =~ s/\\raggedright/RIGHT:/;
     $line =~ s/\\centering/CENTER:/;
     $line =~ s/\\newcommand.*//;
     $line =~ s/\\fakewrap//g;
     $line =~ s/\\newpage//;
 
     # original commands
     $line =~ s/\\image\[.+?\]\{img\/(.+?)\}/\&ref($base_url\/$1,nolink);/;
     #$line =~ s/\\&/\&amp;/g;
     $line =~ s/\\url\{(.+?)\}/$1/g;
     if ($line =~ m/\\key/){
        $line =~ s/\\key\{(.+?)\}/ \&color(black,\#c0c0c0)\{$1\}; /g; 
     }
     $line =~ s/\\\w+lab\{(.+?)\}/\&aname($1);/g;
     $line =~ s/\\\w+ref\{(.+?)\}/\[\[\#$1\]\]/g;
     $line =~ s/\\saku\{(.+?)\}/$1/g;
     $line =~ s/\\goku\{(.+?)\}/\'\'$1\'\'/g;
     $line =~ s/\\daiji\{(.+?)\}/\'\'$1\'\'/g;
     $line =~ s/\\prog\{(.+?)\}/$1/g;
     $line =~ s/\\toolbar\{(.+?)\}/\[\&color(black,\#c0c0c0)\{$1\};\]/g;
     $line =~ s/\\dir\{(.+?)\}/$1/g;
     $line =~ s/\\file\{(.+?)\}/$1/;
     $line =~ s/\\windir\{(.+?)\}/$1/g;
     $line =~ s/\\str\{(.+?)\}/$1/g;
     $line =~ s/\\macnext/>>/g;
     $line =~ s/\\fighskip/&nbsp;/;
     
     # 文字参照
     $line =~ s/\\yen /\&yen;/g;
     $line =~ s/\\yen/\&yen;/g;
     $line =~ s/\\vs /\&nbsp;/g;
     $line =~ s/\\vs/\&nbsp;/g;
     $line =~ s/\\bs/\\/g;
     $line =~ s/\\texttrademark/\&trade;/;
     $line =~ s/\\textregistered/\&reg;/;
     $line =~ s/\\textcopyright/\&copy;/;
     $line =~ s/\\return/ \&color(black,\#c0c0c0)\{\&crarr;\}; /;
     $line =~ s/\\va\{(.+?)\}/\&lang;$1\&rang;/g;
     # いらね
     $line =~ s/\\vaki//g;
     $line =~ s/\\printindex//;
     $line =~ s/\\makeokuduke//;    
     # headlines
     $line =~ s/\\section\{(.+?)\}/\*$1/g;
     $line =~ s/\\subsection\{(.+?)\}/\*\*$1/g;
     $line =~ s/\\subsubsection\{(.+?)\}/\*\*\*$1/g;
     $line =~ s/\\section\*\{(.+?)\}/\*$1/g;
     $line =~ s/\\subsection\*\{(.+?)\}/\*\*$1/g;
     $line =~ s/\\subsubsection\*\{(.+?)\}/\*\*\*$1/g;
 
     # list 
     $line =~ s/\\item/$now_list_item/;
 
     $line =~ s/&/&amp;/gi; 
     $line =~ s/\"/&quot;/gi; 
     $line =~ s/</&lt;/gi; 
     $line =~ s/>/&gt;/gi; 
 
     print $line;
 }
 
 &print_footer ();#
 
 &print_html_footer ();
 
 exit;
 
 sub print_maketitle {
     print "\*$doc_subtitle\n";
 }
 
 sub print_html_header {
     print "<html>\n<head>\n<title>なんとか</title>\n</head>\n<body>\n";
 }
 
 sub print_html_footer {
     print "</pre>\n";
     print "</body>\n</html>\n";
 }
 
 sub print_footer { 
     print "\&br;\n\&br;\n";
     print "RIGHT:$doc_titleー$doc_subtitleー\n";
     print "RIGHT:執 筆 者:$doc_author\n";
     print "RIGHT:最終更新:$doc_date\n";
     print "\n\n";
 }