summaryrefslogtreecommitdiff
path: root/contrib/outline.gawk
blob: fa6726356e305439b613123bb83c870fc80fb532 (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
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
#! /usr/local/bin/gawk -f

# texi.outline ---  produce an outline from a texinfo source file
# 
# Copyright (C) 1998 Arnold David Robbins (arnold@gnu.org)
# 
# TEXI.OUTLINE is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 3 of the License, or
# (at your option) any later version.
# 
# TEXI.OUTLINE is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
# 
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

# NOTE:
#	This program uses gensub(), which is specific to gawk.
#	With some work (split, substr, etc), it could be made to work
#	on other awks, but it's not worth the trouble for me.

BEGIN	\
{
	# Levels at which different nodes can be
	Level["@top"] =	0
	Level["@appendix"] = 1
	Level["@chapter"] = 1
	Level["@majorheading"] = 1
	Level["@unnumbered"] = 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

	# insure that we were called correctly
	if (ARGC != 2) {
		printf("usage: %s texinfo-file\n", ARGV[0]) > "/dev/stderr"
		exit 1
	}

	# init header counters
	app_letters = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
	app_h = 0
	l1_h = l2_h = l3_h = l4_h = 0
}

# skip lines we're not interested in
/^[^@]/	|| ! ($1 in Level)	{ next }

Level[$1] == 1	{
	if ($1 !~ /^@unnumbered/ || $1 !~ /heading/)
		l1_h++
	l2_h = l3_h = l4_h = 0
	Ntabs = 0
	Number = makenumber($1)
	Title = maketitle($0)
	print_title()
}

Level[$1] == 2	{
	l2_h++
	l3_h = l4_h = 0
	Ntabs = 1
	Number = makenumber($1)
	Title = maketitle($0)
	print_title()
}

Level[$1] == 3	{
	l3_h++
	l4_h = 0
	Ntabs = 2
	Number = makenumber($1)
	Title = maketitle($0)
	print_title()
}

Level[$1] == 4	{
	l4_h++
	Ntabs = 3
	Number = makenumber($1)
	Title = maketitle($0)
	print_title()
}

# maketitle --- extract title

function maketitle(str,		text)
{
	$1 = ""		# clobber section keyword
	text = $0
	gsub(/^[ \t]*/, "", text)
	text = gensub(/@[a-z]+{/, "", "g", text)
	text = gensub(/([^@])}/, "\\1", "g", text)
	return text
}

# print_title --- print the title

function print_title(	i)
{
	for (i = 1; i <= Ntabs; i++)
		printf "\t"
	printf("%s %s\n", Number, Title)
}

# makenumber --- construct a heading number from levels and section command

function makenumber(command,	result, lev1)
{
	result = ""
	if (command ~ /^@appendix/) {
		if (Level[command] == 1)
			app_h++

		lev1 = substr(app_letters, app_h, 1)
	} else if (command ~ /^@unnumbered/ || command ~ /heading/) {
		lev1 = "(unnumbered)"
	} else
		lev1 = l1_h ""

	result = lev1 "."
	if (l2_h > 0) {
		result = result l2_h "."
		if (l3_h > 0) {
			result = result l3_h "."
			if (l4_h > 0) {
				result = result l4_h "."
			}
		}
	}
	return result
}