forked from Polymer/old-docs-site
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdirectory.rb
More file actions
197 lines (158 loc) · 5.79 KB
/
directory.rb
File metadata and controls
197 lines (158 loc) · 5.79 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
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
107
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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
require 'pathname'
# {% directory dir:polymer-all/polymer-ui-elements %}
# {% directory demos:true tag:li branch:master dir:polymer-all/polymer-ui-elements glob:polymer-ui-* %}
module Jekyll
class DirectoryTag < Liquid::Tag
# def initialize(tag_name, path, tokens)
# super
# @path = path.strip
# end
include Liquid::StandardFilters
Syntax = /(#{Liquid::QuotedFragment}+)?/
def initialize(tag_name, markup, tokens)
@attributes = {}
# Parse parameters
if markup =~ Syntax
markup.scan(Liquid::TagAttributes) do |key, value|
#p key + ":" + value
@attributes[key] = value
end
else
raise SyntaxError.new("Syntax Error in 'directory' - Valid syntax: directory demo:x branch:x tag:x dir:x glob:x]")
end
@dir = @attributes.has_key?('dir') ? @attributes['dir'] : nil
@glob = @attributes.has_key?('glob') ? @attributes['glob'] : '*/'
@tag = @attributes.has_key?('tag') ? @attributes['tag'] : 'li'
@branch = @attributes.has_key?('branch') ? @attributes['branch'] : 'stable'
@demos = @attributes.has_key?('demos') ? @attributes['demos'] : false
super
end
def render(context)
@config = context.registers[:site].config
project_title = @config['project_title'].downcase
elements = []
#Pathname.glob("#{@path}/#{project_title}-*/").each do |i|
#Pathname.glob("#{project_title}-all/#{@path}/").each do |i|
Pathname.glob("#{@dir}/#{@glob}").each do |i|
elements.push({
'name' => i.basename.to_s.sub('.html', ''),
'full_path' => i.to_s,
'path' => i.to_s.split('/')[1..-1].join('/') # remove polymer-all
})
end
elements.map{|el| render_element(el).strip}
end
def render_element(element)
file_path = "#{element['full_path']}/#{element['name']}.html"
if !File.exists?(file_path)
file_path = "#{element['full_path']}"
end
demo_path = "#{element['full_path']}/index.html"
#if !File.exists?("#{element['full_path']}/index.html")
# demo_path = "#{element['full_path']}"
#end
github_url = github_url(element)
tag_name = element['name']
#iframe_demo = generate_iframe_demo("/#{file_path}", tag_name) if @demos else ''
iframe_demo = generate_iframe_demo2("/#{demo_path}") if @demos else ''
api_doc_file = "#{@dir}/docs/classes/#{element['name']}.html"
begin
f = File.new(api_doc_file, "r")
api_docs = f.read()
rescue => e
api_docs = "<div class=\"nodocs\"></div>"
ensure
f.close unless f.nil?
end
<<-END
<#{@tag} data-element-file="/#{file_path}">
<h3 id="#{tag_name}"><#{tag_name}></h3>
<span class="links">
<a href="#{github_url}" target="_blank">source</a>
<a href="/#{demo_path}" target="_blank" #{'disabled' if !File.exists?(demo_path)}>demo</a>
<div #{'hidden' if !iframe_demo}>#{iframe_demo}</div>
</span>
#{api_docs}
</#{@tag}>
END
end
def generate_iframe_demo(file_path, tag_name)
<<-END
<iframe srcdoc='
<script src="/polymer.min.js"></script>
<link rel="import" href="#{file_path}">
<#{tag_name}></#{tag_name}>'>
</iframe>
END
end
def generate_iframe_demo2(file_path)
<<-END
<iframe style="width: 100%;" src="#{file_path}"></iframe>
END
end
def github_url(element)
github_project_url = "https://github.com/#{@config['project_title']}"
repo, element_path = element['path'].split('/')
"#{github_project_url}/#{repo}/blob/#{@branch}/#{element_path}/#{element['name']}.html"
end
end
end
Liquid::Template.register_tag('directory', Jekyll::DirectoryTag)
# {% comment %}
# #https://github.com/christianhellsten/jekyll-plugins/blob/master/delicious.rb
# {% directory repo:polymer-elements glob:polymer-* %}
# {{item.name}}
# {% enddirectory %}
# {% endcomment %}
# module Jekyll
# class DirectoryTag < Liquid::Block
# include Liquid::StandardFilters
# Syntax = /(#{Liquid::QuotedFragment}+)?/
# def initialize(tag_name, markup, tokens)
# @variable_name = 'item'
# @attributes = {}
# # Parse parameters
# if markup =~ Syntax
# markup.scan(Liquid::TagAttributes) do |key, value|
# #p key + ":" + value
# @attributes[key] = value
# end
# else
# raise SyntaxError.new("Syntax Error in 'directory' - Valid syntax: directory repo:x glob:x]")
# end
# @repo = @attributes.has_key?('repo') ? @attributes['repo'] : nil
# @glob = @attributes['glob']
# @name = 'item'
# super
# end
# def render(context)
# context.registers[:delicious] ||= Hash.new(0)
# if @ttl
# collection = CachedDelicious.tag(@username, @tag, @count, @ttl)
# else
# collection = Delicious.tag(@username, @tag, @count)
# end
# length = collection.length
# result = []
# # loop through found bookmarks and render results
# context.stack do
# collection.each_with_index do |item, index|
# attrs = item.send('table')
# context[@variable_name] = attrs.stringify_keys! if attrs.size > 0
# context['forloop'] = {
# 'name' => @name,
# 'length' => length,
# 'index' => index + 1,
# 'index0' => index,
# 'rindex' => length - index,
# 'rindex0' => length - index -1,
# 'first' => (index == 0),
# 'last' => (index == length - 1) }
# result << render_all(@nodelist, context)
# end
# end
# result
# end
# end
# end
# Liquid::Template.register_tag('directory', Jekyll::DirectoryTag)