Class: Jekyll::Emoji

Inherits:
Object
  • Object
show all
Defined in:
lib/jekyll-openmoji/plugin.rb,
lib/jekyll-openmoji/version.rb

Constant Summary collapse

OPENMOJI_ASSET_HOST_URL =

Extend negarmoji emoji class.

"https://cdn.jsdelivr.net/gh/azadeh-afzar/OpenMoji-Jekyll-Plugin@latest"
ASSET_PATH =
"/images/color/svg"
DEFAULT_DIR =
"/emoji"
FILE_NAME =
"/:file_name"
DEFAULT_EXTENSION =
"svg"
BODY_START_TAG =
"<body"
OPENING_BODY_TAG_REGEX =
%r!<body(.*?)>\s*!m.freeze
VERSION =
"0.1.0"

Class Method Summary collapse

Class Method Details

.emoji_asset_path(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.asset.

If emoji.asset isn’t defined, its value will explicitly set to “emoji”

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

Returns a string to use as the asset path. Defaults to the ASSET_PATH.



102
103
104
105
106
107
108
109
110
111
112
# File 'lib/jekyll-openmoji/plugin.rb', line 102

def emoji_asset_path(config = {})
  if config.key?("emoji") && config["emoji"].key?("src")
    if config["emoji"].key?("asset")
      config["emoji"]["asset"].chomp("/") + FILE_NAME.to_s
    else
      "#{DEFAULT_DIR}#{FILE_NAME}"
    end
  else
    "#{ASSET_PATH}#{FILE_NAME}"
  end
end

.emoji_extension(config = {}) ⇒ Object

Public: return extension for emoji files. The custom emoji extension can be defined in the config as emoji.extension.

If emoji.extension isn’t defined, its value will explicitly set to “svg”

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

Returns a string to use as the extension. Defaults to the DEFAULT_EXTENSION.



123
124
125
126
127
128
129
# File 'lib/jekyll-openmoji/plugin.rb', line 123

def emoji_extension(config = {})
  if config.key?("emoji") && config["emoji"].key?("extension")
    config["emoji"]["extension"]
  else
    DEFAULT_EXTENSION.to_s
  end
end

.emoji_src_root(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 cdn.jsdelivr.net/gh/azadeh-afzar/OpenMoji-Jekyll-Plugin@latest.



85
86
87
88
89
90
91
# File 'lib/jekyll-openmoji/plugin.rb', line 85

def emoji_src_root(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)


136
137
138
139
# File 'lib/jekyll-openmoji/plugin.rb', line 136

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

.emojify(doc) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/jekyll-openmoji/plugin.rb', line 20

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

  doc.output = if doc.output.include? BODY_START_TAG
                 replace_document_body(doc)
               else
                 src_root = emoji_src_root(doc.site.config)
                 asset_path = emoji_asset_path(doc.site.config)
                 file_extension = emoji_extension(doc.site.config)
                 filter_with_emoji(src_root, asset_path, file_extension)
                   .call(doc.output)[:output].to_s
               end
end

.filter_with_emoji(src_root, asset_path, file_extension) ⇒ Object

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

src_root - the asset root URL (e.g. cdn.jsdelivr.net/gh/azadeh-afzar/OpenMoji-Jekyll-Plugin@latest) asset_path - the asset sub-path of src (e.g. “/images/color/svg”)

default = “emoji”

extension - the extension of emoji image files, [default = svg ]

examples of _config.yml:

1. user provided all URLs:
    emoji:
        src: https://example.com/asset
        asset: /images/png
emoji files will serve from https://example.com/asset/images/png

2. user provided just src:
    emoji:
        src: https://example.com/asset
emoji files will serve from https://example.com/emoji

3. user provided nothing:
emoji files will serve from https://cdn.jsdelivr.net/gh/azadeh-afzar/OpenMoji-Jekyll-Plugin@latest/images/color/svg

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



60
61
62
63
64
65
66
67
# File 'lib/jekyll-openmoji/plugin.rb', line 60

def filter_with_emoji(src_root, asset_path, file_extension)
  filters[src_root] ||= HTML::Pipeline.new([HTML::Pipeline::
          NegarMojiHtmlPipeline::NegarehEmojiFilter],
                                           :asset_root => src_root,
                                           :asset_path => asset_path,
                                           :extension  => file_extension,
                                           :img_attrs  => { :align => nil })
end

.filtersObject

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



71
72
73
# File 'lib/jekyll-openmoji/plugin.rb', line 71

def filters
  @filters ||= {}
end