jekyll-svelte-slabs 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 492e0501eea49d35d5e5d966581cac92db8dfdc277ad978551254f3a537335e9
4
+ data.tar.gz: 8f71214acdef390178f451828553c8661c35f3865602220a7e8179b5c08cccb8
5
+ SHA512:
6
+ metadata.gz: 6ac731620815bbb20d3a073f401670aa6452de843ea1427e59eeb4a7692c1e22c6dd86566bc2920864cfd1144fc0e9580e19a38462d48f1c7d28b84860ae922d
7
+ data.tar.gz: 1f5cfd4c30ba4072940cc4c9c6ae44e5b9cf505ba4c23e7b4fe9464779573578b7c2c7e8f7b698c36b17a5d54fcef7fa92ce70ea9e1982cfb1c33dad30c37b84
@@ -0,0 +1,124 @@
1
+ require "base64"
2
+ require 'json'
3
+ require 'digest'
4
+
5
+ module JekyllSvelteSlabs
6
+
7
+ class Tag < Liquid::Block
8
+
9
+ VALID_SYNTAX = %r!
10
+ ([\w-]+)\s*=\s*
11
+ (?:"([^"\\]*(?:\\.[^"\\]*)*)"|'([^'\\]*(?:\\.[^'\\]*)*)'|([\w.-]+))
12
+ !x.freeze
13
+ VARIABLE_SYNTAX = %r!
14
+ (?<variable>[^{]*(\{\{\s*[\w\-.]+\s*(\|.*)?\}\}[^\s{}]*)+)
15
+ (?<params>.*)
16
+ !mx.freeze
17
+
18
+ FULL_VALID_SYNTAX = %r!\A\s*(?:#{VALID_SYNTAX}(?=\s|\z)\s*)*\z!.freeze
19
+ VALID_FILENAME_CHARS = %r!^[\w/.-]+$!.freeze
20
+ INVALID_SEQUENCES = %r![./]{2,}!.freeze
21
+
22
+ def initialize(tag_name, markup, tokens)
23
+ super
24
+ markup = markup.strip
25
+ matched = markup.match(VARIABLE_SYNTAX)
26
+ if matched
27
+ @file = matched["variable"].strip
28
+ @params = matched["params"].strip
29
+ else
30
+ @file, @params = markup.split(%r!\s+!, 2)
31
+ end
32
+ validate_params if @params
33
+ @tag_name = tag_name
34
+ end
35
+
36
+ def blank?
37
+ false
38
+ end
39
+
40
+ def parse_params(context)
41
+ params = {}
42
+ @params.scan(VALID_SYNTAX) do |key, d_quoted, s_quoted, variable|
43
+ value = if d_quoted
44
+ d_quoted.include?('\\"') ? d_quoted.gsub('\\"', '"') : d_quoted
45
+ elsif s_quoted
46
+ s_quoted.include?("\\'") ? s_quoted.gsub("\\'", "'") : s_quoted
47
+ elsif variable
48
+ context[variable]
49
+ end
50
+
51
+ params[key] = value
52
+ end
53
+
54
+ params.each do |key, value|
55
+ if key == 'bind'
56
+ valueHash = {}.merge(value)
57
+ params = valueHash.merge(params)
58
+ next
59
+ end
60
+ end
61
+
62
+ params
63
+ end
64
+
65
+ def validate_file_name(file)
66
+ if INVALID_SEQUENCES.match?(file) || !VALID_FILENAME_CHARS.match?(file)
67
+ raise ArgumentError, <<~MSG
68
+ Invalid syntax for svelte tag. File contains invalid characters or sequences:
69
+ #{file}
70
+ Valid syntax:
71
+ {% #{@tag_name} component-name param='value' param2='value' %}
72
+ <p>HTML Content</p>
73
+ {% end#{@tag_name} %}
74
+ MSG
75
+ end
76
+ end
77
+
78
+ def validate_params
79
+ unless FULL_VALID_SYNTAX.match?(@params)
80
+ raise ArgumentError, <<~MSG
81
+ Invalid syntax for svelte tag:
82
+ #{@params}
83
+ Valid syntax:
84
+ {% #{@tag_name} component-name param='value' param2='value' %}
85
+ <p>HTML Content</p>
86
+ {% end#{@tag_name} %}
87
+ MSG
88
+ end
89
+ end
90
+
91
+ # Render the variable if required
92
+ def render_variable(context)
93
+ Liquid::Template.parse(@file).render(context) if VARIABLE_SYNTAX.match?(@file)
94
+ end
95
+
96
+
97
+ def render(context)
98
+ text = super
99
+ # puts Base64.encode64(context[@data].to_json)
100
+
101
+ # site = context.registers[:site]
102
+ # puts site
103
+
104
+ file = render_variable(context) || @file
105
+ validate_file_name(file)
106
+
107
+ svelte_data = @params ? parse_params(context).to_json : "{}"
108
+ endpoint = Digest::MD5.hexdigest(svelte_data)
109
+
110
+ <<~MSG
111
+ <script>
112
+ window["#{endpoint}"] = #{svelte_data};
113
+ </script>
114
+ <div data-svelte-slab="#{file}" data-svelte-slab-props="window:#{endpoint}">
115
+ #{text}
116
+ </div>
117
+ MSG
118
+ end
119
+
120
+ end
121
+
122
+ end
123
+
124
+ Liquid::Template.register_tag("svelte", JekyllSvelte::Tag)
@@ -0,0 +1,3 @@
1
+ module JekyllSvelteSlabs
2
+ VERSION = "0.0.1"
3
+ end
metadata ADDED
@@ -0,0 +1,65 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: jekyll-svelte-slabs
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Liam Bigelow
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2020-11-12 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: jekyll
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '3.7'
20
+ - - "<"
21
+ - !ruby/object:Gem::Version
22
+ version: '5.0'
23
+ type: :runtime
24
+ prerelease: false
25
+ version_requirements: !ruby/object:Gem::Requirement
26
+ requirements:
27
+ - - ">="
28
+ - !ruby/object:Gem::Version
29
+ version: '3.7'
30
+ - - "<"
31
+ - !ruby/object:Gem::Version
32
+ version: '5.0'
33
+ description:
34
+ email:
35
+ - liam@cloudcannon.com
36
+ executables: []
37
+ extensions: []
38
+ extra_rdoc_files: []
39
+ files:
40
+ - lib/jekyll-svelte-slabs.rb
41
+ - lib/jekyll-svelte-slabs/version.rb
42
+ homepage: https://github.com/cloudcannon/svelte-slabs
43
+ licenses:
44
+ - MIT
45
+ metadata: {}
46
+ post_install_message:
47
+ rdoc_options: []
48
+ require_paths:
49
+ - lib
50
+ required_ruby_version: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ required_rubygems_version: !ruby/object:Gem::Requirement
56
+ requirements:
57
+ - - ">="
58
+ - !ruby/object:Gem::Version
59
+ version: '0'
60
+ requirements: []
61
+ rubygems_version: 3.0.3
62
+ signing_key:
63
+ specification_version: 4
64
+ summary: A Jekyll plugin to help render svelte components on Jekyll websites
65
+ test_files: []