Ik kan geen python, en mijn perl is een beetje roestig, maar dit zou, in pseudocode, moeten doen wat jij wilt.
enum {
ST_BEGIN,
ST_TITLE,
ST_CAT,
ST_YIELD,
ST_INGR,
ST_PRC,
ST_END
} state = ST_BEGIN;
String title;
Array<String> categories;
int yield;
Vector<String> ingredients;
Vector<String> process;
template<class T>
bool expect(T a, T b) {
return a == b;
}
bool expect(String a, String b) {
return a.matchesRegex(b);
}
void error(int lnum, String line) {
fail(format("Parsing error at line %d: %s" % lnum % line));
int main(Array<String> args) {
expect(args, 2) or fail(format("Usage: %s <file>") % args[0]));
File f = args[1];
f.exists() or fail(format("No such file or directory: %s" % f));
f.readable() or fail(format("Cannot read from file: %s" % f));
LineParser in = f;
int lnum = 0;
while(String line = in.readline(), ++i) {
switch(state) {
case ST_BEGIN:
expect(line, "^MMMMM----- Recipe via Meal-Master") or error(lnum,line);
readline();
state = ST_TITLE;
break;
case ST_TITLE:
line = line.trim();
expect(line, "^Title: .*$") or error(lnum,line);
title = line.getRegexSubstring("^Title: (.*)$",1);
state = ST_CAT;
break;
case ST_CAT:
line = line.trim();
expect(line, "^Categories: .*$" or error(lnum,line);
String cat = line.getRegexSubstring("^Categories: (.*)$",1);
categories = cat.split({", "});
state = ST_YIELD;
break;
case ST_YIELD:
// etc... Ik hoop dat je het punt nu snapt
}
}