Help us understand the problem. What is going on with this article?

Sublime Text | emoji を snippet 化して楽に入力する

More than 5 years have passed since last update.

Sublime Text | emoji を snippet 化して楽に入力する

概要

Sublime Text で emoji を楽に入力するために、snippet を作成します。

Snippet 生成スクリプト

仕様

  • EMOJI CHEAT SHEET の Web サイトからスクレイピングで emoji 名を取得する
    • 抽出条件は span タグ かつ class が name のもの
  • 1 emoji 1 ファイルで Snippet を出力する
    • ファイル名は < emoji 名>.sublime-snippet
    • ファイル内容は下記 ( :smile: の例)
<snippet>
  <content><![CDATA[
:smile:
]]></content>
  <tabTrigger>em_smile</tabTrigger>
  <scope>text.html.markdown</scope>
  <description>emoji :smile:</description>
</snippet>
  • 本当は 「em_smile」 ではなく 「:smile:」 をトリガーにしたかったが、動作しなかったため 「em_smile」 にした
  • ついでに全アイコンとアイコン名の表示サンプル HTML を出力する

Snippet 生成スクリプト

require 'open-uri'
require 'nokogiri'
require 'erb'

module Emoji
  module SublimeText
    URL = 'http://www.emoji-cheat-sheet.com/'
    OUTPUT_DIR = 'emoji_snippets'
    OUTPUT_SAMPLE_DIR = 'emoji_samples'

    class Generator
      def self.bulk_output(klasses)
        Dir.mkdir(OUTPUT_DIR) unless Dir.exist?(OUTPUT_DIR)
        klasses.each do |klass|
          # 記号付きの emoji 名は例外的な処理が必要なので手動で対応
          next if %w(+1 -1).include?(klass)
          generator = Generator.new(klass)
          snippet = generator.apply_snippet
          generator.output_snippets(OUTPUT_DIR, snippet)
        end
      end

      def initialize(klass)
        @klass = klass
      end

      def apply_snippet
        klass = @klass
        template =<<-EOS
<snippet>
  <content><![CDATA[
:<%=klass%>:
]]></content>
  <tabTrigger>em_<%=klass%></tabTrigger>
  <scope>text.html.markdown</scope>
  <description>emoji <%=klass%></description>
</snippet>
        EOS
        ERB.new(template).result(binding)
      end

      def output_snippets(output_dir, snippet)
        File.open("./#{output_dir}/#{@klass}.sublime-snippet", "w:utf-8") do |e|
          e.puts(snippet)
        end
      end
    end
  end
end

charset = nil
html = open(Emoji::SublimeText::URL) do |f|
  charset = f.charset
  f.read
end

doc = Nokogiri::HTML.parse(html, nil, charset)
klasses = doc.xpath('//span[@class="name"]').map(&:text)
Emoji::SublimeText::Generator.bulk_output(klasses)

出力内容

  • Snippet 出力例
<snippet>
  <content><![CDATA[
:smile:
]]></content>
  <tabTrigger>em_smile</tabTrigger>
  <scope>text.html.markdown</scope>
  <description>emoji smile</description>
</snippet>

Sublime Text Emoji Snippet

GitHub に公開しておきました。使い方はコピーして %Packages%User 配下にぶち込んでご利用ください。

参照

Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Comments
No comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
ユーザーは見つかりませんでした