Class: HatenablogPublisher::Generator::Body

Inherits:
Object
  • Object
show all
Defined in:
lib/hatenablog_publisher/generator/body.rb

Constant Summary collapse

IMAGE_PATTERN =
/[^`]!\[.*\]\((.*)\)/.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(context, options) ⇒ Body

Returns a new instance of Body.



8
9
10
11
# File 'lib/hatenablog_publisher/generator/body.rb', line 8

def initialize(context, options)
  @context = context
  @options = options
end

Instance Attribute Details

#contextObject (readonly)

Returns the value of attribute context.



4
5
6
# File 'lib/hatenablog_publisher/generator/body.rb', line 4

def context
  @context
end

#optionsObject (readonly)

Returns the value of attribute options.



4
5
6
# File 'lib/hatenablog_publisher/generator/body.rb', line 4

def options
  @options
end

Instance Method Details

#generateObject



13
14
15
16
17
18
19
# File 'lib/hatenablog_publisher/generator/body.rb', line 13

def generate
  markdown = @context.text.dup

  replaced_markdown = remove_textlint_comment(replace_image(markdown))

  CGI.escapeHTML(replaced_markdown)
end

#remove_textlint_comment(markdown) ⇒ Object



21
22
23
# File 'lib/hatenablog_publisher/generator/body.rb', line 21

def remove_textlint_comment(markdown)
  markdown.gsub(/^<!--\s+textlint-(enable|disable).*-->\n/, '')
end

#replace_image(markdown) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
# File 'lib/hatenablog_publisher/generator/body.rb', line 25

def replace_image(markdown)
  markdown.gsub(IMAGE_PATTERN) do |s|
    image_name = Regexp.last_match(1)

    if image_name.match('^http')
      s
    else
      "\n\n" + @context.image_syntax(image_name)
    end
  end
end