=begin
= rd2html-ext-tex.rb

Extention rdtool library for embedded latex source,
created by S. Takehiro, 2005/10/10. 

This library is produced from default dot file for "rd2" 
to add analysis of embedded latex source. 
To use this function, invoke rd2 command with '--with-part=LATEX:latex'.

You can change the default values of the parameter defined in this module
by embedding ruby statement  whose beginning starts with "#" in the LATEX part.
For example, you can change the size of the image of LaTeX part by 
re-setting "pstoimgscale" variable as follows.

    =begin LATEX
    #pstoimgscale=1.5
      \begin{displaymath}   
        \DP{\zeta}{t} = - \zeta\DP{\zeta}{x} - \DP[3]{\zeta}{x}. 
      \end{displaymath}
    =end LATEX


=== Example

  % rd2 -I./ -r rd2html-ext-tex --with-part=LATEX:latex hoge.rd hoge.htm

=== Necessary external commands

* rd/rd2html-ext-lib.rb
* latex 
* dvips 
* pstoimg associated with latex2html

=== Configuration

(1) Check latexcommand, dvips, pstoimg and their options. You may need 
    try and error to find an appropriate value of "pstoimgscale".
(2) Edit "latexheader" and "latextailer" variables

=end

require "rd/rd2html-ext-lib"

# define relation between types of part and Filter to set $RD["filter"]
# RD::INCLUDE_FILTER is used if type doesn't set.
#   $RC["filter"]["((|type|))"] = ((|Filter|))

$RC["filter"]["include"] = RD::INCLUDE_FILTER
$RC["filter"]["rd"] = RD::RD_FILTER
$RC["filter"]["eval"] = RD::EVAL_FILTER

module RD
#
# define LATEX FILTER
#
   LATEX_FILTER = Filter.new(:target) do |inn,out|
#
# set PNG filename
#
   sourcefile=ARGF.filename
   count=1
   pngfile = sourcefile.sub(/\.rd$/,'') + "_" + count.to_s + ".png"
   while(File.exists?(pngfile))
     count += 1
     pngfile = sourcefile.sub(/\.rd$/,'') + "_" + count.to_s + ".png"
   end
#
#  LATEX command and its relatives
#
   latex="platex"
   latexfile="rd2.tex"

   latexheader= <<-'EOF'
\documentclass[a4j,12pt]{jarticle}
\usepackage{D6math}
\pagestyle{empty}
\begin{document}
   EOF

   latextailer= <<-'EOF'
\end{document}
   EOF
#
#  DVIPS command and its options
#
   dvips="dvips"
   dvipsoption="-E"
   dvipsin=latexfile.sub(/\.tex$/,'.dvi')
   dvipsout=pngfile.sub(/\.png$/,'.ps')
#
# PSTOIMG command and its options
#
   pstoimg="pstoimg"
   pstoimgoption="-antialias -transparent"
   pstoimgscale="1.8"
#
# HTML parameters
#
   imgalign="bottom"
   centering=true
#
# create a latex file.
#
   raise "#{latexfile} already exists." if File.exists?(latexfile)

   io = open(latexfile,"w")
   io.print(latexheader)
   inn.each do |line|
#     --- Evaluate as ruby statement if it begins "#". ---
      if line =~ /^#/ then
        eval(line.sub(/^#/,'')) 
      else
        io.print(line)
      end
   end
   io.print(latextailer)
   io.close
#
# convert TeX -PNG
#
   raise "#{dvipsout} already exists." if File.exists?(dvipsout)

   $stderr.print `#{latex} #{latexfile}`
   $stderr.print `#{dvips} #{dvipsoption} -o #{dvipsout} #{dvipsin}`
   $stderr.print `#{pstoimg} #{pstoimgoption} -scale #{pstoimgscale} #{dvipsout}`

   out.print("<center>") if centering
   out.print("<img src=#{pngfile} align=#{imgalign}>")
   out.print("</center>") if centering
#
# Delete temporary files
#
   File.delete(latexfile)
   File.delete(dvipsin)
   File.delete(dvipsin.sub(/.dvi$/,'.aux'))
   File.delete(dvipsin.sub(/.dvi$/,'.log'))
   File.delete(dvipsout)
   end
end

$RC["filter"]["latex"] = RD::LATEX_FILTER

=begin
== script info.
  Extention rdtool library for embedded latex source,
  created by S. Takehiro, 2005/10/10. 

== changes
:0.0.1
  * created. (2005/10/10)
=end
