forked from php-fig/www.php-fig.org
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path_build
More file actions
executable file
·153 lines (127 loc) · 3.88 KB
/
_build
File metadata and controls
executable file
·153 lines (127 loc) · 3.88 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
#!/usr/bin/env ruby
# encoding: utf-8
# A script for programmatically generating PSR, translation and redirect pages
# for php-fig.org
require 'fileutils'
PSR_DIR = File.join(File.dirname(__FILE__), '_includes', 'fig-standards', 'accepted')
EXTRAS = %w(meta examples)
IS_EXTRA = Regexp.new("-(#{EXTRAS.join('|')})\\.md$")
LANG_MAP = {
en: 'English (official)',
pt_BR: 'Brazilian Portuguese',
fr: 'French',
it: 'Italian',
pl: 'Polish',
ru: 'Russian',
sl: 'Slovenian',
es: 'Spanish'
}
fail 'Run "git submodule init"' unless File.directory?(PSR_DIR)
class Psrs
def initialize
@files = load_files
end
def pages
@files.sort_by { |f| f[:num] }
end
def standards
@files
.select { |f| !f[:is_extra] }
.sort_by { |f| f[:num] }
end
def extras
@files
.select { |f| f[:is_extra] }
.sort_by { |f| f[:num] }
end
def translations_for(num)
standards
.select { |f| f[:num] == num }
.sort_by { |f| f[:order] }
end
def extras_for(num, lang)
@files
.select { |f| f[:num] == num && f[:lang] == lang }
.sort_by { |f| f[:is_extra] ? 1 : 0 }
end
def path_for(num, lang, extra = nil)
chunks = ['', 'pages', 'psr', "psr-#{num}"]
chunks << lang.to_s.sub('_', '-').downcase unless lang == :en
chunks << extra unless extra.nil?
chunks.join('/')
end
protected
def load_files
Dir["#{PSR_DIR}/**/PSR-*.md"].map do |filename|
# Skip non-PSR files
next unless File.basename(filename) =~ /^PSR-(\d+)\b/
num = Regexp.last_match[1].to_i
lang = File.basename(File.dirname(filename)).sub('-', '_').to_sym
lang = :en if lang == :accepted # :)
fail "Unknown language: #{lang}" unless LANG_MAP.keys.include?(lang)
if filename =~ IS_EXTRA
extra = Regexp.last_match[1]
else
extra = nil
end
title = File.read(filename).split(/[\n\r]/).first.sub(/^#\s*/, '')
title = "PSR-#{num}: #{title}" unless title.include?("PSR-#{num}")
{
num: num,
lang: lang,
filename: filename,
path: path_for(num, lang, extra),
title: title,
is_extra: !extra.nil?,
inc: filename.sub(%r{.*/fig-standards/}, 'fig-standards/'),
order: LANG_MAP.keys.index(lang)
}
end
end
end
psrs = Psrs.new
psrs.pages.each do |page|
FileUtils.mkdir_p ".#{page[:path]}"
File.open(".#{page[:path]}/index.md", 'w') do |f|
f << "---\n"
f << "# This file is automatically generated by `_build`.\n"
f << "layout: psr\n"
f << "permalink: #{page[:path][7..-1]}/\n"
f << "title: #{page[:title]}\n"
f << "disclaimer: true\n" unless page[:lang] == :en
unless page[:is_extra]
translations = psrs.translations_for(page[:num])
unless translations.empty?
f << "translations:\n"
translations.each do |other|
f << " - name: #{LANG_MAP[other[:lang]]}\n"
f << " path: #{other[:path]}\n" unless other[:path] == page[:path]
end
end
end
extras = psrs.extras_for(page[:num], page[:lang])
if extras.length > 1
f << "additional:\n"
extras.each do |other|
f << " - name: #{other[:title]}\n"
f << " path: #{other[:path]}\n" unless other[:path] == page[:path]
end
end
unless page[:is_extra]
if page[:lang] == :en
f << "redirect_from: /psr/#{page[:num]}/\n"
elsif page[:lang].to_s.include?('_')
# redirect from non-normalized language urls.
base = File.dirname(page[:path])
f << "redirect_from:\n"
f << " - #{base}/#{page[:lang]}/\n"
f << " - #{base}/#{page[:lang].to_s.sub('_', '-')}/\n"
if page[:lang] != page[:lang].downcase
f << " - #{base}/#{page[:lang].to_s.downcase}/\n"
end
end
end
f << "---\n\n"
f << "{% include #{page[:inc]} %}"
end
end