#!/usr/bin/env ruby
=begin
= rd2html-ext-tex.rb

Author:: Shin-ichi Takehiro, Youhei SASAKI
Version:: $Id: rd2html-ext-tex.rb,v 1.2 2008-12-10 07:16:37 uwabami Exp $

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 "imgscale" variable as follows.

    =begin LATEX
    #imgscale=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 --native-inline \
    hoge.rd hoge.htm

=== Necessary external commands

* rd/rd2html-ext-lib.rb
* latex 
* dvipng

=== Configuration

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

=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{article}
\usepackage{fullpage}
\usepackage[varg]{txfonts}
\usepackage{D6math}
\pagestyle{empty}
\begin{document}
   EOF

   latextailer= <<-'EOF'
\end{document}
   EOF
#  DVIPNG command and its options
#
  dvipng="dvipng"
  dvipngoption="-fg Black -bg Transparent -T tight"
  dvipngin=latexfile.sub(/\.tex$/,'.dvi')
  dvipngout=pngfile.sub(/\.png$/,'.png')
  imgscale="1.5"
  pstoimgscale=nil
#
# HTML parameters
#
   cssclass="latexeq"
   centering=true
#
# create a latex file.
#
   raise "#{latexfile} already exists." if File.exists?(latexfile)


   src = ""
   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)
        src += line
      end
   end
   io.print(latextailer)
   io.close
#
# convert TeX -> PNG
#
   raise "#{dvipngout} already exists." if File.exists?(dvipngout)

    # for backward compatibility    
    if pstoimgscale
      scale = pstoimgscale * 750
    else
      scale = imgscale * 1000 
    end
    #
    $stderr.print `#{latex} #{latexfile}`
    $stderr.print `#{dvipng} #{dvipngoption} -x #{scale} -y #{scale} -o #{dvipngout} #{dvipngin}` 

    out.print("<!--\n")
    out.print("#{src}")
    out.print("-->\n")
#    if cssclass
#      out.print("<img class=\"#{cssclass}\" src=\"#{pngfile}\" alt=\"tex\" \/>")
#    else
      out.print("<center>") if centering
      out.print("<img src=\"#{pngfile}\" alt=\"tex eq.\" \/>")
      out.print("</center>") if centering
#    end
#
# Delete temporary files
#
  File.delete(latexfile)
  File.delete(dvipngin)
  File.delete(dvipngin.sub(/.dvi$/,'.aux'))
  File.delete(dvipngin.sub(/.dvi$/,'.log'))
   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. 

  $Id: rd2html-ext-tex.rb,v 1.2 2008-12-10 07:16:37 uwabami Exp $

== changes
:0.0.1
  * created. (2005/10/10)
:0.0.2
  * using dvipng instead of dvips and pstoimg
=end
