Class: ReVIEW::EWBBuilder

Inherits:
Builder show all
Includes:
TextUtils
Defined in:
lib/review/ewbbuilder.rb

Constant Summary collapse

LI =
''
Z_SPACE =

zen-kaku space in EUC-JP

"\241\241"
NAKAGURO =
''

Constants inherited from Builder

Builder::CAPTION_TITLES

Instance Method Summary collapse

Methods included from TextUtils

#detab, #split_paragraph

Methods inherited from Builder

#bibpaper, #bind, #captionblock, #compile_inline, #detab, #error, #extract_chapter_id, #get_chap, #graph, #handle_metric, #image, #image_ext, #include, #inline_column, #inline_column_chap, #inline_fn, #inline_hd, #inline_href, #inline_imgref, #inline_include, #inline_raw, #inline_table, #inline_tcy, #inline_title, #listnum, #parse_metric, #post_paragraph, #pre_paragraph, #print, #puts, #raw, #result, #result_metric, #source, #target_name, #ul_item_begin, #ul_item_end, #warn

Constructor Details

#initialize(chap) ⇒ EWBBuilder

Returns a new instance of EWBBuilder.



12
13
14
15
16
# File 'lib/review/ewbbuilder.rb', line 12

def initialize(chap)
  super
  @footnote_buf = []
  @index_buffer = []
end

Instance Method Details

#add_index(str) ⇒ Object



368
369
370
371
# File 'lib/review/ewbbuilder.rb', line 368

def add_index(str)
  @index_buffer.push str
  "//in#{index_number(@index_buffer.size)}"
end

#cascade(f) ⇒ Object



197
198
199
# File 'lib/review/ewbbuilder.rb', line 197

def cascade(f)
  # FIXME
end

#chapter_idObject



327
328
329
# File 'lib/review/ewbbuilder.rb', line 327

def chapter_id
  File.basename(@filename, '.rd')
end

#chapter_name(key) ⇒ Object



352
353
354
# File 'lib/review/ewbbuilder.rb', line 352

def chapter_name(key)
  '' + text(@chapter_table.title(key)) + ''
end

#chapter_number(key) ⇒ Object



335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
# File 'lib/review/ewbbuilder.rb', line 335

def chapter_number(key)
  curr = @chapter_table.number(chapter_id)
  dest = @chapter_table.number(key)

  case chapter_id
  when /\.ewb\z/, 'tmp', 'temp'
    return '' + dest + ''
  end
  if dest == curr + 1
    '次章'
  elsif dest == curr - 1
    '前章'
  else
    "#{dest}"
  end
end

#chapter_prefixObject



331
332
333
# File 'lib/review/ewbbuilder.rb', line 331

def chapter_prefix
  sprintf('%02d', @chapter_table.number(chapter_id))
end

#char_length(str) ⇒ Object



315
316
317
# File 'lib/review/ewbbuilder.rb', line 315

def char_length(str)
  str.gsub(/./, '.').length
end

#cmd(lines, caption = nil) ⇒ Object

Compiler.defsyntax(:cmd, :block, 0..1) {|args|

  if args[0] and not args[0] == 'noescape'
    raise SyntaxError, "unknown //cmd option: #{args[0]}"
  end
}


58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/review/ewbbuilder.rb', line 58

def cmd(lines, caption = nil)
  puts
  puts caption
  puts '//sc1{'
  lines.each do |line|
    if noescape
      puts detab(line)
    elsif /\AC:.*?>(.+)/ =~ line # DOS prompt hack
      prompt, cmd = *line.split('>', 2)
      puts "#{escape(prompt)}>//command{#{escape(cmd)}}//}"
    else
      puts escape(detab(line))
    end
  end
  puts '//}'
  puts
end

#emlist(lines, caption = nil) ⇒ Object

Compiler.defsyntax(:emlist, :block, 0..1) {|args|

  if args[0] and not args[0] == 'noescape'
    raise SyntaxError, "unknown //emlist option: #{args[0]}"
  end
}


36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/review/ewbbuilder.rb', line 36

def emlist(lines, caption = nil)
  # firstline = f.lineno
  puts
  puts caption
  puts '//lst1'
  lines.each do |line|
    if noescape
      puts detab(line)
    else
      puts escape(detab(line))
    end
  end
  puts '//}'
  puts
end

#escape(str) ⇒ Object



377
378
379
# File 'lib/review/ewbbuilder.rb', line 377

