summaryrefslogtreecommitdiff
path: root/parse.awk
blob: c68ad18ef03471d8a78802e9432115bdc9acf699 (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
BEGIN{
	lnum=0;
	cnum=0;
	type="";
	last_field="";
	FS=":";
	# category is an array of category data
	delete category;
	# link is an array of link data
	delete link;

	### configurable variables
	BASEDIR="./"; # the folder where the hierarchy will be created 
	ROOTSEL="/lawn/index.gph"; # the selector of the root page (back)
	HOST="localhost"; # the default host
	PORT="1500"; # and the default port
}

function is_empty(a){
	for (i in a)
		return 0;
	return 1;
}

function add_link(c){
	lnum+=1;
	for (k in c){
		link[lnum,k]=c[k]; 
	}
}

function add_category(c){
	cnum+=1;
	for (k in c){
		category[cnum,k]=c[k]; 
	}
}


function get_cur(cur){
	if (cur["Type"] == "") {
		print "empty type -- skipping record";
		return;	
	} else if (cur["Type"] == "link"){
		add_link(cur);
		return;
	} else if (cur["Type"] == "category"){
		add_category(cur);
		return;
	} else {
		printf("invalid type: %s -- skipping\n", cur["Type"]) ;
		return;
	}
}


/^[A-Z][-a-zA-Z]+:/{## New field
	
	gsub(/^ +/,"",$2);
	gsub(/ *$/,"",$2);
	cur[$1]=$2;
	last_field=$1
}

/^[\ \f\n\r\t\v]*$/{ ## End of stanza
	if (!is_empty(cur)){
		get_cur(cur);
		delete cur;
		last_field="";
	}
}

/^[\ \f\n\r\t\v]+[^\ \f\n\r\t\v]+/{## Multi-line value
	if (!is_empty(cur) && last_field!=""){
		gsub(/^[\ \f\n\r\t\v]/, "", $0); # remove only the first space
		cur[last_field]=cur[last_field] "\n" $0;
	}
}

END{
	render_init();
	#print "before render_categories" > /dev/stderr
	render_categories();
#	print "before render_post_categories" > /dev/stderr
	render_post_categories();
#	print "before render_links" > /dev/stderr
	render_links();
#	print "before render_finalise" > /dev/stderr
	render_finalise();
#	print "before dump_links" > /dev/stderr
	dump_links();
}