e6a1712000-02-02Per Hedbor /* This is a Roxen(r) module. Copyright (c) Idonex 1997.
84af181997-11-14Per Hedbor  * Released under GPL * made by Per Hedbor */
1973552000-02-10Martin Nilsson constant cvs_version = "$Id: wizard_tag.pike,v 1.23 2000/02/10 07:13:28 nilsson Exp $";
84af181997-11-14Per Hedbor constant thread_safe=1; #include <module.h> inherit "module"; inherit "wizard";
1973552000-02-10Martin Nilsson constant module_type = MODULE_PARSER; constant module_name = "Wizard generator"; constant module_doc = "Generates wizards<p>See &lt;wizard help&gt; for more information\n";
84af181997-11-14Per Hedbor 
cf33bb2000-02-04Marcus Wellhardh string internal_verify(string t, mapping args, string contents, int l, int ol, mapping m) {
e97f1e2000-02-08Marcus Wellhardh  if(sizeof(args)) contents = make_container("if", args, contents); m->verify += ({ ({ contents, ol + l, m->id }) }); return "<__wizard_error__ id=\"id_"+(m->id++)+"\">";
cf33bb2000-02-04Marcus Wellhardh }
f1be801998-02-22Per Hedbor string internal_page(string t, mapping args, string contents, int l, int ol,
e97f1e2000-02-08Marcus Wellhardh  mapping f, RequestID id)
84af181997-11-14Per Hedbor {
cf33bb2000-02-04Marcus Wellhardh  mapping m = ([ "verify":({ }) ]); f->pages += ({ ({ parse_html_lines(contents, ([]), ([ "verify":internal_verify ]), l, m), ol + l, m->verify }) });
84af181997-11-14Per Hedbor }
6d03cf1998-07-22Johan Schön string internal_done(string t, mapping args, string contents, int l, int ol,
e97f1e2000-02-08Marcus Wellhardh  mapping f, RequestID id)
6d03cf1998-07-22Johan Schön { f->done=contents; }
f1be801998-02-22Per Hedbor string fix_relative(string file, object id) { if(file != "" && file[0] == '/') return file; file = combine_path(dirname(id->not_query) + "/", file); return file; } string old_pike = ""; object old_wizard = 0;
959a491998-07-03Marcus Comstedt string tag_wizard(string t, mapping args, string contents, object id, object file, mapping defines)
84af181997-11-14Per Hedbor {
558cba1998-08-17Peter Bortas  if(!id->misc->line) id->misc->line=-1;
84af181997-11-14Per Hedbor  mapping f = ([ "pages":({}) ]);
62fbb11998-07-19Henrik Grubbström (Grubba)  string pike = ("inherit \"wizard\";\n" +
558cba1998-08-17Peter Bortas  sprintf("# "+id->misc->line+" %O\n"
62fbb11998-07-19Henrik Grubbström (Grubba)  "string name = %O;\n",
b796b51998-11-18Per Hedbor  id->not_query, (args->name||"unnamed")));
84af181997-11-14Per Hedbor  int p;
41d0f91998-02-20Per Hedbor  foreach(glob("*-label", indices(args)), string a) {
558cba1998-08-17Peter Bortas  pike += sprintf("# "+id->misc->line+" %O\n",
62fbb11998-07-19Henrik Grubbström (Grubba)  id->not_query); pike += sprintf(" string "+replace(replace(a,"-","_"),({"(",")","+",">"}), ({"","","",""}))+ " = %O;\n", args[a]);
41d0f91998-02-20Per Hedbor  }
f1be801998-02-22Per Hedbor  if(args->ok) {
558cba1998-08-17Peter Bortas  pike += sprintf("# "+id->misc->line+" %O\n", id->not_query);
62fbb11998-07-19Henrik Grubbström (Grubba)  pike += sprintf("mixed wizard_done(object id)\n" "{\n" " id->not_query = %O;\n\""+
b796b51998-11-18Per Hedbor  " return id->conf->get_file( id );\n"
62fbb11998-07-19Henrik Grubbström (Grubba)  "}\n\n", fix_relative(args->ok, id));
f1be801998-02-22Per Hedbor  }
6d03cf1998-07-22Johan Schön  parse_html_lines(contents, ([]), ([ "page":internal_page, "done":internal_done ]),
e97f1e2000-02-08Marcus Wellhardh  (int)id->misc->line, f, id);
0487401998-08-05Henrik Grubbström (Grubba)  if (f->done && !args->ok) {
cb9dc41998-08-19Henrik Grubbström (Grubba)  pike += sprintf("mixed wizard_done(object id)\n" "{\n"
03873f1998-11-02Per Hedbor  " return parse_rxml(%O,id);\n"
cb9dc41998-08-19Henrik Grubbström (Grubba)  "}\n", f->done);
0487401998-08-05Henrik Grubbström (Grubba)  }
f1be801998-02-22Per Hedbor  foreach(f->pages, array q)
84af181997-11-14Per Hedbor  {
62fbb11998-07-19Henrik Grubbström (Grubba)  pike += sprintf("# "+q[1]+" %O\n", id->not_query); pike += sprintf("string page_"+p+"(object id) {" +
03873f1998-11-02Per Hedbor  " return parse_rxml(%O,id);\n"
62fbb11998-07-19Henrik Grubbström (Grubba)  "}\n", q[0]);
e97f1e2000-02-08Marcus Wellhardh  // Code to enable verification of wizard pages.
cf33bb2000-02-04Marcus Wellhardh  if(q[2] && sizeof(q[2])) {
e97f1e2000-02-08Marcus Wellhardh  // FIXME line numbers for verify sections. pike += "int verify_"+p+"(object id) {\n" " int c;\n" " string s = \"\";\n" " id->misc->__wizard_error__ = ([ ]);\n"; foreach(q[2], array v) pike += sprintf(" s = parse_rxml(%O, id);\n" " if(id->misc->defines[\" _ok\"]) {\n" " id->misc->__wizard_error__->id_%d = s;\n" " c++;\n" " }\n", v[0], v[2]); pike += " if(c)\n" " return 1;\n" "}\n";
cf33bb2000-02-04Marcus Wellhardh  }
84af181997-11-14Per Hedbor  p++; }
e97f1e2000-02-08Marcus Wellhardh  //werror("Wiz: %s\n", pike);
f1be801998-02-22Per Hedbor  object w; if(pike == old_pike) w = old_wizard; else { old_wizard = w = compile_string(pike)(); old_pike = pike; }
6d03cf1998-07-22Johan Schön  mixed res = w->wizard_for(id,fix_relative(args->cancel||args->done||"",id));
f1be801998-02-22Per Hedbor 
84af181997-11-14Per Hedbor  if(mappingp(res)) {
959a491998-07-03Marcus Comstedt  defines[" _error"] = res->error; defines[" _extra_heads"] = res->extra_heads;
f1be801998-02-22Per Hedbor  return res->data||(res->file&&res->file->read())||"";
84af181997-11-14Per Hedbor  } return res; }
cf33bb2000-02-04Marcus Wellhardh string tag_wizard_error(string t, mapping args, object id, object file, mapping defines) {
e97f1e2000-02-08Marcus Wellhardh  if(id->misc->__wizard_error__ && args->id && id->misc->__wizard_error__[args->id]) return id->misc->__wizard_error__[args->id];
cf33bb2000-02-04Marcus Wellhardh  return ""; }
84af181997-11-14Per Hedbor 
cf33bb2000-02-04Marcus Wellhardh mapping query_tag_callers() { return ([ "__wizard_error__" : tag_wizard_error ]); }
cd34021999-05-19David Hedbor 
84af181997-11-14Per Hedbor mapping query_container_callers() { return ([ "wizard" : tag_wizard ]); }