def escape(str)
  str.gsub(%r{//}, '////')
end

#figure_filename(key) ⇒ Object



219
220
221
222
223
224
225
226
227
228
229
230
231
232
# File 'lib/review/ewbbuilder.rb', line 219

def figure_filename(key)
  if ext = key.slice(/\.\w+\z/)
    base = key.sub(/\.\w+\z/, '')
  else
    base = key
    ext = '.eps'
  end
  currname = "images/ch_#{chapter_id}_#{base}#{ext}"
  destname = "images/fig#{figure_number(base)}#{ext}"
  unless File.exist? currname
    # error "image file not exist: #{currname}"
  end
  destname
end

#figure_number(key) ⇒ Object



360
361
362
# File 'lib/review/ewbbuilder.rb', line 360

def figure_number(key)
  sprintf(chapter_prefix + '%02d0', @figure_table.number(key))
end

#footnote_number(key) ⇒ Object



364
365
366
# File 'lib/review/ewbbuilder.rb', line 364

def footnote_number(key)
  sprintf('%02d', @footnote_index.number(key) * 5)
end

#generate_indexObject



18
19
20
21
22
# File 'lib/review/ewbbuilder.rb', line 18

def generate_index
  @index_buffer.each_with_index do |str, n|
    printf "%d\t%s\n", index_number(n + 1), str
  end
end

#headline(level, caption) ⇒ Object



24
25
26
27
28
# File 'lib/review/ewbbuilder.rb', line 24

def headline(level, caption)
  puts unless level == 1
  puts "//#{'i' * level} #{caption}"
  puts
end

#image_dummyObject



101
102
103
104
105
106
107
# File 'lib/review/ewbbuilder.rb', line 101

def image_dummy
  puts '//lst1{'
  puts '---- dummy figure ----'
  print dummy
  puts '//}'
  puts
end

#image_header(file, caption) ⇒ Object



93
94
95
96
97
98
99
# File 'lib/review/ewbbuilder.rb', line 93

def image_header(file, caption)
  if /\.png\z/ =~ file and not FileTest.exist?('images/' + file)
    warn "image file not exist: #{file}"
  end
  id = file.sub(/\.\w+\z/, '')
  puts "//f#{figure_number(id)} #{text(caption)} file=#{file}" if id
end

#image_label(str) ⇒ Object



234
235
236
# File 'lib/review/ewbbuilder.rb', line 234

def image_label(str)
  "#{chapter_id}:#{str}"
end

#index_number(n) ⇒ Object



373
374
375
# File 'lib/review/ewbbuilder.rb', line 373

def index_number(n)
  900_000 + @chapter_index.number(chapter_id) * 1000 + n
end

#inline_bou(str) ⇒ Object



311
312
313
# File 'lib/review/ewbbuilder.rb', line 311

def inline_bou(str)
  "//ruby{#{escape(str)},#{NAKAGURO * char_length(str)}//}"
end

#inline_chap(id) ⇒ Object



279
280
281
# File 'lib/review/ewbbuilder.rb', line 279

def inline_chap(id)
  chapter_number(arg)
end

#inline_chapname(id) ⇒ Object



287
288
289
# File 'lib/review/ewbbuilder.rb', line 287

def inline_chapname(id)
  chapter_name(arg)
end

#inline_chapref(id) ⇒ Object



283
284
285
# File 'lib/review/ewbbuilder.rb', line 283

def inline_chapref(id)
  chapter_number(arg) + chapter_name(arg)
end

#inline_em(str) ⇒ Object



319
320
321
# File 'lib/review/ewbbuilder.rb', line 319

def inline_em(str)
  "//g{#{arg}//}"
end

#inline_footnote(id) ⇒ Object



300
301
302
# File 'lib/review/ewbbuilder.rb', line 300

def inline_footnote(id)
  '//ky' + footnote_number(id)
end

#inline_img(arg) ⇒ Object



295
296
297
298
# File 'lib/review/ewbbuilder.rb', line 295

def inline_img(arg)
  error "wrong image label: #{arg}" if /[^a-zA-Z\d\-]/ =~ arg
  '//f' + figure_number(arg)
end

#inline_keytop(arg) ⇒ Object



275
276
277
# File 'lib/review/ewbbuilder.rb', line 275

def inline_keytop(arg)
  "//keytop{#{arg}//}"
end

#inline_kw(arg) ⇒ Object



261
262
263
264
265
266
267
268
269
270
271
272
273
# File 'lib/review/ewbbuilder.rb', line 261

def inline_kw(arg)
  word, eng, abbr = arg.split(/,/).map {|s| s.strip }
  if abbr
    add_index(word) + "//g{#{word}//}" +
    add_index(abbr) + "#{abbr}, " +
    add_index(eng) + "#{eng}"
  elsif eng
    add_index(word) + "//g{#{word}//}" +
    add_index(eng) + "#{eng}"
  else
    add_index(word) + "//g{#{word}//}"
  end
end

#inline_list(arg) ⇒ Object



291
292
293
# File 'lib/review/ewbbuilder.rb', line 291

def inline_list(arg)
  '//l' + list_number(arg)
end

#inline_math(arg) ⇒ Object



323
324
325
# File 'lib/review/ewbbuilder.rb', line 323

def inline_math(arg)
  "//LaTeX{ $#{arg}$ //}"
end

#inline_ruby(arg) ⇒ Object



304
305
306
307
# File 'lib/review/ewbbuilder.rb', line 304

def inline_ruby(arg)
  error 'wrong number of arg: @<ruby>' unless arg.count(',') == 1
  "//ruby{#{arg}//}"
end

#list(lines, id, caption) ⇒ Object

Compiler.defsyntax(:list, :block, 0..1) {|args|

  if args[0] and not args[0] == 'noescape'
    raise SyntaxError, "unknown //list option: #{args[0]}"
  end
}


82
83
84
85
86
87
88
89
90
91
# File 'lib/review/ewbbuilder.rb', line 82

def list(lines, id, caption)
  puts
  puts "//l#{list_number(id)} " + caption
  puts '//lst2{'
  lines.each do |line|
    puts escape(detab(line))
  end
  puts '//}'
  puts
end

#list_number(key) ⇒ Object



356
357
358
# File 'lib/review/ewbbuilder.rb', line 356

def list_number(key)
  sprintf(chapter_prefix + '%02d0', @list_table.number(key))
end

#noindentObject



170
171
172
# File 'lib/review/ewbbuilder.rb', line 170

def noindent
  @noindent = true
end

#note(f, caption) ⇒ Object



188
189
190
191
192
193
194
195
# File 'lib/review/ewbbuilder.rb', line 188

def note(f, caption)
  puts
  puts '//note{'
  puts "//cg{#{caption}//}"
  puts '//h'
  cascade f
  puts '//}'
end

#ol_beginObject



144
145
146
147
# File 'lib/review/ewbbuilder.rb', line 144

def ol_begin
  output.puts
  output.puts '//k2{'
end

#ol_endObject



153
154
155
# File 'lib/review/ewbbuilder.rb', line 153

def ol_end
  puts '//}'
end

#ol_item(num, lines) ⇒ Object



149
150
151
# File 'lib/review/ewbbuilder.rb', line 149

def ol_item(num, lines)
  print "#{num}//|" + lines.join('')
end

#paragraph(lines) ⇒ Object



203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
# File 'lib/review/ewbbuilder.rb', line 203

def paragraph(lines)
  if @noindent
    @noindent = false
  else
    print Z_SPACE
  end
  prev = ''
  lines.each do |line|
    if /[a-zA-Z0-9]\z/ =~ prev and /\A[a-zA-Z0-9]/ =~ line
      print ' '
    end
    print line
  end
  puts
end

#point(f) ⇒ Object



181
182
183
184
185
186
# File 'lib/review/ewbbuilder.rb', line 181

def point(f)
  puts
  puts '//point{'
  cascade f
  puts '//}'
end

#quote(lines) ⇒ Object



157
158
159
160
161
162
163
# File 'lib/review/ewbbuilder.rb', line 157

def quote(lines)
  puts '//c1{'
  lines.each do |line|
    puts text(line)
  end
  puts '//}'
end

#refer(f) ⇒ Object



174
175
176
177
178
179
# File 'lib/review/ewbbuilder.rb', line 174

def refer(f)
  puts
  puts '//refer{'
  cascade f
  puts '//}'
end

#tableObject



109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
# File 'lib/review/ewbbuilder.rb', line 109

def table
  # %r<\A//table\[(\w+)\]>
  spec = $1
  buf = []
  while line = f.gets
    break if %r[\A//\}] === line
    buf.push line.strip.split(/\t+/).map {|s| s == '.' ? '' : s }
  end
  table_type = 'tabm'
  output.puts "//#{table_type}[" + spec + ']{'
  buf.each_with_index do |col, idx|
    if /----/ === col[0]
      output.puts '//kb'
    else
      output.puts col.map {|s| text(s) }.join("\t")
    end
  end
  output.puts '//}'
end

#text(str) ⇒ Object



238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
# File 'lib/review/ewbbuilder.rb', line 238

def text(str)
  str = str.gsub(/\t/, ' ')
  str.gsub(/([^@^]+)|\^(.*?)\^|@<(\w+)>\{(.*?)\}|@\{(.*?)\}|([@^])/) do
    if normal = $1
      escape(normal)
    elsif tt = $2
      '//tt{' + escape(tt) + '//}'
    elsif inline = $3
      compile_inline(inline, $4)
    elsif index = $5
      error 'index not implemented'
      text(index) + index_command(index)
    elsif char = $6
      escape(char)
    else
      error "unknown inline: #{str.inspect}"
    end
  end
rescue DocumentError => e
  error e.message
  return 'ERROR'
end

#ul_beginObject



131
132
133
134
# File 'lib/review/ewbbuilder.rb', line 131

def ul_begin
  puts
  puts '//k1{'
end

#ul_endObject



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

def ul_end
  puts '//}'
end

#ul_item(lines) ⇒ Object



136
137
138
# File 'lib/review/ewbbuilder.rb', line 136

def ul_item(lines)
  puts "#{LI}//|" + lines.join('')
end

#vspaceObject



165
166
167
168
# File 'lib/review/ewbbuilder.rb', line 165

def vspace
  print %Q(
//h)
end