Skip to content

Commit

Permalink
Merge pull request anuraghazra#196 from anuraghazra/auto-theme-readme
Browse files Browse the repository at this point in the history
infra: added auto theme readme generator script
  • Loading branch information
anuraghazra authored Jul 26, 2020
2 parents 7f220bb + ef57cc7 commit 9e8d4ae
Show file tree
Hide file tree
Showing 5 changed files with 215 additions and 55 deletions.
35 changes: 35 additions & 0 deletions .github/workflows/generate-theme-doc.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
name: Generate Theme Readme

on:
push:
branches:
- master
paths:
- "themes/index.js"

jobs:
build:
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v1
- name: setup node
uses: actions/setup-node@v1
with:
node-version: "12.x"

- name: npm install, generate readme
run: |
npm ci
npm run theme-readme-gen
env:
CI: true

- name: Run Script
uses: skx/github-action-tester@master
with:
script: ./push-theme-readme.sh
env:
CI: true
PERSONAL_TOKEN: ${{ secrets.PERSONAL_TOKEN }}
GH_REPO: ${{ secrets.GH_REPO }}
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
"main": "index.js",
"scripts": {
"test": "jest --coverage",
"test:watch": "jest --watch"
"test:watch": "jest --watch",
"theme-readme-gen": "node scripts/generate-theme-doc"
},
"author": "Anurag Hazra",
"license": "MIT",
Expand Down
108 changes: 108 additions & 0 deletions scripts/generate-theme-doc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
const theme = require("../themes/index");
const fs = require("fs");

const TARGET_FILE = "./themes/README.md";
const REPO_CARD_LINKS_FLAG = "<!-- REPO_CARD_LINKS -->";
const STAT_CARD_LINKS_FLAG = "<!-- STATS_CARD_LINKS -->";

const STAT_CARD_TABLE_FLAG = "<!-- STATS_CARD_TABLE -->";
const REPO_CARD_TABLE_FLAG = "<!-- REPO_CARD_TABLE -->";

const THEME_TEMPLATE = `## Available Themes
<!-- DO NOT EDIT THIS FILE DIRECTLY -->
With inbuilt themes you can customize the look of the card without doing any manual customization.
Use \`?theme=THEME_NAME\` parameter like so :-
\`\`\`md
![Anurag's github stats](https://github-readme-stats.vercel.app/api?username=anuraghazra&theme=dark&show_icons=true)
\`\`\`
## Stats
> These themes work both for the Stats Card and Repo Card.
| | | |
| :--: | :--: | :--: |
${STAT_CARD_TABLE_FLAG}
## Repo Card
> These themes work both for the Stats Card and Repo Card.
| | | |
| :--: | :--: | :--: |
${REPO_CARD_TABLE_FLAG}
${REPO_CARD_LINKS_FLAG}
${STAT_CARD_LINKS_FLAG}
[add-theme]: https://github.com/anuraghazra/github-readme-stats/edit/master/themes/index.js
Wanted to add a new theme? Consider reading the [contribution guidelines](../CONTRIBUTING.md#themes-contribution) :D
`;

const createRepoMdLink = (theme) => {
return `\n[${theme}]: https://github-readme-stats.vercel.app/api/pin/?username=anuraghazra&repo=github-readme-stats&cache_seconds=86400&theme=${theme}`;
};
const createStatMdLink = (theme) => {
return `\n[${theme}]: https://github-readme-stats.vercel.app/api?username=anuraghazra&show_icons=true&hide=contribs,prs&cache_seconds=86400&theme=${theme}`;
};

const generateLinks = (fn) => {
return Object.keys(theme)
.map((name) => fn(name))
.join("");
};

const createTableItem = ({ link, label }) => {
return `\`${label}\` ![${link}][${link}]`;
};
const generateTable = ({ isRepoCard }) => {
const rows = [];
const themes = Object.keys(theme).filter(
(name) => name !== (!isRepoCard ? "default_repocard" : "default")
);

for (let i = 0; i < themes.length; i += 3) {
const one = themes[i];
const two = themes[i + 1];
const three = themes[i + 2];

let tableItem1 = createTableItem({ link: one, label: one });
let tableItem2 = createTableItem({ link: two, label: two });
let tableItem3 = createTableItem({ link: one, label: one });

if (three === undefined) {
tableItem3 = `[Add your theme][add-theme]`;
}
rows.push(`| ${tableItem1} | ${tableItem2} | ${tableItem3} |`);
}

return rows.join("\n");
};

