~charles/epbsd-browser

ref: 642df38f6d00efd6d5dae3843961cb00559d9be8 epbsd-browser/bin/generate-md -rwxr-xr-x 4.4 KiB View raw
642df38f — Charles Daniels add some flavor text to index 1 year, 4 months ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
#!/bin/sh

# Script to generate EPBSD browser site
#
# This script will generate files under $2/*/* in the same structure as
# the input ports tree ($1). Files are generated in markdown format for later
# processing by ssg.
#
# The "tree base" specifies the base URL for linking to files in the master
# branch of the EPBSD git repository.
#
# The "commit base" specifies the base URL for linking to commits in the EPBSD
# repository
#
# Usage:
#
#	$1 . . . repository URL
#	$2 . . . output directory
#	$3 . . . tree base
#	$4 . . . commit base

set -e
set -u

if [ $# -ne 4 ] ; then
	echo "$0 REPO_URL OUTPUT_DIR TREE_BASE COMMIT_BASE"
	exit 1
fi

REPO_URL="$1"
TREE_BASE="$3"
COMMIT_BASE="$4"

# get the fully qualified path to the output directory -- we use pwd instead of
# realpath since it's more portable
mkdir -p "$2"
cd "$2"
OUTPUT_DIR="$(pwd)"

# cd to the parent dir of this script
cd "$(dirname "$0")"

# pwd will give us the fully-qualified path to where we are installed
INSTALL_DIR="$(pwd)"

# dir to clone the EPBSD tree into
TEMP_DIR="$(mktemp -d)"

cd "$TEMP_DIR"

# fetch the EPBSD tree
git clone "$REPO_URL" ports
cd ports

get_make_variable () {
	# get a single variable ($1) from $port/Makefile

	# TODO: should use make -C show=$1 - how to set environment variables?
	perl -ne 'print if /^'"$1"'/' < "$port/Makefile" | \
		awk '{$1=$1}1' | \
		cut -d '=' -f 2-
}

gen_port_page () {
	# generate the output Markdown for a single port, specified by the
	# global $port
	#
	# Note that $port needs to have the format ./category/portname/

	# extract primary category and name
	category="$(echo "$port" | cut -d '/' -f 2)"
	name="$(echo "$port" | cut -d '/' -f 3)"

	# extract information from the Makefile
	comment="$(get_make_variable COMMENT)"
	categories="$(get_make_variable CATEGORIES)"
	homepage="$(get_make_variable HOMEPAGE)"
	maintainer="$(get_make_variable MAINTAINER)"

	# get package description
	if [ ! -e "$port/pkg/DESCR" ] ; then
		description="Missing DESCR file."
	else
		description="$(cat "$port/pkg/DESCR")"
	fi

	# create target directory
	page_dir="$OUTPUT_DIR/$category/$name"
	mkdir -p "$page_dir"

	page="$page_dir/index.md"

	# generate page contents
	echo "# [$category](../index.html)/$name - $comment" > "$page"
	echo "" >> "$page"
	echo "## Quick Links" >> "$page"
	echo "" >> "$page"

	# handle empty homepage
	if [ "$homepage" = "" ] ; then
		echo "* HOMEPAGE not set" >> "$page"
	else
		echo "* [$name's homepage]($homepage)" >> "$page"
	fi

	echo "* [ports/$category/$name on GitLab]($TREE_BASE/$category/$name)" >> "$page"
	echo "" >> "$page"
	echo "## Description"  >> "$page"
	echo "" >> "$page"
	echo "$description" >> "$page"
	echo "" >> "$page"
	echo "## Commit History" >> "$page"
	echo "" >> "$page"
	git log --format=format:'%h#%H#%an#%ai#%s' -- "$port" | \
		awk 'BEGIN {FS="#"} {printf("* [`%s`]('"$COMMIT_BASE"'/%s) by %s at %s: %s\n", $1, $2, $3, $4, $5)}' >> "$page"

	# add the page to the index
	echo "$name" >> "$OUTPUT_DIR/$category/index.list"

	# make sure the category has been added to the top-level index
	echo "$category" >> "$OUTPUT_DIR/index.list"

}

# clear existing index files
rm -f "$OUTPUT_DIR/index.list"
rm -f "$OUTPUT_DIR"/*/index.list

# generate pages for each port
for port in ./*/*/ ; do
	echo "$0: begin processing port $port"
	gen_port_page

done

# generate the top-level index
echo "# Index for ./\n" > "$OUTPUT_DIR/index.md"
echo "" >> "$OUTPUT_DIR/index.md"
echo "Welcome to the (unofficial) ports browser for the " >> "$OUTPUT_DIR/index.md"
echo "[EPBSD](https://epbsd.org) project. This site is " >> "$OUTPUT_DIR/index.md"
echo "generated by [epbsd-browser](https://git.sr.ht/~charles/epbsd-browser)." >> "$OUTPUT_DIR/index.md"
echo "" >> "$OUTPUT_DIR/index.md"
sort < "$OUTPUT_DIR/index.list" | uniq | while read -r category ; do
	echo "* [$category](./$category/index.html)" >> "$OUTPUT_DIR/index.md"
done

echo "" >> "$OUTPUT_DIR/index.md"
echo "---" >> "$OUTPUT_DIR/index.md"
echo "" >> "$OUTPUT_DIR/index.md"
echo "page generated: $(date)" >> "$OUTPUT_DIR/index.md"

# generate the index for each category
for category_dir in ./*/ ; do
	category="$(basename "$category_dir")"
	echo "$0: generating index for $category"
	echo "# Index for [.](../index.html)/$category/\n" > "$OUTPUT_DIR/$category/index.md"
	sort < "$OUTPUT_DIR/$category/index.list" | uniq | while read -r name ; do
		echo "* [$name](./$name/index.html)" >> "$OUTPUT_DIR/$category/index.md"
	done
done

cd "$OUTPUT_DIR"
rm -rf "$TEMP_DIR"