0e834c2000-02-24Martin Nilsson // This is a roxen module. Copyright © 1996 - 2000, Roxen IS. // // This module makes it possible to write symbolic names instead of // absoulte hrefs. // // made by Mattias Wingstedt
57f45e1996-11-27Per Hedbor 
29d0342000-04-15Kenneth Johansson constant cvs_version = "$Id: indirect_href.pike,v 1.18 2000/04/14 22:34:01 kuntri Exp $";
07bf511997-08-31Peter Bortas constant thread_safe=1;
b1fca01996-11-12Per Hedbor #include <module.h> inherit "module"; inherit "roxenlib"; mapping hrefs; string tagname; void create() {
0e834c2000-02-24Martin Nilsson  defvar( "hrefs", "", "Indirect hrefs", TYPE_TEXT_FIELD,
59ae152000-04-06Mattias Wingstedt  "The URL database with the syntax:<br>\n"
57f45e1996-11-27Per Hedbor  "[name] = [URL]\n" );
f27cc81998-11-04Peter Bortas  //This pollutes namespace and makes the life hard on the manual writers. //Thus it's turned of for normal users.
59ae152000-04-06Mattias Wingstedt  // defvar( "tagname", "ai", "Tagname", TYPE_STRING|VAR_EXPERT, // "Name of the tag\n" // "&lt;tag name=[name]&gt;foo&lt;/tag&gt; will be replaced with\n" // "&lt;a href=[URL]&gt;foo&lt;/a&gt;" // "if the name is changed, the module has to be reloaded for the " // "namechange to take effect)" );
b1fca01996-11-12Per Hedbor }
1973552000-02-10Martin Nilsson constant module_type = MODULE_PARSER; constant module_name = "Indirect href"; constant module_doc =
69ab4c2000-04-06Mattias Wingstedt #"Indirect href. Adds a new tag <tt>&lt;ai name=&gt;</tt> that works like <tt>&lt;a href=&gt;</tt> but uses a symbolic name instead of a URL. The symbolic name is translated to a proper URL and the tag rewritten to a proper &lt;a href=&gt; tag. The translation between symbolic names and URLs is stored in a module variable. The advantage of this module is that each URL will only be stored in one place and it becomes very easy to change it, no matter how many links use it. As an extra bonus the name <tt>random</tt> will be replaces by a random URL from the list.";
b1fca01996-11-12Per Hedbor 
be754f1999-12-09Martin Nilsson 
b1fca01996-11-12Per Hedbor void start() {
be754f1999-12-09Martin Nilsson  array (string) lines;
57f45e1996-11-27Per Hedbor  string variable, value;
b1fca01996-11-12Per Hedbor  mapping all = ([ ]); hrefs = ([ ]);
be754f1999-12-09Martin Nilsson  if (lines = (query( "hrefs" )-" "-"\t") /"\n") foreach (lines, string line)
b1fca01996-11-12Per Hedbor  if (sscanf( line, "%s=%s", variable, value ) >= 2)
be754f1999-12-09Martin Nilsson  hrefs[ variable ] = value;
59ae152000-04-06Mattias Wingstedt  tagname = "ai";//query( "tagname" );
b1fca01996-11-12Per Hedbor }
604e292000-03-17Martin Nilsson string newa(string tag, mapping m, string q)
b1fca01996-11-12Per Hedbor {
be754f1999-12-09Martin Nilsson  if(!m->name && !m->random) return q; if(m->name) { m->href=hrefs[m->name]; m_delete(m, "name"); } if(m->random) { m->href=values(hrefs)[random(sizeof(hrefs))]; m_delete(m, "random"); } return make_container("a",m,q);
b1fca01996-11-12Per Hedbor }
d024df2000-03-17Martin Nilsson mapping query_simpletag_callers()
b1fca01996-11-12Per Hedbor {
604e292000-03-17Martin Nilsson  return ([ tagname : ({ 0, newa }) ]);
b1fca01996-11-12Per Hedbor }
3870342000-04-15Kenneth Johansson TAGDOCUMENTATION; #ifdef manual constant tagdoc=([
29d0342000-04-15Kenneth Johansson  "ai":#"<desc cont><short>
3870342000-04-15Kenneth Johansson  Makes it possible to use a database of links.</short> Each link is referred to by a symbolic name instead of the URL. <p>The database is updated through the configuration interface. The tag is available through the <module>Indirect href</module> module.</p></desc> <attr name='name' value='string' required> Which link to fetch from the database. There is a special case, <att>name='random'</att> that will choose a random link from the database. <ex> <ai name='roxen'>Roxen WebServer</ai> </ex> </attr>", ]); #endif