Class: Jekyll::WikiLinks::WikiLinkBlock

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

Overview

wikilink classes know everything about the original markdown syntax and its semantic meaning

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(doc_mngr, context_filename, link_type, bullet_type = nil) ⇒ WikiLinkBlock

parameters ordered by appearance in regex



13
14
15
16
17
18
19
# File 'lib/jekyll-wikilinks/util/wikilink.rb', line 13

def initialize(doc_mngr, context_filename, link_type, bullet_type=nil)
  @doc_mngr ||= doc_mngr
  @context_filename ||= context_filename
  @link_type ||= link_type
  @bullet_type ||= bullet_type
  @filenames = []
end

Instance Attribute Details

#filenamesObject (readonly)

Returns the value of attribute filenames.



10
11
12
# File 'lib/jekyll-wikilinks/util/wikilink.rb', line 10

def filenames
  @filenames
end

Returns the value of attribute link_type.



10
11
12
# File 'lib/jekyll-wikilinks/util/wikilink.rb', line 10

def link_type
  @link_type
end

Instance Method Details

#add_item(filename) ⇒ Object



21
22
23
24
# File 'lib/jekyll-wikilinks/util/wikilink.rb', line 21

def add_item(filename)
  Jekyll.logger.error("Jekyll-Wikilinks: 'filename' required") if filename.nil? || filename.empty?
  @filenames << filename
end

#context_docObject



97
98
99
# File 'lib/jekyll-wikilinks/util/wikilink.rb', line 97

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

#context_fm_dataObject

‘fm’ -> frontmatter



82
83
84
85
86
87
# File 'lib/jekyll-wikilinks/util/wikilink.rb', line 82

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

#has_filenames?Boolean

descriptor methods

Returns:

  • (Boolean)


121
122
123
# File 'lib/jekyll-wikilinks/util/wikilink.rb', line 121

def has_filenames?
  return !@filenames.nil? && !@filenames.empty?
end

#is_typed?Boolean

Returns:

  • (Boolean)


125
126
127
# File 'lib/jekyll-wikilinks/util/wikilink.rb', line 125

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

#is_valid?Boolean

validation methods

Returns:

  • (Boolean)


131
132
133
134
135
# File 'lib/jekyll-wikilinks/util/wikilink.rb', line 131

def is_valid?
  all_filenames_missing = linked_docs.empty?
  return false if !is_typed? || !has_filenames? || all_filenames_missing
  return true
end

#linked_docsObject



101
102
103
104
105
106
107
108
# File 'lib/jekyll-wikilinks/util/wikilink.rb', line 101

def linked_docs
  docs = [] 
  @filenames.each do |f|
    doc = @doc_mngr.get_doc_by_fname(f)
    docs << doc if !doc.nil?
  end
  return docs
end

#linked_fm_dataObject



89
90
91
92
93
94
95
# File 'lib/jekyll-wikilinks/util/wikilink.rb', line 89

def linked_fm_data
  valid_urls = self.urls.select{ |url| @doc_mngr.get_doc_by_url(url) }
  return {
    'type' => @link_type,
    'urls' => valid_urls,
  }
end

#md_regexObject

data



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/jekyll-wikilinks/util/wikilink.rb', line 28

def md_regex
  if !is_typed? || !has_filenames? 
    Jekyll.logger.error("Jekyll-Wikilinks: WikiLinkBlock.md_regex error -- type: #{@link_type}, fnames: #{@filenames.inspect}, for: #{@context_filename}")
  end
  # comma (including singles)
  if @bullet_type.nil?
    link_type = /#{@link_type}#{REGEX_LINK_TYPE}/i
    tmp_filenames = @filenames.dup
    first_filename = /\s*#{REGEX_LINK_LEFT}#{tmp_filenames.shift()}#{REGEX_LINK_RIGHT}\s*/i
    filename_strs = tmp_filenames.map { |f| /,\s*#{REGEX_LINK_LEFT}#{f}#{REGEX_LINK_RIGHT}\s*/i }
    md_regex = /#{link_type}#{first_filename}#{filename_strs.join('')}\n/i
  # mkdn
  elsif !@bullet_type.match(REGEX_BULLET).nil?
    link_type = /#{@link_type}#{REGEX_LINK_TYPE}\n/i
    filename_strs = @filenames.map { |f| /\s{0,3}#{Regexp.escape(@bullet_type)}\s#{REGEX_LINK_LEFT}#{f}#{REGEX_LINK_RIGHT}\n/i }
    md_regex = /#{link_type}#{filename_strs.join("")}/i
  else
    Jekyll.logger.error("Jekyll-Wikilinks: WikiLinkBlock.bullet_type error: #{@bullet_type}")
  end
  return md_regex
end

#md_strObject



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/jekyll-wikilinks/util/wikilink.rb', line 50

def md_str
  if !is_typed? || !has_filenames?
    Jekyll.logger.error("Jekyll-Wikilinks: WikiLinkBlockList.md_str error -- type: #{@link_type}, fnames: #{@filenames.inspect}, for: #{@context_filename}")
  end
  # comma (including singles)
  if @bullet_type.nil?
    link_type = "#{@link_type}::"
    filename_strs = @filenames.map { |f| "\[\[#{f}\]\]," }
    md_str = (link_type + filename_strs.join('')).delete_suffix(",")
  # mkdn
  elsif !@bullet_type.match(REGEX_BULLET).nil?
    link_type = "#{@link_type}::\n"
    filename_strs = @filenames.map { |f| li[0] + " \[\[#{li[1]}\]\]\n" }
    md_str = link_type + filename_strs.join('')
  else
    Jekyll.logger.error("Jekyll-Wikilinks: 'bullet_type' invalid: #{@bullet_type}")
  end
  return md_str
end

#missing_doc_filenamesObject



110
111
112
113
114
115
116
117
# File 'lib/jekyll-wikilinks/util/wikilink.rb', line 110

def missing_doc_filenames
  missing_doc_fnames = [] 
  @filenames.each do |f|
    doc = @doc_mngr.get_doc_by_fname(f)
    missing_doc_fnames << f if doc.nil?
  end
  return missing_doc_fnames
end

#urlsObject



70
71
72
73
74
75
76
77
78
# File 'lib/jekyll-wikilinks/util/wikilink.rb', line 70

def urls
  # return @filenames.map { |f| @doc_mngr.get_doc_by_fname(f).url }.compact()
  urls = []
  @filenames.each do |f|
    doc = @doc_mngr.get_doc_by_fname(f)
    urls << doc.url if !doc.nil?
  end
  return urls
end