Skip to content
This repository was archived by the owner on Jan 12, 2021. It is now read-only.

Commit 407b95c

Browse files
author
James Ramsay
committed
Merge pull request #3 from jamesramsay/google-yaml
Google yaml
2 parents 396e7cf + e1226db commit 407b95c

8 files changed

+95
-47
lines changed

.simplecov

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
SimpleCov.start do
2+
add_filter "/lib/google-yaml.rb"
3+
end

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
[![Build Status](https://img.shields.io/travis/jamesramsay/jekyll-app-engine/master.svg)](https://travis-ci.org/jamesramsay/jekyll-app-engine)
44
[![Version](https://img.shields.io/gem/v/jekyll-app-engine.svg)](https://rubygems.org/gems/jekyll-app-engine)
5+
[![Coverage Status](https://img.shields.io/codecov/c/github/jamesramsay/jekyll-app-engine/master.svg)](https://codecov.io/github/jamesramsay/jekyll-app-engine)
56

67
`jekyll-app-engine` makes it easy to deploy your jekyll site to Google App Engine by generating handlers for your `app.yaml`.
78

jekyll-app-engine.gemspec

+1
Original file line numberDiff line numberDiff line change
@@ -18,4 +18,5 @@ Gem::Specification.new do |spec|
1818
spec.add_development_dependency "rspec", "~> 3.0"
1919
spec.add_development_dependency "rake"
2020
spec.add_development_dependency "bundler", "~> 1.6"
21+
spec.add_development_dependency "codecov"
2122
end

lib/google-yaml.rb

+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
# Google App Engine doesn't support achors and alias feature of YAML
2+
# Psych doesn't provide an option to disable achors/alias YAML features
3+
#
4+
# HACK: subclass the native YAMLTree, and bypass logic which checks for aliases
5+
# ../psych/vistors/yaml_tree.rb
6+
class GoogleYAMLTree < Psych::Visitors::YAMLTree
7+
def accept target
8+
# HACK: disabled alias lookup
9+
# return any aliases we find
10+
# if @st.key? target
11+
# oid = @st.id_for target
12+
# node = @st.node_for target
13+
# anchor = oid.to_s
14+
# node.anchor = anchor
15+
# return @emitter.alias anchor
16+
# end
17+
18+
if target.respond_to?(:to_yaml)
19+
begin
20+
loc = target.method(:to_yaml).source_location.first
21+
if loc !~ /(syck\/rubytypes.rb|psych\/core_ext.rb)/
22+
unless target.respond_to?(:encode_with)
23+
if $VERBOSE
24+
warn "implementing to_yaml is deprecated, please implement \"encode_with\""
25+
end
26+
27+
target.to_yaml(:nodump => true)
28+
end
29+
end
30+
rescue
31+
# public_method or source_location might be overridden,
32+
# and it's OK to skip it since it's only to emit a warning
33+
end
34+
end
35+
36+
if target.respond_to?(:encode_with)
37+
dump_coder target
38+
else
39+
send(@dispatch_cache[target.class], target)
40+
end
41+
end
42+
end

lib/jekyll-app-engine.rb

+36-39
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,8 @@
11
require 'fileutils'
2+
require 'google-yaml'
23

34
module Jekyll
45

5-
class GoogleYAMLTree < Psych::Visitors::YAMLTree
6-
def accept target
7-
if target.respond_to?(:to_yaml)
8-
begin
9-
loc = target.method(:to_yaml).source_location.first
10-
if loc !~ /(syck\/rubytypes.rb|psych\/core_ext.rb)/
11-
unless target.respond_to?(:encode_with)
12-
if $VERBOSE
13-
warn "implementing to_yaml is deprecated, please implement \"encode_with\""
14-
end
15-
16-
target.to_yaml(:nodump => true)
17-
end
18-
end
19-
rescue
20-
# public_method or source_location might be overridden,
21-
# and it's OK to skip it since it's only to emit a warning
22-
end
23-
end
24-
25-
if target.respond_to?(:encode_with)
26-
dump_coder target
27-
else
28-
send(@dispatch_cache[target.class], target)
29-
end
30-
end
31-
end
32-
336
class PageWithoutAFile < Page
347
def read_yaml(*)
358
@data ||= {}
@@ -89,6 +62,7 @@ def write
8962
end
9063

9164
def app_yaml_content
65+
# HACK: use sub-classed YAML implementation which disables anchors and aliases
9266
builder = GoogleYAMLTree.create
9367
builder << generate_app_engine_yaml
9468

@@ -99,6 +73,34 @@ def app_yaml_content
9973
return app_yaml.output
10074
end
10175

76+
def page_types
77+
page_types_array = [
78+
{
79+
"content_type" => "posts",
80+
"content_collection" => @site.posts.docs
81+
},
82+
{
83+
"content_type" => "pages",
84+
"content_collection" => @site.pages
85+
},
86+
{
87+
"content_type" => "static",
88+
"content_collection" => @site.static_files
89+
},
90+
]
91+
92+
@site.collections.each_pair do |label, collection|
93+
unless label == "posts"
94+
page_types_array << {
95+
"content_type" => "collections",
96+
"content_collection" => collection.docs
97+
}
98+
end
99+
end
100+
101+
return page_types_array
102+
end
103+
102104
def generate_app_engine_yaml
103105
if source_partial_exists?
104106
app_yaml = YAML.load_file(source_path)
@@ -108,28 +110,23 @@ def generate_app_engine_yaml
108110

109111
app_yaml["handlers"] ||= []
110112

111-
generate_handlers("posts", @site.posts.docs).each { |handler| app_yaml["handlers"] << handler }
112-
generate_handlers("pages", @site.pages).each { |handler| app_yaml["handlers"] << handler }
113-
114-
@site.collections.each_pair do |label, collection|
115-
unless label == "posts"
116-
generate_handlers("collections", collection.docs).each { |handler| app_yaml["handlers"] << handler }
117-
end
113+
page_types.each do |content|
114+
generate_handlers(content).each { |handler| app_yaml["handlers"] << handler }
118115
end
119116

120-
generate_handlers("static", @site.static_files).each { |handler| app_yaml["handlers"] << handler }
121-
122117
return app_yaml
123118
end
124119

125-
def generate_handlers(content_type, collection)
120+
def generate_handlers(content)
121+
content_type = content["content_type"]
122+
content_collection = content["content_collection"]
126123
handlers = []
127124

128125
handler_template = @app_engine["handlers"][content_type] || {}
129126
if handler_template.kind_of?(Array) or handler_template.has_key?("url")
130127
handlers << handler_template
131128
else
132-
collection.each do |doc|
129+
content_collection.each do |doc|
133130
handler = {
134131
"url" => doc.url,
135132
"static_files" => doc.destination("").sub("#{Dir.pwd}/", ""),

spec/fixtures/_config.yml

+4-7
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,7 @@ app_engine:
1717
posts:
1818
http_headers:
1919
Link: "</static/css/style.css>; rel=preload; as=style"
20-
statics:
21-
- url: /images/
22-
static_dir: _site/images
23-
expiration: 4m
24-
- url: /styles/
25-
static_dir: _site/styles
26-
expiration: 8m
20+
collections:
21+
- url: /my_collection/*
22+
static_files: _site/my_collection/*
23+
upload: _site/my_collection/*

spec/jekyll-app-engine_spec.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@
126126
# end
127127

128128
it "includes the correct number of items" do
129-
expect(handlers.length).to eql 18
129+
expect(handlers.length).to eql 15
130130
end
131131

132132
# context "with a baseurl" do

spec/spec_helper.rb

+7
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,10 @@
1+
require 'simplecov'
2+
3+
if ENV['CI'] == 'true'
4+
require 'codecov'
5+
SimpleCov.formatter = SimpleCov::Formatter::Codecov
6+
end
7+
18
require 'jekyll'
29
require File.expand_path('../lib/jekyll-app-engine', File.dirname(__FILE__))
310

0 commit comments

Comments
 (0)