const buildReadme = () => {
return THEME_TEMPLATE.split("\n")
.map((line) => {
if (line.includes(REPO_CARD_LINKS_FLAG)) {
return generateLinks(createRepoMdLink);
}
if (line.includes(STAT_CARD_LINKS_FLAG)) {
return generateLinks(createStatMdLink);
}
if (line.includes(REPO_CARD_TABLE_FLAG)) {
return generateTable({ isRepoCard: true });
}
if (line.includes(STAT_CARD_TABLE_FLAG)) {
return generateTable({ isRepoCard: false });
}
return line;
})
.join("\n");
};

fs.writeFileSync(TARGET_FILE, buildReadme());
14 changes: 14 additions & 0 deletions scripts/push-theme-readme.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#!/bin/bash
set -x
set -e

export BRANCH_NAME=updated-theme-readme
git --version
git config --global user.email "[email protected]"
git config --global user.name "Github Readme Stats Bot"
git branch -d $BRANCH_NAME || true
git checkout -b $BRANCH_NAME
git add --all
git commit --message "docs(theme): Auto update theme readme" || exit 0
git remote add origin-$BRANCH_NAME https://${PERSONAL_TOKEN}@github.com/${GH_REPO}.git
git push --force --quiet --set-upstream origin-$BRANCH_NAME $BRANCH_NAME
110 changes: 56 additions & 54 deletions themes/README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
## Available Themes

<!-- DO NOT EDIT THIS FILE DIRECTLY -->

With inbuilt themes you can customize the look of the card without doing any manual customization.

Use `?theme=THEME_NAME` parameter like so :-
Expand All @@ -12,68 +14,68 @@ Use `?theme=THEME_NAME` parameter like so :-

> These themes work both for the Stats Card and Repo Card.
| | | |
| :------------------------------: | :------------------------------------: | :------------------------------------------------------: |
| `default` ![default][default] | `radical` ![radical][radical] | `merko` ![merko][merko] |
| `gruvbox` ![gruvbox][gruvbox] | `tokyonight` ![tokyonight][tokyonight] | `onedark` ![onedark][onedark] |
| `cobalt` ![cobalt][cobalt] | `synthwave` ![synthwave][synthwave] | `highcontrast` ![highcontrast][highcontrast] |
| `dracula` ![dracula][dracula] | `dark` ![dark][dark] | `prussian` ![prussian][prussian] |
| `monokai` ![monokai][monokai] | `vue` ![vue][vue] | `shades-of-purple` ![shades-of-purple][shades-of-purple] |
| `nightowl` ![nightowl][nightowl] | `buefy` ![buefy][buefy] | [Add your theme][add-theme] |
| | | |
| :--: | :--: | :--: |
| `default` ![default][default] | `dark` ![dark][dark] | `default` ![default][default] |
| `merko` ![merko][merko] | `gruvbox` ![gruvbox][gruvbox] | `merko` ![merko][merko] |
| `onedark` ![onedark][onedark] | `cobalt` ![cobalt][cobalt] | `onedark` ![onedark][onedark] |
| `highcontrast` ![highcontrast][highcontrast] | `dracula` ![dracula][dracula] | `highcontrast` ![highcontrast][highcontrast] |
| `monokai` ![monokai][monokai] | `vue` ![vue][vue] | `monokai` ![monokai][monokai] |
| `nightowl` ![nightowl][nightowl] | `buefy` ![buefy][buefy] | [Add your theme][add-theme] |

## Repo Card

