forked from home-assistant/home-assistant.io
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add My Home Assistant tag support (home-assistant#16714)
Co-authored-by: Bram Kragten <[email protected]>
- Loading branch information
1 parent
0f1b6d5
commit 415c61b
Showing
9 changed files
with
116 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
require 'uri' | ||
|
||
module Jekyll | ||
module HomeAssistant | ||
class My < Liquid::Tag | ||
|
||
def initialize(tag_name, args, tokens) | ||
super | ||
if args.strip =~ SYNTAX | ||
@redirect = Regexp.last_match(1).downcase | ||
@options = Regexp.last_match(2) | ||
else | ||
raise SyntaxError, <<~MSG | ||
Syntax error in tag 'my' while parsing the following options: | ||
` | ||
#{args} | ||
Valid syntax: | ||
{% my <redirect> [title="Link name"] [badge] [icon[="icon-puzzle-piece"]] [addon="core_ssh"] [blueprint_url=""] [domain="hue"] %} | ||
MSG | ||
end | ||
end | ||
|
||
def render(context) | ||
# We parse on render, as we now have context | ||
options = parse_options(@options, context) | ||
|
||
# Base URI | ||
uri = URI.join("https://my.home-assistant.io/redirect/", @redirect) | ||
|
||
# Build query string | ||
query = [] | ||
query += [["addon", options[:addon]]] if options.include? :addon | ||
query += [["blueprint_url", options[:blueprint_url]]] if options.include? :blueprint_url | ||
query += [["domain", options[:domain]]] if options.include? :domain | ||
unless query.empty? | ||
uri.query = URI.encode_www_form(query) | ||
end | ||
|
||
if options[:badge] | ||
title = options[:title] ? options[:title].gsub(/\ /, '_') : @redirect | ||
"<a href='#{uri}' class='my badge' target='_blank'>"\ | ||
"<img src='https://img.shields.io/badge/#{title}-my?style=for-the-badge&label=MY&logo=home-assistant&color=41BDF5&logoColor=white' />"\ | ||
"</a>" | ||
else | ||
title = options[:title] ? options[:title] : @redirect.gsub(/_/, ' ').titlecase | ||
icon = "" | ||
if options[:icon] | ||
raise ArgumentError, "No default icon for redirect #{@redirect}" \ | ||
if !!options[:icon] == options[:icon] and ! DEFAULT_ICONS.include?(@redirect) | ||
icon = !!options[:icon] == options[:icon] ? DEFAULT_ICONS[@redirect] : @options[:icon] | ||
icon = "<i class='#{icon}' /> " | ||
end | ||
"#{icon}<a href='#{uri}' class='my' target='_blank'>#{title}</a>" | ||
end | ||
end | ||
|
||
private | ||
|
||
SYNTAX = %r!^([a-z_]+)((\s+\w+(=([\w\.]+?|".+?"))?)*)$!.freeze | ||
OPTIONS_REGEX = %r!(?:\w="[^"]*"|\w=[\w\.]+|\w)+!.freeze | ||
|
||
DEFAULT_ICONS = { | ||
"config_flow_start" => "icon-plus-sign", | ||
"integrations" => "icon-puzzle-piece", | ||
} | ||
|
||
def parse_options(input, context) | ||
options = {} | ||
return options if input.empty? | ||
# Split along 3 possible forms: key="value", key=value, or just key | ||
input.scan(OPTIONS_REGEX) do |opt| | ||
key, value = opt.split("=") | ||
if !value.nil? | ||
if value&.include?('"') | ||
value.delete!('"') | ||
else | ||
value = context[value] | ||
end | ||
end | ||
options[key.to_sym] = value || true | ||
end | ||
options | ||
end | ||
end | ||
end | ||
end | ||
|
||
Liquid::Template.register_tag('my', Jekyll::HomeAssistant::My) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,6 +6,10 @@ | |
{%- else -%} | ||
<img src='https://brands.home-assistant.io/_/{{ page.ha_domain }}/logo.png' srcset='https://brands.home-assistant.io/_/{{ page.ha_domain }}/[email protected] 2x' /> | ||
{%- endif -%} | ||
|
||
{%- if page.ha_config_flow and page.ha_domain -%} | ||
{% my config_flow_start badge title="Add Integration" domain=page.ha_domain %} | ||
{%- endif -%} | ||
</div> | ||
|
||
{%- if page.ha_domain -%} | ||
|
@@ -42,12 +46,6 @@ | |
</div> | ||
{%- endif -%} | ||
|
||
{%- if page.ha_config_flow -%} | ||
<div class='section'> | ||
This integration is configurable via UI | ||
</div> | ||
{%- endif -%} | ||
|
||
{%- if page.ha_domain -%} | ||
<div class='section'> | ||
Source: <a href='https://github.com/home-assistant/core/tree/dev/homeassistant/components/{{ page.ha_domain }}'>View on GitHub</a> | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters