Class: Jekyll::Emoji

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

Constant Summary collapse

GITHUB_DOT_COM_ASSET_HOST_URL =
"https://github.githubassets.com"
ASSET_PATH =
"/images/icons/"
BODY_START_TAG =
"<body"
OPENING_BODY_TAG_REGEX =
%r!<body(.*?)>\s*!.freeze

Class Method Summary collapse

Class Method Details

.emoji_src(config = {}) ⇒ Object

Public: Calculate the asset root source for the given config. The custom emoji asset root can be defined in the config as emoji.src, and must be a valid URL (i.e. it must include a protocol and valid domain)

config - the hash-like configuration of the document’s site

Returns a full URL to use as the asset root URL. Defaults to the root URL for assets provided by an ASSET_HOST_URL environment variable, otherwise the root URL for emoji assets at assets-cdn.github.com.



53
54
55
56
57
58
59
# File 'lib/jemoji.rb', line 53

def emoji_src(config = {})
  if config.key?("emoji") && config["emoji"].key?("src")
    config["emoji"]["src"]
  else
    default_asset_root
  end
end

.emojiable?(doc) ⇒ Boolean

Public: Defines the conditions for a document to be emojiable.

doc - the Jekyll::Document or Jekyll::Page

Returns true if the doc is written & is HTML.

Returns:

  • (Boolean)


66
67
68
69
# File 'lib/jemoji.rb', line 66

def emojiable?(doc)
  (doc.is_a?(Jekyll::Page) || doc.write?) &&
    doc.output_ext == ".html" || (doc.permalink&.end_with?("/"))
end

.emojify(doc) ⇒ Object



15
16
17
18
19
20
21
22
23
24
# File 'lib/jemoji.rb', line 15

def emojify(doc)
  return unless doc.output =~ HTML::Pipeline::EmojiFilter.emoji_pattern

  doc.output = if doc.output.include? BODY_START_TAG
                 replace_document_body(doc)
               else
                 src = emoji_src(doc.site.config)
                 filter_with_emoji(src).call(doc.output)[:output].to_s
               end
end

.filter_with_emoji(src) ⇒ Object

Public: Create or fetch the filter for the given {src} asset root.

src - the asset root URL (e.g. github.githubassets.com/images/icons/)

Returns an HTML::Pipeline instance for the given asset root.



31
32
33
34
35
# File 'lib/jemoji.rb', line 31

def filter_with_emoji(src)
  filters[src] ||= HTML::Pipeline.new([
    HTML::Pipeline::EmojiFilter,
  ], :asset_root => src, :img_attrs => { :align => nil })
end

.filtersObject

Public: Filters hash where the key is the asset root source. Effectively a cache.



39
40
41
# File 'lib/jemoji.rb', line 39

def filters
  @filters ||= {}
end