├── .gitignore ├── Gemfile ├── LICENSE.txt ├── README.md ├── Rakefile ├── jekyll-autolink_email.gemspec ├── lib └── jekyll-autolink_email.rb └── test ├── helper.rb └── test_autolink_email.rb /.gitignore: -------------------------------------------------------------------------------- 1 | *.gem 2 | *.rbc 3 | .bundle 4 | .config 5 | .yardoc 6 | Gemfile.lock 7 | InstalledFiles 8 | _yardoc 9 | coverage 10 | doc/ 11 | lib/bundler/man 12 | pkg 13 | rdoc 14 | spec/reports 15 | test/tmp 16 | test/version_tmp 17 | tmp 18 | *.bundle 19 | *.so 20 | *.o 21 | *.a 22 | mkmf.log 23 | -------------------------------------------------------------------------------- /Gemfile: -------------------------------------------------------------------------------- 1 | source 'https://rubygems.org' 2 | 3 | gemspec 4 | -------------------------------------------------------------------------------- /LICENSE.txt: -------------------------------------------------------------------------------- 1 | Copyright (c) 2014 Ivan Tse 2 | 3 | MIT License 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining 6 | a copy of this software and associated documentation files (the 7 | "Software"), to deal in the Software without restriction, including 8 | without limitation the rights to use, copy, modify, merge, publish, 9 | distribute, sublicense, and/or sell copies of the Software, and to 10 | permit persons to whom the Software is furnished to do so, subject to 11 | the following conditions: 12 | 13 | The above copyright notice and this permission notice shall be 14 | included in all copies or substantial portions of the Software. 15 | 16 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 17 | EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 18 | MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 19 | NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE 20 | LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 21 | OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION 22 | WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 23 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Jekyll::AutolinkEmail 2 | 3 | Autolink emails for your Jekyll site. 4 | 5 | ## Installation 6 | 7 | Add to your `Gemfile`: 8 | 9 | ``` 10 | gem 'jekyll-autolink_email' 11 | ``` 12 | 13 | Add to your `_config.yml`: 14 | 15 | ```yml 16 | gems: 17 | - jekyll-autolink_email 18 | ``` 19 | 20 | ## Usage 21 | 22 | In any html page or post, emails will be autolinked. So if you write `foo@example.com`, the resulting html will be `foo@example.com` 23 | 24 | ## Configuration 25 | 26 | Autolinking is done by [Rinku](https://github.com/vmg/rinku) so visit that gem for a more in-depth explanation of the configuration options: 27 | 28 | ```yml 29 | autolink_email: 30 | link_attr: class='email-link' # attributes to add to the link 31 | skip_tags: ['div'] # tags to skip 32 | ``` 33 | 34 | Jekyll::AutolinkEmail also provides a `escape: true` option. Turning this on will html encode the email address and url encode the link to prevent spam. 35 | 36 | ## Contributing 37 | 38 | 1. Fork it ( https://github.com/ivantsepp/jekyll-autolink_email/fork ) 39 | 2. Create your feature branch (`git checkout -b my-new-feature`) 40 | 3. Commit your changes (`git commit -am 'Add some feature'`) 41 | 4. Push to the branch (`git push origin my-new-feature`) 42 | 5. Create a new Pull Request 43 | 44 | You should also check out [Jemoji](https://github.com/jekyll/jemoji) and [Jekyll Mentions](https://github.com/jekyll/jekyll-mentions) as those gems were the source of inspiration for this gem. 45 | -------------------------------------------------------------------------------- /Rakefile: -------------------------------------------------------------------------------- 1 | require 'bundler/gem_tasks' 2 | require 'rake/testtask' 3 | 4 | Rake::TestTask.new(:test) do |t| 5 | t.libs << 'test' 6 | t.test_files = FileList['test/test_*.rb'] 7 | t.verbose = true 8 | end 9 | 10 | -------------------------------------------------------------------------------- /jekyll-autolink_email.gemspec: -------------------------------------------------------------------------------- 1 | # coding: utf-8 2 | lib = File.expand_path('../lib', __FILE__) 3 | $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib) 4 | 5 | Gem::Specification.new do |spec| 6 | spec.name = "jekyll-autolink_email" 7 | spec.version = "0.1.0" 8 | spec.authors = ["Ivan Tse"] 9 | spec.email = ["ivan.tse1@gmail.com"] 10 | spec.summary = "Autolink emails for your Jekyll site." 11 | spec.description = "" 12 | spec.homepage = "https://github.com/ivantsepp/jekyll-autolink_email" 13 | spec.license = "MIT" 14 | 15 | spec.files = `git ls-files -z`.split("\x0") 16 | spec.test_files = spec.files.grep(%r{^(test|spec|features)/}) 17 | spec.require_paths = ["lib"] 18 | 19 | spec.add_dependency "jekyll", ['>= 3.0', '< 5.0'] 20 | spec.add_dependency "rinku", '~> 1.7.0' 21 | 22 | spec.add_development_dependency "bundler", "~> 1.6" 23 | spec.add_development_dependency "rake" 24 | spec.add_development_dependency "shoulda" 25 | spec.add_development_dependency "mocha" 26 | end 27 | -------------------------------------------------------------------------------- /lib/jekyll-autolink_email.rb: -------------------------------------------------------------------------------- 1 | require 'jekyll' 2 | require 'rinku' 3 | 4 | module Jekyll 5 | class AutolinkEmail < Jekyll::Generator 6 | 7 | HTML_ENTITIES = { 8 | '@' => '@', 9 | '.' => '.' 10 | } 11 | 12 | attr_accessor :email_addresses 13 | 14 | safe true 15 | 16 | def initialize(config) 17 | config['autolink_email'] ||= {} 18 | self.email_addresses = [] 19 | end 20 | 21 | def generate(site) 22 | @site = site 23 | site.pages.each { |page| autolinkify page if page.html?} 24 | site.posts.docs.each { |page| autolinkify page } 25 | end 26 | 27 | private 28 | 29 | def autolinkify(page) 30 | page.content = +Rinku.auto_link(page.content, :email_addresses, link_attr, skip_tags) do |email_address| 31 | if escape? 32 | email_addresses << email_address.dup 33 | html_encode(email_address) 34 | else 35 | email_address 36 | end 37 | end 38 | url_encode_email_addresses(page.content) if escape? 39 | end 40 | 41 | def html_encode(email_address) 42 | HTML_ENTITIES.each do |char, code| 43 | email_address.gsub!(char, code) 44 | end 45 | email_address 46 | end 47 | 48 | # A hack since Rinku doesn't offer a hook into changing what the link is 49 | def url_encode_email_addresses(content) 50 | content.gsub!(/mailto:(#{email_addresses.join('|')})/) do |m| 51 | m[$1] = ERB::Util.url_encode($1) 52 | m 53 | end 54 | end 55 | 56 | def escape? 57 | @site.config['autolink_email']['escape'] 58 | end 59 | 60 | def link_attr 61 | @link_attr ||= @site.config['autolink_email']['link_attr'] 62 | end 63 | 64 | def skip_tags 65 | @skip_tags ||= Array(@site.config['autolink_email']['skip_tags']) 66 | end 67 | end 68 | end 69 | -------------------------------------------------------------------------------- /test/helper.rb: -------------------------------------------------------------------------------- 1 | require 'minitest/autorun' 2 | require 'minitest/unit' 3 | require 'shoulda' 4 | require 'mocha/minitest' 5 | require 'jekyll-autolink_email' 6 | -------------------------------------------------------------------------------- /test/test_autolink_email.rb: -------------------------------------------------------------------------------- 1 | require 'helper' 2 | 3 | class Jekyll::AutolinkEmailTest < Minitest::Test 4 | context 'AutolinkEmail' do 5 | 6 | setup do 7 | @site = Jekyll::Site.new(Jekyll::Configuration::DEFAULTS.dup) 8 | @autolink_email = Jekyll::AutolinkEmail.new(@site.config) 9 | @page = Jekyll::Page.new(@site, File.expand_path('../../', __FILE__), '', 'README.md') 10 | @page.instance_variable_set(:@content, '