jekyll-generator-single-source 0.0.11 → 0.0.13

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: 253e5fa9d7d9bb93a522582300e462ed95aacf538537b75f366227b746324d55
4
- data.tar.gz: cab91867c2467dbc009b072d331f17e36485e72e3d37ba43e516051dc9a7936e
3
+ metadata.gz: d938be264eb53f48ab4f3a85dcc1dc2a5c92879454e682ea81bb5d1abf29dcf2
4
+ data.tar.gz: b61ee8aa0ee3817b032ba99a4c756a5b93a564cd817511da7db85a9a133160cf
5
5
  SHA512:
6
- metadata.gz: c39a9d017eef7e7f15e13cf8b1d0e96e63afcfa10252d4fa1ca4a99adfb6f6e719eda55f90769839a5511e28be3fb9d6dcfc42247f7788ee23a649b7a6e2d0ee
7
- data.tar.gz: a447aadeb5a01e7d09331ee0668f449c9e6c4b316e0d9a420dd4560cf375147c532764e2d6eb037a953886b9ce3d772dddbc365f16ffaedbb52e4d04a0ac24bb
6
+ metadata.gz: 3a982ea197e630d41bb73170a61b21f1386eaa363dfa81848615d8814fcd704214ef3d2181f4a79971913ade994dac22312dc149e8232fdfe1c7cb811a7ee2c9
7
+ data.tar.gz: d3d644694c3e60c26081160167c4698b7bddb3f327b1a439389f60f4195b415588f790484c39cce4b2308c5a9b143aa4bcbfdcfe954a2d4fc448fb05974bab9d
@@ -3,9 +3,18 @@
3
3
  Jekyll::Hooks.register :pages, :pre_render do |page|
4
4
  # Replace double line breaks when using if_version when
5
5
  # combined with <pre> blocks. This is usually in code samples