> These themes work both for the Stats Card and Repo Card.
| | | |
| :------------------------------------------------------: | :---------------------------------------------: | :------------------------------------------------------------------------: |
| `default_repocard` ![default_repocard][default_repocard] | `radical` ![radical][radical_repocard] | `merko` ![merko][merko_repocard] |
| `gruvbox` ![gruvbox][gruvbox_repocard] | `tokyonight` ![tokyonight][tokyonight_repocard] | `onedark` ![onedark][onedark_repocard] |
| `cobalt` ![cobalt][cobalt_repocard] | `synthwave` ![synthwave][synthwave_repocard] | `highcontrast` ![highcontrast][highcontrast_repocard] |
| `dracula` ![dracula][dracula_repocard] | `dark` ![dark_repocard][dark_repocard] | `prussian` ![prussian_repocard][prussian_repocard] |
| `monokai` ![monokai_repocard][monokai_repocard] | `vue` ![vue_repocard][vue_repocard] | `shades-of-purple` ![shades-of-purple_repocard][shades-of-purple_repocard] |
| `nightowl` ![nightowl_repocard][nightowl_repocard] | `buefy` ![buefy_repocard][buefy_repocard] | [Add your theme][add-theme] |
| | | |
| :--: | :--: | :--: |
| `default_repocard` ![default_repocard][default_repocard] | `dark` ![dark][dark] | `default_repocard` ![default_repocard][default_repocard] |
| `merko` ![merko][merko] | `gruvbox` ![gruvbox][gruvbox] | `merko` ![merko][merko] |
| `onedark` ![onedark][onedark] | `cobalt` ![cobalt][cobalt] | `onedark` ![onedark][onedark] |
| `highcontrast` ![highcontrast][highcontrast] | `dracula` ![dracula][dracula] | `highcontrast` ![highcontrast][highcontrast] |
| `monokai` ![monokai][monokai] | `vue` ![vue][vue] | `monokai` ![monokai][monokai] |
| `nightowl` ![nightowl][nightowl] | `buefy` ![buefy][buefy] | [Add your theme][add-theme] |

<!-- Repo Card Theme previews -->

[default]: https://github-readme-stats.vercel.app/api/pin/?username=anuraghazra&repo=github-readme-stats&cache_seconds=86400&theme=default
[default_repocard]: https://github-readme-stats.vercel.app/api/pin/?username=anuraghazra&repo=github-readme-stats&cache_seconds=86400&theme=default_repocard
[dark_repocard]: https://github-readme-stats.vercel.app/api/pin/?username=anuraghazra&repo=github-readme-stats&cache_seconds=86400&theme=dark
[radical_repocard]: https://github-readme-stats.vercel.app/api/pin/?username=anuraghazra&repo=github-readme-stats&cache_seconds=86400&theme=radical
[merko_repocard]: https://github-readme-stats.vercel.app/api/pin/?username=anuraghazra&repo=github-readme-stats&cache_seconds=86400&theme=merko
[gruvbox_repocard]: https://github-readme-stats.vercel.app/api/pin/?username=anuraghazra&repo=github-readme-stats&cache_seconds=86400&theme=gruvbox
[cobalt_repocard]: https://github-readme-stats.vercel.app/api/pin/?username=anuraghazra&repo=github-readme-stats&cache_seconds=86400&theme=cobalt
[dark_repocard]: https://github-readme-stats.vercel.app/api/pin/?username=anuraghazra&repo=github-readme-stats&cache_seconds=86400&theme=dark
[dracula_repocard]: https://github-readme-stats.vercel.app/api/pin/?username=anuraghazra&repo=github-readme-stats&cache_seconds=86400&theme=dracula
[tokyonight_repocard]: https://github-readme-stats.vercel.app/api/pin/?username=anuraghazra&repo=github-readme-stats&cache_seconds=86400&theme=tokyonight
[synthwave_repocard]: https://github-readme-stats.vercel.app/api/pin/?username=anuraghazra&repo=github-readme-stats&cache_seconds=86400&theme=synthwave
[onedark_repocard]: https://github-readme-stats.vercel.app/api/pin/?username=anuraghazra&repo=github-readme-stats&cache_seconds=86400&theme=onedark
[highcontrast_repocard]: https://github-readme-stats.vercel.app/api/pin/?username=anuraghazra&repo=github-readme-stats&cache_seconds=86400&theme=highcontrast
[prussian_repocard]: https://github-readme-stats.vercel.app/api/pin/?username=anuraghazra&repo=github-readme-stats&cache_seconds=86400&theme=prussian
[monokai_repocard]: https://github-readme-stats.vercel.app/api/pin/?username=anuraghazra&repo=github-readme-stats&cache_seconds=86400&theme=monokai
[vue_repocard]: https://github-readme-stats.vercel.app/api/pin/?username=anuraghazra&repo=github-readme-stats&cache_seconds=86400&theme=vue
[shades-of-purple_repocard]: https://github-readme-stats.vercel.app/api/pin/?username=anuraghazra&repo=github-readme-stats&cache_seconds=86400&theme=shades-of-purple
[nightowl_repocard]: https://github-readme-stats.vercel.app/api/pin/?username=anuraghazra&repo=github-readme-stats&cache_seconds=86400&theme=nightowl
[buefy_repocard]: https://github-readme-stats.vercel.app/api/pin/?username=anuraghazra&repo=github-readme-stats&cache_seconds=86400&theme=buefy

<!-- Stats Theme previews -->

