Class: Jekyll::NavTree

Inherits:
Liquid::Tag
  • Object
show all
Defined in:
lib/jekyll-navigation-tree.rb

Instance Method Summary collapse

Constructor Details

#initialize(tag_name, path, tokens) ⇒ NavTree

Returns a new instance of NavTree.



19
20
21
22
# File 'lib/jekyll-navigation-tree.rb', line 19

def initialize(tag_name, path, tokens)
  super
  @path = path
end

Instance Method Details

#files_first_traverse(prefix, node = {}) ⇒ Object



108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
# File 'lib/jekyll-navigation-tree.rb', line 108

def files_first_traverse(prefix, node = {})
output = ""
output += "#{prefix}<ul>"
node_list = node.sort

# node_list:

# [["/about", {}],
#  ["/assets", {"/assets/main.css"=>{}}],
#  ["/feed.xml", {}],
#  ["/imprint", {}],
#  ["/mixing",
#   {"/mixing/defaults.html"=>{},
#    "/mixing/shure-beta-58a.html"=>{},
#    "/mixing/shure-ulx.html"=>{},
#    "/mixing/t-bone-free-solo-pt-823-mhz.html"=>{}}],
#  ["/rezepte",
#   {"/rezepte/fleisch"=>
#     {"/rezepte/fleisch/amerikanisches-rindersteak.html"=>{},
#      "/rezepte/fleisch/chili-con-carne.html"=>{},
#      "/rezepte/fleisch/fleischpfanzerl.html"=>{},

node_list.each do |base, subtree|
    # base: "/about"
    name = base[1..-1]
    # name: "about"
    if name.index('.html')
      name = @dirs[name]["title"] || name
    # e. g.: "tech/websites/lrz/index.html" => is "tech/websites/lrz/"
    # in @dirs hash.
    elsif @dirs.has_key? name + "/"
      name = @dirs[name + "/"]["title"] || name
    end

    output += "#{prefix}	 <li><a href=\"#{base}\">#{name}</a></li>" if subtree.empty?
end

node_list.each do |base, subtree|
  next if subtree.empty?
    name = base[1..-1]
    # name: "tech/commands"
    if @dirs.has_key? name + '/'
      href = base
      name = @dirs[name + '/']['title'] || name
    elsif name.index('/')
      name = name[name.rindex('/')+1..-1] || name
    end

    if href
      name_link = "<a href=\"#{base}\">#{name}</a>"
    else
      name_link = "<div class=\"subtree-name\">#{name}</div>"
    end

    output += "#{prefix}	<li>#{name_link}"
    output += files_first_traverse(prefix + '	 ', subtree)
    output+= "</li>"

  end

  output += "#{prefix} </ul>"
  output
end

#render(context) ⇒ Object



24
25
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
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
# File 'lib/jekyll-navigation-tree.rb', line 24

def render(context)
  site = context.registers[:site]

  @page_url = context.environments.first["page"]["url"]
  # @page_url: "/tech/"

  @dirs = {}
  tree = {}

  site.pages.each do |page|
    ap page
    if ! page.data.has_key? "nav_tree" || page.data["nav_tree"] == true
      #  page: #<Jekyll:Page @name="Motorola-Moto-E-2nd-Gen-XT1524-4G-LTE.md">
      path = page.url
      # path: /tech/hardware/Motorola-Moto-E-2nd-Gen-XT1524-4G-LTE.html
      path = path.index('/') == 0 ? path[1..-1] : path

      # page.data:

      # {
      #        "layout" => "page",
      #         "title" => "About",
      #     "permalink" => "/about/"
      # }
      @dirs[path] = page.data
    end
  end

  # @dirs:

  # {
  #   "tech/hardware/Motorola-Moto-E-2nd-Gen-XT1524-4G-LTE.html"=>{
  #     "title"=>"Motorola Moto E 2nd Gen XT1524 4G LTE"
  #   },
  #  "about/ "=> {
  #    "layout"=>"page",
  #    "title"=>"About",
  #    "permalink"=>"/about/"
  #   },
  #   "tech/commands/adb.html"=>{
  #     "title"=>"adb (Android Debug Bridge)"
  #   },
  #   "tech/websites/lrz-seite-2003/"=>{
  #     "title"=>"LRZ-Seite (2003)"
  #   },
  # }

  @dirs.each do |path, data|
    current = tree
    path.split("/").inject("") do |sub_path, dir|
      sub_path = File.join(sub_path, dir)

      current[sub_path] ||= {}
      current = current[sub_path]
      sub_path
    end
  end

  puts "generating nav tree for #{@page_url}"

  # tree:
  # {"/tech"=>
  #   {"/tech/hardware"=>
  #     {"/tech/hardware/Motorola-Moto-E-2nd-Gen-XT1524-4G-LTE.html"=>{},
  #      "/tech/hardware/banana-pi-pro.html"=>{},
  #      "/tech/hardware/brother-hl-5350dn.html"=>{},
  if @path == nil || @path == ""
    base_path = @page_url.chomp('/')
  else
    base_path = @path.strip.chomp('/')
  end

  if base_path == ""
    sub_tree = tree
  else
    if base_path.index('/') != 0
      base_path = '/' + base_path
    end
    sub_tree = tree[base_path]
  end

  files_first_traverse("", sub_tree)
end