--- akiary061.cgi	2005-04-28 18:11:11.450040000 +0900
+++ akiary.cgi	2005-05-15 08:02:01.786945600 +0900
@@ -18,6 +18,8 @@
   {
    user_cfg_file=>{''=>'cfg/user.cfg'},
    time_zone=>'+0900',
+   newline => '<br>',
+   charset => 'SHIFT_JIS',
   };
 &parse_cfg_file($akiary_cfg_file,$acfg);
 
@@ -259,9 +261,12 @@
     $DATE{$dids[0]}=$date;
     $TITLE{$dids[0]}=$FORM{'title'};
     $BODY{$dids[0]}=$FORM{'body'};
-    $BODY{$dids[0]}=~s/\x0D\x0A/<br>/g; # ‰ü�sƒR�[ƒh•ÏŠ·(Win)
-    $BODY{$dids[0]}=~s/\x0D/<br>/g;     # ‰ü�sƒR�[ƒh•ÏŠ·(Mac)
-    $BODY{$dids[0]}=~s/\x0A/<br>/g;     # ‰ü�sƒR�[ƒh•ÏŠ·(UNIX)
+    $BODY{$dids[0]}=~s/\x0D\x0A/\n/g; # ‰ü�sƒR�[ƒh•ÏŠ·(Win)
+    $BODY{$dids[0]}=~s/\x0D/\n/g;     # ‰ü�sƒR�[ƒh•ÏŠ·(Mac)
+    $BODY{$dids[0]}=~s/\x0A/\n/g;     # ‰ü�sƒR�[ƒh•ÏŠ·(UNIX)
+    $newline = $acfg->{newline};
+    $newline =~ s/\\n/\n/g;
+    $BODY{$dids[0]} =~ s/\n/$newline/g;
 
     $file=&date2dfn($date);
 
@@ -291,9 +296,12 @@
     $DATE{$dids[0]}=$date;
     $TITLE{$dids[0]}=$FORM{'title'};
     $BODY{$dids[0]}=$FORM{'body'};
-    $BODY{$dids[0]}=~s/\x0D\x0A/<br>/g; # ‰ü�sƒR�[ƒh•ÏŠ·(Win)
-    $BODY{$dids[0]}=~s/\x0D/<br>/g;     # ‰ü�sƒR�[ƒh•ÏŠ·(Mac)
-    $BODY{$dids[0]}=~s/\x0A/<br>/g;     # ‰ü�sƒR�[ƒh•ÏŠ·(UNIX)
+    $BODY{$dids[0]}=~s/\x0D\x0A/\n/g; # ‰ü�sƒR�[ƒh•ÏŠ·(Win)
+    $BODY{$dids[0]}=~s/\x0D/\n/g;     # ‰ü�sƒR�[ƒh•ÏŠ·(Mac)
+    $BODY{$dids[0]}=~s/\x0A/\n/g;     # ‰ü�sƒR�[ƒh•ÏŠ·(UNIX)
+    $newline = $acfg->{newline};
+    $newline =~ s/\\n/\n/g;
+    $BODY{$dids[0]} =~ s/\n/$newline/g;
 
     $file=&date2dfn($date);
 
@@ -459,7 +467,7 @@
     $t=~s/<TMPL_IF NAME="(\w+)">(.*?)<\/TMPL_IF>/($p->{$1})?$2:''/egs;
 
     # <TMPL_VAR NAME="name">
-    $t=~s/<TMPL_VAR NAME="(\w+)">/$p->{$1}/eg;
+    $t=~s/<TMPL_VAR NAME="(\w+)">/$p->{$1}/g;
     return($t);
 }
 
@@ -640,8 +648,10 @@
     ($date,$title,$body)
 	=($date{$FORM{'did'}},$title{$FORM{'did'}},$body{$FORM{'did'}});
     ($syear,$smon,$smday)=($date=~/^(\d{4})(\d{2})(\d{2})/);
-    $body=~tr/\x0D\x0A//d; # ‰ü�sƒR�[ƒh�í�œ
-    $body=~s/<br>/\n/ig;
+	if ($acfg->{newline} eq '<br>') {
+	    $body=~tr/\x0D\x0A//d; # ‰ü�sƒR�[ƒh�í�œ
+	    $body=~s/<br>/\n/ig;
+	}
 
     # ƒy�[ƒW•\Ž¦
     $html=&HtmlHeader("�C�³");
@@ -779,7 +789,7 @@
 		   'akiary.cgi‚ÅƒfƒtƒHƒ‹ƒg’l‚ð�Ý’è‚µ‚Ä‰º‚³‚¢�B');
 	}
 	$v =~ s/"//g;
