Class: RDocF95::Markup::ToXHtmlTexParser

Inherits:
ToHtmlCrossref show all
Defined in:
lib/rdoc-f95/markup/to_xhtml_texparser.rb

Overview

TeX is converted to MathML

TeX formula is converted to MathML. When inline display, TeX formula should be bundled by $ … $ as follows. One or more normal-width blank is necessary before and behind “$”. (The format of CVS keywords, that is “$ID: … $” or “$LOG: … $ etc. is ignored.)

Inline formula is $ f(x) = x^2 + 1 $ .

When block display, TeX formula should be bundled by [ … ] as follows. Describe [ at the head of line.

\[
   \sum_{i=1}^nf_n(x)
\]

To write equations across multiple lines, describe “] [” as follows.

[

d\zeta/dt + J(\psi,\zeta) = Ra \; dT/dx + \nabla\zeta, \] \[
dT/dt + J(\psi,T) - d\psi/dx = \nabla T,               \] \[
\nabla\psi = \zeta

]

MathML library for Ruby version 0.8 is needed to convert TeX formula to MathML. This library is available from Bottega of Hiraku (JAPANESE only). See this site about available TeX commands.

When you browse generated documents, you need to use browers that can handle MathML. See MathML Software - Browsers, etc.

Usage of newcommand and newenvironment

If you want to use macros defined by newcommand and newenvironment commands, make 'mathml/macro' directory under load paths of Ruby, and prepare a file that macro commands are described in the directory. A name of the file is free.

For example, if your load path of Ruby is '/usr/lib/ruby/1.8', you should make '/usr/lib/ruby/1.8/mathml' and '/usr/lib/ruby/1.8/mathml/macro' directories, and make 'D6math.sty' file (already mentioned, the file name is free). When you describe a following newcommand command, you can use 'DP{}{}' (a macro for partial differentiations) command.

\newcommand{\DP}[2]{\frac{\partial #1}{\partial #2}}

As a sample, please use libmathml-macro-dennou-ruby. The original style file is 'D6math.sty' in TeX macro (Dennou style). libmathml-macro-dennou-ruby is a reconfigured package for Mathml library for Ruby.

Constant Summary

Constants inherited from ToHtml

RDocF95::Markup::ToHtml::LIST_TYPE_TO_HTML

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from ToHtmlCrossref

#handle_special_CROSSREF, #handle_special_CROSSREFFILE

Methods inherited from ToHtml

#accept_blank_line, #accept_heading, #accept_list_end, #accept_list_item, #accept_list_start, #accept_paragraph, #accept_rule, #accept_verbatim, #add_tag, #annotate, #end_accepting, #gen_url, #handle_special_HYPERLINK, #handle_special_TIDYLINK, #init_tags, #start_accepting, #wrap

Methods inherited from Formatter

#convert

Constructor Details

#initialize(from_path, context, show_hash, mathml = nil) ⇒ ToXHtmlTexParser

We need to record the html path of our caller so we can generate correct relative paths for any hyperlinks that we find


75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/rdoc-f95/markup/to_xhtml_texparser.rb', line 75

def initialize(from_path, context, show_hash, mathml=nil)
  super(from_path, context, show_hash)
  @mathml = mathml

  if @mathml
    # TeX inline form
    @markup.add_special(/(\$(.*?)[^\\]\$)/im, :TEXINLINE)

    # TeX inline delimiter
    @markup.add_special(/(\\\$)/im, :TEXINLINEDELIMITER)

    # TeX block form
    @markup.add_special(/(\\\[(.+?)\\\])/im, :TEXBLOCK)
  end

  @block_exceptions = []
  if @markup
    @block_exceptions << {
      'name'     => :texblockform,
      'start'    => Regexp.new("^\\\\\\["),
      'end'      => Regexp.new("\\\\\\]$"),
      'replaces' => []
    }
    @block_exceptions[0]['replaces'] << {
      'from' => Regexp.new("\\\\\\\\"),
      'to'   => "\\\\\\\\\\\\\\\\",
    }
  end

end

Instance Attribute Details

#block_exceptionsObject (readonly)

Returns the value of attribute block_exceptions


69
70
71
# File 'lib/rdoc-f95/markup/to_xhtml_texparser.rb', line 69

def block_exceptions
  @block_exceptions
end

#contextObject

Returns the value of attribute context


68
69
70
# File 'lib/rdoc-f95/markup/to_xhtml_texparser.rb', line 68

def context
  @context
end

Instance Method Details

#file_locationObject


106
107
108
109
110
111
112
113
114
115
116
117
118
119
# File 'lib/rdoc-f95/markup/to_xhtml_texparser.rb', line 106

def file_location
  if @context.context.parent
    class_or_method = @context.context.name
  end
  context = @context.context
  while context.parent
    context = context.parent
  end
  location = context.file_relative_name
  if class_or_method
    location += "#"+class_or_method
  end
  return location
end

#handle_special_TEXBLOCK(special) ⇒ Object

TEXBLOCK pattern […] is converted to MathML format when –mathml option is given.


155
156
157
158
159
160
161
162
163
164
165
166
167
# File 'lib/rdoc-f95/markup/to_xhtml_texparser.rb', line 155

def handle_special_TEXBLOCK(special)
  text = special.text
  return text unless @mathml
  text.sub!(/^\\\[/, '')
  text.sub!(/\\\]$/, '')
  tex = MathMLWrapper.new
  mathml, stat = tex.parse(text, true)
  if !(stat == 0)
    $stderr.puts "Warning: in #{file_location}, following TeX commands can not be converted to MathML\n\n",
    "    #{text}\n\n"
  end
  return mathml
end

#handle_special_TEXINLINE(special) ⇒ Object

TEXINLINE pattern $…$ is converted to MathML format when –mathml option is given.


125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
# File 'lib/rdoc-f95/markup/to_xhtml_texparser.rb', line 125

def handle_special_TEXINLINE(special)
  text = special.text
  return text unless @mathml
  raw_text = text.scan(/^.*?\$/).to_s.sub(/\$$/, '')
  return text if text =~ /^.*?\$[A-Z]\w+:/  # CVS keywords are skipped
  text.sub!(/^.*?\$/, '')
  text.sub!(/\$$/, '')
  tex = MathMLWrapper.new
  mathml, stat = tex.parse(text)
  if !(stat == 0)
    $stderr.puts "Warning: in #{file_location}, following TeX commands can not be converted to MathML\n\n",
    "    #{text}\n\n"
  end
  return raw_text + mathml
end

#handle_special_TEXINLINEDELIMITER(special) ⇒ Object

TEXINLINEDELIMITER pattern “$” is converted to single dollar “$” when –mathml option is given.


145
146
147
148
149
# File 'lib/rdoc-f95/markup/to_xhtml_texparser.rb', line 145

def handle_special_TEXINLINEDELIMITER(special)
  text = special.text
  return text unless @mathml
  return text.gsub(/\\\$/, '$')
end