Skip to content

Commit

Permalink
Add spec/generated_page_spec.rb, fix bugs
Browse files Browse the repository at this point in the history
Also tweaked comments and reordered the arguments to GeneratedPage.new.
  • Loading branch information
Mike Bland committed Jun 22, 2015
1 parent 6327669 commit 0ba4ea6
Show file tree
Hide file tree
Showing 4 changed files with 105 additions and 13 deletions.
7 changes: 4 additions & 3 deletions bin/jekyll_pages_api
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,10 @@ Arguments:
title_prefix
Prefix to strip from page titles
body_element_tag
Tag (or tag prefix) identifying the main content element of each tag. Can
be a complete tag (ending in '>'), or the prefix of a longer tag. Used to
strip boilerplate out of the content exported via the API.
Tag (or tag prefix) identifying the main content element within the <body>
element of each document. Can be a complete tag (ending in '>'), or the
prefix of a longer tag. Used to strip boilerplate out of the content
exported via the API.
END_USAGE

if ARGV.length == 1 && ARGV[0] == '-h'
Expand Down
15 changes: 10 additions & 5 deletions lib/jekyll_pages_api/generated_page.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,25 @@ module JekyllPagesApi
class GeneratedPage
attr_reader :path, :relative_path, :data, :content

# @param path [String] full path to the generated page's file
# @param basedir see {GeneratedSite#initialize}
# @param title_prefix see {GeneratedSite#initialize}
# @param body_element_tag see {GeneratedSite#initialize}
# @param path [String] full path to the generated page's file
# @param content [String] HTML content of the generated page's file
def initialize(basedir, title_prefix, body_element_tag, path, content)
# @raises [RuntimError] if path does not begin with basedir
def initialize(path, basedir, title_prefix, body_element_tag, content)
unless path.start_with? basedir
raise "#{path} does not start with #{basedir}"
end

@path = path
basedir_len = basedir.size
basedir_len += 1 if basedir.end_with? File::SEPARATOR
basedir_len -= File::SEPARATOR.size if basedir.end_with? File::SEPARATOR

end_path = path.size
index_suffix = File.join "", "index.html"
end_path -= (index_suffix.size + 1) if path.end_with? index_suffix
@relative_path = path[basedir_len..end_path]
end_path -= index_suffix.size if path.end_with? index_suffix
@relative_path = (path[basedir_len..end_path] || "")
@data, @content = GeneratedPageParser.parse_generated_page(
content, title_prefix, body_element_tag)
end
Expand Down
10 changes: 5 additions & 5 deletions lib/jekyll_pages_api/generated_site.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@ class GeneratedSite
# @param basedir [String] Path to the generated site's root directory
# @param title_prefix [String] Prefix to strip from page titles
# @param body_element_tag [String] Tag (or tag prefix) identifying the
# main content element of each tag. Can be a complete tag (ending in
# '>'), or the prefix of a longer tag. Used to strip boilerplate out of
# the content exported via the API.
# main content element within the <body> element of each document. Can
# be a complete tag (ending in '>'), or the prefix of a longer tag. Used
# to strip boilerplate out of the content exported via the API.
def initialize(baseurl, basedir, title_prefix, body_element_tag)
@baseurl = baseurl
@basedir = basedir
Expand All @@ -33,8 +33,8 @@ def each_site_file
Dir.glob(File.join(self.basedir, '**', '*')) do |f|
next unless f.end_with? '.html'
begin
page = GeneratedPage.new(self.basedir, self.title_prefix,
self.body_element_tag, f, File.read(f))
page = GeneratedPage.new(f, self.basedir, self.title_prefix,
self.body_element_tag, File.read(f))
yield page unless page.data['title'].nil?
rescue
$stderr.puts "Error while processing #{f}:"
Expand Down
86 changes: 86 additions & 0 deletions spec/generated_page_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
module JekyllPagesApi
describe GeneratedPage do
describe '#initialize' do
it "handles all empty strings correctly" do
page = GeneratedPage.new "", "", "", "", ""
expect(page.path).to eq("")
expect(page.relative_path).to eq("")
expect(page.data).to eq({})
expect(page.content).to eq("")
end

it "parses the relative path when basedir ends in SEPARATOR" do
path = File.join "foo", "bar", "baz.html"
basedir = File.join "foo", ""
page = GeneratedPage.new path, basedir, "", "", ""
expect(page.path).to eq("foo/bar/baz.html")
expect(page.relative_path).to eq("/bar/baz.html")
expect(page.data).to eq({})
expect(page.content).to eq("")
end

it "parses the relative path when basedir doesn't end in SEPARATOR" do
path = File.join "foo", "bar", "baz.html"
basedir = "foo"
page = GeneratedPage.new path, basedir, "", "", ""
expect(page.path).to eq("foo/bar/baz.html")
expect(page.relative_path).to eq("/bar/baz.html")
expect(page.data).to eq({})
expect(page.content).to eq("")
end

it "raises RuntimeError when path does not begin with basedir" do
path = File.join "foo", "bar", "baz.html"
basedir = File.join "quux", ""
expect{GeneratedPage.new path, basedir, "", "", ""
}.to raise_error(
RuntimeError, "#{path} does not start with #{basedir}")
end

it "parses out index.html suffix and leaves trailing slash" do
path = File.join "foo", "bar", "index.html"
basedir = File.join "foo", ""
page = GeneratedPage.new path, basedir, "", "", ""
expect(page.path).to eq("foo/bar/index.html")
expect(page.relative_path).to eq("/bar/")
expect(page.data).to eq({})
expect(page.content).to eq("")
end

it "parses out index.html suffix and leaves trailing slash for root" do
path = File.join "foo", "index.html"
basedir = File.join "foo", ""
page = GeneratedPage.new path, basedir, "", "", ""
expect(page.path).to eq("foo/index.html")
expect(page.relative_path).to eq("/")
expect(page.data).to eq({})
expect(page.content).to eq("")
end

it "parses content correctly" do
path = File.join "foo", "bar", "index.html"
basedir = File.join "foo", ""
title_prefix = "18F &mdash; "
body_element_tag = "<div class='content'"
content = "<head><title>18F &mdash; Blah Blah Woof Woof</title>"+
"<meta name='skip-index' content='true'>"+
"<meta content='baz,quux,xyzzy,plugh' name=\"tags\" />"+
"</head>"+
"<body><div>header</div>"+
"<div class='content'>foobar</div>"+
"<div>footer</div></body>"

page = GeneratedPage.new(path, basedir, title_prefix,
body_element_tag, content)
expect(page.path).to eq("foo/bar/index.html")
expect(page.relative_path).to eq("/bar/")
expect(page.data).to eq(
"title" => "Blah Blah Woof Woof",
"skip-index" => "true",
"tags" => "baz,quux,xyzzy,plugh",
)
expect(page.content).to eq("foobar")
end
end
end
end

0 comments on commit 0ba4ea6

Please sign in to comment.