jekyll-wikilinks 0.0.9 → 0.0.12

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: eb34f22e418709f097d81cbcd13a0917ab903247145bb0db4846a346c0b86f38
4
- data.tar.gz: 62bff9a52a5e965a542071a0d577ea3a962c3acd604643d0955de449e924de7a
3
+ metadata.gz: 19fe7bb2a769e61e5d1d554bdbfd2553d7e8359800d06ed58fb5a77e1ed39e60
4
+ data.tar.gz: cc5a57fd28b83455523a2e5a67f1f5e9f3a6a2d335e6bc72b55650fc05aa836b
5
5
  SHA512:
6
- metadata.gz: '087c441fc74aa0da7659bf070eb3c2b079033d8a567002e00603825498afb6ef30d259372e18799506e4fc2b7b3cc3c3501f0a67dd4e559eefd463214ee6a502'
7
- data.tar.gz: 8abe1efd8a9b2eecd13b1d83599c09ee29db9e2e943ed097bf49aa000fd1a6caea2c6cedc1bb05fdce6fb4bfa3d1852b01221420e91f647d8aff61b7bce6ac50
6
+ metadata.gz: 8e24fd21d6c63de04a1e929f0c3cdb5e040643ff2cd1d1cafa4f8f6caf8f067ff96121ee276876f3f43b5f6b63825fe05bc0b2f7dddfd6ceff3f0b92024a8ab0
7
+ data.tar.gz: 2883023cd22018b3d3b8bc76d5960b98c3e906021488cf3928914b9a16568b69b45e08782a909568a8ab851b806ec182ff89e3be7f14b7d491d6a8e34e209d6e
@@ -4,6 +4,7 @@ require_relative "../util/regex"
4
4
  module Jekyll
5
5
  module WikiLinks
6
6
 
7
+ # todo: move these methods to the 'WikiLink' classes...?
7
8
  #
8
9
  # this class is responsible for answering any questions
9
10
  # related to jekyll markdown documents
@@ -55,6 +56,13 @@ module Jekyll
55
56
  return docs[0]
56
57
  end
57
58
 
59
+ def get_doc_by_fpath(file_path)
60
+ Jekyll.logger.error("Jekyll-Wikilinks: Must provide a 'file_path'") if file_path.nil? || file_path.empty?
61
+ docs = @md_docs.select{ |d| d.relative_path == (file_path + ".md") }
62
+ return nil if docs.nil? || docs.empty? || docs.size > 1
63
+ return docs[0]
64
+ end
65
+
58
66
  def get_doc_by_url(url)
59
67
  Jekyll.logger.error("Jekyll-Wikilinks: Must provide a 'url'") if url.nil? || url.empty?
60
68
  docs = @md_docs.select{ |d| d.url == url }
@@ -78,12 +86,15 @@ module Jekyll
78
86
 
79
87
  # validators
80
88
 
81
- def file_exists?(filename)
89
+ def file_exists?(filename, file_path=nil)
82
90
  Jekyll.logger.error("Jekyll-Wikilinks: Must provide a 'filename'") if filename.nil? || filename.empty?
83
- docs = @md_docs.select{ |d| File.basename(d.basename, File.extname(d.basename)) == filename }
84
- docs += @static_files.select{ |d| File.basename(d.relative_path) == filename }
85
- return false if docs.nil? || docs.empty? || docs.size > 1
86
- return true
91
+ if file_path.nil?
92
+ return false if get_doc_by_fname(filename).nil? && get_image_by_fname(filename).nil?
93
+ return true
94
+ else
95
+ return false if get_doc_by_fpath(file_path).nil?
96
+ return true
97
+ end
87
98
  end
88
99
 
89
100
  def doc_has_header?(doc, header)
@@ -23,46 +23,56 @@ module Jekyll
23
23
  end
24
24
 
25
25
  def populate(doc, wikilink_blocks, wikilink_inlines)
26
- # blocks
26
+ # #
27
+ # blocks #
28
+ # #
27
29
  wikilink_blocks.each do |wlbl|
28
30
  if wlbl.is_valid?
31
+ #
29
32
  # attributes
33
+ #
30
34
  target_attr = @index[doc.url].attributes.detect { |atr| atr['type'] == wlbl.link_type }
