/* Time-stamp: <97/01/24 15:31:01 john> */
/* anchor.c --
# Purpose: output text with HTML anchors
*/

#include <stdio.h>
#include "attr.h"
#include "style.h"
#include "grid.h"
#include "misc.h"
#include "anchor.h"

/* This file contains most of the things to do with HTML anchors. */

static void add_link_field(char *field_name, char *field_text)
{
  if (field_text == NULL) return;
#if 0
  /* this will almost certainly require some escaping */
  printf("&%s=%s", field_name, field_text);
#endif
}

/* Output a link to use a given script to access a given feature in a
   given style. This is used to output links to side-roads and so on
   in the current style, and (with prods at the current style record)
   to output the menu of different styles at the bottom of the page.
 */

void link_in_style(char *script,
		   char *type,
		   char *name,
		   struct style *style,
		   char *visible,
		   char *label,
		   char *extra_key,
		   char *extra_val)
{
  printf("<a href=\"%s?type=%s&name=%s", script, type, name);
  if (style->output_mode != NULL)
    {
      printf("&output=%s", style->output_mode);
    }
  if (style->graphics)
    {
      printf("&graphics=on");
    } else {
      if (style->suppress_auto_graphics)
	{
	printf("&graphics=off");
      }
    }
  if (style->edit) printf("&edit=on");
  if (style->detail) printf("&detail=on");
  if (style->pictures)
    {
      printf("&pictures=on");
    } else {
      if (style->suppress_auto_graphics)
	{
	printf("&pictures=off");
      }
    }
  if (style->verbose) printf("&verbose=on");
  if (style->debug) printf("&debug=on");
  if (!style->helpful) printf("&helpful=off");
  if (!style->unvetted) printf("&unvetted=off");
  if (style->hide_unvetted) printf("&hide_unvetted=on");
  if (style->hide_edit) printf("&hide_edit=on");
  if (style->all_edit_links) printf("&all_edit_links=on");
  if (style->parts != (HEAD_PART | BODY_PART))
    {
      if (style->parts == HEAD_PART)
	{
	  printf("&parts=head");
	} else if (style->parts == BODY_PART)
	  {
	    printf("&parts=body");
	  }
    }
  if (style->forced_date)
    {
      printf("&month=%s&day=%s&hour=%d",
	     months[style->month-1],
	     weekdays[style->day-1],
	     style->hour);
    }
  add_link_field("extra", style->extra);
  add_link_field("special", style->special);
  add_link_field("special_page_start", style->special_page_start);
  add_link_field("special_page_end", style->special_page_end);
  if ((extra_key != NULL) && (extra_val != NULL)) printf("&%s=%s", extra_key, extra_val);
  if ((label!=NULL) && (style->browser_features&BROWSER_JUMP_IN_TABLES))
    {
      printf("#%s", as_label(label));
    }
  printf("\">%s</a>", visible);
}

/* Return the string given but in a form suitable for use as a label
   as we use labels -- that is, lower case, with all non-letters
   replaced by dashes.
 */

static char label_buf[128];

char *as_label(char *as_name)
{
  int i;
  char c;
  for (i = 0;
       (c = as_name[i]) != '\0';
       i++)
    {
      if (i > 100) break;
      if (('a' <= c) && (c <= 'z'))
	{
	  label_buf[i] = c;
	} else if (('A' <= c) && (c <= 'Z'))
	  {
	    label_buf[i] = c + ('a' - 'A');
	  } else {
	    label_buf[i] = '-';
	  }
    }
  label_buf[i] = '\0';
  return label_buf;
}

/* Label a cell. */

void output_label(struct cell *cell)
{
  char *label = cell->entry->name;
  char *linkname = NULL;

  if ((cell != NULL) &&
      (cell->entry != NULL) &&
      (cell->x_into == 0) &&
      (cell->y_into == 0))
    {
      linkname = get_attr(cell->entry, "link");
      if (linkname != NULL) label = linkname;
      {
	printf("<a name=\"%s\"></a>", as_label(label));
      }
    }
}

/* Output a piece of text; if it appears to link to another feature on
   the streetplan, make it a link.
 */

void output_with_link(char *text,
		      struct style *style,
		      struct entry *entry_to_link,
		      char *label)
{
  char *link_name = NULL;
  char *homepage_name = NULL;

  link_name = get_attr(entry_to_link, "link");  
  homepage_name = get_attr(entry_to_link, "home-page");

  if (link_name != NULL)
    {
      link_in_style("getplan.cgi",
		    entry_to_link->type,
		    link_name,
		    style,
		    text, label,
		    NULL, NULL);
    } else {
      if (homepage_name != NULL)
	{
	  printf("<a href=\"%s\">%s</a>", homepage_name, text);
	} else {
	  printf("%s", text);
	}
    }
}

/* end of anchor.c */
