Class: HTML

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(string = "") ⇒ HTML

Returns a new instance of HTML.



12
13
14
15
16
17
# File 'lib/html.rb', line 12

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

Instance Attribute Details

#stringObject

Returns the value of attribute string.



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

def string
  @string
end

#strip_decoration_tagObject

Returns the value of attribute strip_decoration_tag.



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

def strip_decoration_tag
  @strip_decoration_tag
end

Instance Method Details

#b_to_aozora(text = @string) ⇒ Object



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

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

#br_to_aozora(text = @string) ⇒ Object



53
54
55
# File 'lib/html.rb', line 53

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

#delete_tag(text = @string) ⇒ Object



49
50
51
# File 'lib/html.rb', line 49

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

#i_to_aozora(text = @string) ⇒ Object



72
73
74
# File 'lib/html.rb', line 72

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

#img_to_aozora(text = @string) ⇒ Object



80
81
82
83
84
85
86
87
88
89
# File 'lib/html.rb', line 80

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



57
58
59
60
61
62
63
64
65
66
# File 'lib/html.rb', line 57

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



76
77
78
# File 'lib/html.rb', line 76

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

#set_illust_setting(options) ⇒ Object

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



22
23
24
25
26
27
28
29
30
# File 'lib/html.rb', line 22

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

青空文庫形式に変換



35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/html.rb', line 35

def to_aozora
  @string = br_to_aozora
  @string = ruby_to_aozora
  unless @strip_decoration_tag
    @string = b_to_aozora
    @string = i_to_aozora
    @string = s_to_aozora
  end
  @string = img_to_aozora
  @string = delete_tag
  @string = Helper.restor_entity(@string)
  @string
end