31
- ## create
35
+ # create
32
36
  if target_attr.nil?
33
37
  @index[doc.url].attributes << wlbl.linked_fm_data
34
- ## append
38
+ # append
35
39
  else
36
- target_attr['urls'] += wlbl.urls
40
+ target_attr['urls'] += wlbl.urls
37
41
  end
42
+ ## append missing docs
43
+ @index[doc.url].missing += wlbl.missing_doc_filenames
44
+ #
38
45
  # attributed
46
+ #
39
47
  wlbl.linked_docs.each do |linked_doc|
40
48
  target_attr = @index[linked_doc.url].attributed.detect { |atr| atr['type'] == wlbl.link_type }
41
- ## create
49
+ # create
42
50
  if target_attr.nil?
43
51
  @index[linked_doc.url].attributed << wlbl.context_fm_data
44
- ## append
52
+ # append
45
53
  else
46
54
  target_attr['urls'] << doc.url
47
55
  end
48
56
  end
49
57
  else
50
- wlbl.filenames.each do |fn|
51
- @index[doc.url].missing << fn
52
- end
58
+ #
59
+ # invalid || empty
60
+ #
61
+ @index[doc.url].missing += wlbl.missing_doc_filenames
53
62
  end
54
63
  end
55
- # inlines
64
+ # #
65
+ # inlines #
66
+ # #
56
67
  wikilink_inlines.each do |wlil|
57
- if !wlil.is_img?
58
- if wlil.is_valid?
59
- # forelink
60
- @index[doc.url].forelinks << wlil.linked_fm_data
61
- # backlink
62
- @index[wlil.linked_doc.url].backlinks << wlil.context_fm_data
63
- else
64
- @index[doc.url].missing << wlil.filename
65
- end
68
+ return if wlil.is_img?
69
+ if wlil.is_valid?
70
+ # forelink
71
+ @index[doc.url].forelinks << wlil.linked_fm_data
72
+ # backlink
73
+ @index[wlil.linked_doc.url].backlinks << wlil.context_fm_data
74
+ else
75
+ @index[doc.url].missing << wlil.filename
66
76
  end
67
77
  end
68
78
  end
@@ -76,6 +76,8 @@ module Jekyll
76
76
  end
77
77
  # replace text
78
78
  return if @wikilink_inlines.nil?
79
+ # process typed wikilinks first so we don't accidentally
80
+ # overwrite them when handling untyped wikilinks
79
81
  self.sort_typed_first
80
82
  @wikilink_inlines.each do |wikilink|
