forked from adambard/learnxinyminutes-site
-
Notifications
You must be signed in to change notification settings - Fork 0
/
config.rb
315 lines (249 loc) · 6.71 KB
/
config.rb
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
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
activate :directory_indexes
#activate :syntax
require 'pygments'
activate :syntax
module ::Middleman::Syntax::Highlighter
def self.highlight(code, language=nil, opts={})
Pygments.highlight(code, lexer: language)
end
end
set :markdown_engine, :redcarpet
set :markdown, :fenced_code_blocks => true, :smartypants => true, :tables => true
set :css_dir, 'css'
set :js_dir, 'js'
set :images_dir, 'img'
with_layout :doc do
page "/docs/*"
end
helpers do
def twitter_share_url(url, doc)
full_url = "https://learnxinyminutes.com" + (url || "")
text = (doc || "")
qs = URI.encode_www_form("url" => full_url, "text" => text)
"https://twitter.com/intent/tweet?#{qs}"
end
end
# Build-specific configuration
configure :build do
# For example, change the Compass output style for deployment
# activate :minify_css
# Minify Javascript on build
# activate :minify_javascript
# Enable cache buster
# activate :cache_buster
# Use relative URLs
# activate :relative_assets
# Compress PNGs after build
# First: gem install middleman-smusher
# require "middleman-smusher"
# activate :smusher
# Or use a different image path
# set :http_path, "/Content/images/"
end
#
###
# Compass
###
# Susy grids in Compass
# First: gem install susy
# require 'susy'
# Change Compass configuration
# compass_config do |config|
# config.output_style = :compact
# end
###
# Page options, layouts, aliases and proxies
###
# Per-page layout changes:
#
# With no layout
# page "/path/to/file.html", :layout => false
#
# With alternative layout
# page "/path/to/file.html", :layout => :otherlayout
#
# A path which all have the same layout
# with_layout :admin do
# page "/admin/*"
# end
# Proxy (fake) files
# page "/this-page-has-no-template.html", :proxy => "/template-file.html" do
# @which_fake_page = "Rendering a fake page with a variable"
# end
###
# Helpers
###
# Automatic image dimensions on image_tag helper
# activate :automatic_image_sizes
# Methods defined in the helpers block are available in templates
# helpers do
# def some_helper
# "Helping"
# end
# end
IGNORED = [
# "docs/ko-kr/lua-kr.html.markdown"
]
class Article
attr_accessor :category, :name, :contributors, :contributor_count, :filename, :rawcode, :url, :language, :translations, :translators
def initialize(r)
@resource = r
@path = r.path
meta = r.metadata[:page]
set_name_and_category(meta)
set_contributors(meta)
set_contributor_count(r.path)
set_rawcode(r)
@language = r.metadata[:page].fetch("lang", "en")
@translations = []
@translators = r.metadata[:page].fetch("translators", [])
@url = r.url
end
def set_name_and_category(meta)
begin
@name = meta.fetch("name", nil)
if @name.nil?
@name = meta.fetch("language", nil)
end
@category = meta.fetch("category", nil)
if @category.nil? && @name
@category = "language"
else
@category = meta.fetch("category", "meta")
end
@name_key = case @category
when "language" then "language"
when "tool" then "tool"
else "name"
end
@name = meta.fetch(@name_key, "")
rescue e
puts "ERROR: " + e.to_s
@name = ""
@category = "meta"
end
end
def set_contributors(meta)
# Get a list of the authors
@contributors = meta.fetch("contributors", nil)
if @contributors.nil?
@contributors = [[meta["author"], meta["author_url"]]]
end
end
def set_contributor_count(path)
# Count other contributors with git blame
system("cd source/docs && git blame #{path.split('/')[1]}.markdown >> tmp.txt")
File::open('source/docs/tmp.txt'){|f|
names = f.readlines.map{ |line|
name = line.split(' ')[1] || ''
name[1..name.length]
}
cnt = Set::new(names).length + @contributors.length - 2
@contributor_count = (cnt > 0 ? cnt : 0)
}
system("rm source/docs/tmp.txt")
end
def set_rawcode(r)
meta = r.metadata[:page]
@filename = meta.fetch("filename", nil)
if @filename.nil?
@rawcode = nil
return
end
s = nil
File::open("source/#{r.path}.markdown") {|f|
s = StringScanner.new(f.read)
}
code = ''
until s.eos?
s.skip_until(/^```.*$/)
out = s.scan_until(/^```/)
if out.nil?
break
end
code += out
end
@rawcode = code.gsub('```', '')
end
def to_s
"Article #{@category}: #{@name} (#{@contributors}): #{@path}"
end
def add_translation a
@translations.push a
end
end
class ArticleManager
attr_accessor :articles, :articles_by_name, :articles_by_category_en
def initialize(sitemap)
@articles = sitemap.resources.select{|r|
not IGNORED.include?(r.path)
}.map{|r|
Article.new(r)
}.sort{|a1, a2| a1.name.downcase <=> a2.name.downcase }
@articles_en = @articles.select{|a|a.language == "en"}
@articles_by_category = @articles.group_by{|r| r.category}
@articles_by_name = @articles.group_by{|r| r.name}
@articles_by_category_en = @articles_en.group_by{|r| r.category}
@articles_by_name_en = @articles_en.group_by(&:name)
#@articles.each{|a| puts a.url + ": " + a.language + " (" + a.category + ")"}
@articles.select{|a| a.language != "en" and not a.name.nil?}.each{|a|
a2 = @articles_by_name_en.fetch(a.name, [nil])[0]
if not a2.nil? and not a2.translations.nil?
a2.add_translation a
end
}
end
def get(page)
get_article(page)
end
def get_category_display_name(c)
case c
when "language" then "Languages"
when "tool" then "Tools"
else c
end
end
def get_article(page)
name = page.fetch("name",
page.fetch("tool",
page.fetch("language", nil)))
language = page.fetch("lang", "en")
@articles_by_name.fetch(name).select{|a|
a.language == language
}[0]
end
def contributors(page)
get_article(page).contributors
end
def name(page)
get_article(page).name
end
def contributor_count(page)
get_article(page).contributor_count
end
def filename(page)
get_article(page).filename
end
def rawcode(page)
get_article(page).rawcode
end
end
class I18N
def initialize(articles)
@articles = articles
end
def t(data, key)
language = @articles.get(data.page).language.sub('-', '_')
translations = data.i18n[language] || data.i18n['default']
translations[key]
end
end
# Count contributors
ready do
articles = ArticleManager.new(sitemap)
set :articles, articles
set :i18n, I18N.new(articles)
articles.articles.select{|a| not a.filename.nil?}.each{|a|
proxy "/docs/files/#{a.filename}", "/docs/file.html", :locals => {:rawcode => a.rawcode}, :ignore => true, :layout => false
}
end