sutty-liquid 0.1.1 → 0.4.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 80dcc3abc95fa1284a2fe605cbea560484b2d711351f60175bb3aba0cc85b2c5
4
- data.tar.gz: a2d79c43e542447a4acf8146c8f80e49a68cc78582aa6f72314da55a68decdf8
3
+ metadata.gz: d90e4da9fe0d1b7d177e20fd9a05f825a07d3465d99b1641d79b75879d526c00
4
+ data.tar.gz: f467555d544fca178ba15dce7f1de53eadfcbea7a263fe91f9a6066ac92664d2
5
5
  SHA512:
6
- metadata.gz: 7a843c2327f75c56684fd34873c999657b4aafcf2bc48e38b593eb0c571d9dde655109a235c309a949e9dd7b705b968476433b423660179e7bcc3ddb2623c0d4
7
- data.tar.gz: 7686d8f2e1317a112a2d86cb479789d2b6316426e6b72eaf035fd105b9f2cc74fa1940f6693fe56184eb3fb5d466b8dea879169568e1361d91c71912ef02ca27
6
+ metadata.gz: 94b85e20c7c10e267e67534ab215d94539b482cc807ff7623f03d8f281463df12395cde294d00814b0b8db04897dfde6cbe0880d882982459050f7d8f53e28eb
7
+ data.tar.gz: 1dd71eb67a50c79a1f796ff0813968f2275e7b3fd56afe5f45234a2efc5244bb87c0da10fc90fb417547d64f72a3dbc534efa09c772dfe95eae063def69a2dd0
@@ -0,0 +1,16 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'jekyll/drops/url_drop'
4
+
5
+ module Jekyll
6
+ module Drops
7
+ module UrlDropDecorator
8
+ # Permits to use layout as an URL placeholder
9
+ def layout
10
+ @obj.data['layout']
11
+ end
12
+ end
13
+ end
14
+ end
15
+
16
+ Jekyll::Drops::UrlDrop.include Jekyll::Drops::UrlDropDecorator
@@ -0,0 +1,47 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Jekyll
4
+ module Filters
5
+ module Assertion
6
+ # Compares two values.
7
+ #
8
+ # Example usage:
9
+ #
10
+ # {{ item.url | equals: page.url }}
11
+ #
12
+ # @param [Any]
13
+ # @param [Any]
14
+ # @return [TrueClass|FalseClass]
15
+ def equals(input, comparison)
16
+ input == comparison
17
+ end
18
+
19
+ # Ternary operator. If the input value is truthy, return first
20
+ # argument, otherwise returns the last.
21
+ #
22
+ # Example usage:
23
+ #
24
+ # {% assign active = item.url | equals: page.url %}
25
+ # {{ active | ternary: 'active', '' }}
26
+ #
27
+ # @param [Any]
28
+ # @param [Any]
29
+ # @param [Any]
30
+ # @return [Any]
31
+ def ternary(input, value, alternative)
32
+ empty = case input
33
+ when Integer then input.zero?
34
+ when Float then input.zero?
35
+ when TrueClass then input
36
+ when FalseClass then input
37
+ when NilClass then false
38
+ else input.empty?
39
+ end
40
+
41
+ (empty || !!input) ? value : alternative
42
+ end
43
+ end
44
+ end
45
+ end
46
+
47
+ Liquid::Template.register_filter(Jekyll::Filters::Assertion)
@@ -5,6 +5,10 @@ module Jekyll
5
5
  module Compact
6
6
  # Removes nil values from an Array
7
7
  #
8
+ # Example usage:
9
+ #
10
+ # {{ 'tag,,tag2' | split: ',' | compact }}
11
+ #
8
12
  # @param [Array]
9
13
  # @return [Array]
10
14
  def compact(array)
