/****
*
*  HTML-Heaven for support Ed and Aweb-II
*  $VER: HTML.rexx 2.2 by Chris Eburn <ceburn@midcoast.com.au>
*
****/
call addlib('rexxreqtools.library',0,-30)
 call addlib('rexxtricks.library',0,-30)
  options RESULTS

true = 1 ; false = 0
SplitCmd = false ; AutoView = false ; Lower = true
rtButtons1 = 'To Ed ' ; rtButtons2 = '  OK  '
html. = ''

AWEB = 'AWeb3:AWeb-II'
EDport = getenv('EDport')              /* this env is set by call thru EDs menus */
                             
parse ARG Auto html.cmd

if Auto = 'SAVE' then do               /* if AUTO, start Browser and set Buttons */
   if ~get_aweb_address() then do            /* start Browser if required */
      address value EDPORT
         'sa ?!Save As!'
         'rv !EdInfo!'
      address COMMAND
         'run <>nil: 'AWeb' file://localhost/'EdInfo.filename
         do for 3
            'waitforport AWEB.1'          /* wait for slow starting browser */
         end
   end

   if get_aweb_address() then do          /* Browser existed or now opened OK */
      AutoView   = true
      rtButtons2 = 'Refresh'
   end
end

/***  HTML.tags; get LOWER tags and strip spaces/quote; cut up twopart tags  ***/

if Lower then html.cmd = translate(html.cmd,'abcdefghijklmnopqrstuvwxyz','ABCDEFGHIJKLMNOPQRSTUVWXYZ')
html.cmd = strip(html.cmd,,' "')

cut = lastpos('<',html.cmd)                          /*  check for two part tags */
if (cut > 1) then do                     /* if '<' Not  firstChr ==  twopart tag */
   parse VAR html.cmd html.left =cut html.right
   rtButtons1 = ' Left| All |Right'
   splitcmd   = true
end
else html.left = html.cmd                      /* single part tag      */

           /***  use requester to edit HTML and select part to paste ***/

DO until rtRESULT = 0                           /* cycle till cancelled */
   html.edit = rtgetstring(html.left html.right,html.cmd,' HTML.rexx  v2.2',rtButtons1'|'rtButtons2)

   if rtRESULT = 0 then iterate                /* leave do, close ezreq */
   if SplitCmd then do
      if rtRESULT = 1 then html.ed = html.left           /* send left   */
      if rtRESULT = 2 then html.ed = html.edit           /* as edited   */
      if rtRESULT = 3 then html.ed = html.right          /* or right    */
   end                                                   /* to ED       */
   else html.ed = html.edit    /* not a split command, send as edited   */

   address value EDPORT
     'e**'html.ed'*'
END

              /***  refresh or reload Browser as required  ***/

if AutoView then do                   /* AUTO and AWEB.port both true */
   address value EDPORT                        /* save and get filename */
     'sa'
     'rv !EdInfo!'

   address value ACTIVE_WINDOW
     'screentofront'
     'get url'                             /* get and compare filenames */
      parse VAR Result . 'st/' LocalURL

   if EdInfo.filename = LocalURL then 'reload'        /* NOT from cache */
   else 'open file://localhost/'EdInfo.filename' reload'
end

exit

GET_AWEB_ADDRESS:
   All_ports = show('P')
   parse VAR All_ports . 'AWEB.' portnumber .
      if portnumber = '' then return 0

   address value 'AWEB.'portnumber
      'get activeport VAR' Active_Window
      'get screen     VAR' Aweb_Screen
return 1