[default]: https://github-readme-stats.vercel.app/api?username=anuraghazra&theme=default&show_icons=true&hide=contribs,prs&cache_seconds=86400
[dark]: https://github-readme-stats.vercel.app/api?username=anuraghazra&theme=dark&show_icons=true&hide=contribs,prs&cache_seconds=86400
[radical]: https://github-readme-stats.vercel.app/api?username=anuraghazra&theme=radical&show_icons=true&hide=contribs,prs&cache_seconds=86400
[merko]: https://github-readme-stats.vercel.app/api?username=anuraghazra&theme=merko&show_icons=true&hide=contribs,prs&cache_seconds=86400
[gruvbox]: https://github-readme-stats.vercel.app/api?username=anuraghazra&theme=gruvbox&show_icons=true&hide=contribs,prs&cache_seconds=86400
[tokyonight]: https://github-readme-stats.vercel.app/api?username=anuraghazra&theme=tokyonight&show_icons=true&hide=contribs,prs&cache_seconds=86400
[onedark]: https://github-readme-stats.vercel.app/api?username=anuraghazra&theme=onedark&show_icons=true&hide=contribs,prs&cache_seconds=86400
[cobalt]: https://github-readme-stats.vercel.app/api?username=anuraghazra&theme=cobalt&show_icons=true&hide=contribs,prs&cache_seconds=86400
[synthwave]: https://github-readme-stats.vercel.app/api?username=anuraghazra&theme=synthwave&show_icons=true&hide=contribs,prs&cache_seconds=86400
[highcontrast]: https://github-readme-stats.vercel.app/api?username=anuraghazra&theme=highcontrast&show_icons=true&hide=contribs,prs&cache_seconds=86400
[dracula]: https://github-readme-stats.vercel.app/api?username=anuraghazra&theme=dracula&show_icons=true&hide=contribs,prs&cache_seconds=86400
[prussian]: https://github-readme-stats.vercel.app/api?username=anuraghazra&theme=prussian&show_icons=true&hide=contribs,prs&cache_seconds=86400
[monokai]: https://github-readme-stats.vercel.app/api?username=anuraghazra&theme=monokai&show_icons=true&hide=contribs,prs&cache_seconds=86400
[vue]: https://github-readme-stats.vercel.app/api?username=anuraghazra&theme=vue&show_icons=true&hide=contribs,prs&cache_seconds=86400
[shades-of-purple]: https://github-readme-stats.vercel.app/api?username=anuraghazra&theme=shades-of-purple&show_icons=true&hide=contribs,prs&cache_seconds=86400
[nightowl]: https://github-readme-stats.vercel.app/api?username=anuraghazra&theme=nightowl&show_icons=true&hide=contribs,prs&cache_seconds=86400
[buefy]: https://github-readme-stats.vercel.app/api?username=anuraghazra&theme=buefy&show_icons=true&hide=contribs,prs&cache_seconds=86400
[dark]: https://github-readme-stats.vercel.app/api/pin/?username=anuraghazra&repo=github-readme-stats&cache_seconds=86400&theme=dark
[radical]: https://github-readme-stats.vercel.app/api/pin/?username=anuraghazra&repo=github-readme-stats&cache_seconds=86400&theme=radical
[merko]: https://github-readme-stats.vercel.app/api/pin/?username=anuraghazra&repo=github-readme-stats&cache_seconds=86400&theme=merko
[gruvbox]: https://github-readme-stats.vercel.app/api/pin/?username=anuraghazra&repo=github-readme-stats&cache_seconds=86400&theme=gruvbox
[tokyonight]: https://github-readme-stats.vercel.app/api/pin/?username=anuraghazra&repo=github-readme-stats&cache_seconds=86400&theme=tokyonight
[onedark]: https://github-readme-stats.vercel.app/api/pin/?username=anuraghazra&repo=github-readme-stats&cache_seconds=86400&theme=onedark
[cobalt]: https://github-readme-stats.vercel.app/api/pin/?username=anuraghazra&repo=github-readme-stats&cache_seconds=86400&theme=cobalt
[synthwave]: https://github-readme-stats.vercel.app/api/pin/?username=anuraghazra&repo=github-readme-stats&cache_seconds=86400&theme=synthwave
[highcontrast]: https://github-readme-stats.vercel.app/api/pin/?username=anuraghazra&repo=github-readme-stats&cache_seconds=86400&theme=highcontrast
[dracula]: https://github-readme-stats.vercel.app/api/pin/?username=anuraghazra&repo=github-readme-stats&cache_seconds=86400&theme=dracula
[prussian]: https://github-readme-stats.vercel.app/api/pin/?username=anuraghazra&repo=github-readme-stats&cache_seconds=86400&theme=prussian
[monokai]: https://github-readme-stats.vercel.app/api/pin/?username=anuraghazra&repo=github-readme-stats&cache_seconds=86400&theme=monokai
[vue]: https://github-readme-stats.vercel.app/api/pin/?username=anuraghazra&repo=github-readme-stats&cache_seconds=86400&theme=vue
[shades-of-purple]: https://github-readme-stats.vercel.app/api/pin/?username=anuraghazra&repo=github-readme-stats&cache_seconds=86400&theme=shades-of-purple
[nightowl]: https://github-readme-stats.vercel.app/api/pin/?username=anuraghazra&repo=github-readme-stats&cache_seconds=86400&theme=nightowl
[buefy]: https://github-readme-stats.vercel.app/api/pin/?username=anuraghazra&repo=github-readme-stats&cache_seconds=86400&theme=buefy


