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 
0e834c2000-02-24Martin Nilsson constant cvs_version = "$Id: indirect_href.pike,v 1.12 2000/02/24 05:20:10 nilsson 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,
f27cc81998-11-04Peter Bortas  "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.
0e834c2000-02-24Martin Nilsson  defvar( "tagname", "ai", "Tagname", TYPE_STRING|VAR_EXPERT,
f27cc81998-11-04Peter Bortas  "Name of the tag\n"
57f45e1996-11-27Per Hedbor  "&lt;tag name=[name]&gt;foo&lt;/tag&gt; will be replaced with\n"
f27cc81998-11-04Peter Bortas  "&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 = "Indirect href. Adds a new container <tt>&lt;ai&gt;</tt>" ", with a single argument, " "name=string. It then uses the name to index a database of " "URLs, and inserts a &lt;a href=...&gt; tag instead. This can " "be very useful, since you can move all links to a document at " "once. It also allows the special case 'name=random'. If this " "is used, a random link will be selected from the database. " "Example:<pre>"
0e834c2000-02-24Martin Nilsson  " roxen=http://www.roxen.com/</pre>";
b1fca01996-11-12Per Hedbor 
be754f1999-12-09Martin Nilsson // Dynamic tagname, hence dynamic documentation. mapping tagdocumentation() { return ([tagname:"<desc cont>ai</desc>"]); }
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;
b1fca01996-11-12Per Hedbor  tagname = query( "tagname" ); }
be754f1999-12-09Martin Nilsson string tag_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 } mapping query_container_callers() { return ([ tagname : tag_newa ]); }