/* Given a file name of an html containing file, identify inputs and */
/* generate get-value and def var statements for them.               */

define input parameter InputFileName as character no-undo.
define input parameter OutputFileName as character no-undo.

define temp-table InputNames
  field InputName as character.
  
/* -------------------------------- Main --------------------------------- */
                                                                             
run ProcessFile (InputFileName).

run GenerateCode (OutputFileName).

/* ----------------------------------------------------------------------- */

procedure ProcessFile:
                       
  define input parameter InputFileName as character no-undo. 

  define variable LineOfText as character no-undo.

  input from value (InputFileName).
  
  repeat on end-key undo, leave:
  
    import unformatted LineOfText.
  
    run ProcessForInput (LineOfText).
    
  end. /* repeat */
  
  input close.

end. /* ProcessFile */


/* ----------------------------------------------------------------------- */

function find_tag returns character
(input ctext as character,
 input ctag_begins as character,
 input ctag_ends as character):

  define variable start_offset as integer no-undo.
  define variable end_offset as integer no-undo.
  define variable working_text as character no-undo.

  assign start_offset = index (ctext, ctag_begins).

  if start_offset LT 1 then return "".

  assign working_text = substring (ctext, start_offset + length(ctag_begins)).

  assign end_offset = index (working_text, ctag_ends) - length(ctag_ends) /* + 1 */.

  if end_offset LT 1 then end_offset = length(working_text).

  return substring (working_text, 1, end_offset).

end. /* find_tag */ 

/* ----------------------------------------------------------------------- */

/* We expect data as ...nameB=B["]value["]... for data. */

function ParseForName returns character (input InputAttributes as character):
  
  define variable WorkingOffset as integer no-undo.
  define variable EncounteredQuote as logical no-undo.
  define variable LengthInputAttributes as integer no-undo.
  define variable TheName as character no-undo.
  define variable CharacterUnderReview as character no-undo.
  
  message "Parsing For Name in " InputAttributes.
  
  /* If we cannot find name then we can't use this data */
  
  WorkingOffset = index (InputAttributes, "name").
  if WorkingOffset < 1 then return ?.
    
  /* Find the = for the attribute. If we cannot find the = */
  /* then we can't use this data                           */
  
  WorkingOffset = WorkingOffset + length("name").
  LengthInputAttributes = length(InputAttributes).
  
  do while substring(InputAttributes, WorkingOffset, 1) <> "=":
    
    WorkingOffset = WorkingOffset + 1.
    if WorkingOffset > LengthInputAttributes then return ?.
      
  end. /* do */
    
  /* Walk past spaces looking for name of input taking */
  /* quotes into account.                              */
  
  TheName = "".
  WorkingOffset = WorkingOffset + 1.
  
  repeat:
  
    CharacterUnderReview = substring (InputAttributes, WorkingOffset, 1).
    
    if CharacterUnderReview = " " and TheName = "" then next.
    if CharacterUnderReview = "~"" and TheName <> "" then leave.
    if CharacterUnderReview = " " and TheName <> "" then leave.
      
    TheName = TheName + CharacterUnderReview.
    
    WorkingOffset = WorkingOffset + 1.
    if WorkingOffset > LengthInputAttributes then return TheName.
      
  end. /* repeat */
    
  TheName = replace (TheName, "~"", "").
  return TheName.
    
end. /* ParseForName */

/* ----------------------------------------------------------------------- */

procedure ProcessForInput:
  
  define input parameter WorkingText as character no-undo.

  define variable InputName as character no-undo.
  define variable InputText as character no-undo.
  define variable ReplaceText as character no-undo.
  
  message "Running ProcessForInput".
  
  repeat:
  
      /* If we cannot find an input text, then we are done */
      
      InputText = find_tag (WorkingText, "<input", ">").
      if InputText = "" then leave.
        
      /* We eliminate processed inputs from the WorkingText */
      
      ReplaceText = "<input" + InputText + ">".
      WorkingText = replace (WorkingText, ReplaceText, "").
      
      /* Try to obtain the name of the input - this is complicated */
      /* to we squirrel that logic into ParseForName.              */
      
      InputName = ParseForName (InputText).
      if InputName <> ? then do:
        
        create InputNames.
        InputNames.InputName = InputName.
        
      end. /* if */
      
  end. /* repeat */

end. /* ProcessForInput */

/* ----------------------------------------------------------------------- */

procedure GenerateCode:
  
  define input parameter FileName as character no-undo.
  
  output to value (FileName).
  
  run GenerateVariables.
  
  put unformatted skip (2).
  
  run GenerateGetValue.
  
  output close.
  
end. /* GenerateCode */

procedure GenerateVariables:
  
  define variable LastName as character no-undo.

  for each InputNames no-lock by InputName:
    
    if InputNames.InputName <> LastName then 
      put unformatted "define variable " InputNames.InputName " as character no-undo." skip.
  
    LastName = InputNames.InputName.
    
  end. /* for each */
  
end. /* GenerateVariables */

procedure GenerateGetValue:
  
  define variable LastName as character no-undo.

  for each InputNames no-lock by InputName:
    
    if InputNames.InputName <> LastName then    
      put unformatted InputNames.InputName ' = get-value("' InputNames.InputName '").' skip.
  
    LastName = InputNames.InputName.
        
  end. /* for each */
  
end. /* GenerateGetValue */
