Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add automated generation of README and website with Github Actions #28

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
188 changes: 188 additions & 0 deletions .github/scripts/github-generator/generator.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,188 @@
package main

import (
"embed"
"encoding/csv"
"html/template"
"io"
"log"
"os"
"regexp"
"sort"
"strings"
)

const othersCategory = "Others"

type Entry struct {
Title string
Link string
Description string
Category string
IsStaffPick bool
}

type Category struct {
Title string
Slug string
Note string
Order int
Entries []*Entry
}

type StaffPick struct {
Link string
Category bool
Page bool
}

var (
//go:embed templates
fs embed.FS
)

func main() {
categoriesFile, err := os.Open("categories.csv")
if err != nil {
panic(err)
}
defer categoriesFile.Close()

categoriesReader := csv.NewReader(categoriesFile)
categoriesRecords, err := categoriesReader.ReadAll()
if err != nil {
panic(err)
}
categories := make(map[string]*Category)
for i, row := range categoriesRecords {
if i == 0 {
continue
}
category := Category{
Title: row[0],
Slug: slugify(row[0]),
Note: row[1],
Order: i,
}

categories[category.Title] = &category
}

staffpicksFile, err := os.Open("staffpicks.csv")
if err != nil {
panic(err)
}
defer staffpicksFile.Close()

staffpicksReader := csv.NewReader(staffpicksFile)
staffpicksRecords, err := staffpicksReader.ReadAll()
if err != nil {
panic(err)
}

staffpicks := make(map[string]*StaffPick)
for i, row := range staffpicksRecords {
if i == 0 {
continue
}
staffpicks[row[0]] = &StaffPick{
Link: strings.TrimSpace(row[0]),
}
}

entriesFile, err := os.Open("entries.csv")
if err != nil {
panic(err)
}
defer entriesFile.Close()

entriesReader := csv.NewReader(entriesFile)
entriesRecords, err := entriesReader.ReadAll()
if err != nil {
panic(err)
}
var entriesWithNoCat []*Entry
for i, row := range entriesRecords {
if i == 0 {
continue
}
entry := Entry{
Title: strings.TrimSpace(row[0]),
Link: strings.TrimSpace(row[1]),
Description: strings.TrimSpace(row[2]),
Category: strings.TrimSpace(row[3]),
}
if _, ok := staffpicks[entry.Link]; ok {
entry.IsStaffPick = true
}
if _, ok := categories[entry.Category]; !ok {
entriesWithNoCat = append(entriesWithNoCat, &entry)
continue
}

categories[entry.Category].Entries = append(categories[entry.Category].Entries, &entry)

}

if len(entriesWithNoCat) > 0 {
categories[othersCategory] = &Category{
Title: othersCategory,
Entries: entriesWithNoCat,
Order: len(categories),
Slug: slugify(othersCategory),
}
}

var cat []*Category
for _, c := range categories {
cat = append(cat, c)
}

sort.Slice(cat, func(i, j int) bool {
return cat[i].Order < cat[j].Order
})

mardownFile, err := os.Create("README.md")
if err != nil {
panic(err)
}
defer mardownFile.Close()

if err := createReadme(mardownFile, cat); err != nil {
panic(err)
}

websiteFile, err := os.Create("website/index.html")
if err != nil {
panic(err)
}
defer websiteFile.Close()

if err := createWebsite(websiteFile, cat); err != nil {
panic(err)
}

log.Println("update README.md and Website")
}

func slugify(title string) string {
slug := strings.ReplaceAll(strings.ToLower(title), " ", "-")
reg := regexp.MustCompile("[^a-z0-9-_]")
return reg.ReplaceAllString(slug, "")
}

func createReadme(w io.Writer, categories []*Category) error {
tmplt, err := template.ParseFS(fs, "templates/readme_template.md")
if err != nil {
return err
}
return tmplt.Execute(w, categories)
}

func createWebsite(w io.Writer, categories []*Category) error {
tmplt, err := template.ParseFS(fs, "templates/website_template.html")
if err != nil {
return err
}
return tmplt.Execute(w, categories)
}
35 changes: 35 additions & 0 deletions .github/scripts/github-generator/templates/readme_template.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<div align="center">
<img src="./banner.png" />
</div>

# Awesome Gno [![Awesome](https://cdn.rawgit.com/sindresorhus/awesome/d7305f38d29fed78fa85652e3a63e154dd8e8829/media/badge.svg)](https://github.com/sindresorhus/awesome)

A curated list of awesome frameworks, libraries, software and resources related to the <a href='https://gno.land'>Gnoland</a> blockchain.

Gnoland is a robust blockchain that provides concurrency and scalability with smart contracts programmed in Gno, a Go interpreter.

> Do you gno?

## Contents

