Summary Files Commits Branches @commit @diff @stats
path: root / awk-edits / Header-description.awk
blob: 1822b88734fd9046d17d023b7d0c28b262ded7d1 (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
#
# == About ==
#
# Every page has a description and og:description meta tags. These descriptions are
# generated from the text inside paragraphs of the current page.
# The value for those meta tags is "[DESCRIPTION]" and it gets replaced with
# either all of the text on the page, or only the first 200 characters.

function appToDesc(l) {
	gsub(/<[^>]*>/, "", l) ; gsub("&#8217;", "'", l)
	gsub(/[^a-zA-Z0-9 '.]/, "", l)
	gsub(/^[ \t]*/, "", l) ; gsub(/[ \t]*$/, "", l)

	desc = desc l
	if (length(desc) >= 200) {
		desc = substr(desc, 1, 197) "..."
		return 1
	}
	return 0
}

BEGIN {
	stop = 0

	# Loop over the whole file, extract all text in first couple of paragraphs,
	# store it in desc (but no more than 200 characters will be saved)
	while(stop == 0 && getline < ARGV[1]) {
		if (index($0, "<p") > 0)
			stop = appToDesc($0)
	}
	close(ARGV[1]) # FILENAME isn't set during BEGIN
}

"\\[DESCRIPTION\\]" {
	gsub("\\[DESCRIPTION\\]", desc)
}