Module: WebLoader::Utils
Constant Summary collapse
- UTF_8 =
'UTF-8'
Class Method Summary collapse
- .detect_charset(str) ⇒ Object
- .to_redirect_url(orig_uri, location) ⇒ Object
- .toutf8(str, charset) ⇒ Object
- .toutf8_charset(str, charset) ⇒ Object
Class Method Details
.detect_charset(str) ⇒ Object
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
# File 'lib/web_loader/utils.rb', line 5 def detect_charset(str) # charsetが指定されていない場合内容からcharsetを判定する # https://learn.microsoft.com/en-us/windows/release-health/status-windows-11-22h2 の場合この処理がないと文字化け # charsetがサーバーから返されず、ASCII-8BITとして判定される。それをKconv.toutf8で変換すると文字化けする # metaタグのcharsetはUTF-8なのでこれを使えば正しいはず charset = nil # Nokogiriの場合 https://qiita.com/tetoralynx/items/273560ad6f75bb685935 # <meta\s)(.*)(charset\s*=\s*([\w-]+))(.*)/i if str =~ /<meta.*?charset=["']*([^"']+)/i charset = $1 end if charset =~ /Shift_JIS/i # Shift_JISの場合、実際はWindows-31J(Windowsの標準コードの場合が多いはず) charset = "Windows-31J" end charset end |
.to_redirect_url(orig_uri, location) ⇒ Object
72 73 74 75 76 77 78 |
# File 'lib/web_loader/utils.rb', line 72 def to_redirect_url(orig_uri, location) redirect_url = location if location =~ /^\// redirect_url = URI.join(orig_uri, location).to_s end redirect_url end |
.toutf8(str, charset) ⇒ Object
54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 |
# File 'lib/web_loader/utils.rb', line 54 def toutf8(str, charset) # 2022/04/04(月) # GITHUBのアポストロフィ(’ U+2019)が文字化け問題に対処するために新設。 # 原因は直接Kconv.toutf8にresponse.bodyをわたしていたことなので(Kconvのguessが失敗していたと思われる)、 # response.type_paramsを見てそれにforce_encodingすることで対処する。渡されているcharsetとWebページの文字コードが一致していればこれで問題はないはず。 result = nil begin # 指定されたcharsetで変換する result = toutf8_charset(str.dup, charset) # charsetによる変換が失敗した場合Kconvを使用 result = Kconv.toutf8(str) if result.nil? rescue => ex puts ex. end result end |
.toutf8_charset(str, charset) ⇒ Object
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
# File 'lib/web_loader/utils.rb', line 25 def toutf8_charset(str, charset) # charsetが指定されていない場合はnil if charset.to_s.length == 0 charset = detect_charset(str) end if charset.to_s.length == 0 return nil end result = nil begin # 文字列のcharsetを変更する str.force_encoding(charset) # 例外が発生する場合あり。例えば"Shift_JIS"ではなく"Shift-JIS"が渡された場合。 # force_encodingが失敗した場合はnil return nil unless str.valid_encoding? result = nil if charset =~ /#{UTF_8}/i result = str else # エンコーディングがUTF8じゃない場合変換する result = str.encode(UTF_8, invalid: :replace, undef: :replace) end rescue => ex puts ex. end result end |