Skip to content

Commit 2676c87

Browse files
dale3hballoob
authored andcommitted
Add Jekyll plugin for Configuration Variables (home-assistant#3415)
* Add Jekyll plugin for configuration variables * Fix requested changes * Remove blank lines after configuration tag * Add component/platform to configuration tag
1 parent eb2e547 commit 2676c87

7 files changed

+730
-270
lines changed

plugins/configuration.rb

+114
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
module Jekyll
2+
class ConfigurationBlock < Liquid::Block
3+
TYPE_LINKS = {
4+
'action' => '/docs/scripts/',
5+
'device_class' => '/components/%{component}/#device_class',
6+
'template' => '/docs/configuration/templating/',
7+
}
8+
9+
def initialize(tag_name, text, tokens)
10+
super
11+
@component, @platform = text.strip.split('.', 2)
12+
end
13+
14+
def slug(key)
15+
key.downcase.strip.gsub(' ', '-').gsub(/[^\w\-]/, '')
16+
end
17+
18+
def type_class(type)
19+
((type.is_a? Array) ? type.join(' ') : type).downcase
20+
end
21+
22+
def type_link(type, component: nil)
23+
if type.include? ','
24+
type = type.split(',')
25+
end
26+
27+
if type.is_a? Array
28+
return (type.map { |t| type_link(t, component: component) }).join(' | ')
29+
end
30+
31+
type.strip!
32+
if TYPE_LINKS.include? type.downcase
33+
url = TYPE_LINKS[type.downcase] % {component: component}
34+
"[%s](%s)" % [type, url]
35+
else
36+
type
37+
end
38+
end
39+
40+
def required_value(value)
41+
if value === true
42+
"Required"
43+
elsif value === false
44+
"Optional"
45+
else
46+
value.strip.titlecase
47+
end
48+
end
49+
50+
def render_config_vars(vars:, component:, platform:, classes: nil)
51+
result = Array.new
52+
result << "<dl class='#{classes}'>"
53+
54+
result << vars.map do |key, attr|
55+
markup = Array.new
56+
markup << "<dt><a class='title-link' name='#{slug(key)}' href='\##{slug(key)}'></a> #{key}</dt>"
57+
markup << "<dd>"
58+
markup << "<p class='desc'>"
59+
if attr.key? 'type'
60+
markup << "<span class='type'>(<span class='#{type_class(attr['type'])}'>"
61+
markup << "#{type_link(attr['type'], component: component)}</span>)</span>"
62+
end
63+
if attr.key? 'required'
64+
markup << "<span class='required'>(#{required_value(attr['required'])})</span>"
65+
end
66+
if attr.key? 'description'
67+
markup << "<span class='description'>#{attr['description']}</span>"
68+
end
69+
markup << "</p>"
70+
if attr.key? 'default'
71+
markup << "<p class='default'>Default value: #{attr['default']}</p>"
72+
end
73+
markup << "</dd>"
74+
75+
# Check for nested configuration variables
76+
if attr.key? 'keys'
77+
markup << "<dd>"
78+
markup << render_config_vars(
79+
vars: attr['keys'], component: component,
80+
platform: platform, classes: 'nested')
81+
markup << "</dd>"
82+
end
83+
84+
markup
85+
end
86+
87+
result << "</dl>"
88+
result.join
89+
end
90+
91+
def render(context)
92+
if @component.nil? and @platform.nil?
93+
page = context.environments.first['page']
94+
@component, @platform = page['slug'].split('.', 2)
95+
end
96+
97+
contents = super(context)
98+
99+
component = Liquid::Template.parse(@component).render context
100+
platform = Liquid::Template.parse(@platform).render context
101+
102+
vars = SafeYAML.load(contents)
103+
104+
<<~MARKUP
105+
<div class="config-vars">
106+
<h3><a class="title-link" name="configuration-variables" href="#configuration-variables"></a> Configuration Variables</h3>
107+
#{render_config_vars(vars: vars, component: component, platform: platform)}
108+
</div>
109+
MARKUP
110+
end
111+
end
112+
end
113+
114+
Liquid::Template.register_tag('configuration', Jekyll::ConfigurationBlock)

sass/custom/_paulus.scss

+62-5
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,6 @@ $primary-color: #049cdb;
100100
}
101101

102102
.frontpage {
103-
104103
.material-card {
105104
margin-bottom: 24px;
106105
}
@@ -109,7 +108,6 @@ $primary-color: #049cdb;
109108
.release-date {
110109
white-space: nowrap;
111110
}
112-
113111
}
114112

115113
.recent-posts {
@@ -202,7 +200,8 @@ h2:hover a.title-link,
202200
h3:hover a.title-link,
203201
h4:hover a.title-link,
204202
h5:hover a.title-link,
205-
h6:hover a.title-link {
203+
h6:hover a.title-link,
204+
dt:hover a.title-link {
206205
position: relative;
207206

208207
&::before {
@@ -237,7 +236,7 @@ h6:hover a.title-link {
237236
}
238237

239238
.icon i {
240-
border: none !important;
239+
border: none !important;
241240
}
242241
}
243242

@@ -310,18 +309,31 @@ article.post, article.page, article.listing {
310309
h2 {
311310
font-size: 1.5em;
312311
margin-top: 2em;
312+
313+
// Future re-design
314+
// margin: 1.5em 0 1rem;
315+
//
316+
// border: 0;
317+
// border-top: 1px solid $primary-color;
318+
// padding-top: 1.4rem;
313319
}
314320

315321
h3 {
316322
text-transform: uppercase;
317323
letter-spacing: 0.125rem;
318324
font-size: 1.2rem;
319325
margin-top: 2em;
326+
327+
// Future re-design
328+
// margin: 2em 0 1rem;
320329
}
321330

322331
h4 {
323332
font-size: 1.1rem;
324333
margin-top: 2em;
334+
335+
// Future re-design
336+
// margin: 1.5em 0 1rem;
325337
}
326338
}
327339

@@ -431,7 +443,7 @@ ul.sidebar-menu {
431443
}
432444

433445
a code {
434-
color: #049cdb;
446+
color: $primary-color;
435447
}
436448

437449
twitterwidget {
@@ -447,3 +459,48 @@ twitterwidget {
447459
max-width: 100%;
448460
overflow: hidden;
449461
}
462+
463+
// Configuration variables
464+
div.config-vars {
465+
// Future re-design
466+
// h3 {
467+
// border: 0;
468+
// border-top: 1px solid $primary-color;
469+
// padding-top: 1.4rem;
470+
// }
471+
472+
dl {
473+
margin-bottom: 1.5em;
474+
475+
&.nested {
476+
border-left: 1px dotted $primary-color;
477+
padding-left: 6px;
478+
}
479+
480+
dt {
481+
font-weight: bold;
482+
}
483+
484+
dd {
485+
margin: 0 0 0.5em 1em;
486+
487+
p.desc {
488+
margin: 0;
489+
490+
span.type,
491+
span.required {
492+
font-style: italic;
493+
494+
&::after {
495+
content: " "
496+
}
497+
}
498+
}
499+
500+
p.default {
501+
font-style: italic;
502+
margin: 0;
503+
}
504+
}
505+
}
506+
}

0 commit comments

Comments
 (0)