jekyll-spaceship 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 597396d665684f4d0fe9c5cf35b6a644f00700f538fdd34933e4ceebffe089bf
4
+ data.tar.gz: 183b6fce2fee3ecee141b34d1fe2143697104ec6d34c2ca6f7cd2dc68005a394
5
+ SHA512:
6
+ metadata.gz: d03b9aca5f5c2c65c3b311a60b562f8e3dc4446f810af91ca6161c7d75c076334d24938ae5532b1829e79b5b1939ae84cdbe1ef258dac410fb37d9375d99b06d
7
+ data.tar.gz: 75dfa323d5b7fe831c51c4fad01aae2c018e83d019bddde6f71f8858ba00b5c590e1ef1cce126804fcee7a4434fcf9258768266f371fed5c96bb72751c83c08d
data/.gitignore ADDED
@@ -0,0 +1,11 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+ .DS_Store
11
+ *.gem
data/Gemfile ADDED
@@ -0,0 +1,11 @@
1
+ # frozen_string_literal: true
2
+
3
+ source "https://rubygems.org"
4
+ gemspec
5
+
6
+ gem "jekyll", ENV["JEKYLL_VERSION"] if ENV["JEKYLL_VERSION"]
7
+
8
+ install_if -> { Gem.win_platform? } do
9
+ gem "tzinfo", "~> 1.2"
10
+ gem "tzinfo-data"
11
+ end
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2019 Jeffrey Tse
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
@@ -0,0 +1,27 @@
1
+ # frozen_string_literal: true
2
+
3
+ lib = File.expand_path('lib', __dir__)
4
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
5
+ require "jekyll-spaceship/version"
6
+
7
+ Gem::Specification.new do |spec|
8
+ spec.name = "jekyll-spaceship"
9
+ spec.version = Jekyll::Spaceship::VERSION
10
+ spec.authors = ["jeffreytse"]
11
+ spec.email = ["jeffreytse.mail@gmail.com"]
12
+ spec.summary = "A Jekyll plugin to provide powerful supports for table, mathjax, plantuml, etc."
13
+ spec.homepage = "https://github.com/jeffreytse/jekyll-spaceship"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
17
+ spec.require_paths = ["lib"]
18
+
19
+ spec.required_ruby_version = ">= 2.3.0"
20
+
21
+ spec.add_runtime_dependency "nokogiri", "~> 1.6"
22
+
23
+ spec.add_development_dependency "jekyll", "~> 3.7"
24
+ spec.add_development_dependency "bundler", "~> 1.10"
25
+ spec.add_development_dependency "rake", "~> 12.0"
26
+ spec.add_development_dependency "rspec", "~> 3.0"
27
+ end
@@ -0,0 +1,12 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'jekyll-spaceship/cores/logger'
4
+ require 'jekyll-spaceship/cores/processor'
5
+ require 'jekyll-spaceship/cores/register'
6
+
7
+ module Jekyll::Spaceship
8
+ Logger.display_info
9
+ Register.use 'table-processor'
10
+ Register.use 'mathjax-processor'
11
+ Register.use 'plantuml-processor'
12
+ end
@@ -0,0 +1,21 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'jekyll-spaceship/version'
4
+
5
+ module Jekyll::Spaceship
6
+ class Logger
7
+ def self.display_info
8
+ self.log "Jekyll-Spaceship #{Jekyll::Spaceship::VERSION}"
9
+ self.log "A powerful Jekyll plugin."
10
+ self.log "https://github.com/jeffreytse/jekyll-spaceship"
11
+ end
12
+
13
+ def self.log(content)
14
+ self.output "Jekyll Spaceship", content
15
+ end
16
+
17
+ def self.output(title, content)
18
+ puts "#{title.rjust(18)}: #{content}"
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,69 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Jekyll::Spaceship
4
+ class Processor
5
+ @@_registers = []
6
+
7
+ def initialize()
8
+ @@_registers.each do |_register|
9
+ container = _register.first
10
+ events = _register.last.uniq
11
+ events = events.filter do |event|
12
+ next true if event.match?(/^post/)
13
+ next !events.one?(event.to_s.gsub(/^pre/, 'post').to_sym)
14
+ end
15
+ events.each do |event|
16
+ register container, event
17
+ end
18
+ end
19
+ @@_registers.clear
20
+ end
21
+
22
+ def content_tag(post)
23
+ # pre-handle content
24
+ content_tag = "<content_tag id=\"#{post.url}\" />"
25
+ end
26
+
27
+ def self.register(container, *events)
28
+ @@_registers << [container, events]
29
+ end
30
+
31
+ def register(container, event, &block)
32
+ # define handle proc
33
+ handle = ->(post) {
34
+ method = "on_#{container}_#{event}"
35
+ self.send method, post if self.respond_to? method
36
+ block.call(post) if block
37
+ }
38
+
39
+ if event.to_s.start_with?("after")
40
+ Jekyll::Hooks.register container, event do |post|
41
+ handle.call post
42
+ end
43
+ elsif event.to_s.start_with?("post")
44
+ Jekyll::Hooks.register container, event do |post|
45
+ # remove content tags
46
+ tag = self.content_tag(post)
47
+ post.content = post.content.gsub(tag, "")
48
+
49
+ handle.call post
50
+
51
+ # replace output content
52
+ post.output = post.output.gsub(/#{tag}.*#{tag}/m, post.content)
53
+ end
54
+
55
+ # auto add pre-event
56
+ register container, event.to_s.sub("post", "pre").to_sym
57
+ elsif event.to_s.start_with?("pre")
58
+ Jekyll::Hooks.register container, event do |post|
59
+ # wrap post content with tags
60
+ tag = self.content_tag(post)
61
+
62
+ handle.call post
63
+
64
+ post.content = "#{tag}\n#{post.content}\n#{tag}"
65
+ end
66
+ end
67
+ end
68
+ end
69
+ end
@@ -0,0 +1,45 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Jekyll::Spaceship
4
+ class Register
5
+ def self.walk(start, &block)
6
+ Dir.foreach start do |x|
7
+ path = File.join(start, x)
8
+ if x == "." or x == ".."
9
+ next
10
+ elsif File.directory?(path)
11
+ block.call(path + "/")
12
+ walk path
13
+ else
14
+ block.call(path)
15
+ end
16
+ end
17
+ end
18
+
19
+ def self.use(name)
20
+ name = name.to_s.gsub(/-/, '').downcase
21
+ name += 'processor' unless name.match?(/processor$/)
22
+
23
+ self.walk(File.join(File.dirname(__FILE__), '/../processors')) do |path|
24
+ filename = File.basename(path, '.rb')
25
+ next if filename.gsub(/-/, '').downcase != name
26
+
27
+ Logger.log "use #{filename}"
28
+
29
+ require path
30
+
31
+ constants = Jekyll::Spaceship.constants.select do |c|
32
+ c.downcase.to_s == name
33
+ end
34
+
35
+ next if constants.first.nil?
36
+
37
+ _class = Jekyll::Spaceship.const_get(constants.first)
38
+
39
+ next unless _class.is_a? Class
40
+
41
+ _class.new
42
+ end
43
+ end
44
+ end
45
+ end
@@ -0,0 +1,20 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'nokogiri'
4
+
5
+ module Jekyll::Spaceship
6
+ class MathjaxProcessor < Processor
7
+ register :posts, :post_render
8
+
9
+ def on_posts_post_render(post)
10
+ # use nokogiri to parse html
11
+ doc = Nokogiri::HTML(post.output)
12
+
13
+ params = "config=TeX-AMS-MML_HTMLorMML"
14
+ src = "//cdn.mathjax.org/mathjax/latest/MathJax.js?#{params}"
15
+ doc.at('head').add_child("<script src=\"#{src}\"></script>")
16
+
17
+ post.output = doc.to_s
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,38 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Jekyll::Spaceship
4
+ class PlantUMLProcessor < Processor
5
+ register :posts, :pre_render
6
+
7
+ def on_posts_pre_render(post)
8
+ # match default plantuml block and code block
9
+ pattern = /(@startuml((?:.|\n)*?)@enduml)|(`{3}\s*plantuml((?:.|\n)*?)`{3})/
10
+ post.content.scan pattern do |match|
11
+ match = match.filter { |m| not m.nil? }
12
+ block = match[0]
13
+ code = match[1]
14
+ Logger.log "handle plantuml block - #{post.path.gsub(/.*_posts\//, '')}"
15
+ post.content = post.content.gsub(
16
+ block,
17
+ handle_plantuml(code)
18
+ )
19
+ end
20
+ end
21
+
22
+ def handle_plantuml(code)
23
+ # wrap plantuml code
24
+ uml = "@startuml#{code}@enduml"
25
+
26
+ dir = File.dirname(__FILE__)
27
+ jar = dir + "/../utils/plantuml/plantuml.jar"
28
+ echo = "echo -e \"#{uml.gsub('"', '\"')}\""
29
+ plantuml = "java -jar \"#{jar}\" -pipe 2>/dev/null"
30
+
31
+ # exec plantuml.jar and output base64 data
32
+ base64 = `#{echo} | #{plantuml} | base64`
33
+
34
+ # return img tag
35
+ "<img src=\"data:image/png;base64, #{base64}\">"
36
+ end
37
+ end
38
+ end
@@ -0,0 +1,193 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "ostruct"
4
+ require 'nokogiri'
5
+
6
+ module Jekyll::Spaceship
7
+ class TableProcessor < Processor
8
+ register :posts, :pre_render, :post_render
9
+
10
+ def on_posts_pre_render(post)
11
+ # pre-handle table in markdown
12
+ post.content = post.content.gsub(/\|(?=\|)/, '\\|')
13
+ end
14
+
15
+ def on_posts_post_render(post)
16
+ # use nokogiri to parse html content
17
+ doc = Nokogiri::HTML(post.content)
18
+
19
+ data = OpenStruct.new(_: OpenStruct.new)
20
+ data.reset = ->(scope, namespace = nil) {
21
+ data._.marshal_dump.each do |key, val|
22
+ if namespace == key or namespace.nil?
23
+ data._[key][scope] = OpenStruct.new
24
+ end
25
+ end
26
+ }
27
+ data.scope = ->(namespace) {
28
+ if not data._[namespace]
29
+ data._[namespace] = OpenStruct.new(
30
+ table: OpenStruct.new,
31
+ row: OpenStruct.new
32
+ )
33
+ end
34
+ data._[namespace]
35
+ }
36
+
37
+ doc.css('table').each do |table|
38
+ rows = table.css('tr')
39
+ data.table= table
40
+ data.rows = rows
41
+ data.reset.call :table
42
+ rows.each do |row|
43
+ cells = row.css('th, td')
44
+ data.row = row
45
+ data.cells = cells
46
+ data.reset.call :row
47
+ cells.each do |cell|
48
+ data.cell = cell
49
+ handle_colspan(data)
50
+ handle_multi_rows(data)
51
+ handle_text_align(data)
52
+ handle_rowspan(data)
53
+ end
54
+ end
55
+ end
56
+
57
+ post.content = doc.at('body').inner_html
58
+ end
59
+
60
+ def handle_colspan(data)
61
+ scope = data.scope.call __method__
62
+ scope_table = scope.table
63
+ scope_row = scope.row
64
+ cells = data.cells
65
+ cell = data.cell
66
+
67
+ if scope_table.row != data.row
68
+ scope_table.row = data.row
69
+ scope_row.colspan = 0
70
+ end
71
+
72
+ # handle colspan
73
+ result = cell.content.match(/(\s*\|)+$/)
74
+ if cell == cells.last and scope_row.colspan > 0
75
+ range = (cells.count - scope_row.colspan)...cells.count
76
+ for i in range do
77
+ cells[i].remove
78
+ end
79
+ end
80
+ if result
81
+ result = result[0]
82
+ scope_row.colspan += result.scan(/\|/).count
83
+ cell.content = cell.content.gsub(/(\s*\|)+$/, "")
84
+ cell.set_attribute("colspan", scope_row.colspan + 1)
85
+ end
86
+ end
87
+
88
+ def handle_multi_rows(data)
89
+ scope = data.scope.call __method__
90
+ scope_table = scope.table
91
+ cells = data.cells
92
+ row = data.row
93
+ cell = data.cell
94
+
95
+ if scope_table.table != data.table
96
+ scope_table.table = data.table
97
+ scope_table.multi_row_cells = nil
98
+ scope_table.multi_row_start = false
99
+ end
100
+
101
+ # handle multi-rows
102
+ return if cell != cells.last
103
+
104
+ match = cell.content.match?(/\\$/)
105
+ if match
106
+ cell.content = cell.content.gsub(/\\$/, '')
107
+ if not scope_table.multi_row_start
108
+ scope_table.multi_row_cells = cells
109
+ scope_table.multi_row_start = true
110
+ end
111
+ end
112
+
113
+ if scope_table.multi_row_cells != cells and scope_table.multi_row_start
114
+ for i in 0...scope_table.multi_row_cells.count do
115
+ scope_table.multi_row_cells[i].inner_html += "<br>#{cells[i].content}"
116
+ end
117
+ row.remove
118
+ end
119
+ scope_table.multi_row_start = false if not match
120
+ end
121
+
122
+ def handle_rowspan(data)
123
+ scope = data.scope.call __method__
124
+ scope_table = scope.table
125
+ scope_row = scope.row
126
+ cell = data.cell
127
+ cells = data.cells
128
+
129
+ if scope_table.table != data.table
130
+ scope_table.table = data.table
131
+ scope_table.span_row_cells = []
132
+ end
133
+
134
+ if scope_row.row != data.row
135
+ scope_row.row = data.row
136
+ scope_row.col_index = 0
137
+ end
138
+
139
+ # handle rowspan
140
+ span_cell = scope_table.span_row_cells[scope_row.col_index]
141
+ if span_cell and cell.content.match?(/^\^{2}/)
142
+ cell.content = cell.content.gsub(/^\^{2}/, "")
143
+ span_cell.inner_html += "<br>#{cell.content}"
144
+ rowspan = span_cell.get_attribute("rowspan") || 1
145
+ rowspan = rowspan.to_i + 1
146
+ span_cell.set_attribute("rowspan", "#{rowspan}")
147
+ cell.remove
148
+ else
149
+ scope_table.span_row_cells[scope_row.col_index] = cell
150
+ end
151
+
152
+ scope_row.col_index += 1
153
+ end
154
+
155
+ def handle_text_align(data)
156
+ cell = data.cell
157
+
158
+ # pre-handle text align
159
+ align = 0
160
+ if cell.content.match?(/^:(?!:)/)
161
+ cell.content = cell.content.gsub(/^:/, "")
162
+ align += 1
163
+ end
164
+ if cell.content.match?(/(?<!:):$/)
165
+ cell.content = cell.content.gsub(/:$/, "")
166
+ align += 2
167
+ end
168
+
169
+ # handle escape colon
170
+ cell.content = cell.content.gsub(/::/, ":")
171
+
172
+ # handle text align
173
+ return if align == 0
174
+
175
+ style = cell.get_attribute("style")
176
+ if align == 1
177
+ align = "text-align: left"
178
+ elsif align == 2
179
+ align = "text-align: right"
180
+ elsif align == 3
181
+ align = "text-align: center"
182
+ end
183
+
184
+ # handle existed inline-style
185
+ if style&.match?(/text-align:.+/)
186
+ style = style.gsub(/text-align:.+/, align)
187
+ else
188
+ style = align
189
+ end
190
+ cell.set_attribute("style", style)
191
+ end
192
+ end
193
+ end
@@ -0,0 +1,7 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Jekyll
4
+ module Spaceship
5
+ VERSION = "0.1.0"
6
+ end
7
+ end
metadata ADDED
@@ -0,0 +1,127 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: jekyll-spaceship
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - jeffreytse
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2019-12-04 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: nokogiri
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.6'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.6'
27
+ - !ruby/object:Gem::Dependency
28
+ name: jekyll
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '3.7'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '3.7'
41
+ - !ruby/object:Gem::Dependency
42
+ name: bundler
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '1.10'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '1.10'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rake
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '12.0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '12.0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: rspec
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '3.0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '3.0'
83
+ description:
84
+ email:
85
+ - jeffreytse.mail@gmail.com
86
+ executables: []
87
+ extensions: []
88
+ extra_rdoc_files: []
89
+ files:
90
+ - ".gitignore"
91
+ - Gemfile
92
+ - LICENSE.txt
93
+ - jekyll-spaceship.gemspec
94
+ - lib/jekyll-spaceship.rb
95
+ - lib/jekyll-spaceship/cores/logger.rb
96
+ - lib/jekyll-spaceship/cores/processor.rb
97
+ - lib/jekyll-spaceship/cores/register.rb
98
+ - lib/jekyll-spaceship/processors/mathjax-processor.rb
99
+ - lib/jekyll-spaceship/processors/plantuml-processor.rb
100
+ - lib/jekyll-spaceship/processors/table-processor.rb
101
+ - lib/jekyll-spaceship/utils/plantuml/plantuml.jar
102
+ - lib/jekyll-spaceship/version.rb
103
+ homepage: https://github.com/jeffreytse/jekyll-spaceship
104
+ licenses:
105
+ - MIT
106
+ metadata: {}
107
+ post_install_message:
108
+ rdoc_options: []
109
+ require_paths:
110
+ - lib
111
+ required_ruby_version: !ruby/object:Gem::Requirement
112
+ requirements:
113
+ - - ">="
114
+ - !ruby/object:Gem::Version
115
+ version: 2.3.0
116
+ required_rubygems_version: !ruby/object:Gem::Requirement
117
+ requirements:
118
+ - - ">="
119
+ - !ruby/object:Gem::Version
120
+ version: '0'
121
+ requirements: []
122
+ rubygems_version: 3.0.6
123
+ signing_key:
124
+ specification_version: 4
125
+ summary: A Jekyll plugin to provide powerful supports for table, mathjax, plantuml,
126
+ etc.
127
+ test_files: []