xref: /openbsd-src/gnu/usr.bin/texinfo/util/outline.gawk (revision 1cc83814301f5a7b2c129e7d6202348011f53ea6)
1*1cc83814Sespie#! /usr/local/bin/gawk -f
2*1cc83814Sespie
3*1cc83814Sespie# texi.outline ---  produce an outline from a texinfo source file
4*1cc83814Sespie#
5*1cc83814Sespie# Copyright (C) 1998 Arnold David Robbins (arnold@gnu.org)
6*1cc83814Sespie#
7*1cc83814Sespie# TEXI.OUTLINE is free software; you can redistribute it and/or modify
8*1cc83814Sespie# it under the terms of the GNU General Public License as published by
9*1cc83814Sespie# the Free Software Foundation; either version 2 of the License, or
10*1cc83814Sespie# (at your option) any later version.
11*1cc83814Sespie#
12*1cc83814Sespie# TEXI.OUTLINE is distributed in the hope that it will be useful,
13*1cc83814Sespie# but WITHOUT ANY WARRANTY; without even the implied warranty of
14*1cc83814Sespie# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15*1cc83814Sespie# GNU General Public License for more details.
16*1cc83814Sespie#
17*1cc83814Sespie# You should have received a copy of the GNU General Public License
18*1cc83814Sespie# along with this program; if not, write to the Free Software
19*1cc83814Sespie# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA
20*1cc83814Sespie
21*1cc83814Sespie# NOTE:
22*1cc83814Sespie#	This program uses gensub(), which is specific to gawk.
23*1cc83814Sespie#	With some work (split, substr, etc), it could be made to work
24*1cc83814Sespie#	on other awks, but it's not worth the trouble for me.
25*1cc83814Sespie
26*1cc83814SespieBEGIN	\
27*1cc83814Sespie{
28*1cc83814Sespie	# Levels at which different nodes can be
29*1cc83814Sespie	Level["@top"] =	0
30*1cc83814Sespie	Level["@appendix"] = 1
31*1cc83814Sespie	Level["@chapter"] = 1
32*1cc83814Sespie	Level["@majorheading"] = 1
33*1cc83814Sespie	Level["@unnumbered"] = 1
34*1cc83814Sespie	Level["@appendixsec"] = 2
35*1cc83814Sespie	Level["@heading"] = 2
36*1cc83814Sespie	Level["@section"] = 2
37*1cc83814Sespie	Level["@unnumberedsec"] = 2
38*1cc83814Sespie	Level["@unnumberedsubsec"] = 3
39*1cc83814Sespie	Level["@appendixsubsec"] = 3
40*1cc83814Sespie	Level["@subheading"] = 3
41*1cc83814Sespie	Level["@subsection"] = 3
42*1cc83814Sespie	Level["@appendixsubsubsec"] = 4
43*1cc83814Sespie	Level["@subsubheading"] = 4
44*1cc83814Sespie	Level["@subsubsection"] = 4
45*1cc83814Sespie	Level["@unnumberedsubsubsec"] = 4
46*1cc83814Sespie
47*1cc83814Sespie	# insure that we were called correctly
48*1cc83814Sespie	if (ARGC != 2) {
49*1cc83814Sespie		printf("usage: %s texinfo-file\n", ARGV[0]) > "/dev/stderr"
50*1cc83814Sespie		exit 1
51*1cc83814Sespie	}
52*1cc83814Sespie
53*1cc83814Sespie	# init header counters
54*1cc83814Sespie	app_letters = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
55*1cc83814Sespie	app_h = 0
56*1cc83814Sespie	l1_h = l2_h = l3_h = l4_h = 0
57*1cc83814Sespie}
58*1cc83814Sespie
59*1cc83814Sespie# skip lines we're not interested in
60*1cc83814Sespie/^[^@]/	|| ! ($1 in Level)	{ next }
61*1cc83814Sespie
62*1cc83814SespieLevel[$1] == 1	{
63*1cc83814Sespie	if ($1 !~ /^@unnumbered/ || $1 !~ /heading/)
64*1cc83814Sespie		l1_h++
65*1cc83814Sespie	l2_h = l3_h = l4_h = 0
66*1cc83814Sespie	Ntabs = 0
67*1cc83814Sespie	Number = makenumber($1)
68*1cc83814Sespie	Title = maketitle($0)
69*1cc83814Sespie	print_title()
70*1cc83814Sespie}
71*1cc83814Sespie
72*1cc83814SespieLevel[$1] == 2	{
73*1cc83814Sespie	l2_h++
74*1cc83814Sespie	l3_h = l4_h = 0
75*1cc83814Sespie	Ntabs = 1
76*1cc83814Sespie	Number = makenumber($1)
77*1cc83814Sespie	Title = maketitle($0)
78*1cc83814Sespie	print_title()
79*1cc83814Sespie}
80*1cc83814Sespie
81*1cc83814SespieLevel[$1] == 3	{
82*1cc83814Sespie	l3_h++
83*1cc83814Sespie	l4_h = 0
84*1cc83814Sespie	Ntabs = 2
85*1cc83814Sespie	Number = makenumber($1)
86*1cc83814Sespie	Title = maketitle($0)
87*1cc83814Sespie	print_title()
88*1cc83814Sespie}
89*1cc83814Sespie
90*1cc83814SespieLevel[$1] == 4	{
91*1cc83814Sespie	l4_h++
92*1cc83814Sespie	Ntabs = 3
93*1cc83814Sespie	Number = makenumber($1)
94*1cc83814Sespie	Title = maketitle($0)
95*1cc83814Sespie	print_title()
96*1cc83814Sespie}
97*1cc83814Sespie
98*1cc83814Sespie# maketitle --- extract title
99*1cc83814Sespie
100*1cc83814Sespiefunction maketitle(str,		text)
101*1cc83814Sespie{
102*1cc83814Sespie	$1 = ""		# clobber section keyword
103*1cc83814Sespie	text = $0
104*1cc83814Sespie	gsub(/^[ \t]*/, "", text)
105*1cc83814Sespie	text = gensub(/@[a-z]+{/, "", "g", text)
106*1cc83814Sespie	text = gensub(/([^@])}/, "\\1", "g", text)
107*1cc83814Sespie	return text
108*1cc83814Sespie}
109*1cc83814Sespie
110*1cc83814Sespie# print_title --- print the title
111*1cc83814Sespie
112*1cc83814Sespiefunction print_title(	i)
113*1cc83814Sespie{
114*1cc83814Sespie	for (i = 1; i <= Ntabs; i++)
115*1cc83814Sespie		printf "\t"
116*1cc83814Sespie	printf("%s %s\n", Number, Title)
117*1cc83814Sespie}
118*1cc83814Sespie
119*1cc83814Sespie# makenumber --- construct a heading number from levels and section command
120*1cc83814Sespie
121*1cc83814Sespiefunction makenumber(command,	result, lev1)
122*1cc83814Sespie{
123*1cc83814Sespie	result = ""
124*1cc83814Sespie	if (command ~ /^@appendix/) {
125*1cc83814Sespie		if (Level[command] == 1)
126*1cc83814Sespie			app_h++
127*1cc83814Sespie
128*1cc83814Sespie		lev1 = substr(app_letters, app_h, 1)
129*1cc83814Sespie	} else if (command ~ /^@unnumbered/ || command ~ /heading/) {
130*1cc83814Sespie		lev1 = "(unnumbered)"
131*1cc83814Sespie	} else
132*1cc83814Sespie		lev1 = l1_h ""
133*1cc83814Sespie
134*1cc83814Sespie	result = lev1 "."
135*1cc83814Sespie	if (l2_h > 0) {
136*1cc83814Sespie		result = result l2_h "."
137*1cc83814Sespie		if (l3_h > 0) {
138*1cc83814Sespie			result = result l3_h "."
139*1cc83814Sespie			if (l4_h > 0) {
140*1cc83814Sespie				result = result l4_h "."
141*1cc83814Sespie			}
142*1cc83814Sespie		}
143*1cc83814Sespie	}
144*1cc83814Sespie	return result
145*1cc83814Sespie}
146