Skip to content

Commit 37078a8

Browse files
alexchdshafik
authored andcommitted
TOC now works with locale
1 parent 900f3c7 commit 37078a8

File tree

6 files changed

+28
-18
lines changed

6 files changed

+28
-18
lines changed

app.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ def initialize
2626
super
2727
@here = File.expand_path(File.dirname(__FILE__))
2828
@default_sites = {en: "docs", es: "hola"}
29-
@default_locale = "en" # nil for English # todo: make a cleaner way to switch default locales
29+
@default_locale = "en"
3030
end
3131

3232
attr_reader :here, :default_locale

lib/contents.rb

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,19 +3,19 @@
33
class Contents < Erector::Widget
44
attr_accessor :site_dir
55
attr_accessor :page_name
6-
needs :page_name, :site_name, :site_dir => nil
6+
# todo: replace site_name, locale, site_dir with Site object
7+
needs :page_name, :site_name, :locale => nil, :site_dir => nil
78

89
def initialize options
910
super options
1011

1112
self.page_name = options[:page_name]
1213

13-
if options.include? :site_dir
14-
self.site_dir = options[:site_dir]
14+
if options.include? :site_dir # used in tests
15+
@site_dir = options[:site_dir]
1516
else
16-
here = File.expand_path File.dirname(__FILE__)
17-
top = File.expand_path "#{here}/.."
18-
self.site_dir = "#{top}/sites/#{@site_name}"
17+
site = Site.named(@site_name, @locale)
18+
@site_dir = site.dir if site
1919
end
2020
end
2121

lib/doc_page.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ def body_content
107107
}
108108
}
109109

110-
widget Contents, site_name: site_name, page_name: page_name
110+
widget Contents, locale: @locale, site_name: site_name, page_name: page_name
111111

112112
main {
113113
h1 doc_title, class: "doc_title"

lib/site.rb

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
class Site
22
@@here = File.expand_path(File.dirname(__FILE__))
33
@@project_root = File.dirname(@@here)
4-
4+
55
def self.sites_dir locale = "en"
66
sites_dir = File.join(["sites", locale].compact)
77
File.expand_path(sites_dir, @@project_root)
@@ -10,25 +10,27 @@ def self.sites_dir locale = "en"
1010
def self.all locale = "en"
1111
Dir["#{sites_dir(locale)}/*"].map{|dir| Site.new(dir)}
1212
end
13-
14-
def self.named name
15-
all.detect{|site| site.name == name }
13+
14+
def self.named name, locale = "en"
15+
all(locale).detect{|site| site.name == name }
1616
end
17-
17+
1818
DOC_TYPES = %w{step md deck.md mw}
19-
19+
20+
attr_reader :dir
21+
2022
def initialize dir
2123
@dir = dir
2224
end
23-
25+
2426
def name
2527
@dir.split('/').last
2628
end
2729

2830
def docs
2931
Dir["#{@dir}/*.{#{DOC_TYPES.join(',')}}"].map{|path| Doc.new(path)}
3032
end
31-
33+
3234
class Doc
3335
attr_reader :path
3436

@@ -39,10 +41,10 @@ def initialize path
3941
def filename
4042
@path.split('/').last
4143
end
42-
44+
4345
def name
4446
filename.split('.').first
4547
end
46-
48+
4749
end
4850
end

spec/markdown_spec.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,10 @@
2424
doc_path: "/tmp/hello.step",
2525
locale: "en"
2626
)
27+
28+
# this is a hack to make the TOC work in the absence of a real site
29+
Site.should_receive(:named).and_return(mock(dir: "/tmp"))
30+
2731
html_doc = Nokogiri.parse(page.to_html)
2832
main_html = html_doc.css("main").first.serialize(:save_with => 0).chomp
2933

spec/step_page_spec.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,10 @@
1616
doc_path: "/tmp/hello.step",
1717
locale: "en"
1818
)
19+
20+
# this is a hack to make the TOC work in the absence of a real site
21+
Site.should_receive(:named).and_return(mock(dir: "/tmp"))
22+
1923
html_doc = Nokogiri.parse(page.to_html)
2024
main_html = html_doc.css("main").first.serialize(:save_with => 0).chomp
2125
checkbox_html = %q{<input class="big_checkbox" id="big_checkbox_1" name="big_checkbox_1" type="checkbox" value="valuable"><label for="big_checkbox_1"></label>}

0 commit comments

Comments
 (0)