Class: Illustration

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

Overview

挿絵管理

Defined Under Namespace

Classes: UnknownMIMEType

Constant Summary collapse

ILLUST_DIR =
"挿絵/"
NAROU_ILLUST_URL =
"http://%s.mitemin.net/userpageimage/viewimage/icode/%s/"
NAROU_ILLUST_TAG_PATTERN =
/[  \t]*?<(i[0-9]+)\|([0-9]+)>\n?/m
MIME =
{ "image/jpeg" => "jpg", "image/png" => "png", "image/gif" => "gif", "image/bmp" => "bmp" }

Instance Method Summary collapse

Constructor Details

#initialize(setting, inspector) ⇒ Illustration

Returns a new instance of Illustration.



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

def initialize(setting, inspector)
  @setting = setting
  @inspector = inspector
end

Instance Method Details

#create_illust_path(basename) ⇒ Object



78
79
80
81
82
# File 'lib/illustration.rb', line 78

def create_illust_path(basename)
  illust_abs_dir = File.join(@setting.archive_path, ILLUST_DIR)
  Dir.mkdir(illust_abs_dir) unless File.exist?(illust_abs_dir)
  File.join(illust_abs_dir, basename)
end

#download_image(url, basename = nil) ⇒ Object

画像のURLからデータを保存して、保存したファイルの絶対パスを返す



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/illustration.rb', line 49

def download_image(url, basename = nil)
  basename = File.basename(basename ? basename : url, ".*")
  if path = search_image(basename)
    return path
  end
  open(url, make_open_uri_options(allow_redirections: :safe)) do |fp|
    content_type = fp.meta["content-type"]
    ext = MIME[content_type] or raise UnknownMIMEType, content_type
    illust_abs_path = create_illust_path(basename) + "." + ext
    open(illust_abs_path, "wb") do |write_fp|
      write_fp.write(fp.read)
    end
    @inspector.info("挿絵「#{File.basename(illust_abs_path)}」を保存しました。")
    return illust_abs_path
  end
rescue UnknownMIMEType => e
  @inspector.error("Illustration#download_image: #{url} は未対応の画像フォーマットです" +
                   "(content-type: #{e.message})")
  nil
rescue StandardError => e
  @inspector.error("Illustration#download_image: #{url} を処理中に例外が発生しました(#{e})")
  nil
end

#make_illust_chuki(illust_path) ⇒ Object



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

def make_illust_chuki(illust_path)
  rel_illust_path = to_rel(@setting.archive_path, illust_path)
  "[#挿絵(#{rel_illust_path})入る]"
end

#scanner(source, &block) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/illustration.rb', line 25

def scanner(source, &block)
  source.gsub!(/[#挿絵((.+?))入る]/) do |match|
    url = $1
    if url =~ URI.regexp
      path = download_image(url)
      path ? block.call(make_illust_chuki(path)) : ""
    else
      # URLでなければ、ローカルのパスが指定されたと判断してそのままの注記を使う
      block.call(match)
    end
  end
  source.gsub!(NAROU_ILLUST_TAG_PATTERN) do
    id1, id2 = $1, $2
    basename = "#{id1},#{id2}.*"
    url = NAROU_ILLUST_URL % [id2, id1]
    path = download_image(url, basename)
    path ? block.call(make_illust_chuki(path)) : ""
  end
  source
end

#search_image(basename) ⇒ Object



73
74
75
76
# File 'lib/illustration.rb', line 73

def search_image(basename)
  path = create_illust_path(basename) + ".*"
  Dir.glob(path)[0]
end

#to_rel(base, target) ⇒ Object

絶対パスから相対パスを作成 blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-list/36985



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

def to_rel(base, target)
  sep = /#{File::SEPARATOR}+/o
  base = base.split(sep)
  base.pop
  target = target.split(sep)
  while base.shift == target.shift
  end
  File.join([".."]*base.size+target)
end