jekyll-wikilinks 0.0.7 → 0.0.8
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 +4 -4
- data/lib/jekyll-wikilinks/config.rb +17 -13
- data/lib/jekyll-wikilinks/patch/doc_manager.rb +43 -19
- data/lib/jekyll-wikilinks/patch/site.rb +1 -1
- data/lib/jekyll-wikilinks/plugins/filter.rb +23 -21
- data/lib/jekyll-wikilinks/plugins/generator.rb +4 -4
- data/lib/jekyll-wikilinks/util/link_index.rb +42 -65
- data/lib/jekyll-wikilinks/util/parser.rb +70 -364
- data/lib/jekyll-wikilinks/util/regex.rb +36 -30
- data/lib/jekyll-wikilinks/util/wikilink.rb +278 -0
- data/lib/jekyll-wikilinks/version.rb +1 -1
- data/lib/jekyll-wikilinks.rb +3 -22
- metadata +3 -2
@@ -0,0 +1,278 @@
|
|
1
|
+
# wiki data structures
|
2
|
+
require_relative "regex"
|
3
|
+
|
4
|
+
module Jekyll
|
5
|
+
module WikiLinks
|
6
|
+
|
7
|
+
# wikilink classes know everything about the original markdown syntax and its semantic meaning
|
8
|
+
|
9
|
+
class WikiLinkBlock
|
10
|
+
attr_accessor :link_type, :filenames
|
11
|
+
|
12
|
+
# parameters ordered by appearance in regex
|
13
|
+
def initialize(doc_mngr, context_filename, link_type, bullet_type=nil)
|
14
|
+
@doc_mngr ||= doc_mngr
|
15
|
+
@context_filename ||= context_filename
|
16
|
+
@link_type ||= link_type
|
17
|
+
@bullet_type ||= bullet_type
|
18
|
+
@filenames = []
|
19
|
+
end
|
20
|
+
|
21
|
+
def add_item(filename)
|
22
|
+
Jekyll.logger.error("Jekyll-Wikilinks: 'filename' required") if filename.nil? || filename.empty?
|
23
|
+
@filenames << filename
|
24
|
+
end
|
25
|
+
|
26
|
+
# data
|
27
|
+
|
28
|
+
def md_regex
|
29
|
+
if !is_typed? || !has_filenames?
|
30
|
+
Jekyll.logger.error("Jekyll-Wikilinks: WikiLinkBlock.md_regex error -- type: #{@link_type}, fnames: #{@filenames.inspect}, for: #{@context_filename}")
|
31
|
+
end
|
32
|
+
# comma (including singles)
|
33
|
+
if @bullet_type.nil?
|
34
|
+
link_type = /#{@link_type}#{REGEX_LINK_TYPE}/i
|
35
|
+
tmp_filenames = @filenames.dup
|
36
|
+
first_filename = /\s*#{REGEX_LINK_LEFT}#{tmp_filenames.shift()}#{REGEX_LINK_RIGHT}\s*/i
|
37
|
+
filename_strs = tmp_filenames.map { |f| /,\s*#{REGEX_LINK_LEFT}#{f}#{REGEX_LINK_RIGHT}\s*/i }
|
38
|
+
md_regex = /#{link_type}#{first_filename}#{filename_strs.join('')}\n/i
|
39
|
+
# mkdn
|
40
|
+
elsif !@bullet_type.match(REGEX_BULLET).nil?
|
41
|
+
link_type = /#{@link_type}#{REGEX_LINK_TYPE}\n/i
|
42
|
+
filename_strs = @filenames.map { |f| /\s{0,3}#{Regexp.escape(@bullet_type)}\s#{REGEX_LINK_LEFT}#{f}#{REGEX_LINK_RIGHT}\n/i }
|
43
|
+
md_regex = /#{link_type}#{filename_strs.join("")}/i
|
44
|
+
else
|
45
|
+
Jekyll.logger.error("Jekyll-Wikilinks: WikiLinkBlock.bullet_type error: #{@bullet_type}")
|
46
|
+
end
|
47
|
+
return md_regex
|
48
|
+
end
|
49
|
+
|
50
|
+
def md_str
|
51
|
+
if !is_typed? || !has_filenames?
|
52
|
+
Jekyll.logger.error("Jekyll-Wikilinks: WikiLinkBlockList.md_str error -- type: #{@link_type}, fnames: #{@filenames.inspect}, for: #{@context_filename}")
|
53
|
+
end
|
54
|
+
# comma (including singles)
|
55
|
+
if @bullet_type.nil?
|
56
|
+
link_type = "#{@link_type}::"
|
57
|
+
filename_strs = @filenames.map { |f| "\[\[#{f}\]\]," }
|
58
|
+
md_str = (link_type + filename_strs.join('')).delete_suffix(",")
|
59
|
+
# mkdn
|
60
|
+
elsif !@bullet_type.match(REGEX_BULLET).nil?
|
61
|
+
link_type = "#{@link_type}::\n"
|
62
|
+
filename_strs = @filenames.map { |f| li[0] + " \[\[#{li[1]}\]\]\n" }
|
63
|
+
md_str = link_type + filename_strs.join('')
|
64
|
+
else
|
65
|
+
Jekyll.logger.error("Jekyll-Wikilinks: 'bullet_type' invalid: #{@bullet_type}")
|
66
|
+
end
|
67
|
+
return md_str
|
68
|
+
end
|
69
|
+
|
70
|
+
def urls
|
71
|
+
# return @filenames.map { |f| @doc_mngr.get_doc_by_fname(f) }
|
72
|
+
urls = []
|
73
|
+
@filenames.each do |f|
|
74
|
+
doc = @doc_mngr.get_doc_by_fname(f)
|
75
|
+
urls << doc.url if !doc.nil?
|
76
|
+
end
|
77
|
+
return urls
|
78
|
+
end
|
79
|
+
|
80
|
+
# 'fm' -> frontmatter
|
81
|
+
|
82
|
+
def context_fm_data
|
83
|
+
return {
|
84
|
+
'type' => @link_type,
|
85
|
+
'urls' => [self.context_doc.url],
|
86
|
+
}
|
87
|
+
end
|
88
|
+
|
89
|
+
def linked_fm_data
|
90
|
+
return {
|
91
|
+
'type' => @link_type,
|
92
|
+
'urls' => self.urls,
|
93
|
+
}
|
94
|
+
end
|
95
|
+
|
96
|
+
def context_doc
|
97
|
+
return @doc_mngr.get_doc_by_fname(@context_filename)
|
98
|
+
end
|
99
|
+
|
100
|
+
def linked_docs
|
101
|
+
docs = []
|
102
|
+
@filenames.each do |f|
|
103
|
+
doc = @doc_mngr.get_doc_by_fname(f)
|
104
|
+
docs << doc if !doc.nil?
|
105
|
+
end
|
106
|
+
return docs
|
107
|
+
end
|
108
|
+
|
109
|
+
# descriptor methods
|
110
|
+
|
111
|
+
def has_filenames?
|
112
|
+
return !@filenames.nil? && !@filenames.empty?
|
113
|
+
end
|
114
|
+
|
115
|
+
def is_typed?
|
116
|
+
return !@link_type.nil? && !@link_type.empty?
|
117
|
+
end
|
118
|
+
|
119
|
+
# validation methods
|
120
|
+
|
121
|
+
def is_valid?
|
122
|
+
return false if !is_typed?
|
123
|
+
return false if !has_filenames?
|
124
|
+
@filenames.each do |f|
|
125
|
+
return false if !@doc_mngr.file_exists?(f)
|
126
|
+
end
|
127
|
+
return true
|
128
|
+
end
|
129
|
+
end
|
130
|
+
|
131
|
+
class WikiLinkInline
|
132
|
+
attr_accessor :context_filename, :embed, :link_type, :filename, :header_txt, :block_id, :label_txt
|
133
|
+
|
134
|
+
FILENAME = "filename"
|
135
|
+
HEADER_TXT = "header_txt"
|
136
|
+
BLOCK_ID = "block_id"
|
137
|
+
|
138
|
+
# parameters ordered by appearance in regex
|
139
|
+
def initialize(doc_mngr, context_filename, embed, link_type, filename, header_txt, block_id, label_txt)
|
140
|
+
@doc_mngr ||= doc_mngr
|
141
|
+
@context_filename ||= context_filename
|
142
|
+
@embed ||= embed
|
143
|
+
@link_type ||= link_type
|
144
|
+
@filename ||= filename
|
145
|
+
@header_txt ||= header_txt
|
146
|
+
@block_id ||= block_id
|
147
|
+
@label_txt ||= label_txt
|
148
|
+
end
|
149
|
+
|
150
|
+
# escape square brackets if they appear in label text
|
151
|
+
def label_txt
|
152
|
+
return @label_txt.sub("[", "\\[").sub("]", "\\]")
|
153
|
+
end
|
154
|
+
|
155
|
+
# data
|
156
|
+
|
157
|
+
def md_regex
|
158
|
+
regex_embed = embedded? ? REGEX_LINK_EMBED : %r{}
|
159
|
+
regex_link_type = is_typed? ? %r{#{@link_type}#{REGEX_LINK_TYPE}} : %r{}
|
160
|
+
filename = described?(FILENAME) ? @filename : ""
|
161
|
+
if described?(HEADER_TXT)
|
162
|
+
header = %r{#{REGEX_LINK_HEADER}#{@header_txt}}
|
163
|
+
block = %r{}
|
164
|
+
elsif described?(BLOCK_ID)
|
165
|
+
header = %r{}
|
166
|
+
block = %r{#{REGEX_LINK_BLOCK}#{@block_id}}
|
167
|
+
elsif !described?(FILENAME)
|
168
|
+
Jekyll.logger.error("Jekyll-Wikilinks: WikiLinkInline.md_regex error")
|
169
|
+
end
|
170
|
+
label_ = labelled? ? %r{#{REGEX_LINK_LABEL}#{label_txt}} : %r{}
|
171
|
+
return %r{#{regex_embed}#{regex_link_type}#{REGEX_LINK_LEFT}#{filename}#{header}#{block}#{label_}#{REGEX_LINK_RIGHT}}
|
172
|
+
end
|
173
|
+
|
174
|
+
def md_str
|
175
|
+
embed = embedded? ? "!" : ""
|
176
|
+
link_type = is_typed? ? "#{@link_type}::" : ""
|
177
|
+
filename = described?(FILENAME) ? @filename : ""
|
178
|
+
if described?(HEADER_TXT)
|
179
|
+
header = "\##{@header_txt}"
|
180
|
+
block = ""
|
181
|
+
elsif described?(BLOCK_ID)
|
182
|
+
header = ""
|
183
|
+
block = "\#\^#{@block_id}"
|
184
|
+
elsif !described?(FILENAME)
|
185
|
+
Jekyll.logger.error("Jekyll-Wikilinks: WikiLinkInline.md_str error")
|
186
|
+
end
|
187
|
+
label_ = labelled? ? "\|#{@label_txt}" : ""
|
188
|
+
return "#{embed}#{link_type}\[\[#{filename}#{header}#{block}#{label_}\]\]"
|
189
|
+
end
|
190
|
+
|
191
|
+
# 'fm' -> frontmatter
|
192
|
+
|
193
|
+
def context_fm_data
|
194
|
+
return {
|
195
|
+
'type' => @link_type,
|
196
|
+
'url' => self.context_doc.url,
|
197
|
+
}
|
198
|
+
end
|
199
|
+
|
200
|
+
def linked_fm_data
|
201
|
+
return {
|
202
|
+
'type' => @link_type,
|
203
|
+
'url' => self.linked_doc.url,
|
204
|
+
}
|
205
|
+
end
|
206
|
+
|
207
|
+
def context_doc
|
208
|
+
return @doc_mngr.get_doc_by_fname(@context_filename)
|
209
|
+
end
|
210
|
+
|
211
|
+
def linked_doc
|
212
|
+
return @doc_mngr.get_doc_by_fname(@filename)
|
213
|
+
end
|
214
|
+
|
215
|
+
def linked_img
|
216
|
+
return @doc_mngr.get_image_by_fname(@filename) if self.is_img?
|
217
|
+
return nil
|
218
|
+
end
|
219
|
+
|
220
|
+
# descriptor methods
|
221
|
+
|
222
|
+
# def describe
|
223
|
+
# return {
|
224
|
+
# 'level' => level,
|
225
|
+
# 'labelled' => labelled?,
|
226
|
+
# 'embedded' => embedded?,
|
227
|
+
# 'typed_link' => is_typed?,
|
228
|
+
# }
|
229
|
+
# end
|
230
|
+
|
231
|
+
def labelled?
|
232
|
+
return !@label_txt.nil? && !@label_txt.empty?
|
233
|
+
end
|
234
|
+
|
235
|
+
def is_typed?
|
236
|
+
return !@link_type.nil? && !@link_type.empty?
|
237
|
+
end
|
238
|
+
|
239
|
+
def embedded?
|
240
|
+
return !@embed.nil? && @embed == "!"
|
241
|
+
end
|
242
|
+
|
243
|
+
def is_img?
|
244
|
+
# github supported image formats: https://docs.github.com/en/github/managing-files-in-a-repository/working-with-non-code-files/rendering-and-diffing-images
|
245
|
+
return SUPPORTED_IMG_FORMATS.any?{ |ext| ext == File.extname(@filename).downcase }
|
246
|
+
end
|
247
|
+
|
248
|
+
def is_img_svg?
|
249
|
+
return File.extname(@filename).downcase == ".svg"
|
250
|
+
end
|
251
|
+
|
252
|
+
# this method helps to make the 'WikiLinkInline.level' code read like a clean truth table.
|
253
|
+
def described?(chunk)
|
254
|
+
return (!@filename.nil? && !@filename.empty?) if chunk == FILENAME
|
255
|
+
return (!@header_txt.nil? && !@header_txt.empty?) if chunk == HEADER_TXT
|
256
|
+
return (!@block_id.nil? && !@block_id.empty?) if chunk == BLOCK_ID
|
257
|
+
Jekyll.logger.error("Jekyll-Wikilinks: There is no link level '#{chunk}' in the WikiLink Class")
|
258
|
+
end
|
259
|
+
|
260
|
+
def level
|
261
|
+
return "file" if described?(FILENAME) && !described?(HEADER_TXT) && !described?(BLOCK_ID)
|
262
|
+
return "header" if described?(FILENAME) && described?(HEADER_TXT) && !described?(BLOCK_ID)
|
263
|
+
return "block" if described?(FILENAME) && !described?(HEADER_TXT) && described?(BLOCK_ID)
|
264
|
+
return "invalid"
|
265
|
+
end
|
266
|
+
|
267
|
+
# validation methods
|
268
|
+
|
269
|
+
def is_valid?
|
270
|
+
return false if !@doc_mngr.file_exists?(@filename)
|
271
|
+
return false if (self.level == "header") && !@doc_mngr.doc_has_header?(self.linked_doc, @header_txt)
|
272
|
+
return false if (self.level == "block") && !@doc_mngr.doc_has_block_id?(self.linked_doc, @block_id)
|
273
|
+
return true
|
274
|
+
end
|
275
|
+
end
|
276
|
+
|
277
|
+
end
|
278
|
+
end
|
data/lib/jekyll-wikilinks.rb
CHANGED
@@ -3,8 +3,6 @@ require "jekyll"
|
|
3
3
|
|
4
4
|
require_relative "jekyll-wikilinks/version"
|
5
5
|
|
6
|
-
# in order of expected execution
|
7
|
-
|
8
6
|
# setup config
|
9
7
|
require_relative "jekyll-wikilinks/config"
|
10
8
|
Jekyll::Hooks.register :site, :after_init do |site|
|
@@ -22,28 +20,11 @@ Jekyll::Hooks.register :site, :post_read do |site|
|
|
22
20
|
end
|
23
21
|
end
|
24
22
|
|
25
|
-
#
|
26
|
-
require_relative "jekyll-wikilinks/plugins/converter"
|
27
|
-
|
28
|
-
# generate
|
23
|
+
# parse wikilinks / generate metadata
|
29
24
|
require_relative "jekyll-wikilinks/plugins/generator"
|
30
25
|
|
31
|
-
# convert
|
32
|
-
|
33
|
-
# Jekyll:WikiLinks::Parser.parse_blocks(doc)
|
34
|
-
# @site.link_index.populate_fores(doc, typed_link_blocks, md_docs)
|
35
|
-
# end
|
36
|
-
|
37
|
-
# convert backs
|
38
|
-
# Jekyll::Hooks.register :documents, :pre_convert do |doc|
|
39
|
-
# Jekyll:WikiLinks::Parser.parse_inlines(doc)
|
40
|
-
# @site.link_index.populate_backs(doc, typed_link_blocks, md_docs)
|
41
|
-
# end
|
42
|
-
|
43
|
-
# generate metadata
|
44
|
-
# Jekyll::Hooks.register :documents, :post_convert do |doc|
|
45
|
-
# Jekyll:WikiLinks::Generator.generate(doc)
|
46
|
-
# end
|
26
|
+
# convert weblinks
|
27
|
+
require_relative "jekyll-wikilinks/plugins/converter"
|
47
28
|
|
48
29
|
# hook up liquid filters
|
49
30
|
require_relative "jekyll-wikilinks/plugins/filter"
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: jekyll-wikilinks
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.8
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- manunamz
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2021-11-
|
11
|
+
date: 2021-11-23 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: jekyll
|
@@ -56,6 +56,7 @@ files:
|
|
56
56
|
- lib/jekyll-wikilinks/util/link_index.rb
|
57
57
|
- lib/jekyll-wikilinks/util/parser.rb
|
58
58
|
- lib/jekyll-wikilinks/util/regex.rb
|
59
|
+
- lib/jekyll-wikilinks/util/wikilink.rb
|
59
60
|
- lib/jekyll-wikilinks/version.rb
|
60
61
|
homepage: https://github.com/manunamz/jekyll-wikilinks
|
61
62
|
licenses:
|