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.



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

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.



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

def string
  @string
end

#strip_decoration_tagObject

Returns the value of attribute strip_decoration_tag.



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

def strip_decoration_tag
  @strip_decoration_tag
end

Instance Method Details

#b_to_aozora(text = @string) ⇒ Object



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

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

#br_to_aozora(text = @string) ⇒ Object



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

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

#clearObject



21
22
23
# File 'lib/html.rb', line 21

def clear
  @string = +""
end

#delete_ruby_tag(text = @string) ⇒ Object



112
113
114
# File 'lib/html.rb', line 112

def delete_ruby_tag(text = @string)
  text.gsub(%r!<\/?(?:ruby|rb|rp|rt)>!, "")
end

#delete_tag(text = @string) ⇒ Object



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

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

#em_to_sesame(text = @string) ⇒ Object



108
109
110
# File 'lib/html.rb', line 108

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

#i_to_aozora(text = @string) ⇒ Object



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

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

#img_to_aozora(text = @string) ⇒ Object



97
98
99
100
101
102
103
104
105
106
# File 'lib/html.rb', line 97

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

#ln_to_br(text = @string) ⇒ Object



116
117
118
# File 'lib/html.rb', line 116

def ln_to_br(text = @string)
  text.to_s.gsub("\n", "<br>")
end

#p_to_aozora(text = @string) ⇒ Object

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



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

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

#ruby_to_aozora(text = @string) ⇒ Object



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

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



93
94
95
# File 'lib/html.rb', line 93

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

#set_illust_setting(options) ⇒ Object

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



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

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_aozora(pre_html: false) ⇒ Object

青空文庫形式に変換



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

def to_aozora(pre_html: false)
  @string = br_to_aozora unless pre_html
  @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.restore_entity(@string)
  @string
end