-	$v =~ s/\$ENV\{'(\w+)'\}/$ENV{$1}/eg;
+	$v =~ s/\$ENV\{'(\w+)'\}/$ENV{$1}/g;
 
 	if ($ke =~ /\{"(\w*)"\}\{"(\w*)"\}/) {
 	    ${ $cfg->{$k} }{$1}{$2}=$v;
@@ -850,9 +860,7 @@
 # Print HTTP
 #
 sub PrintHTTP{
-    print "Content-Type: text/html\n";
-    print "\n";
-    print $_[0];
+    print "Content-Type:text/html;charset=", $acfg->{charset}, "\n\n", $_[0];
 }
 
 #
@@ -962,7 +970,7 @@
 #    ƒfƒtƒHƒ‹ƒg‚Í"%Y0”N%M0ŒŽ%D0“ú(%W0)"
 #
 sub date2sdate{
-    local($date,$format)=@_;
+    local($date,$format,$no_tag)=@_;
     local($y0,$m0,$d0,$w);
     local(@m2,@m3,@w0,@w1,@w2);
     local($color,$class,$sdate);
@@ -997,28 +1005,29 @@
     $sdate=~s/%Y2/$y0-1988/eg;                # •½�¬Œ³�†
     $sdate=~s/%M0/sprintf("%d",$m0)/eg;   # ŒŽ
     $sdate=~s/%M1/sprintf("%02d",$m0)/eg; # ŒŽ(0•t‚«)
-    $sdate=~s/%M2/$m2[$m0-1]/eg;          # ŒŽ(‰pŒê’Z�k)
-    $sdate=~s/%M3/$m3[$m0-1]/eg;          # ŒŽ(‰pŒê)
+    $sdate=~s/%M2/$m2[$m0-1]/g;          # ŒŽ(‰pŒê’Z�k)
+    $sdate=~s/%M3/$m3[$m0-1]/g;          # ŒŽ(‰pŒê)
     $sdate=~s/%D0/sprintf("%d",$d0)/eg;   # “ú
     $sdate=~s/%D1/sprintf("%02d",$d0)/eg; # “ú(0•t‚«)
-    $sdate=~s/%W0/$w0[$w]/eg; # —j“ú
-    $sdate=~s/%W1/$w1[$w]/eg; # —j“ú(‰pŒê’Z�k)
-    $sdate=~s/%W2/$w2[$w]/eg; # —j“ú(‰pŒê)
+    $sdate=~s/%W0/$w0[$w]/g; # —j“ú
+    $sdate=~s/%W1/$w1[$w]/g; # —j“ú(‰pŒê’Z�k)
+    $sdate=~s/%W2/$w2[$w]/g; # —j“ú(‰pŒê)
+
+    if ($no_tag) {
 
-    if ($CFG{'date_color_tag'} eq 'font'){
+    } elsif ($CFG{'date_color_tag'} eq 'font') {
 	$color="#000000";              # •½“ú‚Ì�F
 	$color="#DD0000" if ($w == 0); # “ú—j“ú‚Ì�F
 	$color="#3333FF" if ($w == 6); # “y—j“ú‚Ì�F
 	$sdate="<font color=\"$color\">$sdate</font>";
-    } else {
+    } elsif ($CFG{'date_color_tag'} eq 'span') {
 	$class="weekday";               # •½“ú‚Ì�F
 	$class="sunday"   if ($w == 0); # “ú—j“ú‚Ì�F
 	$class="saturday" if ($w == 6); # “y—j“ú‚Ì�F
 	$sdate="<span class=\"$class\">$sdate</span>";
     }
-
     # ƒŠƒ^�[ƒ“
-    return("$sdate");
+    return $sdate;
 }
 
 #
@@ -1402,16 +1411,28 @@
 
 	# body•”�ì�¬
 	foreach(@k){
-	    print FH "<a name=\"$bndate{$_}\"></a>\n";
+	    print FH qq(<a name="$bndate{$_}"></a><a name="$_"></a>);
 	    print FH "<!--akiary_diary did=$_ date=$bndate{$_}-->";
 	    $tmp=$tmpbnbody;
 	    # “ú•t
 	    $dates=&date2sdate($bndate{$_});
-	    $tmp=~s/<!--\s*akiary_date\s*-->/$dates/;
+	    $w3cdtf=&date2sdate($bndate{$_}, '%Y0-%M1-%D1', 1);
+	    $tmp=~s/<!--\s*akiary_date\s*-->/$dates/g;
+	    $tmp=~s/<!--\s*akiary_w3cdtf\s*-->/$w3cdtf/g;
 	    # ƒ^ƒCƒgƒ‹
 	    $tmp=~s/<!--\s*akiary_title\s*-->/<!--akiary_title-->$bntitle{$_}<!--\/akiary_title-->/;
+	    $tmp=~s/<!--\s*akiary_title_2\s*-->/$bntitle{$_}/g;
 	    # “ú‹L–{•¶
 	    $tmp=~s/<!--\s*akiary_body\s*-->/<!--akiary_body-->$bnbody{$_}<!--\/akiary_body-->/;
+	    # �¶did
+	    $tmp =~ s/<!--\s*akiary_did\s*-->/$_/g;
+	    # Permalink
+		$bn_file = $bndate{$_};
+		$bn_file =~ s/^(\d{6}).*/$1.html/;
+		$base_uri = diary_dir_uri();
+		$permalink = $base_uri . $bn_file . '#' . $_;
+	    $tmp =~ s/<!--\s*akiary_link\s*-->/$permalink/g;
+
 	    print FH "$tmp";
 	    print FH "<!--/akiary_diary-->\n";
 	}
@@ -1595,8 +1616,8 @@
 	    $tmp =~ s/%Y2/$y-1988/eg;                # Œ³�†
 	    $tmp =~ s/%M0/sprintf("%d",$m)/eg;   # ŒŽ
 	    $tmp =~ s/%M1/sprintf("%02d",$m)/eg; # ŒŽ(0•t‚«)
-	    $tmp =~ s/%M2/$m2[$m-1]/eg;          # ŒŽ(‰pŒê’Z�k)
-	    $tmp =~ s/%M3/$m3[$m-1]/eg;          # ŒŽ(‰pŒê)
+	    $tmp =~ s/%M2/$m2[$m-1]/g;          # ŒŽ(‰pŒê’Z�k)
+	    $tmp =~ s/%M3/$m3[$m-1]/g;          # ŒŽ(‰pŒê)
 	    if (defined($index{$y})){
 		$index{$y} .= $CFG{'index_format_between_months'};
 	    }
@@ -1685,15 +1706,27 @@
     @k=splice(@k,-$latest_times,$latest_times);
     @k=reverse(@k) if ($reverse==1);
     foreach(@k){
-	$ret.="<a name=\"$date{$_}\"></a>\n";
+	$ret .= qq(<a name="$date{$_}"></a><a name="$_"></a>);
 	$tmp=$match;
 	# “ú•t
 	$dates=&date2sdate($date{$_});
+	$w3cdtf=&date2sdate($date{$_}, '%Y0-%M1-%D1', 1);
 	$tmp=~s/<!--\s*akiary_date\s*-->/$dates/g;
+	$tmp=~s/<!--\s*akiary_w3cdtf\s*-->/$w3cdtf/g;
 	# ƒ^ƒCƒgƒ‹
-	$tmp=~s/<!--\s*akiary_title\s*-->/$title{$_}/g;
+	$tmp=~s/<!--\s*akiary_title\s*-->/$title{$_}/;
+	$tmp=~s/<!--\s*akiary_title_2\s*-->/$title{$_}/g;
 	# “ú‹L–{•¶
-	$tmp=~s/<!--\s*akiary_body\s*-->/$body{$_}/g;
+	$tmp=~s/<!--\s*akiary_body\s*-->/$body{$_}/;
+	# �¶did
+	$tmp =~ s/<!--\s*akiary_did\s*-->/$_/g;
+	# Permalink
+	$bn_file = $date{$_};
+	$bn_file =~ s/^(\d{6}).*/$1.html/;
+	$base_uri = diary_dir_uri();
+	$permalink = $base_uri . $bn_file . '#' . $_;
+	$tmp =~ s/<!--\s*akiary_link\s*-->/$permalink/g;
+
 	$ret.=$tmp;
     }
 
@@ -1775,3 +1808,9 @@
     }
     return("$y${m}01.html");
 }
+
+sub diary_dir_uri {
+	my $uri = $ENV{'SCRIPT_NAME'};
+	$uri =~ s!^(.*?)(?:[\./]+?)(?:\w+\.cgi|/)$!$1!;
+	return 'http://' . $ENV{'HTTP_HOST'} . $uri . '/' . (($dcfg->{'diary_dir'} eq './') ? '' : $dcfg->{'diary_dir'});
+}
