f41b982009-05-07Martin Stjernholm // This is a roxen module. Copyright © 1996 - 2009, Roxen IS.
0e834c2000-02-24Martin Nilsson // // This module makes it possible to write symbolic names instead of // absoulte hrefs. // // made by Mattias Wingstedt
57f45e1996-11-27Per Hedbor 
eaca172000-04-19Martin Nilsson #pragma strict_types
0917d32013-03-04Anders Johansson constant cvs_version = "$Id$";
de905c2000-04-16Martin Nilsson constant thread_safe = 1;
b1fca01996-11-12Per Hedbor #include <module.h> inherit "module"; void create() {
de905c2000-04-16Martin Nilsson  defvar( "hrefs", "roxen = http://www.roxen.com\n" "community = http://community.roxen.com", "Indirect hrefs", TYPE_TEXT_FIELD, "The URL database with the syntax:<br>\n"
57f45e1996-11-27Per Hedbor  "[name] = [URL]\n" );
de905c2000-04-16Martin Nilsson  /* defvar( "tagname", "ai", "Tag name", TYPE_STRING, "The name of the tag." ); */
b1fca01996-11-12Per Hedbor }
b3281f2000-09-10Martin Nilsson constant module_type = MODULE_TAG;
bc0fa02001-03-08Per Hedbor constant module_name = "Tags: Indirect href";
de905c2000-04-16Martin Nilsson //constant module_unique = 0;
1973552000-02-10Martin Nilsson constant module_doc =
eaca172000-04-19Martin Nilsson #"Indirect href. Adds a new tag <tt>&lt;ai&nbsp;name=\"\"&gt;&lt;/ai&gt;</tt> that works like <tt>&lt;a&nbsp;href=\"\"&gt;&lt;/a&gt;</tt> but uses a symbolic name instead of a URL. The
69ab4c2000-04-06Mattias Wingstedt symbolic name is translated to a proper URL and the tag rewritten to a
eaca172000-04-19Martin Nilsson proper <tt>&lt;a&nbsp;href=\"\"&gt;&lt;/a&gt;</tt> tag. The translation between symbolic names and
69ab4c2000-04-06Mattias Wingstedt 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 
eaca172000-04-19Martin Nilsson mapping(string:string) hrefs;
be754f1999-12-09Martin Nilsson 
b1fca01996-11-12Per Hedbor void start() {
de905c2000-04-16Martin Nilsson  array(string) lines;
57f45e1996-11-27Per Hedbor  string variable, value;
b1fca01996-11-12Per Hedbor  hrefs = ([ ]);
eaca172000-04-19Martin Nilsson  if (lines = ([string]query( "hrefs" )-" "-"\t") /"\n")
be754f1999-12-09Martin Nilsson  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 }
de905c2000-04-16Martin Nilsson class TagAI { inherit RXML.Tag; string name;
1831062002-01-17Martin Nilsson  mapping(string:object(RXML.Type)) req_arg_types = (["name":RXML.t_text(RXML.PEnt)]);
de905c2000-04-16Martin Nilsson  void create() { if(variables->tagname)
eaca172000-04-19Martin Nilsson  name = [string]query("tagname");
de905c2000-04-16Martin Nilsson  else name = "ai";
be754f1999-12-09Martin Nilsson  }
b1fca01996-11-12Per Hedbor 
de905c2000-04-16Martin Nilsson  class Frame { inherit RXML.Frame; array do_return() { if(!args->name || !sizeof(hrefs)) { result = content; return 0; } if(!(hrefs->random) && args->name=="random") args->href=values(hrefs)[random(sizeof(hrefs))]; else args->href=hrefs[args->name];
60bf7c2001-08-17Martin Nilsson  m_delete([mapping(string:string)]args, "name");
de905c2000-04-16Martin Nilsson 
60bf7c2001-08-17Martin Nilsson  result = Roxen.make_container("a", [mapping(string:string)]args, [string]content);
de905c2000-04-16Martin Nilsson  return 0; } }
b1fca01996-11-12Per Hedbor }
c589a22000-04-15Per Hedbor TAGDOCUMENTATION; #ifdef manual constant tagdoc=([
ce8fb02001-09-21Johan Sundström  "ai":#"<desc type='cont'><p><short>
c589a22000-04-15Per Hedbor  Makes it possible to use a database of links.</short> Each link is referred
9b03652001-03-07Kenneth Johansson  to by a symbolic name instead of the URL.</p>
c589a22000-04-15Per Hedbor  <p>The database is updated through the configuration interface. The
945a522001-03-12Kenneth Johansson  tag is available through the <i>Indirect href</i>
c589a22000-04-15Per Hedbor  module.</p></desc>
9b03652001-03-07Kenneth Johansson  <attr name='name' value='string' required='required'><p>
c589a22000-04-15Per Hedbor  Which link to fetch from the database. There is a special case, <att>name='random'</att> that will choose a random link from the
9b03652001-03-07Kenneth Johansson  database.</p>
bb77cb2001-08-23Martin Nilsson  <ex><ai name='roxen'>Roxen Internet Software</ai></ex>
c589a22000-04-15Per Hedbor  </attr>", ]); #endif