jekyll-spaceship 0.6.3 → 0.7.0

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: 8df3e5c04610d7f6ae766303a8b884e1b3bd0047f72ac489275dd26d133d392a
4
- data.tar.gz: '0728453eaace46c6d88aa0395f1d17ae88c17a727aa3be9b10236c222fc3d85e'
3
+ metadata.gz: 38113b9fb933fde7dbf9a201839d3529f22e27687baa2c8e5768097b47eb7eef
4
+ data.tar.gz: acb2c8d069864444b6d15ad235c13322e9a817ebbab83e8eab9058060979c256
5
5
  SHA512:
6
- metadata.gz: 1b5e65d14b80d87ebb06b9e3c5cc7c71f6d654940d58034ab314d232d5ff6614bd3b22649259d3658690b5e94b5c4e6983214a0b3e4bdd91cd03e7702901a51e
7
- data.tar.gz: f7a171a023c65ceb729ade833fe7193b05000aa24483c1bd0bc6201c28df47f69d503aa2612a866aba1627fc86626f63d38b7db41b8d75a616805b14f1d71fff
6
+ metadata.gz: 1ae51763f919f940079fe169d7c05889a234f69a42d9369cce5858670605a3b6bb88839162d4ce95ce8cc07d8cd035228069f1bc4e189bad3ca41ee13623cc85
7
+ data.tar.gz: 2f5dda310b60fd8d69460299a81844aa791fd0877e7cf8d9d58556f4a8e780996aef7512924fe5786080379dc5be5a33d8f67c55de65fa7c0a2c2070283ce2f4
data/README.md CHANGED
@@ -115,6 +115,7 @@ Spaceship is a minimalistic, powerful and extremely customizable [Jekyll](https:
115
115
  - [6.1 Escape Ordered List](#escape-ordered-list)
116
116
  - [7. Emoji Usage](#7-emoji-usage)
117
117
  - [7.1 Emoji Customizing](#71-emoji-customizing)
118
+ - [8. Modifying Element Usage](#8-modifying-element-usage)
118
119
  - [Credits](#credits)
119
120
  - [Contributing](#contributing)
120
121
  - [License](#license)
@@ -154,6 +155,7 @@ jekyll-spaceship:
154
155
  - polyfill-processor
155
156
  - video-processor
156
157
  - emoji-processor
158
+ - element-processor
157
159
  mathjax-processor:
158
160
  src: //cdn.mathjax.org/mathjax/latest/MathJax.js?config=TeX-AMS-MML_HTMLorMML
159
161
  config:
@@ -755,6 +757,51 @@ jekyll-spaceship:
755
757
 
756
758
  See the [Gemoji](https://github.com/github/gemoji) documentation for generating image files.
757
759
 
760
+ ### 8. Modifying Element Usage
761
+
762
+ It allows us to modify elements via `CSS3 selectors`. Through it you can easily
763
+ modify the attributes of a element tag, replace the child nodes and so on, it's
764
+ very flexible, but here is example usage for modifying a document:
765
+
766
+ ```yml
767
+ # Here is a comprehensive example
768
+ jekyll-spaceship:
769
+ element-processor:
770
+ css:
771
+ - a: '<h1>Test</h1>' # Replace all `a` tags (String Style)
772
+ - ['a.link1', 'a.link2']: # Replace all `a.link1`, `a.link2` tags (Hash Style)
773
+ name: img # Replace element tag name
774
+ props: # Replace element properties
775
+ title: Good image # Add a title attribute
776
+ src: ['(^.*$)', '\0?a=123'] # Add query string to src attribute by regex pattern
777
+ style: # Add style attribute (String Style)
778
+ color: red
779
+ font-size: '1.2em'
780
+ children: # Add children to the element
781
+ - # First empty for adding after the last child node
782
+ - "<span>Google</span>" # First child node (String Style)
783
+ - # Middle empty for wrapping the children nodes
784
+ - name: span # Second child node (Hash Style)
785
+ props:
786
+ prop1: "1" # Custom property1
787
+ prop2: "2" # Custom property2
788
+ prop3: "3" # Custom property3
789
+ children: # Add nested chidren nodes
790
+ - "<span>Jekyll</span>" # First child node (String Style)
791
+ - name: span # Second child node (Hash Style)
792
+ props: # Modify child node (Hash Style)
793
+ prop1: "a"
794
+ prop2: "b"
795
+ prop3: "c"
796
+ children: "<b>Yap!</b>" # Add children nodes (String Style)
797
+ - # Last empty for adding before the first child node
798
+ - a.link: '<a href="//t.com">Link</a>' # Replace all `a.link` tags (String Style)
799
+ - h1#title: # Replace `h1#title` tags (Hash Style)
800
+ children: I'm a title! # Replace inner html to new text
801
+
802
+ ```
803
+
804
+
758
805
 
759
806
  ## Credits
760
807
 
@@ -12,7 +12,8 @@ module Jekyll::Spaceship
12
12
  'plantuml-processor',
13
13
  'polyfill-processor',
14
14
  'video-processor',
15
- 'emoji-processor'
15
+ 'emoji-processor',
16
+ 'element-processor'
16
17
  ]
17
18
  }
18
19
 
@@ -34,7 +35,7 @@ module Jekyll::Spaceship
34
35
  def self.store(section, default)
35
36
  return if @@store[section].nil?
36
37
  return @@store[section] if default.nil?
37
- @@store[section] = deep_merge(@@store[section], default)
38
+ @@store[section] = deep_merge(default, @@store[section])
38
39
  end
39
40
 
40
41
  def self.load(filename = '_config.yml')
@@ -0,0 +1,152 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'nokogiri'
4
+
5
+ module Jekyll::Spaceship
6
+ class ElementProcessor < Processor
7
+ priority :lowest
8
+
9
+ def self.config
10
+ { 'css' => [] }
11
+ end
12
+
13
+ def on_handle_html(content)
14
+ return content if config['css'].size.zero?
15
+
16
+ # use nokogiri to parse html content
17
+ doc = Nokogiri::HTML(content)
18
+
19
+ # handle each css pattern
20
+ config['css'].each do |data|
21
+ data.each do |key, val|
22
+ key = [key] if key.kind_of? String
23
+ key.each do |pattern|
24
+ nodes = doc.css(pattern)
25
+ nodes.each do |element|
26
+ handle_css_pattern({
27
+ :doc => doc,
28
+ :element => element,
29
+ :data => val
30
+ })
31
+ end
32
+ self.handled = true
33
+ end
34
+ end
35
+ end
36
+
37
+ doc.to_html
38
+ end
39
+
40
+ def handle_css_pattern(data)
41
+ doc = data[:doc]
42
+ element = data[:element]
43
+ data = data[:data]
44
+
45
+ if data.kind_of? String
46
+ element.replace Nokogiri::HTML.fragment(data)
47
+ elsif data.kind_of? Hash
48
+ # set name
49
+ element.name = data['name'] unless data['name'].nil?
50
+
51
+ # set props
52
+ data['props']&.each do |prop, val|
53
+ next element.remove_attribute if val.nil?
54
+ if val.kind_of? Array
55
+ next if val.size != 2
56
+ v = element[prop]
57
+ v = '' if v.nil?
58
+ val = v.sub(/#{val[0]}/, val[1])
59
+ elsif val.kind_of? Hash
60
+ result = []
61
+ val.each { |k, v| result.push "#{k}: #{v}" }
62
+ val = result.join(";")
63
+ end
64
+ element.set_attribute(prop, val)
65
+ end
66
+
67
+ # processing children
68
+ return unless data.has_key?('children')
69
+ return element.inner_html = "" if data['children'].nil?
70
+ children = self.create_children({
71
+ :doc => doc,
72
+ :data => data['children']
73
+ })
74
+
75
+ # replace whole inner html
76
+ unless data['children'].kind_of? Array
77
+ return element.inner_html = children
78
+ end
79
+
80
+ if element.children.size.zero?
81
+ return element.inner_html = children
82
+ end
83
+
84
+ index = data['children'].index(nil)
85
+ if index.nil?
86
+ return element.inner_html = children
87
+ end
88
+
89
+ # insert to the end of children
90
+ if index == 0
91
+ return element.children.last.after(children)
92
+ end
93
+
94
+ # insert to the begin of children
95
+ rindex = data['children'].rindex { |item| !item.nil? }
96
+ if index == rindex + 1
97
+ return element.children.first.before children
98
+ end
99
+
100
+ # wrap the children
101
+ element.children.first.before children[0..index]
102
+ element.children.last.after children[index..children.size]
103
+ end
104
+ end
105
+
106
+ def create_children(data)
107
+ doc = data[:doc]
108
+ data = data[:data]
109
+ root = Nokogiri::HTML.fragment("")
110
+
111
+ data = [data] unless data.kind_of? Array
112
+ data.each do |child_data|
113
+ node = self.create_element({
114
+ :doc => doc,
115
+ :data => child_data
116
+ })
117
+ next if node.nil?
118
+ unless child_data['children'].nil?
119
+ node.children = self.create_children({
120
+ :doc => doc,
121
+ :data => child_data['children']
122
+ })
123
+ end
124
+ root.add_child node
125
+ end
126
+ root.children
127
+ end
128
+
129
+ def create_element(data)
130
+ doc = data[:doc]
131
+ data = data[:data]
132
+
133
+ return if data.nil?
134
+ return Nokogiri::HTML.fragment(data) if data.kind_of? String
135
+ return if data['name'].nil?
136
+
137
+ # create node
138
+ node = doc.create_element(data['name'])
139
+
140
+ # set props
141
+ data['props']&.each do |prop, val|
142
+ if val.kind_of? Hash
143
+ result = []
144
+ val.each { |k, v| result.push "#{k}: #{v}" }
145
+ val = result.join(";")
146
+ end
147
+ node.set_attribute(prop, val)
148
+ end
149
+ node
150
+ end
151
+ end
152
+ end
@@ -2,6 +2,6 @@
2
2
 
3
3
  module Jekyll
4
4
  module Spaceship
5
- VERSION = "0.6.3"
5
+ VERSION = "0.7.0"
6
6
  end
7
7
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: jekyll-spaceship
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.6.3
4
+ version: 0.7.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - jeffreytse
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2020-06-13 00:00:00.000000000 Z
11
+ date: 2020-06-14 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: jekyll
@@ -137,6 +137,7 @@ files:
137
137
  - lib/jekyll-spaceship/cores/processor.rb
138
138
  - lib/jekyll-spaceship/cores/register.rb
139
139
  - lib/jekyll-spaceship/cores/type.rb
140
+ - lib/jekyll-spaceship/processors/element-processor.rb
140
141
  - lib/jekyll-spaceship/processors/emoji-processor.rb
141
142
  - lib/jekyll-spaceship/processors/mathjax-processor.rb
142
143
  - lib/jekyll-spaceship/processors/plantuml-processor.rb