######################################################################
#
#  title: rd2html  Make file for personal use
#  Author: Youhei SASAKI<uwabami@ep.sci.hokudai.ac.jp>
#  Date: $Lastupdate: 2006/10/10 16:46:35 $
#
######################################################################
## 設定
RUBY    = /usr/bin/ruby
# css の置き場所
#CSS     = "http://www.gfd-dennou.org/arch/gfdsemi/html/gfdsemi.css"
# 日本語英語セパレータ
######################################################################
SEP-JA  = $(RUBY) -e 'inbody = false ;\
		      lines = readlines ;\
		      puts "=begin head" ;\
                      lines.each {|line| ;\
	  inbody = true  if /^=begin\s+head\s*$$/ =~ line ;\
	  inbody = false if /^=end\s*$$/ =~ line ;\
	  next if line =~ /^=(begin|end)\s*$$/ ;\
	  next if line =~ /^=(begin\s+|end\s+)\w+\s*$$/ ;\
	  if inbody ;\
	    puts line ;\
	  end ;\
	} ;\
	puts "=end" ;\
	puts "=begin" ;\
	lines.each {|line| ;\
	  inbody = true  if /^=begin\s*$$/ =~ line ;\
	  inbody = true  if /^=begin\s+JA\s*$$/ =~ line ;\
	  inbody = false if /^=end\s*$$/ =~ line ;\
	  inbody = false if /^=end\s+JA\s*$$/ =~ line ;\
	  next if line =~ /^=(begin|end)\s*$$/ ;\
	  next if line =~ /^=(begin\s+|end\s+)\w+\s*$$/ ;\
	  if inbody ;\
	    puts line ;\
	  end ;\
	} ;\
	puts "=end" ;\
	'
######################################################################
SEP-EN  = $(RUBY) -e 'inbody = false ;\
		      lines = readlines ;\
		      puts "=begin head" ;\
                      lines.each {|line| ;\
	  inbody = true  if /^=begin\s+head\s*$$/ =~ line ;\
	  inbody = false if /^=end\s*$$/ =~ line ;\
	  next if line =~ /^=(begin|end)\s*$$/ ;\
	  next if line =~ /^=(begin\s+|end\s+)\w+\s*$$/ ;\
	  if inbody ;\
	    puts line ;\
	  end ;\
	} ;\
	puts "=end" ;\
	puts "=begin" ;\
	lines.each {|line| ;\
	  inbody = true  if /^=begin\s*$$/ =~ line ;\
	  inbody = true  if /^=begin\s+EN\s*$$/ =~ line ;\
	  inbody = false if /^=end\s*$$/ =~ line ;\
	  inbody = false if /^=end\s+EN\s*$$/ =~ line ;\
	  next if line =~ /^=(begin|end)\s*$$/ ;\
	  next if line =~ /^=(begin\s+|end\s+)\w+\s*$$/ ;\
	  if inbody ;\
	    puts line ;\
	  end ;\
	} ;\
	puts "=end" ;\
	'
######################################################################
H1ToTitle  = $(RUBY) -e 'body = open(ARGV[0]){|io| io.read}; \
			 if /\<h1.*?\>(.*?)\<\/h1.*?\>/ =~ body then      ;\
			   h1 = $$1 ;\
			   h1 = h1.chomp.gsub!(/\<.*?\>/){|s| s = ""} ;\
			   mod = body.gsub(/\<title\>.*<\/title\>/,         \
			         "\<title\>" + h1 + "<\/title\>") ;\
			 else ;\
			   mod = body ;\
			 end ;\
			 \
			 if /(\<title\>.*?\<\/title\>)/im =~ mod then ;\
			   title = $$1 ;\
			   title_reg = Regexp.new(title.gsub(/\(/, "\\(").gsub(/\)/, "\\)"), 5) ;\
			   if /(\<meta\s+http-equiv=\"content-type\"\s+content=\".*?\"\s+\/\>)/im =~ mod then ;\
			     meta = $$1 ;\
			     meta_reg = Regexp.new(meta.gsub(/\(/, "\\(").gsub(/\)/, "\\)"), 5) ;\
			     mod.sub!(meta_reg, title) ;\
			     mod.sub!(title_reg, meta) ;\
			   end ;\
			 end ;\
			 \
			 out = open(ARGV[0], "w"); out.puts(mod)         ;\
			 out.close'
######################################################################
# General command
RD2 = rd2 -r rd/rd2html-ext-lib \
	 --native-inline --ref-extension \
	 --head-element --out-code=euc --html-charset=EUC-JP \
#	 --with-css=$(CSS)

######################################################################
## Search "rd" files
__AllRDFiles__ = $(wildcard *.rd)
RDFiles = $(__AllRDFiles__:%.v.rd=)
GenerateFiles = $(HtmByJA) $(HtmByEN)
HtmByJA = $(RDFiles:.rd=.htm.ja)
HtmByEN = $(RDFiles:.rd=.htm.en)

##########################################################################
## 本体

all: $(GenerateFiles)

%.htm.ja: %.rd
	    $(SEP-JA) < $<  > $<.tmp ;\
	    $(RD2) $<.tmp > $@;\
            $(H1ToTitle) $@; \
	    rm -f $<.tmp ;\


%.htm.en: %.rd
	    $(SEP-EN) < $<  > $<.tmp ;\
	    $(RD2) $<.tmp > $@ ;\
            $(H1ToTitle) $@; \
	    rm -f $<.tmp ;\

