diff --git a/index-generation.sh b/index-generation.sh new file mode 100755 index 00000000..c98af91d --- /dev/null +++ b/index-generation.sh @@ -0,0 +1,104 @@ +#!/bin/bash + +DATE_CMD="date" + +# Check if the script is running on macOS +if [[ "$OSTYPE" == "darwin"* ]]; then + DATE_CMD="gdate" +fi + +# URL of the Discourse top.json +DISCOURSE_URL="https://discuss.privacyguides.net/top.json?period=weekly" + +# Fetch the JSON data +json_data=$(curl -s $DISCOURSE_URL) + +# Extract the first 3 topics +topics=$(echo $json_data | jq -r '.topic_list.topics[:3]') + +# Generate HTML for the first 3 posts +html_output="" +for row in $(echo "${topics}" | jq -r '.[] | @base64'); do + _jq() { + echo ${row} | base64 --decode | jq -r ${1} + } + + title=$(_jq '.title') + id=$(_jq '.id') + like_count=$(_jq '.like_count') + reply_count=$(_jq '.posts_count') + views=$(_jq '.views') + last_poster_username=$(_jq '.last_poster_username') + last_posted_at=$(_jq '.last_posted_at') + readable_date=$($DATE_CMD -d "$last_posted_at" +"%b %d, %Y %H:%M") + + html_output+="
  • " + html_output+="

    ${title}

    " + html_output+="
    " + html_output+="

    " + html_output+="" + html_output+="${last_poster_username}" + html_output+=" Last updated: ${readable_date}" + html_output+="" + html_output+="

    " + html_output+="

    " + html_output+="eye" + html_output+=" ${views} " + html_output+="heart" + html_output+=" ${like_count} " + html_output+="reply" + html_output+=" ${reply_count} " + html_output+="

    " + html_output+="
  • " +done + +# Insert the HTML output between the comments in index.html +sed -i '' '//,//{//!d;}; //r /dev/stdin' /Volumes/Git/privacyguides.org/site/en/index.html <<< "$html_output" + +# URL of the Discourse latest.json +DISCOURSE_URL="https://discuss.privacyguides.net/latest.json" + +# Fetch the JSON data +json_data=$(curl -s $DISCOURSE_URL) + +# Extract the first 12 topics +topics=$(echo $json_data | jq -r '.topic_list.topics[:12]') + +# Generate HTML for the first 12 posts +html_output="" +for row in $(echo "${topics}" | jq -r '.[] | @base64'); do + _jq() { + echo ${row} | base64 --decode | jq -r ${1} + } + + title=$(_jq '.title') + id=$(_jq '.id') + like_count=$(_jq '.like_count') + reply_count=$(_jq '.posts_count') + views=$(_jq '.views') + last_poster_username=$(_jq '.last_poster_username') + last_posted_at=$(_jq '.last_posted_at') + readable_date=$($DATE_CMD -d "$last_posted_at" +"%B %d, %Y %H:%M") + + html_output+="
  • " + html_output+="

    ${title}

    " + html_output+="
    " + html_output+="

    " + html_output+="" + html_output+="${last_poster_username}" + html_output+=" Last updated: ${readable_date}" + html_output+="" + html_output+="

    " + html_output+="

    " + html_output+="eye" + html_output+=" ${views} " + html_output+="heart" + html_output+=" ${like_count} " + html_output+="reply" + html_output+=" ${reply_count} " + html_output+="

    " + html_output+="
  • " +done + +# Insert the HTML output between the comments in index.html +sed -i '' '//,//{//!d;}; //r /dev/stdin' /Volumes/Git/privacyguides.org/site/en/index.html <<< "$html_output" diff --git a/theme/assets/javascripts/discourse-topics.js b/theme/assets/javascripts/discourse-topics.js deleted file mode 100644 index 1a070aef..00000000 --- a/theme/assets/javascripts/discourse-topics.js +++ /dev/null @@ -1,167 +0,0 @@ -/** - * @overview Generates a list of topics on a Discourse forum. - * @author Jonah Aragon - * @version 3.1.0 - * @license - * Copyright (c) 2023 - 2024 Jonah Aragon - * - * Permission is hereby granted, free of charge, to any person obtaining a copy - * of this software and associated documentation files (the "Software"), to deal - * in the Software without restriction, including without limitation the rights - * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell - * copies of the Software, and to permit persons to whom the Software is - * furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included in all - * copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE - * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, - * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE - * SOFTWARE. - */ - -async function getData(url) { - const response = await fetch(url); - return response.json() -} - -async function main() { - const elements = document.querySelectorAll("ul[data-forum]"); - - for (let j = 0; j < elements.length; j++) { - - var topics = elements[j]; - var dataset = topics.dataset; - - console.log("Fetching data from " + dataset.feed) - const data = await getData(dataset.feed); - var list = data['topic_list']['topics']; - var profiles = data['users']; - var count = dataset.count; - - for (var i = 0; i < count; i++) { - - if (list[i]['pinned'] == true) { - count++; - continue; - } - - var title = list[i]['title']; - var id = list[i]['id']; - - var topic = document.createElement("li"); - topic.className = "discourse-topic"; - - var h3 = document.createElement('p'); - h3.className = "discourse-title"; - var a1 = document.createElement('a'); - - a1.href = dataset.forum + '/t/' + id; - - var boldTitle = document.createElement('strong'); - boldTitle.innerText = title; - a1.appendChild(boldTitle); - h3.appendChild(a1); - - var authorinfo = document.createElement('p'); - authorinfo.className = "discourse-author"; - - var author_id = list[i]['posters'][0]['user_id']; - var author_data = profiles.find(profile => profile['id'] == author_id); - var author = document.createElement('span'); - author.className = "discourse-author"; - var avatar = document.createElement('img'); - avatar.src = dataset.forum + author_data['avatar_template'].replace("{size}", "40"); - avatar.width = 20; - avatar.height = 20; - avatar.className = "middle"; - author.appendChild(avatar); - var namespan = document.createElement('span'); - namespan.innerText = " Posted by " + author_data['username']; - author.appendChild(namespan); - authorinfo.appendChild(author); - - var postinfo = document.createElement('p'); - postinfo.className = "discourse-data"; - - var dateIcon = document.createElement('span'); - dateIcon.className = "twemoji"; - dateIcon.innerHTML = ''; - - var date = document.createElement('span'); - date.className = "discourse-date"; - var datestring = list[i]['bumped_at']; - var dateobject = new Date(datestring); - var now = new Date(); - var diff = now - dateobject; - var minutes = Math.floor(diff / 60000); - var hours = Math.floor(minutes / 60); - var days = Math.floor(hours / 24); - if (days > 0) { - if (days == 1) { - date.innerText = " 1 day ago "; - } - else { - date.innerText = " " + days + " days ago "; - } - } - else if (hours > 0){ - if (hours == 1) { - date.innerText = " 1 hour ago "; - } - else { - date.innerText = " " + hours + " hours ago "; - } - } - else { - if (minutes == 1) { - date.innerText = " 1 minute ago "; - } - else { - date.innerText = " " + minutes + " minutes ago "; - } - } - postinfo.appendChild(dateIcon); - postinfo.appendChild(date); - - var likesicon = document.createElement('span'); - likesicon.classList = "twemoji pg-red"; - likesicon.innerHTML = ''; - - var likes = document.createElement('span'); - likes.className = "discourse-likes"; - likes.innerText = " " + list[i]['like_count'] + " "; - postinfo.appendChild(likesicon); - postinfo.appendChild(likes); - - var replyIcon = document.createElement('span'); - replyIcon.classList = "twemoji"; - replyIcon.innerHTML = ''; - - var replies = document.createElement('span'); - replies.className = "discourse-replies"; - - var reply_count = list[i]['posts_count'] - 1; - if (reply_count == 1) { - replies.innerText = "1 Reply" - } - else { - replies.innerText = " " + reply_count - } - postinfo.appendChild(replyIcon); - postinfo.appendChild(replies); - - topic.appendChild(h3); - topic.appendChild(document.createElement('hr')); - topic.appendChild(authorinfo); - topic.appendChild(postinfo); - topics.appendChild(topic); - } - } -} - -main(); diff --git a/theme/home.html b/theme/home.html index 018d9345..3fa6a186 100644 --- a/theme/home.html +++ b/theme/home.html @@ -52,11 +52,10 @@ {% if config.theme.language == "en" %}

    Top discussions this week

    -
    @@ -103,11 +102,10 @@

    Latest discussions

    -
      +
        + + +
    {% endif %} {% endblock %} -{% block scripts %} - - {{ super() }} -{% endblock %}