summaryrefslogtreecommitdiff
path: root/test/cases/colm.d/mediawiki/garticle.rl
blob: cc101364783bc03e9fec6bc26d689682d4dd458e (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
#include <iostream>
#include <fstream>
#include <stdlib.h>
#include <string.h>

using std::cout;
using std::cerr;
using std::endl;
using std::ifstream;
using std::ofstream;

%%{
	machine garticle;
	write data;
}%%


int main( int argc, char **argv )
{
	std::ios::sync_with_stdio(false);

	if ( argc != 5 ) {
		cerr << "usage: garticle <dump-file> <article-index> <section> <article>" << endl;
		return -1;
	}

	char *dumpFile = argv[1];
	char *articleIndex = argv[2];
	char *section = argv[3];
	char *article = argv[4];

	ifstream dump( dumpFile );
	if ( !dump.is_open() ) {
		cerr << "error: unable to open " << dumpFile << " for reading" << endl;
		return -1;
	}

	ifstream index( articleIndex );
	if ( !index.is_open() ) {
		cerr << "error: unable to open " << articleIndex <<  " for writing" << endl;
		return -1;
	}

	long long articleNum = atoll(article);
	index.seekg( articleNum * sizeof(long long) );

	long long start, end;
	index.read( (char*)&start, sizeof(long long) );
	index.read( (char*)&end, sizeof(long long) );

	long long len = end - start;
	char *buf = new char[len];
	dump.seekg( start-5 );
	dump.read( buf, len );

	char tn[2048];
	long ptn = 0;
	bool emit = false;

	char *p = buf, *pe = buf+len;
	int cs;

	%%{
		newline = '\n';
		sp = [ \t\n\r];

		name = [a-zA-Z:_0-9]+;

		# Tag names.
		tag_name = name
			>{ ptn = 0; }
			${ tn[ptn++] = *p; }
			%{ tn[ptn++] = 0; }
			;

		attr_name = name;

		# Attributes
		attr_val = '"' ( [^"\\] | newline | ( '\\' any ) )* '"';
		attr = attr_name '=' attr_val;
		attrs = ( sp attr )*;

		action maybe_open {
			if ( strcmp( tn, section ) == 0 )
				emit = true;
		}

		action maybe_close {
			if ( strcmp( tn, section ) == 0 )
				emit = false;
		}

		# Tags
		tag = '<' tag_name %maybe_open attrs sp? ( '>' | '/>' );
		close_tag = '</' tag_name %maybe_close '>';

		# Character data, not spaces and not tag starts.
		char_data_char = ^('<'|'&');
		char_data = char_data_char+
			${ 
				if ( emit )
					cout << *p;
			} ;

		defined_entities =
			'quot' %{if (emit) cout << '"';} |
			'amp' %{if (emit) cout << '&';} |
			'apos' %{if (emit) cout << '\'';} |
			'lt' %{if (emit) cout << '<';} |
			'gt' %{if (emit) cout << '>';};

		entity_ref = '&' defined_entities ';';

		main := ( 
				tag |
				close_tag |
				entity_ref |
				char_data
			)*
			;

		write init;
		write exec;
	}%%

	if ( cs < garticle_first_final ) {
		cerr << endl << endl << "garticle: error parsing dump file" << endl;
		return 1;
	}

	//cout.write( buf, len );
	cout << endl;

	return 0;
}