81
83
  doc_content.gsub!(
@@ -125,24 +127,34 @@ module Jekyll
125
127
  # markdown file processing
126
128
  linked_doc = wikilink.linked_doc
127
129
  link_type_txt = wikilink.is_typed? ? " #{$wiki_conf.css_name("typed")} #{wikilink.link_type}" : ""
128
-
129
- inner_txt = wikilink.label_txt if wikilink.labelled?
130
130
  lnk_doc_rel_url = relative_url(linked_doc.url)
131
-
132
- if (wikilink.level == "file")
133
- inner_txt = "#{linked_doc['title'].downcase}" if inner_txt.nil?
131
+ if wikilink.labelled?
132
+ inner_txt = wikilink.label_txt
133
+ elsif linked_doc.data.keys.include?('title')
134
+ inner_txt = linked_doc.data['title'].downcase
135
+ # in case there is no 'title' frontmatter attribute
136
+ # (i'm seeing deprecation warnings, but there might
137
+ # be bugs caused by not using this...)
138
+ elsif linked_doc.respond_to?(:title)
139
+ inner_txt = linked_doc.title.downcase
140
+ # pages don't have automatically generated titles
141
+ else
142
+ inner_txt = Jekyll::Utils.slugify(linked_doc.basename)
143
+ end
144
+ # level-specific
145
+ if (wikilink.level == "file_path" || wikilink.level == "filename")
134
146
  return build_html_embed(
135
- linked_doc['title'],
147
+ linked_doc.title,
136
148
  linked_doc.content,
137
149
  lnk_doc_rel_url
138
150
  ) if wikilink.embedded?
139
151
  elsif (wikilink.level == "header")
140
152
  # from: https://github.com/jekyll/jekyll/blob/6855200ebda6c0e33f487da69e4e02ec3d8286b7/Rakefile#L74
141
153
  lnk_doc_rel_url += "\#" + Jekyll::Utils.slugify(wikilink.header_txt)
142
- inner_txt = "#{linked_doc['title'].downcase} > #{wikilink.header_txt.downcase}" if inner_txt.nil?
154
+ inner_txt += " > #{wikilink.header_txt.downcase}" if !wikilink.labelled?
143
155
  elsif (wikilink.level == "block")
144
156
  lnk_doc_rel_url += "\#" + wikilink.block_id
145
- inner_txt = "#{linked_doc['title'].downcase} > ^#{wikilink.block_id}" if inner_txt.nil?
157
+ inner_txt += " > ^#{wikilink.block_id}" if !wikilink.labelled?
146
158
  else
147
159
  Jekyll.logger.error("Jekyll-Wikilinks: Invalid wikilink level")
148
160
  end
@@ -12,7 +12,7 @@ module Jekyll
12
12
  SUPPORTED_IMG_FORMATS = Set.new(['.png', '.jpg', '.gif', '.psd', '.svg'])
13
13
 
14
14
  # wikilink constants
15
- REGEX_LINK_LEFT = /\[\[/
15
+ REGEX_LINK_LEFT = /\[\[/
16
16
  REGEX_LINK_RIGHT = /\]\]/
17
17
  REGEX_LINK_EMBED = /(?<embed>\!)/
18
18
  REGEX_LINK_TYPE = /\s*::\s*/
@@ -22,7 +22,7 @@ module Jekyll
22
22
 
23
23
  # wikitext usable char requirements
24
24
  REGEX_LINK_TYPE_CHARS = /[^\n\s\!\#\^\|\]]+/i
25
- REGEX_FILENAME_CHARS = /[^\\\/:\#\^\|\[\]]+/i
25
+ REGEX_FILENAME_CHARS = /[^\\:\#\^\|\[\]]+/i
26
26
  REGEX_HEADER_CHARS = /[^\!\#\^\|\[\]]+/i
27
27
  REGEX_BLOCK_ID_CHARS = /[^\\\/:\!\#\^\|\[\]^\n]+/i
28
28
  REGEX_LABEL_CHARS = /(.+?)(?=\]{2}[^\]])/i
@@ -7,7 +7,7 @@ module Jekyll
7
7
  # wikilink classes know everything about the original markdown syntax and its semantic meaning
8
8
 
9
9
  class WikiLinkBlock
10
- attr_accessor :link_type, :filenames
10
+ attr_reader :link_type, :filenames
11
11
 
12
12
  # parameters ordered by appearance in regex
13
13
  def initialize(doc_mngr, context_filename, link_type, bullet_type=nil)
@@ -68,7 +68,7 @@ module Jekyll
68
68
  end
69
69
 
70
70
  def urls
71
- # return @filenames.map { |f| @doc_mngr.get_doc_by_fname(f) }
71
+ # return @filenames.map { |f| @doc_mngr.get_doc_by_fname(f).url }.compact()
72
72
  urls = []
73
73
  @filenames.each do |f|
74
74
  doc = @doc_mngr.get_doc_by_fname(f)
@@ -87,9 +87,10 @@ module Jekyll
87
87
  end
88
88
 
89
89
  def linked_fm_data
90
+ valid_urls = self.urls.select{ |url| @doc_mngr.get_doc_by_url(url) }
90
91
  return {
91
92
  'type' => @link_type,
92
- 'urls' => self.urls,
93
+ 'urls' => valid_urls,
93
94
  }
94
95
  end
95
96
 
@@ -106,6 +107,15 @@ module Jekyll
106
107
  return docs
107
108
  end
108
109
 
110
+ def missing_doc_filenames
111
+ missing_doc_fnames = []
112
+ @filenames.each do |f|
113
+ doc = @doc_mngr.get_doc_by_fname(f)
114
+ missing_doc_fnames << f if doc.nil?
115
+ end
116
+ return missing_doc_fnames
117
+ end
118
+
109
119
  # descriptor methods
110
120
 
111
121
  def has_filenames?
@@ -119,29 +129,37 @@ module Jekyll
119
129
  # validation methods
120
130
 
121
131
  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
132
+ all_filenames_missing = linked_docs.empty?
133
+ return false if !is_typed? || !has_filenames? || all_filenames_missing
127
134
  return true
128
135
  end
129
136
  end
130
137
 
131
138
  class WikiLinkInline
132
- attr_accessor :context_filename, :embed, :link_type, :filename, :header_txt, :block_id, :label_txt
139
+ attr_reader :link_type, :filename, :header_txt, :block_id
133
140
 
141
+ FILE_PATH = "file_path"
134
142
  FILENAME = "filename"
135
143
  HEADER_TXT = "header_txt"
136
144
  BLOCK_ID = "block_id"
137
145
 
138
146
  # parameters ordered by appearance in regex
139
- def initialize(doc_mngr, context_filename, embed, link_type, filename, header_txt, block_id, label_txt)
147
+ def initialize(doc_mngr, context_filename, embed, link_type, file_string, header_txt, block_id, label_txt)
148
+ if file_string.include?('/') && file_string[0] == '/'
149
+ @path_type = "absolute"
150
+ @file_path ||= file_string[1...] # remove leading '/' to match `jekyll_collection_doc.relative_path`
151
+ @filename ||= file_string.split('/').last
152
+ elsif file_string.include?('/') && file_string[0] != '/'
153
+ Jekyll.logger.error("Jekyll-Wikilinks: Relative file paths are not yet supported, please use absolute file paths that start with '/' for #{file_string}")
154
+ # todo:
155
+ # @path_type = "relative"
156
+ else
157
+ @filename ||= file_string
158
+ end
140
159
  @doc_mngr ||= doc_mngr
141
160
  @context_filename ||= context_filename
142
161
  @embed ||= embed
143
162
  @link_type ||= link_type
144
- @filename ||= filename
145
163
  @header_txt ||= header_txt
146
164
  @block_id ||= block_id
147
165
  @label_txt ||= label_txt
@@ -157,35 +175,45 @@ module Jekyll
157
175
  def md_regex
158
176
  regex_embed = embedded? ? REGEX_LINK_EMBED : %r{}
159
177
  regex_link_type = is_typed? ? %r{#{@link_type}#{REGEX_LINK_TYPE}} : %r{}
160
- filename = described?(FILENAME) ? @filename : ""
178
+ if !@file_path.nil?
179
+ file_string = described?(FILE_PATH) ? @file_path : ""
180
+ file_string = '/' + file_string if @path_type == "absolute"
181
+ else
182
+ file_string = described?(FILENAME) ? @filename : ""
183
+ end
161
184
  if described?(HEADER_TXT)
162
185
  header = %r{#{REGEX_LINK_HEADER}#{@header_txt}}
163
186
  block = %r{}
164
187
  elsif described?(BLOCK_ID)
165
188
  header = %r{}
166
189
  block = %r{#{REGEX_LINK_BLOCK}#{@block_id}}
167
- elsif !described?(FILENAME)
190
+ elsif !described?(FILENAME) && !described?(FILE_PATH)
168
191
  Jekyll.logger.error("Jekyll-Wikilinks: WikiLinkInline.md_regex error")
169
192
  end
170
193
  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}}
194
+ return %r{#{regex_embed}#{regex_link_type}#{REGEX_LINK_LEFT}#{file_string}#{header}#{block}#{label_}#{REGEX_LINK_RIGHT}}
172
195
  end
173
196
 
174
197
  def md_str
175
198
  embed = embedded? ? "!" : ""
176
199
  link_type = is_typed? ? "#{@link_type}::" : ""
177
- filename = described?(FILENAME) ? @filename : ""
200
+ if !@file_path.nil?
201
+ file_string = described?(FILE_PATH) ? @file_path : ""
202
+ file_string = '/' + file_string if @path_type == "absolute"
203
+ else
204
+ file_string = described?(FILENAME) ? @filename : ""
205
+ end
178
206
  if described?(HEADER_TXT)
179
207
  header = "\##{@header_txt}"
180
208
  block = ""
181
209
  elsif described?(BLOCK_ID)
182
210
  header = ""
183
211
  block = "\#\^#{@block_id}"
184
- elsif !described?(FILENAME)
212
+ elsif !described?(FILENAME) && !described?(FILE_PATH)
185
213
  Jekyll.logger.error("Jekyll-Wikilinks: WikiLinkInline.md_str error")
186
214
  end
187
215
  label_ = labelled? ? "\|#{@label_txt}" : ""
188
- return "#{embed}#{link_type}\[\[#{filename}#{header}#{block}#{label_}\]\]"
216
+ return "#{embed}#{link_type}\[\[#{file_string}#{header}#{block}#{label_}\]\]"
189
217
  end
190
218
 
191
219
  # 'fm' -> frontmatter
@@ -209,7 +237,11 @@ module Jekyll
209
237
  end
210
238
 
211
239
  def linked_doc
212
- return @doc_mngr.get_doc_by_fname(@filename)
240
+ # by file path
241
+ return @doc_mngr.get_doc_by_fpath(@file_path) if !@file_path.nil?
242
+ # by filename
243
+ return @doc_mngr.get_doc_by_fname(@filename) if @file_path.nil?
244
+ return nil
213
245
  end
214
246
 
215
247
  def linked_img
@@ -251,6 +283,7 @@ module Jekyll
251
283
 
252
284
  # this method helps to make the 'WikiLinkInline.level' code read like a clean truth table.
253
285
  def described?(chunk)
286
+ return (!@file_path.nil? && !@file_path.empty?) if chunk == FILE_PATH
254
287
  return (!@filename.nil? && !@filename.empty?) if chunk == FILENAME
255
288
  return (!@header_txt.nil? && !@header_txt.empty?) if chunk == HEADER_TXT
256
289
  return (!@block_id.nil? && !@block_id.empty?) if chunk == BLOCK_ID
@@ -258,16 +291,17 @@ module Jekyll
258
291
  end
259
292
 
260
293
  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)
294
+ return "file_path" if described?(FILE_PATH) && described?(FILENAME) && !described?(HEADER_TXT) && !described?(BLOCK_ID)
295
+ return "filename" if !described?(FILE_PATH) && described?(FILENAME) && !described?(HEADER_TXT) && !described?(BLOCK_ID)
296
+ return "header" if (described?(FILE_PATH) || described?(FILENAME)) && described?(HEADER_TXT) && !described?(BLOCK_ID)
297
+ return "block" if (described?(FILE_PATH) || described?(FILENAME)) && !described?(HEADER_TXT) && described?(BLOCK_ID)
264
298
  return "invalid"
265
299
  end
266
300
 
267
301
  # validation methods
268
302
 
269
303
  def is_valid?
270
- return false if !@doc_mngr.file_exists?(@filename)
304
+ return false if !@doc_mngr.file_exists?(@filename, @file_path)
271
305
  return false if (self.level == "header") && !@doc_mngr.doc_has_header?(self.linked_doc, @header_txt)
272
306
  return false if (self.level == "block") && !@doc_mngr.doc_has_block_id?(self.linked_doc, @block_id)
273
307
  return true
@@ -3,7 +3,7 @@
3
3
  module Jekyll
4
4
  module WikiLinks
5
5
 
6
- VERSION = "0.0.9"
6
+ VERSION = "0.0.12"
7
7
 
8
8
  end
9
9
  end
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.9
4
+ version: 0.0.12
5
5
  platform: ruby
6
6
  authors:
7
7
  - manunamz
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2022-01-20 00:00:00.000000000 Z
11
+ date: 2022-04-07 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: jekyll
@@ -30,14 +30,14 @@ dependencies:
30
30
  requirements:
31
31
  - - "~>"
32
32
  - !ruby/object:Gem::Version
33
- version: 1.12.3
33
+ version: 1.13.3
34
34
  type: :runtime
35
35
  prerelease: false
36
36
  version_requirements: !ruby/object:Gem::Requirement
37
37
  requirements:
38
38
  - - "~>"
39
39
  - !ruby/object:Gem::Version
40
- version: 1.12.3
40
+ version: 1.13.3
41
41
  description:
42
42
  email:
43
43
  - manunamz@pm.me