Summary Files Commits Branches @commit @diff @stats
path: root / generate-hooks / create-file-lists.awk
blob: 113bca6e7707f80f960037aafc318b1657670391 (plain) (blame)
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
# This is a really rudementary way to make making file lists less HTML-y
# I made it so:
# 1) You don't have to write quite as much HTML
# 2) The Data columns for each file get to be formatted as Markdown (<td> is not recognized as a block-level element, so the stuff inside get formatted https://kristaps.bsd.lv/lowdown/lowdown.5.html#HTML_Blocks)

# Very roughly, each file list has either this:
# <details><summary>Folder Info</summary> <table> <tr>File Data</tr> </table> </details>
# or this:
# <table> <tr>File Data</tr> </table>
# structure, the difference being that in the first, all of the files are in a folder.

# In Markdown you type in something like (without the "# " in front):
#
# &&fos
# Description
# &&file0
# <td>Data 1</td>
# <td>Data 1.1</td>
# <td>Data 1.2</td>
# &&file
# <td>Data 2</td>
# <td>Data 2.1</td>
# <td>Data 2.2</td>
# &&fod
#
# or
#
# &&nos
# <td>Data 1</td>
# <td>Data 1.1</td>
# <td>Data 1.2</td>
# &&file
# <td>Data 2</td>
# <td>Data 2.1</td>
# <td>Data 2.2</td>
# &&nod

#
# And the lines starting with && (I'll call them ftags) get replaced by some HTML. The <td></td> around each file Data is mandatory.
# &&file0 must only be used on before the very first file data, when files are in a folder. There must be no empty lines, lines mustn't start with spaces, there mustn't be anything around ftags and ftags have to be on their own lines.
# The commands have fairly intuitive abbreviations:
# &&nos    no folder start (start file list, without making a folder)
# &&fos    folder start
# &&file0    file start of first file in folder (must only be used when items are in a folder!)
# &&file     file start
# &&nod    no folder end (end file list, without having made a folder)
# &&fod    folder end

BEGIN {
	fileStarted = 0
	noFolder    = 1
	searchEndP  = 0

	# Special formatting
	nam[1] = "folder"
	rep[1] = "<details class=\"file-wrapper\"><summary>"
	nam[2] = "file"
	rep[2] = "</tr><tr>"
	nam[3] = "&#47;files" # /files
	rep[3] = "</tr></table></details>"
	nam[4] = "fd"
	rep[4] = "" # Not used, refer to substitute function

	nam[5] = "ft-photo"
	rep[5] = "photo-tabler.svg"
	nam[6] = "ft-text"
	rep[6] = "file-text-tabler.svg"
	nam[7] = "ft-zip"
	rep[7] = "file-zip-tabler.svg"
	nam[8] = "ft-movie"
	rep[8] = "movie-tabler.svg"
	nam[9] = "ft-markdown"
	rep[9] = "markdown-tabler.svg"

	for (i = 1; i <= length(nam); i++) {
		pat[i] = "&#38;&#38;" nam[i]
		if (index(nam[i], "ft") == 1)
			rep[i] = "<td><img class=\"file-icon unzoomable\" src=\"/img/" rep[i] "\"></td>"
	}
}

function substitute(ind) {
	gsub(/(<p>|<\/p>)/, "")

	# &&file
	if (ind == 2 && fileStarted == 0) {
		if (noFolder == 1)
			gsub("&#38;&#38;file", "<table class=\"file-list\"><tr>")
		else
			gsub("&#38;&#38;file", "</summary><table class=\"file-list\"><tr>")
		fileStarted = 1
	}
	# &&/files
	else if (ind == 3 && noFolder == 1) {
		gsub("&#38;&#38;&#47;files", "</tr></table>")
	}
	# &&fd
	else if (ind == 4) {
		gsub(/&#38;&#38;fd[ \t]*/, "<td>")
		gsub(/$/, "</td>")
	}
	else
		gsub(pat[ind], rep[ind])

	# &&folder
	if (ind == 1) {
		noFolder   = 0
		searchEndP = 1
	}
	# &&/files
	else if (ind == 3) {
		fileStarted = 0
		noFolder    = 1
	}
}

/^[ \t]*(<p>|)&#38;&#38;/ {
	for (i = 1; i <= length(nam); i++) {
		if (index($0, pat[i]) != 0)
			substitute(i)
	}
}

searchEndP == 1 && /<\/p>/ {
	gsub(/<\/p>/, "")
	searchEndP = 0
}

{
	print
}