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
245 lines (198 loc) · 7.5 KB
/
directory.rb
File metadata and controls
245 lines (198 loc) · 7.5 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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
require 'pathname'
# {% directory dir:components/polymer-ui-elements %}
# {% directory demos:true tag:li branch:master dir:components/polymer-ui-elements glob:polymer-ui-* blaclistglob:polymer-something-* blacklist:"polymer-dev polymer-expressions polymer-elements" %}
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 blacklist:\"x\"]")
end
@dir = @attributes.has_key?('dir') ? @attributes['dir'] : nil
@glob = @attributes.has_key?('glob') ? @attributes['glob'] : '*/'
@blacklist_glob = @attributes.has_key?('blacklistglob') ? @attributes['blacklistglob'] : ''
@tag = @attributes.has_key?('tag') ? @attributes['tag'] : 'li'
@branch = @attributes.has_key?('branch') ? @attributes['branch'] : 'master'
@demos = @attributes.has_key?('demos') ? @attributes['demos'] : false
# Establish blacklist of elements to not include.
@blacklist = []
if @attributes.has_key?('blacklist')
@attributes['blacklist'] = @attributes['blacklist'][1..-2]
@blacklist = @attributes['blacklist'].split
end
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}") - Pathname.glob("#{@dir}/#{@blacklist_glob}")).each do |i|
name = i.basename.to_s.sub('.html', '')
if !in_blacklist?(name)
elements.push({
'name' => name,
'full_path' => i.to_s,
'path' => i.to_s.split('/')[1..-1].join('/') # remove "components"
})
end
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']}/smoke.html"
if !File.exists?(demo_path)
demo_path = "#{element['full_path']}/index.html"
end
github_url = github_url(element)
bower_use_url = bower_use_url(element)
bower_install_url = bower_install_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
p api_doc_file
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}> <small><a href="#{github_url}" target="_blank">source</a></small></h3>
<span class="bower_install_instructions">
<label>Install in your app:</label>
<pre class="prettyprint">bower install #{@config['project_title']}/#{bower_install_url}</pre>
<label>Import using:</label>
<pre class="prettyprint"><link rel="import"
href="#{bower_use_url}"></pre>
</span>
<span class="links">
<a href="/#{demo_path}" target="_blank" class="btn btn-primary" #{'disabled' if !File.exists?(demo_path)}>Try demo »</a>
<div #{'hidden' if !iframe_demo}>#{iframe_demo}</div>
</span>
<span class="api_documentation">
<label>Usage:</label>
#{api_docs}
</span>
</#{@tag}>
END
end
# Generates and iframe embed to demo the element using <iframe srcdoc>.
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
# Generates and iframe embed to demo the element using <iframe src>.
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
def bower_install_url(element)
repo, element_path = element['path'].split('/')
"#{repo}"
end
def bower_use_url(element)
repo, element_path = element['path'].split('/')
"bower_components/#{repo}/#{element_path}#{element['name']}.html"
end
def in_blacklist?(s)
@blacklist.each do |b|
if s.match(b.strip)
return true
end
end
return false
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)