Class: Md2site::HTMLUtils

Inherits:
Object
  • Object
show all
Defined in:
lib/md2site/htmlutils0.rb,
lib/md2site/htmlutils.rb

Overview

HTMLファイル分割クラス

Instance Method Summary collapse

Constructor Details

#initialize(htmlfname, mes) ⇒ void

初期化

Parameters:

  • htmlfname (String)

    分割対象HTMLファイル名

  • mes (Messagex)

    Messagexクラスのインスタンス



13
14
15
16
# File 'lib/md2site/htmlutils0.rb', line 13

def initialize(htmlfname, mes)
  @htmlfname = htmlfname
  @mes = mes
end

Instance Method Details

#divide_htmlArray

HTMLファイル分割

Returns:

  • (Array)

    第0要素:HTML先頭部 第1要素:HTML中間部 第2要素:HTML末尾部



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

def divide_html
  part_struct = Struct.new(:lines, :status)
  @part0 = part_struct.new([], false)
  @part1 = part_struct.new([], false)
  @part2 = part_struct.new([], false)

  encoding = Nkfguess.guess_file(@htmlfname, @mes)
  mode = "r:#{encoding}"
  f = @mes.exc_file_gets(@htmlfname) { File.open(@htmlfname, mode) }

  @lines = []
  @lineno = 0
  state = :IN_PART0
  while (line = @mes.exc_file_gets(@htmlfname) { f.gets })
    state = parse_line(state, line)
  end
  @part2.lines << @lines

  @mes.exc_file_close(@htmlfname) { f.close }

  [@part0.lines.flatten.join("\n"),
   @part1.lines.flatten.join("\n"),
   @part2.lines.flatten.join("\n")]
end

#parse_line(state, line) ⇒ Symbol

1行解析

Parameters:

  • state (Symbol)

    分割対象HTMLファイル名

  • line (String)

    解析対象行

Returns:

  • (Symbol)

    構文解析上の次の状態



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/md2site/htmlutils0.rb', line 24

def parse_line(state, line)
  event = get_event(line)

  @mes.output_debug("#{@lineno}|#{state}|#{event}|#{line}")

  case state
  when :IN_PART0
    next_state = state_in_part0(event, line)
  when :IN_PART1
    next_state = state_in_part1(event, line)
  when :IN_PART2
    next_state = state_in_part2(event, line)
  else
    @mes.output_fatal("state=#{state}")
    @mes.output_fatal("event=#{event}")
    raise
  end
  next_state
end