{{ range . }}
{{ .Order }}. [{{ .Title }}](#{{ .Slug }})
{{ end }}

{{ range . }}

## {{ .Title }}

{{ if .Note }}{{ .Note }}{{ end }}

{{ range .Entries }}

- [{{ .Title }}]({{ .Link }}){{ if .IsStaffPick }} - ![staffpick](./staffpick.png){{ end }} {{ if .Description }}- {{ .Description }}{{ end }}
{{ end }}
{{ end }}

## Contributing

Your contributions are always welcome! Please take a look at the [contribution guidelines](https://github.com/gnolang/awesome-gno/blob/master/CONTRIBUTING.md) first.

We will keep some pull requests open if we're not sure whether those libraries are awesome. You could [vote for them](https://github.com/gnolang/awesome-gno/pulls) by adding :+1: to them.
92 changes: 92 additions & 0 deletions .github/scripts/github-generator/templates/website_template.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<meta
name="description"
content="A curated list of awesome frameworks, libraries, software and resources related to the Gnoland blockchain."
/>
<meta
name="keywords"
content="Gnoland, Gno, blockchain, awesome, resources"
/>
<meta property="og:title" content="Awesome Gno" />
<meta property="og:type" content="website" />
<meta property="og:url" content="https://github.com/gnolang/awesome-gno" />
<meta
property="og:description"
content="A curated list of awesome frameworks, libraries, software and resources related to the Gnoland blockchain."
/>
<link rel="stylesheet" href="index.css" />
<title>Awesome Gno</title>
</head>
<body>
<div class="header">
<div class="header__logo">
<img class="logo__img" width="200" src="img/logo.png" alt="gno" />
</div>
<h1 class="header__title">Awesome GNØ</h1>
<a href="https://github.com/sindresorhus/awesome">
<img
src="https://cdn.rawgit.com/sindresorhus/awesome/d7305f38d29fed78fa85652e3a63e154dd8e8829/media/badge.svg"
alt="Awesome"
/>
</a>
<p class="header__description">
A curated list of awesome frameworks, libraries, software and resources
related to the Gnoland blockchain.
</p>
<p class="header__subdescription">
Gnoland is a robust blockchain that provides concurrency and scalability
with smart contracts programmed in Gno, a Go interpreter.
</p>
</div>

<div class="main">
<div class="main__sidebar">
<nav aria-label="Sidebar" class="sidebar">
<ul class="sidebar__list">
{{ range . }}
<li class="sidebar__item">
<a class="sidebar__link" href="#{{ .Slug }}" aria-current="page">
<span class="sidebar__title">{{ .Title }}</span>
<span class="sidebar__count">{{ len .Entries }}</span>
</a>
</li>
{{ end }}
</ul>
</nav>
</div>
<div class="main__content">
{{ range . }}
<div class="section">
<div class="section__header">
<h2 class="section__title" id="{{ .Slug }}">{{ .Title }}</h2>
{{ if .Note }}
<p class="section__description">{{ .Note }}</p>
{{ end }}
</div>
<ul class="section__list">
{{ range .Entries }}
<li
class="section__item{{ if .IsStaffPick }} section__item--staffpick{{ end }}"
>
<div class="item__title">
<a class="item__link" href="{{ .Link }}">{{ .Title}}</a>
{{ if .IsStaffPick }}<img
class="item__badge"
src="img/staffpick.png"
alt="Gno Staff Pick"
/>{{ end }}
</div>
<p class="item__description">{{ .Description }}</p>
</li>
{{ end }}
</ul>
</div>
{{ end }}
</div>
</div>
</body>
</html>
62 changes: 62 additions & 0 deletions .github/workflows/run-github-generator.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
name: Build README.md and Website

on:
push:
branches:
- main

workflow_dispatch:

permissions:
contents: write
pages: write
id-token: write

jobs:
build:
name: Update README.md and Website
runs-on: ubuntu-latest
steps:
- name: Checkout repository
uses: actions/checkout@v3
- name: Set up Go
uses: actions/setup-go@v3
with:
go-version: "stable"
- name: Run script
run: go run .github/scripts/github-generator/generator.go
- name: Configure Git
run: |
git config --global user.name "GitHub Actions"
git config --global user.email "[email protected]"
- name: Check if README was modified
run: |
if ! git diff --quiet README.md; then
git commit -am "Update README.md"
git push
fi
- name: Check if Website was modified
run: |
if ! git diff --quiet website/index.html; then
git commit -am "Update website"
git push
fi
deploy-website:
name: Deploy Website
needs: build
environment:
name: github-pages
url: ${{ steps.deployment.outputs.page_url }}
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v3
- name: Setup Pages
uses: actions/configure-pages@v3
- name: Upload artifact
uses: actions/upload-pages-artifact@v1
with:
path: "website"
- name: Deploy to GitHub Pages
id: deployment
uses: actions/deploy-pages@v1
Loading