37daba1998-11-02Peter Bortas /* This is a Roxen module. Copyright © 1996, 1997, 1998, Idonex AB. * * This module makes it possible to write symbolic names instead of * absoulte hrefs. * * made by Mattias Wingstedt <peter@idonex.se> -96 */
57f45e1996-11-27Per Hedbor 
37daba1998-11-02Peter Bortas constant cvs_version = "$Id: indirect_href.pike,v 1.8 1998/11/02 06:28:47 peter 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() { defvar( "hrefs", "", "Indirect hrefs", TYPE_TEXT_FIELD,
37daba1998-11-02Peter Bortas  "Syntax:<br>\n"
57f45e1996-11-27Per Hedbor  "[name] = [URL]\n" );
37daba1998-11-02Peter Bortas  //This pollutes namespace and makes the life hard on the manual writers. //Thus it's turned of for normal users. defvar( "tagname", "ai", "Tagname", TYPE_STRING|VAR_EXPERT, "Name of the tag\n"
57f45e1996-11-27Per Hedbor  "&lt;tag name=[name]&gt;foo&lt;/tag&gt; will be replaced with\n"
37daba1998-11-02Peter 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 }
57f45e1996-11-27Per Hedbor array (mixed) register_module()
b1fca01996-11-12Per Hedbor {
07bf511997-08-31Peter Bortas  return ({ MODULE_PARSER, "Indirect href",
37daba1998-11-02Peter Bortas  "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>" " roxen=http://www.roxen.com/\n" " idonex=http://www.idonex.se/</pre>", });
b1fca01996-11-12Per Hedbor } void start() {
57f45e1996-11-27Per Hedbor  array (string) lines, foo; string line; string variable, value;
b1fca01996-11-12Per Hedbor  string dir = ""; mapping all = ([ ]); hrefs = ([ ]); if (lines = query( "hrefs" ) /"\n") foreach (lines, line) if (sscanf( line, "%s=%s", variable, value ) >= 2) hrefs[ variable - " " - "\t" ] = value - " " - "\t"; tagname = query( "tagname" ); } string tag_newa( string tag, mapping m, string q, mapping got ) { if (m[ "name" ] && hrefs[ m[ "name" ] ]) return "<a href=" + hrefs[ m[ "name" ] ] + ">" + q + "</a>"; else if (m[ "random" ]) return "<a href=" + values( hrefs )[ random( sizeof( hrefs ) ) ] + ">" + q + "</a>"; else return q; } mapping query_container_callers() { return ([ tagname : tag_newa ]); }