Skip to content

Commit b7ca6b9

Browse files
Generate Navigation Tree
1 parent a8ffb16 commit b7ca6b9

File tree

10 files changed

+51
-20
lines changed

10 files changed

+51
-20
lines changed

app/controllers/articles_controller.rb

+35-1
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,44 @@ def markdown(text)
2828
def index
2929
@parent_path = request.path
3030
puts "PATH = " + @parent_path
31+
puts File.basename(Dir.getwd)
32+
@nav_tree = walk("./app/markdown", "")
33+
puts @nav_tree
34+
end
35+
36+
def titleize(name)
37+
lowercase_words = %w{a an the and but or for nor of}
38+
name.split.each_with_index.map{|x, index| lowercase_words.include?(x) && index > 0 ? x : x.capitalize }.join(" ")
39+
end
40+
41+
def walk(start, parent)
42+
temp_hash = {}
43+
44+
Dir.foreach(start) do |x|
45+
path = File.join(start, x)
46+
if x == "." or x == ".."
47+
next
48+
elsif File.directory?(path)
49+
# puts path + "/" # remove this line if you want; just prints directories
50+
name = titleize(x.gsub('-', ' '))
51+
temp_hash[name] = walk(path, parent + "_" + x)
52+
else
53+
# Convert path to name
54+
name = titleize(x[0..-4].gsub('-', ' '))
55+
56+
temp_hash[name] = parent + '_' + x[0..-4]
57+
58+
if parent == ""
59+
temp_hash[name] = name
60+
end
61+
end
62+
end
63+
64+
temp_hash
3165
end
3266

3367
def open_article
34-
title = Rails.root.to_s + "/app/markdown/" + params[:title] + ".md"
68+
title = Rails.root.to_s + "/app/markdown/" + params[:title].gsub('_', '/') + ".md"
3569
@content = markdown(File.read(title))
3670
end
3771

app/javascript/controllers/articles/page_controller.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ export default class extends Controller {
1010

1111
// Account for empty fragment
1212
if (fragment === "" || fragment === undefined) {
13-
window.location.hash = "getting-started";
13+
window.location.hash = "getting-started_index";
1414
fragment = window.location.hash.split("#")[1];
1515
}
1616

File renamed without changes.
File renamed without changes.

app/markdown/types-of-charts.md

-10
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# Bar Chart
2+
Words go here.

app/markdown/types-of-charts/index.md

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# Types of Charts
2+
This is about types of charts.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# Pie Chart
2+
Words go here.

app/views/articles/index.html.erb

+9-8
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,15 @@
1010
</div>
1111

1212
<div class='links padding-top'>
13-
<%= button_tag "Getting Started", id: "getting-started", class: "nav-button getting-started", data: { action: "click->articles--page#navigate" } %>
14-
<%= button_tag "Types of Charts", id: "types-of-charts", class: "nav-button types-of-charts", data: { action: "click->articles--page#navigate" } %>
15-
<%= button_tag "Chart Styling", id: "chart-styling", class: "nav-button chart-styling", data: { action: "click->articles--page#navigate" } %>
16-
<%= button_tag "Contribution", id: "contribution", class: "nav-button contribution", data: { action: "click->articles--page#navigate" } %>
13+
<% @nav_tree.each do |key, value| %>
14+
<%= button_tag key, id: value["Index"], class: "nav-button #{value}", data: { action: "click->articles--page#navigate" } %>
15+
16+
<% value.each do |skey, svalue| %>
17+
<% if skey != "Index" %>
18+
<%= button_tag skey, id: svalue["Index"], class: "nav-button #{svalue}", data: { action: "click->articles--page#navigate" } %>
19+
<% end %>
20+
<% end %>
21+
<% end %>
1722
</div>
1823
</div>
1924

@@ -56,7 +61,3 @@
5661
</div>
5762
</div>
5863
</div>
59-
60-
<script>
61-
62-
</script>

0 commit comments

Comments
 (0)