File: mkfaq

package info (click to toggle)
tela 1.28-2
  • links: PTS
  • area: main
  • in suites: slink
  • size: 6,596 kB
  • ctags: 5,519
  • sloc: ansic: 14,013; cpp: 13,376; lex: 1,651; fortran: 1,048; yacc: 834; sh: 715; makefile: 464
file content (158 lines) | stat: -rwxr-xr-x 2,980 bytes parent folder | download | duplicates (4)
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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
#!/bin/sh
#
# mkfaq : produce fyle.faq.txt and fyle.faq.html from fyle.faq
# PJ 30.11.1994
# Generalized 28.2.1995 PJ.

if [ $# -ne 1 ]; then
	echo 'Usage: mkfaq file.faq'
	echo '  produces file.faq.txt and file.faq.html from a FAQ'
	echo '  master file.'
	exit 1
fi

inputfile=$1

qfile=qfile.mkfaqfile$$
tmp=tmp.mkfaqfile$$
header=header.mkfaqfile$$
cp /dev/null "$header"
cp /dev/null "$qfile"

for html in false true
do

if [ "$html" = "true" ]
then outfile=${inputfile}.html
else outfile=${inputfile}.txt
fi
nawk -v "qfile=${qfile}" -v "header=${header}" -v "ishtml=${html}" '
BEGIN {
	Qmode = 0;
	Amode = 0;
	A2mode = 0;
	Qno = 1;
	sectno = 0;
	QSeen = 0;
	TitleWritten = 0
	html = (ishtml == "true");
	BigHeader = 2;
}
function prn (file) {
	if (file == 1) print; else print >>file
	if (html) {
		if (length() == 0) {
			if (file == 1) printf "<p>"; else printf "<p>" >>file
		}
	}
}
function StartQuoteMode() {
	if (html) printf "<blockquote><code><pre>"
}
function EndQuoteMode() {
	if (html) printf "</pre></code></blockquote>"
}
/\/Q\// {
	Qmode = 1
	QSeen = 1
	next
}
/\/A\// {
	Amode = 1
	Qmode = 0
	StartQuoteMode();
	next
}
/\/Answer\// {
	A2mode = 1
	Amode = 1
	Qmode = 0
	StartQuoteMode();
	next
}
/\/EndAnswer\// {
	Amode = 0
	A2mode = 0
	Qmode = 0
	EndQuoteMode();
	printf ""
	next
}
/^[A-Z][A-Z 0-9]*$/ {
	sectno++
	Qno = 1
	QSeen = 1
	TitleMode = 1
	if (html) {printf "<h2>" >>qfile; printf "<h2>"}
	prn(qfile)
	#prn(1)
	print
	if (html) {printf "</h2>" >>qfile; printf "</h2>"}
	next
}
/.*/ {
	TitleMode = 0
	# Substitute HTML abbreviations
	if (html) {
		sub("<","\\&lt;");
		sub(">","\\&gt;");
	}
	if (Qmode) {
		if (sectno==0) sectno=1;
		if (html) printf "<a name=\"Q%d.%d\"> <h%d>",sectno,Qno,SmallHeader
		printf "Q%d.%d: %s",sectno,Qno,$0
		if (substr($0,length())!="." && substr($0,length())!="!") printf "?"
		if (html) printf "</h%d></a>",SmallHeader
		printf "\n"
		if (html) printf "<a href=\"#Q%d.%d\">",sectno,Qno >>qfile
		printf "Q%d.%d: %s",sectno,Qno,$0 >>qfile
		if (substr($0,length())!="." && substr($0,length())!="!") printf "?" >>qfile
		if (html) printf "</a><p>" >>qfile
		printf "\n" >>qfile
		Qmode = 0;
		Qno++;
	} else if (Amode) {
		#prn(1)
		print
		if (!A2mode) {
			if (length()==0) {
				Amode = 0;
				EndQuoteMode();
			}
		}
	} else {
		if (QSeen) {
			prn(1)
		} else {
			if (html) {
				if (!TitleWritten) {
					printf "<title>Tela Frequently Asked Questions</title>\n" >>header
					printf "<h1>%s</h1>\n",$0 >>header
					TitleWritten = 1
				} else
					printf "<h4>%s</h4>\n",$0 >>header
			} else prn(header)
		}
	}
	next
}
END {
	if (html) {
		printf "<hr>\n<address>tela-bugs@fmi.fi</address><p>\n"
		printf "<a href=\"http://www.geo.fmi.fi/prog/tela.html\">Back to Tela home page</a>\n"
	}
}
' <$inputfile >"$tmp"

if [ "$html" = "true" ]
then echo '<hr><h1>Answers</h1>' >>"$qfile"
else echo '
ANSWERS TO QUESTIONS
' >>"$qfile"
fi

cat "$header" "$qfile" "$tmp" >$outfile
rm "$qfile" "$header" "$tmp"

done