Skip to content

Commit

Permalink
Add ProductDataEnricher plugin to enrich product pages metadata
Browse files Browse the repository at this point in the history
With this approach, enriched data is only computed once and available both in layouts and in plugins (such as the APIv1 plugin - see #2080).
This make things easier in both places.

validate.py now checks if releaseLabel is a string.

This PR relates to (and conflicts with) #2226.
  • Loading branch information
marcwrobel committed Jan 4, 2023
1 parent a5fa223 commit bfb74a4
Show file tree
Hide file tree
Showing 6 changed files with 143 additions and 57 deletions.
3 changes: 3 additions & 0 deletions _auto/validate.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,9 @@ def validate_product(file):
for r in data["releases"]:
assert_that_type_is(str, r["releaseCycle"], 'releaseCycle', file)

if "releaseLabel" in r:
assert_that_type_is(str, r["releaseLabel"], 'releaseLabel', file)

# eolColumn is assumed to be present, so check it unless it is disabled:
if not ("eolColumn" in data and data["eolColumn"]==False):
assert_that_type_is((bool, date), r["eol"], 'eol', file)
Expand Down
1 change: 1 addition & 0 deletions _config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -63,4 +63,5 @@ defaults:
path: "products"
values:
layout: product
LTSLabel: <abbr title="Long Term Support">LTS</abbr>
encoding: utf-8
11 changes: 0 additions & 11 deletions _includes/ltslabel.html

This file was deleted.

60 changes: 15 additions & 45 deletions _layouts/product.html
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
layout: default
---

{% if page.iconSlug %}
<img alt="{{page.title}} Logo" class=productlogo width=50 height=50 src="https://simpleicons.org/icons/{{page.iconSlug}}.svg" >
{% if page.iconUrl %}
<img alt="{{page.title}} Logo" class=productlogo width=50 height=50 src="{{page.iconUrl}}" >
{% endif %}

<div class="description">{{content | extract_element:'blockquote' | first }}</div>
Expand Down Expand Up @@ -60,60 +60,30 @@
{% assign diffSupport = support | minus:now %}
{% assign diffSecurity6 = diff | minus:10651938 %}
{% assign diffSupport6 = diffSupport | minus:10651938 %}
{% assign latestVersionNumber = r.latest | liquify %}
{% assign LTSLabel = page.LTSLabel | default: '<abbr title="Long Term Support">LTS</abbr>' %}

{% if r.releaseLabel %}{% assign t=r.releaseLabel%}{%else%}{%assign t=page.releaseLabel | default: '__RELEASE_CYCLE__' %}{%endif%}
{% capture releaseCycleText %}{{
t |
replace: '__RELEASE_CYCLE__',r.releaseCycle |
replace: '__LATEST__',latestVersionNumber |
replace: '__CODENAME__',r.codename |
liquify
}}{% include ltslabel.html lts=r.lts label=LTSLabel %}{% endcapture %}

{% capture releaseLink %}
{% if r.link and diff > 0 %}
{{r.link}}
{% elsif page.changelogTemplate and diff > 0 %}
{{
page.changelogTemplate |
replace: '__RELEASE_CYCLE__',r.releaseCycle |
replace: '__LATEST__',latestVersionNumber |
replace: '__CODENAME__',r.codename |
replace: '__LATEST_RELEASE_DATE__', r.latestReleaseDate
}}
{% endif %}
{% endcapture %}
{% assign releaseLink = releaseLink | liquify | strip %}

<!-- Main Row starts here -->
<tr>
<td>
{% comment %}Only put a link in the version column if the release column is not shown{% endcomment %}
{% if releaseLink != "" and page.releaseColumn == false %}
<a href="{{releaseLink}}" title="Release Notes / Changelog for {{releaseCycleText | strip_html}}">{{releaseCycleText}}</a>
{% if r.link and page.releaseColumn == false %}
<a href="{{ r.link }}" title="Release Notes / Changelog for {{ r.label | strip_html }}">{{ r.label }}</a>
{% else %}
{{releaseCycleText}}
{{ r.label }}
{% endif %}
</td>

{% if page.releaseDateColumn %}
<td>
{{r.releaseDate | timeago}} <div>({{r.releaseDate | date_to_string}})</div>
</td>
<td>{{ r.releaseDate | timeago }} <div>({{ r.releaseDate | date_to_string }})</div></td>
{% endif %}

{% if page.discontinuedColumn %}
<td class="{% unless r.discontinued %}bg-green-000{% endunless %}">
{% if r.discontinued %}
{% if r.discontinued == true%}
Discontinued
{% else %}
{{r.discontinued | timeago}} <div>({{r.discontinued | date_to_string}})</div>
{% endif %}
{% if r.discontinued == true %}
Discontinued
{% elsif r.discontinued == false %}
In Production
{% else %}
In Production
{{ r.discontinued | timeago }} <div>({{ r.discontinued | date_to_string }})</div>
{% endif %}
</td>
{% endif %}
Expand Down Expand Up @@ -191,13 +161,13 @@

{% if page.releaseColumn != false %}
<td {% if diff <= 0 %} class = "txt-linethrough" {% endif %} > <!-- if the support finished add txt-linethrough class -->
{% if releaseLink != "" %}
<a href="{{releaseLink}}" title="Release Notes / Changelog">{{latestVersionNumber}}</a>
{% if r.link != "" %}
<a href="{{ r.link }}" title="Release Notes / Changelog">{{ r.latest }}</a>
{% else %}
{{latestVersionNumber}}
{{ r.latest }}
{% endif %}
{% if diff > 0 and r.latestReleaseDate %}
<div>({{r.latestReleaseDate | date_to_string}})</div>
<div>({{ r.latestReleaseDate | date_to_string }})</div>
{% endif %}
</td>
{% endif %}
Expand Down
123 changes: 123 additions & 0 deletions _plugins/product-data-enricher.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
# This plugin enriches the product pages by setting or precomputing its metadata, so that it can be
# easily consumed in layouts or plugins (such as the APIv1 plugin).
module Jekyll
class ProductDataEnricher
class << self

TOPIC = "EndOfLife Product Data Enricher:"

def enrich(page)
Jekyll.logger.debug TOPIC, "Enriching #{page.name}"

set_id(page)
set_icon_url(page)
set_tags(page)

page.data["releases"].each { |release| enrich_release(page, release) }
end

def is_product?(page)
page.data['layout'] == 'product'
end

private

# Build the product id from the permalink.
def set_id(page)
page.data['id'] = page.data['permalink'][1..page.data['permalink'].length]
end

# Build the icon URL from the icon slug.
def set_icon_url(page)
if page['iconSlug']
page.data['iconUrl'] = "https://simpleicons.org/icons/#{page['iconSlug']}.svg"
end
end

# Explode tags space-separated string to a list if necessary.
# Also add the category as a default tag.
def set_tags(page)
tags = page.data['tags']

if tags
tags = (tags.kind_of?(Array) ? tags : tags.split)
else
tags = []
end

tags << page.data['category']
page.data['tags'] = tags
end

def enrich_release(page, cycle)
set_cycle_id(cycle)
set_cycle_lts(cycle)
set_cycle_discontinued(cycle)
set_cycle_link(page, cycle)
set_cycle_label(page, cycle)
add_lts_label_to_cycle_label(page, cycle)
end

# Build the cycle id from the permalink.
def set_cycle_id(cycle)
cycle['id'] = cycle['releaseCycle'].tr('/', '-')
end

def set_cycle_lts(cycle)
if !cycle['lts']
cycle['lts'] = false
end
end

def set_cycle_discontinued(cycle)
if !cycle['discontinued']
cycle['discontinued'] = false
end
end

def set_cycle_link(page, cycle)
if !cycle['link'] && page['changelogTemplate']
link = page['changelogTemplate'].gsub('__RELEASE_CYCLE__', cycle['releaseCycle'] || '')
link.gsub!('__CODENAME__', cycle['codename'] || '')
link.gsub!('__LATEST__', cycle['latest'] || '')
link.gsub!('__LATEST_RELEASE_DATE__', cycle['latestReleaseDate'] ? cycle['latestReleaseDate'].iso8601 : '')
cycle['link'] = Liquid::Template.parse(link).render(@context)
end
end

def set_cycle_label(page, cycle)
template = cycle['releaseLabel'] || page.data['releaseLabel']

if template
label = template.gsub('__RELEASE_CYCLE__', cycle['releaseCycle'] || '')
label.gsub!('__CODENAME__', cycle['codename'] || '')
label.gsub!('__LATEST__', cycle['latest'] || '')
cycle['label'] = Liquid::Template.parse(label).render(@context)
else
cycle['label'] = cycle['releaseCycle']
end
end

def add_lts_label_to_cycle_label(page, cycle)
if cycle['lts']
lts = cycle['lts']
lts_label = page.data['LTSLabel']

if lts == true
cycle['label'] = "#{cycle['label']} (#{lts_label})"
elsif lts.respond_to?(:strftime) # lts is a date
if lts > Date.today
cycle['label'] = "#{cycle['label']} (<span title=\"#{lts.iso8601}\">Upcoming</span> #{lts_label})"
else
cycle['label'] = "#{cycle['label']} (#{lts_label})"
end
end
end
end
end
end
end

Jekyll::Hooks.register [:pages], :post_init do |page|
Jekyll::ProductDataEnricher.enrich(page) if Jekyll::ProductDataEnricher.is_product?(page)
end
2 changes: 1 addition & 1 deletion products/raspberrypi.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ releases:
link: https://www.raspberrypi.com/products/raspberry-pi-pico/

- releaseCycle: "4-400"
releaseLabel: 400
releaseLabel: "400"
# https://www.raspberrypi.com/news/raspberry-pi-400-the-70-desktop-pc/
releaseDate: 2020-11-02
discontinued: 2026-01-01
Expand Down

0 comments on commit bfb74a4

Please sign in to comment.