@@ -0,0 +1,63 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Jekyll
4
+ module Filters
5
+ module DateLocal
6
+ # Translates month and day names. This requires certain values on
7
+ # your _data/LANG.yml
8
+ #
9
+ # Example usage:
10
+ #
11
+ # {{ page.date | date_local: '%e de %B de %Y' }}
12
+ #
13
+ # @see {_data/es.yml}
14
+ # @param [String|Date]
15
+ # @param [String]
16
+ # @return [String]
17
+ def date_local(input, format)
18
+ require 'date'
19
+
20
+ input = Date.parse(input) if input.is_a? String
21
+
22
+ # Return early if we don't need to translate
23
+ return input.strftime(format) unless /%(|\^)[aAbBpP]/ =~ format
24
+
25
+ input.strftime translate_localization_format(input, format)
26
+ rescue ArgumentError
27
+ Jekyll.logger.warn "#{input} is not a valid Date"
28
+ input
29
+ end
30
+
31
+ private
32
+
33
+ # Adapted from the i18n gem
34
+ # @see {https://github.com/ruby-i18n/i18n/blob/a8f4fdcb197e56b5a698d1bc68007dd0871c03bf/lib/i18n/backend/base.rb}
35
+ def translate_localization_format(object, format)
36
+ format.to_s.gsub(/%(|\^)[aAbBpP]/) do |match|
37
+ case match
38
+ when '%a' then i18n.dig('date', 'abbr_day_names', object.wday - 1)
39
+ when '%^a' then i18n.dig('date', 'abbr_day_names', object.wday - 1)&.upcase
40
+ when '%A' then i18n.dig('date', 'day_names', object.wday - 1)
41
+ when '%^A' then i18n.dig('date', 'day_names', object.wday - 1)&.upcase
42
+ when '%b' then i18n.dig('date', 'abbr_month_names', object.mon - 1)
43
+ when '%^b' then i18n.dig('date', 'abbr_month_names', object.mon - 1)&.upcase
44
+ when '%B' then i18n.dig('date', 'month_names', object.mon - 1)
45
+ when '%^B' then i18n.dig('date', 'month_names', object.mon - 1)&.upcase
46
+ when '%p' then i18n.dig('time', object.hour < 12 ? 'am' : 'pm')&.upcase if object.respond_to? :hour
47
+ when '%P' then i18n.dig('time', object.hour < 12 ? 'am' : 'pm')&.downcase if object.respond_to? :hour
48
+ end
49
+ end
50
+ end
51
+
52
+ def site
53
+ @site ||= @context.registers[:site]
54
+ end
55
+
56
+ def i18n
57
+ @i18n ||= site.data.dig site.config['lang']
58
+ end
59
+ end
60
+ end
61
+ end
62
+
63
+ Liquid::Template.register_filter(Jekyll::Filters::DateLocal)
@@ -3,7 +3,11 @@
3
3
  module Jekyll
4
4
  module Filters
5
5
  module File
6
- # File extension
6
+ # File extension.
7
+ #
8
+ # Example usage:
9
+ #
10
+ # {{ page.file.path | extname }}
7
11
  #
8
12
  # @param [String]
9
13
  # @return [Nil|String]
@@ -1,11 +1,12 @@
1
1
  # frozen_string_literal: true
2
2
 
3
-
4
3
  module Jekyll
5
4
  module Filters
6
5
  module Json
7
6
  # Converts an object to JSON
8
7
  #
8
+ # Note: Jekyll 4 has a jsonify filter now.
9
+ #
9
10
  # @param [Any]
10
11
  # @return [String]
11
12
  def json(object)
@@ -0,0 +1,65 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Jekyll
4
+ module Filters
5
+ module Menu
6
+ # Verifies if the given URL is an active item on the menu.
7
+ #
8
+ # Example usage:
9
+ #
10
+ # {% assign active_url = page.url | menu_active %}
11
+ # {% for item in site.i18n.menu.items %}
12
+ # <a
13
+ # href="{{ item.href }}"
14
+ # class="{{ page.url | menu_active | equals: item.href | ternary: 'active', '' }}">
15
+ #
16
+ # {{ item.title }}
17
+ # </a>
18
+ # {% endfor %}
19
+ #
20
+ # @param [String] The URL to be verified
21
+ # @return [String] The item URL
22
+ def menu_active_item(page_url)
23
+ site_menu&.find do |key, value|
24
+ key == page_url || page_url.start_with?(key)
25
+ end&.last
26
+ end
27
+
28
+ # The menu is defined in a data file that corresponds to the site
29
+ # language. This method converts the menu items into a map of URL
30
+ # parts and actual URLs.
31
+ #
32
+ # @see _data/es.yml
33
+ # @see {https://0xacab.org/sutty/jekyll/jekyll-locales}
34
+ # @return [Hash]
35
+ def site_menu
36
+ site = @context.registers[:site]
37
+ @site_menu ||= site.data.dig(site.config['locale'], 'menu', 'items')&.reduce({}) do |menu, item|
38
+ # If the item has a layout, we pick the first post and update
39
+ # the href. We can't do the same for all posts because we
40
+ # wouldn't be able to cache the menu.
41
+ if item['layout']
42
+ doc = site.documents.find do |doc|
43
+ doc.data['layout'] == item['layout']
44
+ end
45
+
46
+ item['href'] = doc&.url
47
+ end
48
+
49
+ # Ignore empty or anchor items
50
+ next menu if item['href'].nil? || item['href'].empty? || item['href']&.start_with?('#')
51
+
52
+ menu[item['href']] = item['href']
53
+
54
+ item['active']&.each do |a|
55
+ menu[a] = item['href']
56
+ end
57
+
58
+ menu
59
+ end
60
+ end
61
+ end
62
+ end
63
+ end
64
+
65
+ Liquid::Template.register_filter(Jekyll::Filters::Menu)
@@ -0,0 +1,24 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Jekyll
4
+ module Filters
5
+ # Extracts numbers from a String, useful for converting phone
6
+ # numbers to tel: links.
7
+ #
8
+ # Example usage:
9
+ #
10
+ # <a href="tel:{{ page.tel | numbers }}">{{ page.tel }}</a>
11
+ #
12
+ # @param [String]
13
+ # @return [String]
14
+ module Numbers
15
+ def numbers(input)
16
+ return unless input.respond_to? :gsub
17
+
18
+ input.gsub(%r{[^0-9]}, '')
19
+ end
20
+ end
21
+ end
22
+ end
23
+
24
+ Liquid::Template.register_filter(Jekyll::Filters::Numbers)
@@ -4,6 +4,10 @@ module Jekyll
4
4
  module Filters
