class Md2site::HTMLUtils

HTMLファイル分割クラス

Public Class Methods

new(htmlfname, mes) click to toggle source
# File lib/md2site/htmlutils.rb, line 5
def initialize(htmlfname, mes)
  @htmlfname = htmlfname
  @mes = mes
end

Public Instance Methods

divide_html() click to toggle source
# File lib/md2site/htmlutils.rb, line 30
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) click to toggle source
# File lib/md2site/htmlutils.rb, line 10
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

Private Instance Methods

get_event(line) click to toggle source
# File lib/md2site/htmlutils.rb, line 57
def get_event(line)
  line.chomp!
  begin
    ldc = line.downcase
  rescue Error => e
    @mes.output_exception(e)
    @mes.output_fatal("Can't convert downcase #{@htmlfname}")
    exit(100)
  rescue ArgumentError => e
    @mes.output_exception(e)
    @mes.output_fatal("Can't convert downcase #{@htmlfname}")
    exit(100)
  end
  @lineno += 1
  if ldc.index("<body")
    event = :BODY_BEGIN

  elsif ldc.index("</body>")
    event = :BODY_END

  else
    event = :ELSE
  end

  event
end
state_in_part0(event, line) click to toggle source
# File lib/md2site/htmlutils.rb, line 84
def state_in_part0(event, line)
  case event
  when :BODY_BEGIN
    @part0.lines << @lines
    @lines = []
    @lines << line
    next_state = :IN_PART1
  when :BODY_END
    @lines << line
    @part1.lines << @lines
    @lines = []
    next_state = :IN_PART2
  when :ELSE
    @lines << line
    next_state = :IN_PART0
  else
    raise
  end

  next_state
end
state_in_part0_contents(event, line) click to toggle source

part0_contents部のイベント処理

@param event [Symbol] トークン種別 @param line [String] 解析対象行 @return [Symbol] 構文解析上の次の状態

# File lib/md2site/htmlutils0.rb, line 185
def state_in_part0_contents(event, line)
  case event
  when :CONTENTS_BEGIN
    @lines << line
    next_state = :IN_PART0_CONTENTS
  when :CONTENTS_END
    next_state = state_in_part0_contents_contents_end(line)
  when :ID_BODY_MAIN
    @lines << line
    @div_body_main = line
    next_state = :IN_PART0_CONTENTS
  when :ID_CSE
    @lines << line
    @div_cse = line
    next_state = :IN_PART0_CONTENTS
  when :WHITE_SPACE
    @lines << line
    next_state = :IN_PART0_CONTENTS
  when :ELSE
    @part0.lines << @lines
    if !@div_body_main.nil? && !@div_cse.nil?
      @part0.status = true
    end
    @lines = []
    @lines << line
    next_state = :IN_PART1
  else
    raise
  end
  next_state
end
state_in_part0_contents_contents_end(line) click to toggle source

part0_contents部のcontents_endイベント処理

@param line [String] 解析対象行 @return [Symbol] 構文解析上の次の状態

# File lib/md2site/htmlutils0.rb, line 167
def state_in_part0_contents_contents_end(line)
  @part1.lines << @lines
  if !@div_body_main.nil? && !@div_cse.nil?
    @part1.status = true
  end
  @lines = []
  @lines << line
  raise unless @div_body_main
  raise unless @div_cse
  :IN_PART2
end
state_in_part0_contents_end(line) click to toggle source

part0部のCONTENTS_ENDイベント処理

@param line [String] 解析対象行 @return [Symbol] 構文解析上の次の状態

# File lib/md2site/htmlutils0.rb, line 112
def state_in_part0_contents_end(line)
  @part0.lines << @lines
  if !@div_body_main.nil? && !@div_cse.nil?
    @part0.status = true
  end
  @lines = []
  @lines << line
  raise unless @div_body_main
  raise unless @div_cse
  :IN_PART2
end
state_in_part1(event, line) click to toggle source
# File lib/md2site/htmlutils.rb, line 106
def state_in_part1(event, line)
  case event
  when :BODY_BEGIN
    @lines << line
    next_state = :IN_PART1
  when :BODY_END
    @lines << line
    @part1.lines << @lines
    @lines = []
    next_state = :IN_PART2
  when :ELSE
    @lines << line
    next_state = :IN_PART1
  else
    raise
  end

  next_state
end
state_in_part1_contents_end(line) click to toggle source

part1部のcontents_endイベント処理

@param line [String] 解析対象行 @return [Symbol] 構文解析上の次の状態

# File lib/md2site/htmlutils0.rb, line 222
def state_in_part1_contents_end(line)
  @part1.lines << @lines
  if !@div_body_main.nil? && !@div_cse.nil?
    @part1.status = true
  end
  @lines = []
  @lines << line
  raise unless @div_body_main
  raise unless @div_cse
  :IN_PART2
end
state_in_part2(event, line) click to toggle source
# File lib/md2site/htmlutils.rb, line 126
def state_in_part2(event, line)
  case event
  when :BODY_BEGIN
    raise
  when :BODY_END
    raise
  when :ELSE
    @lines << line
    next_state = :IN_PART2
  else
    raise
  end

  next_state
end