feat: generate ref-style table - first cut

This commit is contained in:
Mike Cunneen 2025-08-19 16:11:08 +08:00
parent dd91b6dbad
commit 8498f2f736
5 changed files with 144 additions and 0 deletions

View file

@ -0,0 +1 @@
md-badges.md

View file

@ -0,0 +1,76 @@
BEGIN{
# initialize vars
TRUE=1;
FALSE=0;
count=0;
shouldSkip=FALSE;
# 'delete' initializes a var as an array
delete labelsArray[0];
delete defsArray[0];
delete refsArray[0];
# printf "" > "errors.txt";
}
/^| \[/{
# initialize our variables
shouldSkip=FALSE;
label=$2;
url=$2;
# extract the label by removing everything before and after it
gsub(/^[[:space:]]*\[!\[/, "", label);
gsub(/\].*$/, "", label);
# extract the URL by removing everything before and after it
gsub(/^[^\(]*\(/,"",url);
gsub(/\)\].*$/,"",url);
if (label == "") {
# print "label is empty: '"label"'; url='"url"'\n" >> "errors.txt" ;
shouldSkip=TRUE;
}
# ensure our label is unique by appending _alt to it
if (shouldSkip == FALSE) {
while(label in labelsRecordArray && shouldSkip == FALSE) {
# print "Found duplicate label: '"label"'; labelsRecordArray[label]=='"labelsRecordArray[label]"'; url='"url"'\n" >> "errors.txt" ;
if (labelsRecordArray[label] == url) {
shouldSkip=TRUE;
} else {
label=label"_alt" ;
}
}
}
if (shouldSkip == FALSE) {
# append this label to the set of unique labels
labelsRecordArray[label] = url;
# generate the markdown code for this label and url
refcode = "!["label"]["label"]";
defcode = "["label"]: "url" \""label"\"";
# store our markdown code against the label
labelsArray[count] = label;
defsArray[count] = defcode;
refsArray[count] = refcode;
count++;
}
}
END{
# output the markdown table
print "| Preview | Reference Markdown Code | Definition Markdown Code |"
print "|---------|------------------------|------------------------|"
for(i=0; i<count; i++) {
print "| "refsArray[i]" | `"refsArray[i]"` | `"defsArray[i]"`|" ;
}
# blank line to end the (visible) table and start the (non-visible) reference definitions
print;
# output the references
for(j=0; j<count; j++) {
print defsArray[j];
}
print;
}

View file

@ -0,0 +1,44 @@
#!/usr/bin/env bash
# #################################################################################
# extract-refstyle-badges.sh
#
# Description:
# Creates a table of reference-style markdown badges from the table rows in README.md
#
# #################################################################################
# get a reference to the current directory
DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null 2>&1 && pwd )"
READMEFILE="${DIR}/../../../README.md"
# extract the table rows from README.md, sort them, and pass them to
# the awk script to generate the reference-style link for each row.
# Add a newline at the end and write to md-badges.md
readmerows=$(
cat "${READMEFILE}" | \
grep -E '^\| \[' | \
sort
)
# add a newline before processing with awk
printf "%s\n" "$readmerows" \
| awk -F '|' -f "${DIR}/extract-refstyle-badges.awk" \
> md-badges.md
# now insert the generated md-badges.md content into README.md
lead='^<\!-- ### BEGIN GENERATED CONTENT -->'
tail='^<\!-- ### END GENERATED CONTENT -->'
sed -e "/$lead/,/$tail/{
/$lead/{
p;
r md-badges.md
};
/$tail/p;
d
}" "${READMEFILE}" > "${READMEFILE}.tmp"
mv "${READMEFILE}.tmp" "${READMEFILE}"
# echo "${READMEFILE}"

View file

@ -0,0 +1,14 @@
name: Extract refstyle badges
on:
push:
branches:
- main
- feature/reference-style-table
jobs:
extract-refstyle-badges:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- name: extract refstyle badges
run: ${{github.workspace}}/.github/scripts/extract-refstyle-badges/extract-refstyle-badges.sh

View file

@ -42,6 +42,7 @@ An extensive list of static Shields.io badges, sorted by category.
* [Version Control](#-version-control) * [Version Control](#-version-control)
* [Virtual Reality](#%EF%B8%8F-virtual-reality) * [Virtual Reality](#%EF%B8%8F-virtual-reality)
* [Website Status](#-website-status) * [Website Status](#-website-status)
* [Reference-Style Badges](#reference-style-badges)
> **Tip:** Use <kbd>Ctrl</kbd> + <kbd>F</kbd> to quickly search for and find a badge. > **Tip:** Use <kbd>Ctrl</kbd> + <kbd>F</kbd> to quickly search for and find a badge.
@ -974,6 +975,14 @@ An extensive list of static Shields.io badges, sorted by category.
--- ---
## Reference-Style Badges
The table below is automatically created. It contains the above badges, in a reference-style format.
<!-- ### BEGIN GENERATED CONTENT -->
<!-- any content between the above and below lines will be overwritten by the github action script -->
<!-- ### END GENERATED CONTENT -->
## CLI ## CLI
If you want to find badges from directly within your terminal, check out [mdbadges-cli](https://github.com/inttter/mdbadges-cli), which includes all the badges listed under this repository, and also offers various commands, like creating custom badges! If you want to find badges from directly within your terminal, check out [mdbadges-cli](https://github.com/inttter/mdbadges-cli), which includes all the badges listed under this repository, and also offers various commands, like creating custom badges!