Class: HTML
- Inherits:
-
Object
- Object
- HTML
- Defined in:
- lib/html.rb
Instance Attribute Summary collapse
-
#string ⇒ Object
Returns the value of attribute string.
Instance Method Summary collapse
- #b_to_aozora(text = @string) ⇒ Object
- #br_to_aozora(text = @string) ⇒ Object
- #delete_tag(text = @string) ⇒ Object
- #i_to_aozora(text = @string) ⇒ Object
- #img_to_aozora(text = @string) ⇒ Object
-
#initialize(string = "") ⇒ HTML
constructor
A new instance of HTML.
- #ruby_to_aozora(text = @string) ⇒ Object
- #s_to_aozora(text = @string) ⇒ Object
-
#set_illust_setting(options) ⇒ Object
挿絵を置換するための設定を変更する.
-
#to_aozora ⇒ Object
青空文庫形式に変換.
Constructor Details
#initialize(string = "") ⇒ HTML
Returns a new instance of HTML.
11 12 13 14 15 |
# File 'lib/html.rb', line 11 def initialize(string = "") @string = string @illust_current_url = nil @illust_grep_pattern = /<img.+?src=\"(?<src>.+?)\".*?>/i end |
Instance Attribute Details
#string ⇒ Object
Returns the value of attribute string.
9 10 11 |
# File 'lib/html.rb', line 9 def string @string end |
Instance Method Details
#b_to_aozora(text = @string) ⇒ Object
64 65 66 |
# File 'lib/html.rb', line 64 def b_to_aozora(text = @string) text.gsub(/<b>/i, "[#太字]").gsub(/<\/b>/i, "[#太字終わり]") end |
#br_to_aozora(text = @string) ⇒ Object
49 50 51 |
# File 'lib/html.rb', line 49 def br_to_aozora(text = @string) text.gsub(/[\r\n]+/, "").gsub(/<br.*?>/i, "\n") end |
#delete_tag(text = @string) ⇒ Object
45 46 47 |
# File 'lib/html.rb', line 45 def delete_tag(text = @string) text.gsub(/<.+?>/, "") end |
#i_to_aozora(text = @string) ⇒ Object
68 69 70 |
# File 'lib/html.rb', line 68 def i_to_aozora(text = @string) text.gsub(/<i>/i, "[#斜体]").gsub(/<\/i>/i, "[#斜体終わり]") end |
#img_to_aozora(text = @string) ⇒ Object
76 77 78 79 80 81 82 83 84 85 |
# File 'lib/html.rb', line 76 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
53 54 55 56 57 58 59 60 61 62 |
# File 'lib/html.rb', line 53 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
72 73 74 |
# File 'lib/html.rb', line 72 def s_to_aozora(text = @string) text.gsub(/<s>/i, "[#取消線]").gsub(/<\/s>/i, "[#取消線終わり]") end |
#set_illust_setting(options) ⇒ Object
挿絵を置換するための設定を変更する
20 21 22 23 24 25 26 27 28 |
# File 'lib/html.rb', line 20 def set_illust_setting() unless .kind_of?(Hash) raise ArgumentError, "invalid parameter(s), need Hash" end @illust_current_url = [:current_url] if [:current_url] if grep_pattern = [:grep_pattern] @illust_grep_pattern = grep_pattern.kind_of?(Regexp) ? grep_pattern : /#{grep_pattern}/m end end |
#to_aozora ⇒ Object
青空文庫形式に変換
33 34 35 36 37 38 39 40 41 42 43 |
# File 'lib/html.rb', line 33 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 = Helper.restor_entity(@string) @string end |