summaryrefslogtreecommitdiff
path: root/helpers/do.outline
blob: 203d27ad4b0171d805ff99468249fbd1316d484b (plain)
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
#! /usr/local/bin2/gawk -f

# do.outline --- produce an outline of a texinfo document

BEGIN {
	# manifest constants
	TRUE = 1
	FALSE = 0

	# Levels at which different nodes can be
	Level["@top"] =	0
	Level["@appendix"] = 1
	Level["@chapter"] = 1
	Level["@majorheading"] = 1
	Level["@unnumbered"] = 1
	Level["@preface"] = 1
	Level["@appendixsec"] = 2
	Level["@heading"] = 2
	Level["@section"] = 2
	Level["@unnumberedsec"] = 2
	Level["@unnumberedsubsec"] = 3
	Level["@appendixsubsec"] = 3
	Level["@subheading"] = 3
	Level["@subsection"] = 3
	Level["@appendixsubsubsec"] = 4
	Level["@subsubheading"] = 4
	Level["@subsubsection"] = 4
	Level["@unnumberedsubsubsec"] = 4

	ah = bh = ch = 0

	appendix = 0
}

/^@ignore/ && Pass == 1, /^@end[ \t]+ignore/ && Pass == 1 {
	next
}


$1 in Level {
	# save type
	type = $1

	lev = Level[$1]

	if (lev == 1 && tolower($0) !~ /preface|foreword/) {
		if ($1 == "@appendix") {
			appendix = next_appendix(appendix)
			ah = appendix
		} else
			ah++
		bh = ch = dh = 0
	} else if (lev == 2) {
		bh++
		ch = dh = 0
	} else if (lev == 3) {
		ch++
		dh = 0
	} else if (lev == 4)
		dh++

	Unnumbered = ($1 ~ /^@unnumbered/)

	$1 = ""
	$0 = preprocess($0)

	for (i = 1; i < lev; i++)
		printf "\t"

	if (! Unnumbered) {
		printf("%s.", ah)
		if (bh)
			printf("%d.", bh)
		if (ch)
			printf("%d.", ch)
		if (dh)
			printf("%d.", dh)
	}

	printf("%s\n", $0)
}

function preprocess(record)
{
	record = gensub(/@(code|command|samp|env)\{([^\}]+)\}/, "\\2", "g", record)
	record = gensub(/@dots\{\}/, "...", "g", record)

	return record
}

function next_appendix(cur,	ind, letters)
{
	if (cur == 0)
		return "A"	# first time

	letters = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
	ind = index(letters, cur)
	if (ind > 0 && ind <= 26)
		return substr(letters, ++ind, 1)

	return "Z"
}