Skip to content

Commit

Permalink
Initial Commit 🎉
Browse files Browse the repository at this point in the history
  • Loading branch information
ashmaroli committed Sep 21, 2018
0 parents commit 7b89865
Showing 12 changed files with 240 additions and 0 deletions.
10 changes: 10 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
_site
.logs
.sass-cache
.jekyll-cache
.fixtures
.gems
.vendor
.jekyll-metadata
Gemfile.lock
*.gem
8 changes: 8 additions & 0 deletions .rubocop.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
require: rubocop-jekyll
inherit_gem:
rubocop-jekyll: .rubocop.yml

Metrics/CyclomaticComplexity:
Max: 7
Style/ClassAndModuleChildren:
Enabled: false
6 changes: 6 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# frozen_string_literal: true

source "https://rubygems.org"
gemspec

gem "rubocop-jekyll"
21 changes: 21 additions & 0 deletions LICENSE.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
The MIT License (MIT)

Copyright (c) 2018 Ashwin Maroli

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Jekyll Locale

A localization plugin for Jekyll
27 changes: 27 additions & 0 deletions jekyll-locale.gemspec
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# frozen_string_literal: true

lib = File.expand_path("lib", __dir__)
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
require "jekyll/locale/version"

Gem::Specification.new do |spec|
spec.name = "jekyll-locale"
spec.version = Jekyll::Locale::VERSION
spec.authors = ["Ashwin Maroli"]
spec.email = ["ashmaroli@gmail.com"]

spec.summary = "A localization plugin for Jekyll"
spec.homepage = "https://github.com/ashmaroli/jekyll-locale"
spec.license = "MIT"

spec.metadata = { "allowed_push_host" => "https://rubygems.org" }

spec.files = `git ls-files -z`.split("\x0").select do |f|
f.match(%r!^(lib/|(LICENSE|README)((\.(txt|md|markdown)|$)))!i)
end

spec.require_paths = ["lib"]
spec.required_ruby_version = ">= 2.3.0"

spec.add_runtime_dependency "jekyll", "~> 3.8"
end
15 changes: 15 additions & 0 deletions lib/jekyll-locale.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# frozen_string_literal: true

module Jekyll
module Locale
autoload :Drop, "jekyll/locale/drop"
autoload :Handler, "jekyll/locale/handler"
end
end

require_relative "jekyll/patches/site"
require_relative "jekyll/patches/utils"

Jekyll::Hooks.register :site, :after_reset do |site|
site.locale_handler.reset
end
10 changes: 10 additions & 0 deletions lib/jekyll/locale/drop.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# frozen_string_literal: true

module Jekyll
class Locale::Drop < Drops::Drop
extend Forwardable

mutable false
private def_delegator :@obj, :data, :fallback_data
end
end
42 changes: 42 additions & 0 deletions lib/jekyll/locale/handler.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
# frozen_string_literal: true

module Jekyll
class Locale::Handler
def initialize(site)
@site = site
@config = site.config
end

def reset
@data = nil
end

def data
@data ||= begin
fallback = site.site_data.dig(locales_dir, locale)
return {} unless fallback.is_a?(Hash)

# transform hash to one with "latinized lowercased string keys"
Jekyll::Utils.snake_case_keys(fallback)
end
end

private

attr_reader :site, :config

def locales_dir
@locales_dir ||= begin
value = config["locales_dir"]
value.to_s.empty? ? "locales" : value
end
end

def locale
@locale ||= begin
value = config["locale"]
value.to_s.empty? ? "en" : value
end
end
end
end
7 changes: 7 additions & 0 deletions lib/jekyll/locale/version.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# frozen_string_literal: true

module Jekyll
module Locale
VERSION = "0.1.0"
end
end
15 changes: 15 additions & 0 deletions lib/jekyll/patches/site.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# frozen_string_literal: true

module Jekyll
class Drops::UnifiedPayloadDrop
def locale
@locale ||= Jekyll::Locale::Drop.new(@obj.locale_handler)
end
end

class Site
def locale_handler
@locale_handler ||= Jekyll::Locale::Handler.new(self)
end
end
end
76 changes: 76 additions & 0 deletions lib/jekyll/patches/utils.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
# frozen_string_literal: true

module Jekyll
module Utils
extend self

# Convert all keys to snake_case by substituting non-alphanumeric characters
# with an underscore.
# The keys are first converted to lowercase strings and then any letters with accents
# are replaced with their plaintext counterpart
#
# hash - the hash to which to apply this transformation
#
# Returns a new hash with snake_cased keys or a hash with stringified keys.
def snake_case_keys(hash)
transform_keys(hash) do |key|
begin
Utils.slugify(key.to_s, :mode => "latin", :replacement => "_")
rescue StandardError
key.to_s
end
end
end

def slugify(string, mode: nil, cased: false, replacement: "-")
mode ||= "default"
return nil if string.nil?

unless SLUGIFY_MODES.include?(mode)
return cased ? string : string.downcase
end

# Drop accent marks from latin characters. Everything else turns to ?
if mode == "latin"
I18n.config.available_locales = :en if I18n.config.available_locales.empty?
string = I18n.transliterate(string)
end

slug = replace_character_sequence(string, :mode => mode, :replacement => replacement)

# Remove leading/trailing hyphen
slug.gsub!(%r!^\-|\-$!i, "")

slug.downcase! unless cased
slug
end

private

# Replace each character sequence with a hyphen.
#
# See Utils#slugify for a description of the character sequence specified
# by each mode.
def replace_character_sequence(string, **opts)
replaceable_char =
case opts[:mode]
when "raw"
SLUGIFY_RAW_REGEXP
when "pretty"
# "._~!$&'()+,;=@" is human readable (not URI-escaped) in URL
# and is allowed in both extN and NTFS.
SLUGIFY_PRETTY_REGEXP
when "ascii"
# For web servers not being able to handle Unicode, the safe
# method is to ditch anything else but latin letters and numeric
# digits.
SLUGIFY_ASCII_REGEXP
else
SLUGIFY_DEFAULT_REGEXP
end

# Strip according to the mode
string.gsub(replaceable_char, opts[:replacement])
end
end
end

0 comments on commit 7b89865

Please sign in to comment.