forked from pivotal-cf/bookbinder
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bookbinder_helpers.rb
282 lines (221 loc) · 8.26 KB
/
bookbinder_helpers.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
require 'bookbinder/code_example_reader'
require 'bookbinder/ingest/cloner_factory'
require 'bookbinder/ingest/git_accessor'
require 'bookbinder/local_filesystem_accessor'
require 'date'
require_relative 'archive_drop_down_menu'
require_relative 'quicklinks_renderer'
I18n.enforce_available_locales = false
module Bookbinder
class Helpers < ::Middleman::Extension
# class << self
# def registered(app)
# app.helpers HelperMethods
# end
# alias :included :registered
# end
module HelperMethods
def yield_for_code_snippet(from: nil, at: nil)
cloner_factory = Ingest::ClonerFactory.new({out: $stdout},
LocalFilesystemAccessor.new,
Ingest::GitAccessor.new)
cloner = cloner_factory.produce(config[:local_repo_dir])
code_example_reader = CodeExampleReader.new({out: $stdout},
LocalFilesystemAccessor.new)
working_copy = cloner.call(source_repo_name: from,
source_ref: 'master',
destination_parent_dir: config[:workspace])
snippet, language = code_example_reader.get_snippet_and_language_at(at, working_copy)
delimiter = '```'
snippet.prepend("#{delimiter}#{language}\n").concat("\n#{delimiter}")
end
def elastic_search?
!!config[:elastic_search]
end
def yield_for_subnav
partial "subnavs/#{subnav_template_name}"
end
def yield_for_archive_drop_down_menu
menu = ArchiveDropDownMenu.new(
config[:archive_menu],
current_path: current_page.path
)
unless menu.empty?
partial 'archive_menus/default', locals: { menu_title: menu.title,
dropdown_links: menu.dropdown_links }
end
end
def exclude_feedback
current_page.add_metadata({page: {feedback_disabled: true}})
end
def yield_for_feedback
partial 'layouts/feedback' if config[:feedback_enabled] && !current_page.metadata[:page][:feedback_disabled]
end
def exclude_repo_link
current_page.add_metadata({page: {repo_link_disabled: true}})
end
def render_repo_link
if config[:repo_link_enabled] && repo_url && !current_page.metadata[:page][:repo_link_disabled]
"<a id='repo-link' href='#{repo_url}'>Create a pull request or raise an issue on the source for this page in GitHub</a>"
end
end
def mermaid_diagram(&blk)
escaped_text = capture(&blk).gsub('-','\-')
@_out_buf.concat "<div class='mermaid'>#{escaped_text}</div>"
end
def modified_date(default_date: nil)
parsed_default_date = Time.parse(default_date).utc if default_date
date = page_last_modified_date || parsed_default_date
"Page last updated: <span data-behavior=\"DisplayModifiedDate\" data-modified-date=\"#{date.to_i}000\"></span>" if date
end
def breadcrumbs
page_chain = add_ancestors_of(current_page, [])
breadcrumbs = page_chain.map do |page|
make_breadcrumb(page, page == current_page)
end.compact
return if breadcrumbs.size < 2
return content_tag :ul, breadcrumbs.reverse.join(' '), class: 'breadcrumbs'
end
def vars
OpenStruct.new config[:template_variables]
end
def product_name
return vars.product_name
end
def product_name_long
return vars.product_name_long
end
def product_version
return vars.product_version
end
def set_title(*args)
current_page.data.title= args.join(' ')
end
def set_title_no_spaces(*args)
current_page.data.title= args.join
end
def product_info
config[:product_info].fetch(template_key, {})
end
def production_host
config[:production_host]
end
def quick_links
page_src = File.read(current_page.source_file)
quicklinks_renderer = QuicklinksRenderer.new(vars)
Redcarpet::Markdown.new(quicklinks_renderer).render(page_src)
end
def owners
html_resources = sitemap.resources.select { |r| r.path.end_with?('.html') }
html_resources.each.with_object({}) { |resource, owners|
owners[resource.path] = Array(resource.data['owner'])
}
end
def template_key
decreasingly_specific_namespaces.detect { |ns|
config[:subnav_templates].has_key?(ns)
}
end
def body_classes(path=current_path.dup, options={})
if path.is_a? Hash
options = path
path = current_path.dup
end
basename = File.basename(path)
dirname = File.dirname(path).gsub('.', '_')
page_classes(File.join(dirname, basename), options)
end
private
def subnav_template_name
config[:subnav_templates][template_key] || 'default'
end
def decreasingly_specific_namespaces
body_classes(numeric_prefix: numeric_class_prefix).
split(' ').reverse.drop(1).
map {|ns| ns.sub(/^#{numeric_class_prefix}/, '')}
end
def numeric_class_prefix
'NUMERIC_CLASS_PREFIX'
end
def page_last_modified_date
git_accessor = Ingest::GitAccessor.new
current_date = if current_page.data.dita
git_accessor.author_date(preprocessing_path(current_page.source_file), dita: true)
else
git_accessor.author_date(current_page.source_file)
end
current_date.utc if current_date
end
def repo_url
nested_dir, filename = parse_out_nested_dir_and_filename
repo_dir = match_repo_dir(nested_dir)
page_repo_config = config[:repo_links][repo_dir]
if page_repo_config && page_repo_config['ref']
org_repo = Pathname(page_repo_config['repo'])
ref = Pathname(page_repo_config['ref'])
at_path = at_path(page_repo_config)
nested_dir = extract_nested_directory(nested_dir, repo_dir)
"http:https://github.com/#{org_repo.join(Pathname('tree'), ref, Pathname(nested_dir), at_path, source_file(filename))}"
end
end
def match_repo_dir(nested_dir)
config[:repo_links].keys
.select{ |key| nested_dir.match(/^#{key}/) }
.sort_by{ |key| key.length }
.last
end
def source_file(filename)
fs = LocalFilesystemAccessor.new
if current_page.data.dita
source_filename = "#{filename}.xml"
if fs.source_file_exists?(Pathname(preprocessing_path(current_page.source_file)).dirname,
source_filename)
source_filename
else
''
end
else
"#{filename}.html.md.erb"
end
end
def preprocessing_path(current_source_path)
root_path, nested_repo_path = current_source_path.split('source')
root_path.gsub!('/output/master_middleman', '')
"#{root_path}output/preprocessing/sections#{nested_repo_path}"
end
def parse_out_nested_dir_and_filename
current_page.path
.match(/\/?(.*?)\/?([^\/]*)\.html$?/)
.captures
end
def extract_nested_directory(nested_dir, repo_dir)
nested_dir = nested_dir.gsub("#{repo_dir}", '')
nested_dir = nested_dir.sub('/', '') if nested_dir[0] == '/'
nested_dir
end
def at_path(page_repo_config)
path = page_repo_config['at_path'] || ""
Pathname(path)
end
def add_ancestors_of(page, ancestors)
if page
add_ancestors_of(page.parent, ancestors + [page])
else
ancestors
end
end
def make_breadcrumb(page, is_current_page)
return nil unless (text = page.data.breadcrumb || page.data.title)
if is_current_page
css_class = 'active'
link = content_tag :span, text
else
link = link_to(text, '/' + page.path)
end
content_tag :li, link, :class => css_class
end
end
helpers HelperMethods
end
end
::Middleman::Extensions.register(:bookbinder, Bookbinder::Helpers)