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.



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

def initialize(string = "")
  self.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.



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

def strip_decoration_tag
  @strip_decoration_tag
end

Instance Method Details

#b_to_aozora(text = @string) ⇒ Object



80
81
82
# File 'lib/html.rb', line 80

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

#br_to_aozora(text = @string) ⇒ Object



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

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

#delete_tag(text = @string) ⇒ Object



56
57
58
# File 'lib/html.rb', line 56

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

#em_to_sesame(text = @string) ⇒ Object



103
104
105
# File 'lib/html.rb', line 103

def em_to_sesame(text = @string)
  text.gsub(%r!<em class="emphasisDots">(.+?)</em>!, "[#傍点]\\1[#傍点終わり]")
end

#i_to_aozora(text = @string) ⇒ Object



84
85
86
# File 'lib/html.rb', line 84

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

#img_to_aozora(text = @string) ⇒ Object



92
93
94
95
96
97
98
99
100
101
# File 'lib/html.rb', line 92

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

#p_to_aozora(text = @string) ⇒ Object

p タグで段落を作ってる場合(brタグが無い場合)に改行されるように



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

def p_to_aozora(text = @string)
  text.gsub(/\n?<\/p>/i, "\n")
end

#ruby_to_aozora(text = @string) ⇒ Object



69
70
71
72
73
74
75
76
77
78
# File 'lib/html.rb', line 69

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



88
89
90
# File 'lib/html.rb', line 88

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

#set_illust_setting(options) ⇒ Object

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



27
28
29
30
31
32
33
34
35
# File 'lib/html.rb', line 27

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

青空文庫形式に変換



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/html.rb', line 40

def to_aozora
  @string = br_to_aozora
  @string = p_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 = em_to_sesame
  @string = delete_tag
  @string = Helper.restor_entity(@string)
  @string
end