6
- page.content = page.content.gsub(/\n(\s*{% if_version)/, '\1')
7
- page.content = page.content.gsub("{% endif_version %}\n\n", "{% endif_version %}\n")
6
+ # TODO: comment this for now, I need to double-check if we need it
7
+ # page.content = page.content.gsub(/\n(\s*{% if_version)/, '\1')
8
+ # page.content = page.content.gsub("{% endif_version %}\n\n", "{% endif_version %}\n")
8
9
 
9
10
  # Also allow for a newline after endif_version when in a table
10
- page.content = page.content.gsub("{% endif_version %}\n|", '{% endif_version %}|')
11
+ # strip extra new lines
12
+ page.content = page.content.gsub(/\|\s*\n\n\s*{% if_version /, "|\n{% if_version ")
13
+ page.content = page.content.gsub(/\|\s*\n{% endif_version %}\n+\|/, "|\n{% endif_version -%}\n\|")
14
+ page.content = page.content.gsub(/\|\s*\n{% endif_version %}\n+\s*{% if_version (.*) %}\n/) do |_match|
15
+ "|\n{% endif_version -%}\n{% if_version #{$1} -%}\n"
16
+ end
17
+ page.content = page.content.gsub(/\|\s*\n+{% if_version (.*) %}\n/) do |_match|
18
+ "|\n{% if_version #{$1} -%}\n"
19
+ end
11
20
  end
@@ -13,7 +13,7 @@ module Jekyll
13
13
  @release = release
14
14
  end
15
15
 
16
- def_delegators :@release, :value, :label, :latest?, :to_s, :to_str, :versions
16
+ def_delegators :@release, :value, :label, :latest?, :to_s, :to_str, :versions, :tag
17
17
 
18
18
  def hash
19
19
  @hash ||= "#{@release.edition}-#{@release.value}".hash
@@ -5,69 +5,126 @@ module Jekyll
5
5
  module Liquid
6
6
  module Tags
7
7
  class VersionIs < ::Liquid::Block
8
- def initialize(tag_name, markup, tokens)
9
- @tag = markup
8
+ attr_reader :blocks
10
9
 
11
- @params = {}
12
- markup.scan(::Liquid::TagAttributes) do |key, value|
13
- @params[key.to_sym] = value
14
- end
10
+ def initialize(tag_name, markup, options)
15
11
  super
12
+ @blocks = []
13
+ push_block('if_version'.freeze, markup)
16
14
  end
17
15
 
18
- def render(context) # rubocop:disable Metrics/AbcSize, Metrics/MethodLength, Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity
19
- page = context.environments.first['page']
16
+ def nodelist
17
+ @blocks.map(&:attachment)
18
+ end
20
19
 
21
- current_version = to_version(page['release'].value)
20
+ def parse(tokens)
21
+ while parse_body(@blocks.last.attachment, tokens)
22
+ end
23
+ end
22
24
 
23
- # If there's an exact match, check only that
24
- if @params.key?(:eq)
25
- version = to_version(@params[:eq])
26
- return '' unless current_version == version
25
+ def unknown_tag(tag, markup, tokens)
26
+ if 'else'.freeze == tag
27
+ push_block(tag, markup)
28
+ else
29
+ super
27
30
  end
31
+ end
28
32
 
29
- # If there's a greater than or equal to check, fail if it's lower
30
- if @params.key?(:gte)
31
- version = to_version(@params[:gte])
32
- return '' unless current_version >= version
33
+ def render(context)
34
+ context.stack do
35
+ @blocks.each do |block|
36
+ if block.evaluate(context)
37
+ return block.attachment.render(context)
38
+ end
39
+ end
40
+ ''.freeze
33
41
  end
42
+ end
34
43
 
35
- # If there's a less than or equal to heck, fail if it's higher
36
- if @params.key?(:lte)
37
- version = to_version(@params[:lte])
38
- return '' unless current_version <= version
44
+ private
45
+
46
+ def push_block(tag, markup)
47
+ block = if tag == 'else'.freeze
48
+ ::Liquid::ElseCondition.new
49
+ else
50
+ parse_with_selected_parser(markup)
51
+ end
52
+
53
+ @blocks.push(block)
54
+ block.attach(::Liquid::BlockBody.new)
55
+ end
56
+
57
+ def lax_parse(markup)
58
+ parse_condition(markup)
59
+ end
60
+
61
+ def strict_parse(markup)
62
+ parse_condition(markup)
63
+ end
64
+
65
+ def parse_condition(markup)
66
+ IfVersionCondition.new(markup)
67
+ end
68
+
69
+ class IfVersionCondition < ::Liquid::Condition
70
+ # Extracted from:
71
+ # https://github.com/Shopify/liquid/blob/ae3057e94b7c4d657e6bc02e1d50398e34cc6ed7/lib/liquid.rb#L37
72
+ # and modified to support comma-separated values
73
+ TAG_ATTRIBUTES = /(\w+)\s*\:\s*((?-mix:(?-mix:"[^"]*"|'[^']*')|(?:[^\s\|'"]|(?-mix:"[^"]*"|'[^']*'))+))/o
74
+
75
+ def else?
76
+ false
39
77
  end
40
78
 
41
- # Don't evaluate the liquid block unconditionally,
42
- # only evaluate it if the version matches
43
- contents = super
79
+ def evaluate(context)
80
+ params = {}
81
+ @left.scan(TAG_ATTRIBUTES) do |key, value|
82
+ params[key.to_sym] = value
83
+ end
44
84
 
45
- # Table rows (starts newline then |, ends with | then newline) need
46
- # special handline
47
- is_table_row = /^\n\s*\|/.match(contents) && /\|\s*\n$/.match(contents)
85
+ page = context.environments.first['page']
48
86
 
49
- # Remove the leading whitespace and return
50
- # We can't use .strip as that removes all leading whitespace,
51
- # including indentation
52
- contents = contents.sub(/^\n/, '')
87
+ current_version = to_version(page['release'].value)
53
88
 
54
- # Remove the trailing whitespace
55
- # But ONLY if it's not a table row
56
- contents = contents.sub(/\n$/, '') unless is_table_row
89
+ if params.key? :eq
90
+ # If there's an exact match, check only that
91
+ versions = params[:eq].split(',').map { |v| to_version(v) }
92
+ return false unless versions.any? { |v| v == current_version }
93
+ end
94
+ if params.key? :gte
95
+ # If there's a greater than or equal to check, fail if it's lower
96
+ version = to_version(params[:gte])
97
+ return false unless current_version >= version
98
+ end
99
+ if params.key? :lte
100
+ # If there's a less than or equal to check, fail if it's higher
101
+ version = to_version(params[:lte])
102
+ return false unless current_version <= version
103
+ end
104
+ if params.key? :neq
105
+ # If there's a not-equal to check, fail if they are equal
106
+ version = to_version(params[:neq])
107
+ return false if current_version == version
108
+ end
57
109
 
58
- # If it's not a table row, put the \n that we removed in pre_render back
59
- contents = "\n#{contents}\n" unless is_table_row || @params[:inline]
110
+ true
111
+ end
60
112
 
61
- contents
113
+ def to_version(input)
114
+ Gem::Version.new(input.to_s.gsub(/\.x$/, '.0'))
115
+ end
62
116
  end
63
117
 
64
- def to_version(input)
65
- Gem::Version.new(input.to_s.gsub(/\.x$/, '.0'))
118
+ class ParseTreeVisitor < ::Liquid::ParseTreeVisitor
119
+ def children
120
+ @node.blocks
121
+ end
66
122
  end
67
123
  end
124
+
68
125
  end
69
126
  end
70
127
  end
71
128
  end
72
129
 
73
- Liquid::Template.register_tag('if_version', Jekyll::GeneratorSingleSource::Liquid::Tags::VersionIs)
130
+ ::Liquid::Template.register_tag('if_version'.freeze, Jekyll::GeneratorSingleSource::Liquid::Tags::VersionIs)
@@ -38,6 +38,10 @@ module Jekyll
38
38
  end
39
39
  end
40
40
 
41
+ def tag
42
+ @tag ||= value.split('.')[0..1].join('.')
43
+ end
44
+
41
45
  def default_version
42
46
  @default_version ||= versions['default']
43
47
  end
@@ -2,6 +2,6 @@
2
2
 
3
3
  module Jekyll
4
4
  module GeneratorSingleSource
5
- VERSION = '0.0.11'
5
+ VERSION = '0.0.13'
6
6
  end
7
7
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: jekyll-generator-single-source
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.11
4
+ version: 0.0.13
5
5
  platform: ruby
6
6
  authors:
7
7
  - Fabian Rodriguez
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2024-01-31 00:00:00.000000000 Z
11
+ date: 2024-04-17 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: jekyll