Class: Growi::ImageConverter::Body

Inherits:
Object
  • Object
show all
Defined in:
lib/growi/image_converter/body.rb

Overview

markdown bodyを扱うクラス

Constant Summary collapse

REGEX_SPACE =
'[ \t]'
REGEX_URL_PREFIX_ESA =
'https?://img.esa.io/'
REGEX_SPACE_OR_RETURN =
"(?:#{REGEX_SPACE}*?\\n)?#{REGEX_SPACE}*?"
REGEX_SPACE_GE_1_OR_RETURN =
"(?:#{REGEX_SPACE}+?|#{REGEX_SPACE_OR_RETURN})"
REGEX_TITLE =
"(?:#{REGEX_SPACE_GE_1_OR_RETURN}\".*?\")?"
FILE_PATH_PREFIX =
'/attachment/'

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(body) ⇒ Body

Returns a new instance of Body.



15
16
17
18
# File 'lib/growi/image_converter/body.rb', line 15

def initialize(body)
  @body = body
  @markdown_images = []
end

Instance Attribute Details

#bodyObject

Returns the value of attribute body.



19
20
21
# File 'lib/growi/image_converter/body.rb', line 19

def body
  @body
end

#markdown_imagesObject

Returns the value of attribute markdown_images.



19
20
21
# File 'lib/growi/image_converter/body.rb', line 19

def markdown_images
  @markdown_images
end

Instance Method Details

#group_by_urlObject



48
49
50
51
52
53
54
# File 'lib/growi/image_converter/body.rb', line 48

def group_by_url
  group_by_url = {}
  markdown_images.each do |markdown_image|
    (group_by_url[markdown_image.url] ||= []).push(markdown_image)
  end
  group_by_url
end

#replace_markdown_image(attached_file) ⇒ Object



56
57
58
59
60
61
# File 'lib/growi/image_converter/body.rb', line 56

def replace_markdown_image(attached_file)
  attached_file_path = FILE_PATH_PREFIX + attached_file.data[:attachment]._id
  attached_file.markdown_images.each do |markdown_image|
    body.sub! markdown_image.syntax, markdown_image.replace_url(attached_file_path)
  end
end

#scan_markdown_image_esaObject



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/growi/image_converter/body.rb', line 21

def scan_markdown_image_esa
  matches = []

  # Image syntax inline
  matches.concat(body.scan(
                   /
                   !\[#{REGEX_SPACE_OR_RETURN}.*?#{REGEX_SPACE_OR_RETURN}\]
                   \(#{REGEX_SPACE_OR_RETURN}
                   #{REGEX_URL_PREFIX_ESA}.*?#{REGEX_TITLE}
                   #{REGEX_SPACE_OR_RETURN}\)
                   /x
                 ))

  # Image syntax reference-style
  matches.concat(body.scan(
                   /
                   \[#{REGEX_SPACE_OR_RETURN}.*?#{REGEX_SPACE_OR_RETURN}\]:
                   #{REGEX_SPACE_OR_RETURN}#{REGEX_URL_PREFIX_ESA}.*
                   /x
                 ))

  # Image syntax img tag
  matches.concat(body.scan(/<img#{REGEX_SPACE_GE_1_OR_RETURN}.*?#{REGEX_URL_PREFIX_ESA}.*?>/m))

  @markdown_images = matches.map { |match| Growi::ImageConverter::MarkdownImage.new match }
end