Class: Jekyll::Namespaces::Generator

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

Constant Summary collapse

CONVERTER_CLASS =
Jekyll::Converters::Markdown
CONFIG_KEY =

config

"namespaces"
ENABLED_KEY =
"enabled"
EXCLUDE_KEY =
"exclude"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config) ⇒ Generator

Returns a new instance of Generator.



22
23
24
# File 'lib/jekyll-namespaces.rb', line 22

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

Instance Attribute Details

#configObject (readonly)

for testing



14
15
16
# File 'lib/jekyll-namespaces.rb', line 14

def config
  @config
end

Instance Method Details

#disabled?Boolean

config helpers

Returns:

  • (Boolean)


60
61
62
# File 'lib/jekyll-namespaces.rb', line 60

def disabled?
  option(ENABLED_KEY) == false
end

#excluded?(type) ⇒ Boolean

Returns:

  • (Boolean)


64
65
66
67
# File 'lib/jekyll-namespaces.rb', line 64

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

#generate(site) ⇒ Object



26
27
28
29
30
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
# File 'lib/jekyll-namespaces.rb', line 26

def generate(site)
  return if disabled?
  self.old_config_warn()

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

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

  # tree setup
  root_doc = @md_docs.detect { |d| d.data['slug'] == 'root' }
  if root_doc.nil?
    Jekyll.logger.error("Jekyll-Namespaces: No root.md detected.")
  end
  @site.tree = Tree.new(root_doc, @md_docs)

  # generate metadata
  @md_docs.each do |doc|
    doc.data['namespace'] = doc.data['slug']
    doc.data['ancestors'], doc.data['children'] = @site.tree.(doc)
  end

end

#markdown_converterObject



73
74
75
# File 'lib/jekyll-namespaces.rb', line 73

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

#markdown_extension?(extension) ⇒ Boolean

Returns:

  • (Boolean)


69
70
71
# File 'lib/jekyll-namespaces.rb', line 69

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

#old_config_warnObject



87
88
89
90
91
92
93
94
# File 'lib/jekyll-namespaces.rb', line 87

def old_config_warn()
  if @config.include?("d3_graph_data")
    Jekyll.logger.warn("Jekyll-Namespaces: As of 0.0.2, 'd3_graph_data' should now be 'd3' and requires the 'jekyll-graph' plugin.")
  end
  if option_exist?("include")
    Jekyll.logger.warn("Jekyll-Namespaces: As of 0.0.2, all markdown files are processed by default. Use 'exclude' config to exclude document types.")
  end
end

#option(key) ⇒ Object



77
78
79
# File 'lib/jekyll-namespaces.rb', line 77

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

#option_exist?(key) ⇒ Boolean

!! deprecated !!

Returns:

  • (Boolean)


83
84
85
# File 'lib/jekyll-namespaces.rb', line 83

def option_exist?(key)
  @config[CONFIG_KEY] && @config[CONFIG_KEY].include?(key)
end