[default]: https://github-readme-stats.vercel.app/api?username=anuraghazra&show_icons=true&hide=contribs,prs&cache_seconds=86400&theme=default
[default_repocard]: https://github-readme-stats.vercel.app/api?username=anuraghazra&show_icons=true&hide=contribs,prs&cache_seconds=86400&theme=default_repocard
[dark]: https://github-readme-stats.vercel.app/api?username=anuraghazra&show_icons=true&hide=contribs,prs&cache_seconds=86400&theme=dark
[radical]: https://github-readme-stats.vercel.app/api?username=anuraghazra&show_icons=true&hide=contribs,prs&cache_seconds=86400&theme=radical
[merko]: https://github-readme-stats.vercel.app/api?username=anuraghazra&show_icons=true&hide=contribs,prs&cache_seconds=86400&theme=merko
[gruvbox]: https://github-readme-stats.vercel.app/api?username=anuraghazra&show_icons=true&hide=contribs,prs&cache_seconds=86400&theme=gruvbox
[tokyonight]: https://github-readme-stats.vercel.app/api?username=anuraghazra&show_icons=true&hide=contribs,prs&cache_seconds=86400&theme=tokyonight
[onedark]: https://github-readme-stats.vercel.app/api?username=anuraghazra&show_icons=true&hide=contribs,prs&cache_seconds=86400&theme=onedark
[cobalt]: https://github-readme-stats.vercel.app/api?username=anuraghazra&show_icons=true&hide=contribs,prs&cache_seconds=86400&theme=cobalt
[synthwave]: https://github-readme-stats.vercel.app/api?username=anuraghazra&show_icons=true&hide=contribs,prs&cache_seconds=86400&theme=synthwave
[highcontrast]: https://github-readme-stats.vercel.app/api?username=anuraghazra&show_icons=true&hide=contribs,prs&cache_seconds=86400&theme=highcontrast
[dracula]: https://github-readme-stats.vercel.app/api?username=anuraghazra&show_icons=true&hide=contribs,prs&cache_seconds=86400&theme=dracula
[prussian]: https://github-readme-stats.vercel.app/api?username=anuraghazra&show_icons=true&hide=contribs,prs&cache_seconds=86400&theme=prussian
[monokai]: https://github-readme-stats.vercel.app/api?username=anuraghazra&show_icons=true&hide=contribs,prs&cache_seconds=86400&theme=monokai
[vue]: https://github-readme-stats.vercel.app/api?username=anuraghazra&show_icons=true&hide=contribs,prs&cache_seconds=86400&theme=vue
[shades-of-purple]: https://github-readme-stats.vercel.app/api?username=anuraghazra&show_icons=true&hide=contribs,prs&cache_seconds=86400&theme=shades-of-purple
[nightowl]: https://github-readme-stats.vercel.app/api?username=anuraghazra&show_icons=true&hide=contribs,prs&cache_seconds=86400&theme=nightowl
[buefy]: https://github-readme-stats.vercel.app/api?username=anuraghazra&show_icons=true&hide=contribs,prs&cache_seconds=86400&theme=buefy

[add-theme]: https://github.com/anuraghazra/github-readme-stats/edit/master/themes/index.js

Wanted to add a new theme? Consider reading the [contribution guidelines](../CONTRIBUTING.md#themes-contribution) :D

0 comments on commit 9e8d4ae

Please sign in to comment.