5
5
  module Pry
6
6
  # Pry into the input and args
7
+ #
8
+ # Example usage:
9
+ #
10
+ # {{ site.posts | pry }}
7
11
  def pry(input, *args)
8
12
  require 'pry'
9
13
 
@@ -3,7 +3,11 @@
3
3
  module Jekyll
4
4
  module Filters
5
5
  module Sample
6
- # Returns a random item from an Array
6
+ # Returns a random item from an Array.
7
+ #
8
+ # Example usage:
9
+ #
10
+ # {{ site.posts | sample }}
7
11
  #
8
12
  # @input [Array]
9
13
  # @return [Any]
@@ -7,6 +7,15 @@ module Jekyll
7
7
  # want to generate social network buttons from an undetermined
8
8
  # list of URLs.
9
9
  #
10
+ # Example usage:
11
+ #
12
+ # {% assign mastodon = 'https://todon.nl/@sutty' | social_network %}
13
+ # <a href="{{ mastodon.url }}">
14
+ # <i class="fa-{{ mastodon.name }}"></i>
15
+ #
16
+ # {{ mastodon.name | capitalize }}
17
+ # </a>
18
+ #
10
19
  # @param [String]
11
20
  # @return [Hash]
12
21
  def social_network(url)
@@ -15,6 +24,7 @@ module Jekyll
15
24
  require 'uri'
16
25
 
17
26
  uri = URI url
27
+ uri.host.sub! 'www.', ''
18
28
  name = %r{/@\w+} =~ uri.query ? 'mastodon' : uri.host.split('.', 2).first
19
29
 
20
30
  { 'host' => uri.host, 'name' => name, 'url' => url }.to_liquid
@@ -4,6 +4,10 @@ module Jekyll
4
4
  module Tags
5
5
  class Pry < Liquid::Tag
6
6
  # Pry into the rendering context
7
+ #
8
+ # Example usage:
9
+ #
10
+ # {% pry %}
7
11
  def render(context)
8
12
  require 'pry'
9
13
 
@@ -1,11 +1,17 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require_relative 'jekyll/filters/compact.rb'
4
- require_relative 'jekyll/filters/infinite.rb'
5
- require_relative 'jekyll/filters/file.rb'
6
- require_relative 'jekyll/filters/json.rb'
7
- require_relative 'jekyll/filters/sample.rb'
8
- require_relative 'jekyll/filters/social_network.rb'
3
+ require_relative 'jekyll/filters/assertion'
4
+ require_relative 'jekyll/filters/compact'
5
+ require_relative 'jekyll/filters/date_local'
6
+ require_relative 'jekyll/filters/infinite'
7
+ require_relative 'jekyll/filters/file'
8
+ require_relative 'jekyll/filters/json'
9
+ require_relative 'jekyll/filters/menu'
10
+ require_relative 'jekyll/filters/number'
11
+ require_relative 'jekyll/filters/sample'
12
+ require_relative 'jekyll/filters/social_network'
9
13
 
10
- require_relative 'jekyll/filters/pry.rb'
11
- require_relative 'jekyll/tags/pry.rb'
14
+ require_relative 'jekyll/filters/pry'
15
+ require_relative 'jekyll/tags/pry'
16
+
17
+ require_relative 'jekyll/drops/url_drop_decorator'
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: sutty-liquid
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.4.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - f
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2020-09-24 00:00:00.000000000 Z
11
+ date: 2020-11-11 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: jekyll
@@ -35,10 +35,15 @@ extra_rdoc_files:
35
35
  files:
36
36
  - LICENSE.txt
37
37
  - README.md
38
+ - lib/jekyll/drops/url_drop_decorator.rb
39
+ - lib/jekyll/filters/assertion.rb
38
40
  - lib/jekyll/filters/compact.rb
41
+ - lib/jekyll/filters/date_local.rb
39
42
  - lib/jekyll/filters/file.rb
40
43
  - lib/jekyll/filters/infinite.rb
41
44
  - lib/jekyll/filters/json.rb
45
+ - lib/jekyll/filters/menu.rb
46
+ - lib/jekyll/filters/number.rb
42
47
  - lib/jekyll/filters/pry.rb
43
48
  - lib/jekyll/filters/sample.rb
44
49
  - lib/jekyll/filters/social_network.rb
@@ -75,7 +80,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
75
80
  - !ruby/object:Gem::Version
76
81
  version: '0'
77
82
  requirements: []
78
- rubygems_version: 3.0.3
83
+ rubygems_version: 3.1.2
79
84
  signing_key:
80
85
  specification_version: 4
81
86
  summary: Liquid filters