Summary Files Commits Branches @commit @diff @stats
path: root / md-meta.awk
blob: e4ef2bfc32aa6d127de35e6ec545a915a84f8a22 (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
# No character classes, since they aren't supported on every system
function emit(ind, val) {
	print "meta[\"" FILENAME "\", \"" ind "\"]=\"" val "\""
}
function erit(id, href, title) {
	print "reference[\"" id "\", \"href\"]=\"" href "\""
	print "reference[\"" id "\", \"title\"]=\"\\\"" title "\\\"\""
}
function efit(id, contents) {
	print "footnote[\"" id "\"]=\"" contents "\""
}

title != "" && stopdesc == 0 && /^[A-Za-z]/ {
	startedGathering = 1
	# Take only anchor text
	gsub(/\]\([^)]*\)/, "")
	gsub(/\[/, "")
	# Append line
	if (desc == "") desc = $0
	else desc = desc " " $0
	# Trim line to 200 characters
	if (length(desc) > 200) {
		desc = substr(desc, 1, 197) "..."
		stopdesc = 1
	}
}
stopdesc == 0 && startedGathering == 1 && /^[ \t]*$/ { stopdesc = 1 }

{
	words += NF
}

title == "" && /^# / {
	sub(/^#[ \t]*/, "")
	sub(/[ \t#]*$/, "")
	# sub(/.*/, titlef)
	title = $0
}

/^\[[^^].*\]:/ {
	match($0, /^\[.+\]:/)
	id = substr($0, 2, RLENGTH - 3)

	href = substr($0, RLENGTH + 1)
	reftitle = ""
	if (match(href, /[ \t]+["'(].*[)'"]$/)) {
		reftitle = substr(href, RSTART)
		href = substr(href, 1, RSTART - 1)

		sub(/^[ \t]["'(]*/, "", reftitle)
		sub(/[)'"][ \t]*$/, "", reftitle)
	}
	sub(/^[ \t]*<?/, "", href)
	sub(/>?[ \t]*$/, "", href)

	erit(id, href, reftitle)
}

/^\[\^[^\]]+\]:/ {
	match($0, /^\[\^[^\]]+\]:/)
	footnote_name = substr($0, 3, RLENGTH - 4)
	footnote_def = substr($0, RSTART + RLENGTH)
	sub(/^[ \t]*/, "", footnote_def)
	efit(footnote_name, footnote_def)
}

END {
	emit("title", title)
	emit("description", desc)
	emit("words", words)
}