/********************************************************************
                         FindLinks

   Description: Finds all the links on a page
   Syntax:      FindLinks

********************************************************************/

#include <windows.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <editor/doc.h>
#include <editor/macros.h>
//#include <editor/options.h>
#include <editor/dlg.h>
#include <editor/app.h>


// function prototypes
void ScriptMain(HDOC);
void AddLinksName( HDOC hDoc );


/*-------------------------------------------------------------------
                    main function
-------------------------------------------------------------------*/

int main(int argc, char *argv[])
{
   HDOC hDoc;

   hDoc = Gte_GetActiveDocument();
   if(hDoc == NULL) {
      MsgBox("Cannot get handle of the active document!", MB_OK);
      return 1;
   }
   
   // turn off line drawing and/or selecting mode
   GM_EndLineDraw(hDoc);
   GM_EndSelect(hDoc);

   {

      // call main script function
      ScriptMain(hDoc);
   }

   return 0;
}


/*-------------------------------------------------------------------
                         ScriptMain

  Main script function.

  PARAMETERS

     hDoc - Handle of the active document.
-------------------------------------------------------------------*/

void ScriptMain(HDOC hDoc)
{
  HDOC newDoc;
  long xCur, yCur;
  char *href;
  int finder;
  char lineNum[1024];


  // go to the top of the page
  GM_TextStart(hDoc);


  // create the new document to hold the Links
  newDoc = Gte_NewDocument();

  while( TRUE )
  {
   // get our current position and search for a link tag.  I use
   // a regular expression to do this.
   Gte_GetCursorPos( hDoc, &xCur, &yCur);
   finder = Gte_Find(hDoc, "<a href[^>]*>",xCur, yCur, FALSE, FALSE, TRUE, TRUE);

   // check to make sure I found a tag
   if( finder == 0 )
   {
     // we found some text, so get it into a string
     href = Gte_GetSelection(hDoc);

     // get our line position of where we found the link
     Gte_GetCursorPos( hDoc, &xCur, &yCur);

     // now print the line number, and the whole tag into another string
     sprintf(lineNum,"%5d - %sLine#%d</a><br>\n",yCur, href, yCur);

     // insert this into the new link document
     Gte_InsertText(newDoc, lineNum );

     // remember we have to free the memory that was returned to us
     // on the GetSelection call.  I personally thing that we should be
     // passing the function an array of chars, but this is fine.
     Gte_MemFree(href);
   }
   else
   {
      // if we don't find a link tag, then exit the loop.
     break;
   }
  }

  AddLinksName(newDoc);
}




void AddLinksName( HDOC hDoc )
{
  long xCur, yCur;
  int finder;

  GM_TextStart(hDoc);

  while(TRUE)
  {
    Gte_GetCursorPos(hDoc, &xCur, &yCur );
    finder = Gte_Find(hDoc,"\"[^\"]*\"",xCur, yCur, FALSE, FALSE, TRUE, TRUE);

    if( finder == 0 )
    {
      GM_Copy(hDoc);

      Gte_GetCursorPos(hDoc, &xCur, &yCur );
      Gte_Find(hDoc, "</a>", xCur, yCur, FALSE, FALSE, FALSE, TRUE );

      GM_CursorLeft(hDoc);
      GM_CursorLeft(hDoc);
      GM_CursorLeft(hDoc);
      GM_CursorLeft(hDoc);

      Gte_InsertText(hDoc," - ");
      GM_Paste(hDoc);
      GM_LineStart(hDoc);
      GM_CursorDown(hDoc);
    }
    else
    {
      break;
    }
  }
}

