#!/usr/local/bin/ruby

=begin
		mkdiary.rb ѥ ΡʸHTMLե륿ס

mkdiary.rb Υᥤ⡼ɤǤϡ(ФμιԤ)ʸʬ
Ŭ˸äHTMLˤ륹ץȤǤ롣ȤСΤ
ܤץȤȤ ~/.mkdiaryrc 

	bodyfilter=body2html

Ƚ񤤤ơΥץȤưƤ mkdiary.rb Ʊǥ쥯ȥ
ԡȡᥤǤΥץȤѤ褦ˤʤ롣

ΥץȼΤλͤϰʲ̤ꡣ

H1(<h1></h1>)
		ƬȾѤ = Ⱦѥڡ
		<h1>ۤۤ</h1> 
(<p></p>)
		ƬʸϤ񤭻Ϥ᤿ʬ顢Ԥޤǡ
(<blockquote><p></p></blockquote>)
		ְľʸϤ񤭻Ϥ᤿ʬ顢Ԥޤǡ
(<p aligh="center"><font size="150%"></font></p>)
		ľʸϤ񤭻Ϥ᤿ʬ顢Ԥޤǡ
		ƹԤˤ줾<br>ɲä롣
PRE(<blockquote><pre></pre></blockquote>)
		ڡľʸϤ񤭻Ϥ᤿ʬ顢Ԥޤǡ
(<p aligh="center"><font size="150%"></font></p>)

վ(<ul></ul>)
		ְľ "* " (ꥹ+Ⱦѥڡ)
ֹդվ(<ol></ol>)
		ְľ ȾѿȾѥԥꥪ

Ƥñ̤϶ԤǶڤΤǽꤿʬˤϲԤιԤ˺
ʤ褦դ뤳!! ޤñ̤Ƭ < ǤϤޤäƤ
ϤιԤHTMLεҤȤߤʤƤΤޤɽιԤȽǤꤹ
롣


Ѵ:

ڥᥤʸ------------------------------------------------------------
m=Ѵˡ
Ƭɤɤ³Ƥʬ
HTMLǤ p ȤȤʤ롣

	1. ְ +  + ԥꥪ
	2. ְ + * + ڡ
	3. ְ + ʸ
	4.  + ʸ
	5. ڡ + 

Ȥʬ̤˽롣줾졢ֹդվ, վ, ֥å
ȡ󥿥+ǤʸȤpreȡѴ
롣ʤ̤˥󥿥󥰤 &lt;a href=...&gt; ʤɤ򼨤褦ʴʰ׵Ƴ
ͽ̵ʤʤ顢ʰ׵ˡƳȡɤɤ䤷ʤꡢ
켫ȤФΤѤˤʤä

		äHTMLФᤤ

Ȥ۰֤ˤʤäƤޤǤ롣

ѴHTMLʬ------------------------------------------------------
<p>Ƭɤɤ³Ƥʬ
HTMLǤ p ȤȤʤ롣
</p>
<ol>
 <li> ְ +  + ԥꥪ
 <li> ְ + * + ڡ
 <li> ְ + ʸ
 <li>  + ʸ
 <li> ڡ + 
</ol>
<p>Ȥʬ̤˽롣줾졢ֹդվ, վ, ֥å
ȡ󥿥+ǤʸȤpreȡѴ
롣ʤ̤˥󥿥󥰤 &lt;a href=...&gt; ʤɤ򼨤褦ʴʰ׵Ƴ
ͽ̵ʤʤ顢ʰ׵ˡƳȡɤɤ䤷ʤꡢ
켫ȤФΤѤˤʤä
</p>
<p class="shout" align="center"><font size="150%">äHTMLФᤤ
</font></p>
<p>Ȥڰ֤ˤʤäƤޤǤ롣
</p>
---------------------------------------------------------------------------

Ȥгȳڤ˥ᥤHTML񤱤Ǥ⤢ʡä
TABɤʤ󡼡㤢ڡĤTABĤ
ȤˤǤ⤷Ƥ
=end

$quotestarter	= "<blockquote>\n<p>"
$quoteender	= "</p>\n</blockquote>"
$samplestarter	= "<blockquote>\n<pre>"
$sampleender	= "</pre>\n</blockquote>"
$shoutstarter	= "<p class=\"shout\" align=\"center\"><big><big><big>"
$shoutender	= "</big></big></big></p>"
$ulpattern	= "[-*]\s"
$ulstarter	= "<ul>"
$ulender	= "</ul>"
$olpattern	= "(\\d+\.\|\\(?\\d+\\))"
$olstarter	= "<ol>"
$olender	= "</ol>"

$tab		= "(\t|  )"

in_p = false
in_qt = false
in_ul = false
in_ol = false
in_pre = false
in_samp = false
in_shout = false
ender = ''


while line=gets
  if (in_p || in_qt || in_shout || in_samp) then
    if /^$/ =~ line
      print "#{ender}\n"
      if in_p
	in_p= false
      elsif in_qt
	in_qt = false
      elsif in_shout
	in_shout = false
      elsif in_samp
	in_samp = false
      end
    else			# continues
      if in_samp then
	line.sub!(/^ /, '')	# strip one indent exactly (in <pre>)
      else
	line.sub!(/^\s*/, '')
      end
      if (in_shout) then print "<br>" end
      print line
    end
  elsif (in_ul)
    if /^#{$tab}#{$ulpattern}/ =~ line
      print " <li>" + $'
    elsif /^$/ =~ line
      in_ul = false
      print "#{ender}\n"
    else
      print line
    end
  elsif (in_ol)
    if /^#{$tab}#{$olpattern}/ =~ line
      print " <li>" + $'
    elsif /^$/ =~ line
      in_ol = false
      print "#{ender}\n"
    else
      print line
    end
  elsif (in_pre)
    print line
    if %r,</pre>$, =~ line
      in_pre = false
    end
  elsif /^</ =~ line		# lines that begin with <tag>
    print line
  elsif /^#{$tab}#{$tab}\S/ =~ line
    print "#{$shoutstarter}" + line.sub!(/^\s*/, '')
    in_shout = true; ender = $shoutender
  elsif /^#{$tab}#{$ulpattern}/ =~ line
    line = $'
    print "#{$ulstarter}\n <li>" + line
    in_ul = true; ender = $ulender
  elsif /^#{$tab}#{$olpattern}/ =~ line
    line = $'
    print "#{$olstarter}\n <li>" + line
    in_ol = true; ender = $olender
  elsif /^#{$tab}\S/ =~ line
    print "#{$quotestarter}" + line.sub!(/^\s*/, '')
    in_qt = true; ender = $quoteender
  elsif /^<pre>/i =~ line
    print line
    in_pre = true
  elsif /^ \S/ =~ line
    print "#{$samplestarter}" + line.sub!(/^\s*/, '')
    in_samp = true; ender = $sampleender
  elsif /^= (.*)$/ =~ line
    print "<h1>" + $1 + "</h1>\n"
  elsif /^\S/ =~ line
    print "<p>" + line
    in_p = true; ender = "</p>";
  end
end

if (in_p)
  print "</p>\n"
elsif (in_qt)
  print "#{$quoteender}\n"
elsif (in_shout)
  print "#{$shoutender}\n"
elsif (in_samp)
  print "#{$sampleender}\n"
elsif (in_ol)
  print "#{$olender}\n"
elsif (in_ul)
  print "#{$ulender}\n"
elsif (in_shout)
  print "#{$shoutender}\n"
end
