Class: Middleman::Gemoji::Converter

Inherits:
Object
  • Object
show all
Defined in:
lib/middleman-gemoji/converter.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(app, options) ⇒ Converter

Returns a new instance of Converter.



8
9
10
11
12
13
14
15
16
# File 'lib/middleman-gemoji/converter.rb', line 8

def initialize(app, options)
  if !app.is_a?(Middleman::Application)
    raise "app is not Middleman::Application"
  end

  @app = app
  @options = options
  set_base_path
end

Instance Attribute Details

#appObject (readonly)

Returns the value of attribute app.



6
7
8
# File 'lib/middleman-gemoji/converter.rb', line 6

def app
  @app
end

#base_pathObject (readonly)

Returns the value of attribute base_path.



6
7
8
# File 'lib/middleman-gemoji/converter.rb', line 6

def base_path
  @base_path
end

#optionsObject (readonly)

Returns the value of attribute options.



6
7
8
# File 'lib/middleman-gemoji/converter.rb', line 6

def options
  @options
end

Instance Method Details

#convert(content) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/middleman-gemoji/converter.rb', line 18

def convert(content)
  return content if content.blank?

  content.to_str.gsub(/:([\w+-]+):/) do |match|
    emoji = Emoji.find_by_alias($1)
    if emoji
      image = []
      image << %(alt="#{$1}")
      image << src(emoji.image_filename)
      image << size if size
      image << style if style

      %(<img class="gemoji" #{image.join(' ').strip} />)
    else
      match
    end
  end
end

#emoji_file_exist?Boolean

Returns:

  • (Boolean)


49
50
51
52
53
# File 'lib/middleman-gemoji/converter.rb', line 49

def emoji_file_exist?
  Dir.exist?(
    File.join(@app.root, @app.config.source, @options[:emoji_dir], 'unicode')
  )
end

#set_base_pathObject



55
56
57
58
59
60
61
62
63
64
# File 'lib/middleman-gemoji/converter.rb', line 55

def set_base_path
  @base_path = 'https://assets-cdn.github.com/images/icons/emoji/'
  if emoji_file_exist?
    if /^http/ =~ @app.config[:http_prefix]
      @base_path = File.join(app.config[:http_prefix], options[:emoji_dir])
    else
      @base_path = File.join('/', app.config[:http_prefix], options[:emoji_dir])
    end
  end
end

#sizeObject



41
42
43
# File 'lib/middleman-gemoji/converter.rb', line 41

def size
  %(width="#{@options[:size]}" height="#{@options[:size]}") if @options[:size]
end

#src(path) ⇒ Object



37
38
39
# File 'lib/middleman-gemoji/converter.rb', line 37

def src(path)
  %(src="#{File.join(@base_path, path)}")
end

#styleObject



45
46
47
# File 'lib/middleman-gemoji/converter.rb', line 45

def style
  %(style="#{@options[:style]}") if @options[:style]
end