Class: Jekyll::WikiLinks::WikiLinkInline

Inherits:
Object
  • Object
show all
Defined in:
lib/jekyll-wikilinks/util/wikilink.rb

Constant Summary collapse

FILE_PATH =
"file_path"
FILENAME =
"filename"
HEADER_TXT =
"header_txt"
BLOCK_ID =
"block_id"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(doc_mngr, context_filename, embed, link_type, file_string, header_txt, block_id, label_txt) ⇒ WikiLinkInline

parameters ordered by appearance in regex



147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
# File 'lib/jekyll-wikilinks/util/wikilink.rb', line 147

def initialize(doc_mngr, context_filename, embed, link_type, file_string, header_txt, block_id, label_txt)
  if file_string.include?('/') && file_string[0] == '/'
    @path_type = "absolute"
    @file_path ||= file_string[1...] # remove leading '/' to match `jekyll_collection_doc.relative_path`
    @filename ||= file_string.split('/').last
  elsif file_string.include?('/') && file_string[0] != '/'
    Jekyll.logger.error("Jekyll-Wikilinks: Relative file paths are not yet supported, please use absolute file paths that start with '/' for #{file_string}")
    # todo:
    # @path_type = "relative"
  else
    @filename ||= file_string
  end
  @doc_mngr ||= doc_mngr
  @context_filename ||= context_filename
  @embed ||= embed
  @link_type ||= link_type
  @header_txt ||= header_txt
  @block_id ||= block_id
  @label_txt ||= label_txt
end

Instance Attribute Details

#block_idObject (readonly)

Returns the value of attribute block_id.



139
140
141
# File 'lib/jekyll-wikilinks/util/wikilink.rb', line 139

def block_id
  @block_id
end

#filenameObject (readonly)

Returns the value of attribute filename.



139
140
141
# File 'lib/jekyll-wikilinks/util/wikilink.rb', line 139

def filename
  @filename
end

#header_txtObject (readonly)

Returns the value of attribute header_txt.



139
140
141
# File 'lib/jekyll-wikilinks/util/wikilink.rb', line 139

def header_txt
  @header_txt
end

Returns the value of attribute link_type.



139
140
141
# File 'lib/jekyll-wikilinks/util/wikilink.rb', line 139

def link_type
  @link_type
end

Instance Method Details

#context_docObject



235
236
237
# File 'lib/jekyll-wikilinks/util/wikilink.rb', line 235

def context_doc
  return @doc_mngr.get_doc_by_fname(@context_filename)
end

#context_fm_dataObject

‘fm’ -> frontmatter



221
222
223
224
225
226
# File 'lib/jekyll-wikilinks/util/wikilink.rb', line 221

def context_fm_data
  return {
    'type' => @link_type,
    'url' => self.context_doc.url,
  }
end

#described?(chunk) ⇒ Boolean

this method helps to make the ‘WikiLinkInline.level’ code read like a clean truth table.

Returns:

  • (Boolean)


285
286
287
288
289
290
291
# File 'lib/jekyll-wikilinks/util/wikilink.rb', line 285

def described?(chunk)
  return (!@file_path.nil? && !@file_path.empty?) if chunk == FILE_PATH
  return (!@filename.nil? && !@filename.empty?) if chunk == FILENAME
  return (!@header_txt.nil? && !@header_txt.empty?) if chunk == HEADER_TXT
  return (!@block_id.nil? && !@block_id.empty?) if chunk == BLOCK_ID
  Jekyll.logger.error("Jekyll-Wikilinks: There is no link level '#{chunk}' in the WikiLink Class")
end

#embedded?Boolean

Returns:

  • (Boolean)


271
272
273
# File 'lib/jekyll-wikilinks/util/wikilink.rb', line 271

def embedded?
  return !@embed.nil? && @embed == "!"
end

#is_img?Boolean

Returns:

  • (Boolean)


275
276
277
278
# File 'lib/jekyll-wikilinks/util/wikilink.rb', line 275

def is_img?
  # github supported image formats: https://docs.github.com/en/github/managing-files-in-a-repository/working-with-non-code-files/rendering-and-diffing-images
  return SUPPORTED_IMG_FORMATS.any?{ |ext| ext == File.extname(@filename).downcase }
end

#is_img_svg?Boolean

Returns:

  • (Boolean)


280
281
282
# File 'lib/jekyll-wikilinks/util/wikilink.rb', line 280

def is_img_svg?
  return File.extname(@filename).downcase == ".svg"
end

#is_typed?Boolean

Returns:

  • (Boolean)


267
268
269
# File 'lib/jekyll-wikilinks/util/wikilink.rb', line 267

def is_typed?
  return !@link_type.nil? && !@link_type.empty?
end

#is_valid?Boolean

validation methods

Returns:

  • (Boolean)


303
304
305
306
307
308
# File 'lib/jekyll-wikilinks/util/wikilink.rb', line 303

