Class: LatexOutput

Inherits:
Object
  • Object
show all
Defined in:
lib/hiki2latex/hiki2latex.rb

Constant Summary collapse

DT_ALIGN =
'l'

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ LatexOutput

Returns a new instance of LatexOutput.



15
16
17
18
19
20
# File 'lib/hiki2latex/hiki2latex.rb', line 15

def initialize(options={})
  @suffix = ""
  @listings = options[:listings]
#    @f = nil
  reset
end

Instance Method Details

#block_plugin(str) ⇒ Object



86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/hiki2latex/hiki2latex.rb', line 86

def block_plugin(str)
   tmp=[]
   if ( /(\w+)\((.+)\)/ =~ str ) or ( /(\w+).\'(.+)\'/ =~str ) or (/(\w+)/ =~ str)
     tmp = [$1,$2]
   end

   case tmp[0]
   when 'toc'
     @f << "\\tableofcontents\n"
   when 'attach_anchor'
     @f << "\\input\{#{tmp[1]}\}\n"
   when 'attach_view'
     @f << "\\begin{figure}[htbp]\\begin{center}\n"
     @f << "\\includegraphics[width=6cm]\{./#{tmp[1]}\}\n"
     @f << "\\caption\{"+@caption+"\}\n"
     @f << "\\label\{default\}\\end\{center\}\\end\{figure\}\n"
     @caption = "" #reset caption
   when 'dmath'
     @f << "\\begin{equation}\n#{tmp[1]}\n\\end{equation}"
   when 'math'
     @f << "\$#{tmp[1]}\$"
   else
     @f << "Don\'t know \\verb|{{#{str}}}|\n"
   end
end

#block_preformatted(str, info) ⇒ Object



197
198
199
200
201
202
203
204
205
206
207
# File 'lib/hiki2latex/hiki2latex.rb', line 197

def block_preformatted(str, info)
  if (@listings==true and info!=nil) then
    style='customRuby' if info=='ruby'
    style='customCsh' if (info=='tcsh' or info=='csh')
    style='customTeX' if ( info=='tex' or info=='latex')
    style='customJava' if info=='java'
    preformatted_with_style(str,style)
  else
    preformatted(text(str))
  end
end

#blockquote_closeObject



175
176
177
# File 'lib/hiki2latex/hiki2latex.rb', line 175

def blockquote_close()
  @f << "\\end{quotation}\n"
end

#blockquote_openObject



172
173
174
# File 'lib/hiki2latex/hiki2latex.rb', line 172

def blockquote_open()
  @f << "\\begin{quotation}\n"
end

#container(_for = nil) ⇒ Object



37
38
39
40
41
42
43
44
# File 'lib/hiki2latex/hiki2latex.rb', line 37

def container(_for=nil)
  case _for
  when :paragraph
    []
  else
    ""
  end
end

#del(item) ⇒ Object



179
180
181
# File 'lib/hiki2latex/hiki2latex.rb', line 179

def del(item)
  # use ulem or jumoline
end

#dlist_closeObject



268
269
270
# File 'lib/hiki2latex/hiki2latex.rb', line 268

def dlist_close
  @f << "\\end{description}\n"
end

#dlist_item(dt, dd) ⇒ Object



272
273
274
275
276
277
278
279
280
281
282
283
# File 'lib/hiki2latex/hiki2latex.rb', line 272

def dlist_item(dt, dd)
  tmp_dd = escape_snake_names(dd)
  tmp_dt = escape_snake_names(dt)
  case
  when dd.empty?
    @f << "\\item[#{tmp_dt}]"
  when dt.empty?
    @f << "\\item #{tmp_dd}"
  else
    @f << "\\item[#{tmp_dt}] #{tmp_dd}\n"
  end
end

#dlist_openObject



263
264
265
266
# File 'lib/hiki2latex/hiki2latex.rb', line 263

def dlist_open
  @f.slice!(-1)
  @f << "\\begin{description}\n"
end

#em(item) ⇒ Object



187
188
189
190
# File 'lib/hiki2latex/hiki2latex.rb', line 187

def em(item)
  "\\textbf\{#{item}\}"
#    "#{item}"
end

#escape_htm_param(str) ⇒ Object



148
149
150
# File 'lib/hiki2latex/hiki2latex.rb', line 148

def escape_htm_param(str)
  str
end

#escape_html(text) ⇒ Object



140
141
142
# File 'lib/hiki2latex/hiki2latex.rb', line 140

def escape_html(text)
  text
end

#escape_snake_names(str) ⇒ Object



152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
# File 'lib/hiki2latex/hiki2latex.rb', line 152

def escape_snake_names(str)
  str.gsub!(/_/,"\\_")
  patterns = [/\$(.+?)\$/ , /\\verb\|(.+?)\|/, /equation(.+?)equation/m ]
  patterns.each{|pattern|
#      str.gsub!(/\\_/,"_")    if str.match(pattern)
    str.gsub!(pattern){|text|
      if text =~ /\\_/ then
        text.gsub!(/\\_/,'_')
      else
        text
      end
    }
  }
  str
end

#finishObject



27
28
29
30
31
32
33
34
35
# File 'lib/hiki2latex/hiki2latex.rb', line 27

def finish
  if @head != "" then
    @head << "\\date\{\}\n" if !@head.include?("date")
    @head << "\\maketitle\n"
    return @head+@f
  else
    return @f
  end
end

#headline(level, title) ⇒ Object

Procedures



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/hiki2latex/hiki2latex.rb', line 50

def headline(level, title)
  title = escape_snake_names(title)
  if tmp=title.match(/(.+?):(.+)/)
#    if tmp.size==2 then
    case tmp[1]
    when 'title','author','date','abstract'
      @head << "\\#{tmp[1]}\{#{tmp[2]}\}\n"
    when 'Title','Author','Date','Abstract'
      @head << "\\#{tmp[1].downcase}\{#{tmp[2]}\}\n"
    when 'caption'
      @caption << "#{tmp[2]}"
    when 'reference'
      @f << "\\section*\{#{tmp[2]}\}\n"
    else
#        @f << "\\#{tmp[0]}\{#{tmp[1]}\}\n"
      headline_section(level,title)
    end
    return
  else
    headline_section(level,title)
  end
end

#headline_section(level, title) ⇒ Object



73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/hiki2latex/hiki2latex.rb', line 73

def headline_section(level,title)
  case level
  when 1
    @f << "\\section\{#{title}\}\n"
  when 2
    @f << "\\subsection\{#{title}\}\n"
  when 3
    @f << "\\subsubsection\{#{title}\}\n"
  else
    @f << "\\paragraph\{#{title}\}\n"
  end
end

#hruleObject



183
184
185
# File 'lib/hiki2latex/hiki2latex.rb', line 183

def hrule
  "\\hrulefill"
end


384
385
386
387
388
389
390
# File 'lib/hiki2latex/hiki2latex.rb', line 384

def hyperlink(uri, title)
  if uri==title then
    %Q(\\verb\|#{uri}\|)
  else
    %Q(\\verb\|#{title}(#{uri})\|)
  end
end

#inline_plugin(src) ⇒ Object

inline means only in table??



117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
# File 'lib/hiki2latex/hiki2latex.rb', line 117

def inline_plugin(src)
  if ( /(\w+)\s+\'(.+)\'/ =~src ) or ( /(\w+)\((.+)\)/ =~ src ) or (/(\w+)/ =~ src)
    tmp = [$1,$2]
  end
  case tmp[0]
  when 'dmath'
     "\\begin{equation}\n#{tmp[1]}\n\\end{equation}"
  when 'math'
     "\$#{tmp[1]}\$"
#    when 'attach_view'
#       "\n\\includegraphics[scale=0.3]\{./#{tmp[1]}\}\n"
  else
     %Q(\\verb\|{{#{src}}}\|)
  end
end

#list_beginObject



224
225
# File 'lib/hiki2latex/hiki2latex.rb', line 224

def list_begin
end

#list_close(type) ⇒ Object



242
243
244
245
246
247
248
249
# File 'lib/hiki2latex/hiki2latex.rb', line 242

def list_close(type)
  case type
  when 'ul' then
    @f <<  "\\end{itemize}\n"
  when 'ol' then
    @f << "\\end{enumerate}\n"
  end
end

#list_endObject



227
228
# File 'lib/hiki2latex/hiki2latex.rb', line 227

def list_end
end

#list_open(type) ⇒ Object



230
231
232
233
234
235
236
237
238
239
240
# File 'lib/hiki2latex/hiki2latex.rb', line 230

def list_open(type)
  #    @f.slice!(-1) #\\beign{enumerate}が消える場合があるので,再度アウト
  #こいつがわからん.\begin{item..}の前に改行が入るから...なぜ?
  #二重のitemizeでおかしくなるが,へんなところで改行は入らないので.
  case type
  when 'ul' then
    @f << "\\begin{itemize}\n"
  when 'ol' then
    @f << "\\begin{enumerate}\n"
  end
end

#listitem(item) ⇒ Object



259
260
261
# File 'lib/hiki2latex/hiki2latex.rb', line 259

def listitem(item)
  @f << escape_snake_names(item) << "\n"
end

#listitem_closeObject



255
256
257
# File 'lib/hiki2latex/hiki2latex.rb', line 255

def listitem_close
  #      @f << "\n" #closeでやるとうまくいかないときあり.listitemに改行を付け足した.
end

#listitem_openObject



251
252
253
# File 'lib/hiki2latex/hiki2latex.rb', line 251

def listitem_open
  @f << "\\item "
end

#make_matrix(cont) ⇒ Object

tableから連結作用素に対応したmatrixを作る input:lineごとに分割されたcont output:matrixと最長列数



297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
# File 'lib/hiki2latex/hiki2latex.rb', line 297

def make_matrix(cont)
  t_matrix=[]
  cont.each{|line|
    tmp=line.split('||')
    tmp.slice!(0)
    tmp.slice!(-1) if tmp.slice(-1)=="\n"
    tmp.each_with_index{|ele,i| tmp[i] = ele.match(/\s*(.+)/)[1]}
    t_matrix << tmp
  }
  t_matrix.each_with_index{|line,i|
    line.each_with_index{|ele,j|
      if ele=~/\^+/ then
        t_matrix[i][j]=""
        rs=$&.size
        c_rs=rs/2
        rs.times{|k| t_matrix[i+k+1].insert(j,"")}
        t_matrix[i+c_rs][j]=$'
      end
    }
  }
  max_col=0
  t_matrix.each_with_index{|line,i|
    n_col=line.size
    line.each_with_index{|ele,j|
      if ele=~/>+/ then
        cs=$&.size
        t_matrix[i][j]= "\\multicolumn{#{cs+1}}{#{DT_ALIGN}}{#{$'}} "
        n_col+=cs
      end
    }
    max_col = n_col if n_col>max_col
  }
  return t_matrix,max_col
end

#make_tableObject

tableを整形する



333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
# File 'lib/hiki2latex/hiki2latex.rb', line 333

def make_table
  cont,max_col = make_matrix(@table.split("\n"))

  position = "{"
  max_col.times{ position << DT_ALIGN}
  position << "}"
  buf = "\\begin{table}[htbp]\\begin{center}\n"
  buf << "\\caption{#{@caption}}\n"
  buf << "\\begin{tabular}#{position}\n\\hline\n"

  cont.each_with_index{|line,i|
    line.each{|ele|
#        buf << "#{ele} &"
      buf << escape_snake_names(ele)+" &"
    }
#      buf.slice!(-2)
    buf.slice!(-4..-1)
    buf << ((i==0)? "\\\\ \\hline\n" : "\\\\\n")
  }
  buf << "\\hline\n\\end{tabular}\n"
  buf << "\\label{default}\n\\end{center}\\end{table}\n"
  @caption = ""
  buf << "%for inserting separate lines, use \\hline, \\cline{2-3} etc.\n\n"
  return buf
end

#paragraph(lines) ⇒ Object



168
169
170
171
# File 'lib/hiki2latex/hiki2latex.rb', line 168

def paragraph(lines)
  lines.each{|line| line = escape_snake_names(line) }
  @f << "#{lines.join("\n")}\n\n"
end

#preformatted(str) ⇒ Object



209
210
211
212
213
214
# File 'lib/hiki2latex/hiki2latex.rb', line 209

def preformatted(str)
  @f.slice!(-1)
  @f << "\\begin{quote}\\begin{verbatim}\n"
  @f << str+"\n"
  @f << "\\end{verbatim}\\end{quote}\n"
end

#preformatted_with_style(str, style) ⇒ Object



216
217
218
219
220
221
222
# File 'lib/hiki2latex/hiki2latex.rb', line 216

def preformatted_with_style(str,style)
  @f.slice!(-1)
  @f << "\\begin{lstlisting}[style=#{style},basicstyle={\\scriptsize\\ttfamily}]\n"
#    @f << "\\begin{lstlisting}[style=#{style}]\n"
  @f << str+"\n"
  @f << "\\end{lstlisting}\n"
end

#resetObject



22
23
24
25
# File 'lib/hiki2latex/hiki2latex.rb', line 22

def reset
#    @f = StringIO.new
  @f, @head, @caption, @table ="","","",""
end

#strong(item) ⇒ Object



192
193
194
195
# File 'lib/hiki2latex/hiki2latex.rb', line 192

def strong(item)
#    p item
  "\\textbf\{#{item}\}"
end

#table_closeObject



289
290
291
# File 'lib/hiki2latex/hiki2latex.rb', line 289

def table_close
  @f << make_table
end

#table_data(item, rs, cs) ⇒ Object



371
372
373
# File 'lib/hiki2latex/hiki2latex.rb', line 371

def table_data(item, rs, cs)
  @table << "#{tdattr(rs,cs)}#{item.chomp} \|\| "
end

#table_head(item, rs, cs) ⇒ Object



367
368
369
# File 'lib/hiki2latex/hiki2latex.rb', line 367

def table_head(item, rs, cs)
  @table << item.chomp+" \|\| "
end

#table_openObject



285
286
287
# File 'lib/hiki2latex/hiki2latex.rb', line 285

def table_open
  @table = ""
end

#table_record_closeObject



363
364
365
# File 'lib/hiki2latex/hiki2latex.rb', line 363

def table_record_close
  @table << " \n"
end

#table_record_openObject



359
360
361
# File 'lib/hiki2latex/hiki2latex.rb', line 359

def table_record_open
  @table << "\|\| "
end

#text(str) ⇒ Object



112
113
114
# File 'lib/hiki2latex/hiki2latex.rb', line 112

def text(str)
  str
end

#unescape_html(text) ⇒ Object



144
145
146
# File 'lib/hiki2latex/hiki2latex.rb', line 144

def unescape_html(text)
  text
end

#wiki_name(text) ⇒ Object

def compile_inline_markup(text)



134
135
136
# File 'lib/hiki2latex/hiki2latex.rb', line 134

def wiki_name(text)
  text
end