[internal] Switch to a hash for topic-post counts

This commit is contained in:
Nemo 2023-11-30 10:08:52 +05:30
parent 14e5d33669
commit fe1cf59b9a
4 changed files with 16 additions and 23 deletions

View File

@ -1,5 +1,5 @@
<nav> <nav>
<ul> <ul>
{% for topic in site.data.topics %}<li class="topic-list"><a href="/{{ topic}}/">{{topic | capitalize|strip}}</a></li>{% endfor %} {% for T in site.data.topics %}{%if T[1]>0%}<li class="topic-list"><a href="/{{ T[0]}}/">{{T[0] | capitalize|strip}}</a></li>{%endif%}{% endfor %}
</ul> </ul>
</nav> </nav>

View File

@ -40,28 +40,27 @@ class BeatrootNews < Jekyll::Generator
# Main plugin action, called by Jekyll-core # Main plugin action, called by Jekyll-core
def generate(site) def generate(site)
@site = site @site = site
site.data['topics'] = Set.new site.data['topics'] # Topic Counter
count = 0 site.data['topics'] = site.data['topics'].to_h {|topic| [topic, 0]}
get_content.each do |article| get_content.each do |article|
page = make_page(article['attributes']['modules']) page = make_page(article['attributes']['modules'])
if page if page
site.pages << page site.pages << page
page['topics'].each do |topic| page['topics'].each do |topic|
unless site.data['topics'].include? topic unless site.data['topics'].include? topic
site.data['topics'] << topic site.data['topics'][topic] = 0
Jekyll.logger.warn "News:", "New Topic #{topic}" Jekyll.logger.warn "News:", "New Topic #{topic}"
end end
end end
count+=1 site.data['topics'][page['topics'].first] += 1
end end
end end
site.data['topics'].each do |topic| site.data['topics'].each do |topic, count|
@site.pages << make_topic_page(topic) @site.pages << make_topic_page(topic)
end end
site.data['topics'] = site.data['topics'].to_a.sort Jekyll.logger.info "News:", "Generated #{site.data['topics'].values.sum} pages"
Jekyll.logger.info "News:", "Generated #{count} pages"
end end
private private

View File

@ -7,23 +7,17 @@ layout: default
<h1 class="topic-heading">{{page.title}}</h1> <h1 class="topic-heading">{{page.title}}</h1>
<div class="hr-line"></div> <div class="hr-line"></div>
<p class="topic-date"><time datetime="{{site.time | date: '%y-%m-%d'}}">{{site.time | date: "%A, %b, %e, %Y"}}</time></p> <p class="topic-date"><time datetime="{{site.time | date: '%y-%m-%d'}}">{{site.time | date: "%A, %b, %e, %Y"}}</time></p>
{% for topic in site.data.topics %} {% for T in site.data.topics %}
{% assign post_count = 0 %} {% assign post_count = T[1] %}
{% capture h %} {% if post_count > 0 %}
<div class="topic-{{topic|downcase}}"> <div class="topic-{{topic|downcase}}">
<h2><a href="/{{topic}}/">{{topic | capitalize}}</a></h2> <h2><a href="/{{T[0]}}/">{{T[0] | capitalize}}</a></h2>
{% for post in site.pages %} {% for post in site.pages %}
{% if post.topics and post.topics.first == topic %} {% if post.topics and post.topics.first == T[0] %}
{% include_cached story.html story=post heading_level=3%} {% include_cached story.html story=post heading_level=3%}
{% assign post_count = post_count | plus: 1 %}
{% endif %} {% endif %}
{% endfor %} {% endfor %}
</div> </div>
{% endcapture %}
{% if post_count > 0 %}
{{h}}
{% endif %} {% endif %}
{% endfor %} {% endfor %}

View File

@ -20,11 +20,11 @@ noindex: true
<h2>Hide These Topics</h2> <h2>Hide These Topics</h2>
<fieldset> <fieldset>
<legend>Topics are only hidden on the home screen</legend> <legend>Topics are only hidden on the home screen</legend>
{% for topic in site.data.topics %} {% for T in site.data.topics %}
<label for="checkbox-hide-{{topic}}"> <label for="checkbox-hide-{{T[0]}}">
<input type=checkbox class="checkbox-hide" id="checkbox-hide-{{topic}}" value="{{topic}}"> <input type=checkbox class="checkbox-hide" id="checkbox-hide-{{T[0]}}" value="{{T[0]}}">
{{topic | capitalize}} {{T[0] | capitalize}}
</label> </label>
{% endfor %} {% endfor %}
</fieldset> </fieldset>