def is_valid?
  return false if !@doc_mngr.file_exists?(@filename, @file_path)
  return false if (self.level == "header") && !@doc_mngr.doc_has_header?(self.linked_doc, @header_txt)
  return false if (self.level == "block") && !@doc_mngr.doc_has_block_id?(self.linked_doc, @block_id)
  return true
end

#label_txtObject

escape square brackets if they appear in label text



169
170
171
# File 'lib/jekyll-wikilinks/util/wikilink.rb', line 169

def label_txt
  return @label_txt.sub("[", "\\[").sub("]", "\\]")
end

#labelled?Boolean

def describe

return {
  'level' => level,
  'labelled' => labelled?,
  'embedded' => embedded?,
  'typed_link' => is_typed?,
}

end

Returns:

  • (Boolean)


263
264
265
# File 'lib/jekyll-wikilinks/util/wikilink.rb', line 263

def labelled?
  return !@label_txt.nil? && !@label_txt.empty?
end

#levelObject



293
294
295
296
297
298
299
# File 'lib/jekyll-wikilinks/util/wikilink.rb', line 293

def level
  return "file_path" if described?(FILE_PATH) && described?(FILENAME) && !described?(HEADER_TXT) && !described?(BLOCK_ID)
  return "filename"  if !described?(FILE_PATH) && described?(FILENAME) && !described?(HEADER_TXT) && !described?(BLOCK_ID)
  return "header"    if (described?(FILE_PATH) || described?(FILENAME)) && described?(HEADER_TXT) && !described?(BLOCK_ID)
  return "block"     if (described?(FILE_PATH) || described?(FILENAME)) && !described?(HEADER_TXT) && described?(BLOCK_ID)
  return "invalid"
end

#linked_docObject



239
240
241
242
243
244
245
# File 'lib/jekyll-wikilinks/util/wikilink.rb', line 239

def linked_doc
  # by file path
  return @doc_mngr.get_doc_by_fpath(@file_path) if !@file_path.nil?
  # by filename
  return @doc_mngr.get_doc_by_fname(@filename) if @file_path.nil?
  return nil
end

#linked_fm_dataObject



228
229
230
231
232
233
# File 'lib/jekyll-wikilinks/util/wikilink.rb', line 228

def linked_fm_data
  return {
    'type' => @link_type,
    'url' => self.linked_doc.url,
  }
end

#linked_imgObject



247
248
249
250
# File 'lib/jekyll-wikilinks/util/wikilink.rb', line 247

def linked_img
  return @doc_mngr.get_image_by_fname(@filename) if self.is_img?
  return nil
end

#md_regexObject

data



175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
# File 'lib/jekyll-wikilinks/util/wikilink.rb', line 175

def md_regex
  regex_embed = embedded? ? REGEX_LINK_EMBED : %r{}
  regex_link_type = is_typed? ? %r{#{@link_type}#{REGEX_LINK_TYPE}} : %r{}
  if !@file_path.nil?
    file_string = described?(FILE_PATH) ? @file_path : ""
    file_string = '/' + file_string if @path_type == "absolute"
  else
    file_string = described?(FILENAME) ? @filename : ""
  end
  if described?(HEADER_TXT)
    header = %r{#{REGEX_LINK_HEADER}#{@header_txt}}
    block = %r{}
  elsif described?(BLOCK_ID)
    header = %r{}
    block = %r{#{REGEX_LINK_BLOCK}#{@block_id}}
  elsif !described?(FILENAME) && !described?(FILE_PATH)
    Jekyll.logger.error("Jekyll-Wikilinks: WikiLinkInline.md_regex error")
  end
  label_ =  labelled? ? %r{#{REGEX_LINK_LABEL}#{label_txt}} : %r{}
  return %r{#{regex_embed}#{regex_link_type}#{REGEX_LINK_LEFT}#{file_string}#{header}#{block}#{label_}#{REGEX_LINK_RIGHT}}
end

#md_strObject



197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
# File 'lib/jekyll-wikilinks/util/wikilink.rb', line 197

def md_str
  embed = embedded? ? "!" : ""
  link_type = is_typed? ? "#{@link_type}::" : ""
  if !@file_path.nil?
    file_string = described?(FILE_PATH) ? @file_path : ""
    file_string = '/' + file_string if @path_type == "absolute"
  else
    file_string = described?(FILENAME) ? @filename : ""
  end
  if described?(HEADER_TXT)
    header = "\##{@header_txt}"
    block = ""
  elsif described?(BLOCK_ID)
    header = ""
    block = "\#\^#{@block_id}"
  elsif !described?(FILENAME) && !described?(FILE_PATH)
    Jekyll.logger.error("Jekyll-Wikilinks: WikiLinkInline.md_str error")
  end
  label_ = labelled? ? "\|#{@label_txt}" : ""
  return "#{embed}#{link_type}\[\[#{file_string}#{header}#{block}#{label_}\]\]"
end