Class: HTML

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

Overview

Copyright 2013 whiteleaf. All rights reserved.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(string = "") ⇒ HTML

Returns a new instance of HTML.



9
10
11
12
13
# File 'lib/html.rb', line 9

def initialize(string = "")
  @string = string
  @illust_current_url = nil
  @illust_grep_pattern = /<img.+?src=\"(?<src>.+?)\".*?>/i
end

Instance Attribute Details

#stringObject

Returns the value of attribute string.



7
8
9
# File 'lib/html.rb', line 7

def string
  @string
end

Instance Method Details

#b_to_aozora(text = @string) ⇒ Object



61
62
63
# File 'lib/html.rb', line 61

def b_to_aozora(text = @string)
  text.gsub(/<b>/i, "[#太字]").gsub(/<\/b>/i, "[#太字終わり]")
end

#br_to_aozora(text = @string) ⇒ Object



46
47
48
# File 'lib/html.rb', line 46

def br_to_aozora(text = @string)
  text.gsub(/[\r\n]+/, "").gsub(/<br.*?>/i, "\n")
end

#delete_tag(text = @string) ⇒ Object



42
43
44
# File 'lib/html.rb', line 42

def delete_tag(text = @string)
  text.gsub(/<.+?>/, "")
end

#i_to_aozora(text = @string) ⇒ Object



65
66
67
# File 'lib/html.rb', line 65

def i_to_aozora(text = @string)
  text.gsub(/<i>/i, "[#斜体]").gsub(/<\/i>/i, "[#斜体終わり]")
end

#img_to_aozora(text = @string) ⇒ Object



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

def img_to_aozora(text = @string)
  if @illust_grep_pattern
    text.gsub(@illust_grep_pattern) do
      url = @illust_current_url ? URI.join(@illust_current_url, $~[:src]) : $~[:src]
      "[#挿絵(#{url})入る]"
    end
  else
    text
  end
end

#ruby_to_aozora(text = @string) ⇒ Object



50
51
52
53
54
55
56
57
58
59
# File 'lib/html.rb', line 50

def ruby_to_aozora(text = @string)
  text.tr("《》", "≪≫")
      .gsub(/<ruby>(.+?)<\/ruby>/i) do
    splited_ruby = $1.split(/<rt>/i)
    next delete_tag(splited_ruby[0]) unless splited_ruby[1]
    ruby_base = delete_tag(splited_ruby[0].split(/<rp>/i)[0])
    ruby_text = delete_tag(splited_ruby[1].split(/<rp>/i)[0])
    "#{ruby_base}#{ruby_text}"
  end
end

#s_to_aozora(text = @string) ⇒ Object



69
70
71
# File 'lib/html.rb', line 69

def s_to_aozora(text = @string)
  text.gsub(/<s>/i, "[#取消線]").gsub(/<\/s>/i, "[#取消線終わり]")
end

#set_illust_setting(options) ⇒ Object

挿絵を置換するための設定を変更する



18
19
20
21
22
23
24
25
26
# File 'lib/html.rb', line 18

def set_illust_setting(options)
  unless options.kind_of?(Hash)
    raise ArgumentError, "invalid parameter(s), need Hash"
  end
  @illust_current_url = options[:current_url] if options[:current_url]
  if grep_pattern = options[:grep_pattern]
    @illust_grep_pattern = grep_pattern.kind_of?(Regexp) ? grep_pattern : /#{grep_pattern}/m
  end
end

#to_aozoraObject

青空文庫形式に変換



31
32
33
34
35
36
37
38
39
40
# File 'lib/html.rb', line 31

def to_aozora
  @string = br_to_aozora
  @string = ruby_to_aozora
  @string = b_to_aozora
  @string = i_to_aozora
  @string = s_to_aozora
  @string = img_to_aozora
  @string = delete_tag
  @string
end