Class: Jekyll::SemTree::Generator

Inherits:
Generator
  • Object
show all
Defined in:
lib/jekyll-semtree.rb

Constant Summary collapse

CONVERTER_CLASS =
Jekyll::Converters::Markdown
CONFIG_KEY =

config keys

"semtree"
ENABLED_KEY =
"enabled"
EXCLUDE_KEY =
"exclude"
DOCTYPE_KEY =
"doctype"
PAGE_KEY =
"map"
ROOT_KEY =
"root"
VIRTUAL_TRUNK_KEY =
"virtual_trunk"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config) ⇒ Generator

Returns a new instance of Generator.



27
28
29
# File 'lib/jekyll-semtree.rb', line 27

def initialize(config)
  @config ||= config
end

Instance Attribute Details

#configObject (readonly)

for testing



15
16
17
# File 'lib/jekyll-semtree.rb', line 15

def config
  @config
end

Instance Method Details

#build_tree_hash(collection_doc) ⇒ Object



118
119
120
121
122
123
124
125
126
127
# File 'lib/jekyll-semtree.rb', line 118

def build_tree_hash(collection_doc)
  tree_data = {}
  collection_doc.each do |d|
    if d.type.to_s == self.option_doctype_name
      fname = File.basename(d.basename, File.extname(d.basename))
      tree_data[fname] = d.content
    end
  end
  return tree_data
end

#disabled?Boolean

config helpers

Returns:

  • (Boolean)


131
132
133
# File 'lib/jekyll-semtree.rb', line 131

def disabled?
  option(ENABLED_KEY) == false
end

#excluded?(type) ⇒ Boolean

Returns:

  • (Boolean)


135
136
137
138
# File 'lib/jekyll-semtree.rb', line 135

def excluded?(type)
  return false unless option(EXCLUDE_KEY)
  return option(EXCLUDE_KEY).include?(type.to_s)
end

#fnames_to_urls(fnames) ⇒ Object



109
110
111
112
113
114
115
116
# File 'lib/jekyll-semtree.rb', line 109

def fnames_to_urls(fnames)
  docs = []
  fnames.each do |fname|
    doc = @md_docs.detect { |d| fname == File.basename(d.basename, File.extname(d.basename)) }
    docs << (doc.nil? ? fname : doc.url)
  end
  return docs
end

#generate(site) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/jekyll-semtree.rb', line 31

def generate(site)
  return if disabled?

  # setup site
  @site = site
  @context ||= Context.new(site)

  # setup docs (based on configs)
  # unless @site.keys.include('doc_mngr') # may have been installed by jekyll-wikirefs or jekyll-wikilinks already
  #   require_relative "jekyll-semtree/patch/doc_manager"
  #   Jekyll::Hooks.register :site, :post_read do |site|
  #     if !self.disabled?
  #       site.doc_mngr = Jekyll::SemTree::DocManager.new(site)
  #     end
  #   end
  # end

  # setup markdown docs
  docs = []
  docs += site.pages # if !$wiki_conf.exclude?(:pages)
  docs += site.docs_to_write.filter { |d| !self.excluded?(d.type) }
  @md_docs = docs.filter { |doc| self.markdown_extension?(doc.extname) }
  if @md_docs.empty?
    Jekyll.logger.warn("Jekyll-SemTree: No semtree files to process.")
  end

  # tree setup
  # root
  root_doc = @md_docs.detect { |d| d.data['slug'] == self.option_root_name }
  if root_doc.nil?
    Jekyll.logger.error("Jekyll-SemTree: No root doc detected.")
  end
  # trunk / index docs
  index_docs = @site.docs_to_write.filter { |d| d.type.to_s == self.option_doctype_name }
  if index_docs.empty?
    Jekyll.logger.error("Jekyll-SemTree: No trunk (index docs) detected.")
  end
  # tree content hash
  tree_hash = build_tree_hash(@site.collections[self.option_doctype_name])
  if tree_hash.empty?
    Jekyll.logger.error("Jekyll-SemTree: No trunk (index docs) detected.")
  end
  if root_doc.nil? || index_docs.empty? || tree_hash.empty?
    Jekyll.logger.error("Jekyll-SemTree: Unable to build semantic tree.")
    return
  end
  # build tree
  @site.tree = Tree.new(tree_hash, root_doc, option_virtual_trunk)

  # generate metadata
  @site.tree.nodes.each do |n|
    doc = @md_docs.detect { |d| n.text == File.basename(d.basename, File.extname(d.basename)) }
    if !doc.nil?
      n.doc = doc
      ancestorNames, childrenNames = @site.tree.(doc)
      doc.data['ancestors'] = fnames_to_urls(ancestorNames)
      doc.data['children'] = fnames_to_urls(childrenNames)
    end
  end
  map_doc = @md_docs.detect { |d| option_page == File.basename(d.basename, File.extname(d.basename)) }
  # root_node = @site.tree.nodes.detect { |n| n.text == @site.tree.root }
  map_doc.data['nodes'] = @site.tree.nodes.map { |n| {
    'text' => n.text,
    'url' => n.url,
    'ancestors' => n.ancestors,
    'children' => n.children,
    }
  }
  dangling_entries = []
  @site.collections['entries'].each do |d|
    fname = File.basename(d.basename, File.extname(d.basename))
    if !@site.tree.in_tree?(fname)
      dangling_entries << fname
    end
  end
  Jekyll.logger.warn("Jekyll-SemTree: entries not listed in the tree: #{dangling_entries}")
end

#markdown_converterObject



144
145
146
# File 'lib/jekyll-semtree.rb', line 144

def markdown_converter
  @markdown_converter ||= @site.find_converter_instance(CONVERTER_CLASS)
end

#markdown_extension?(extension) ⇒ Boolean

Returns:

  • (Boolean)


140
141
142
# File 'lib/jekyll-semtree.rb', line 140

def markdown_extension?(extension)
  markdown_converter.matches(extension)
end

#option(key) ⇒ Object



148
149
150
# File 'lib/jekyll-semtree.rb', line 148

def option(key)
  @config[CONFIG_KEY] && @config[CONFIG_KEY][key]
end

#option_doctype_nameObject



156
157
158
# File 'lib/jekyll-semtree.rb', line 156

def option_doctype_name
  return option(DOCTYPE_KEY) ? @config[CONFIG_KEY][DOCTYPE_KEY] : 'index'
end

#option_pageObject



164
165
166
# File 'lib/jekyll-semtree.rb', line 164

def option_page
  return option(PAGE_KEY) ? @config[CONFIG_KEY][PAGE_KEY] : 'map'
end

#option_root_nameObject



152
153
154
# File 'lib/jekyll-semtree.rb', line 152

def option_root_name
  return option(ROOT_KEY) ? @config[CONFIG_KEY][ROOT_KEY] : 'i.bonsai'
end

#option_virtual_trunkObject



160
161
162
# File 'lib/jekyll-semtree.rb', line 160

def option_virtual_trunk
  return option(VIRTUAL_TRUNK_KEY) ? @config[CONFIG_KEY][VIRTUAL_TRUNK_KEY] : false
end