home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 13 / AACD13.ISO / AACD / Resources / System / BoingBag1 / Internet / AWeb / Plugins / HTTX / HTTXPlugin.awebrx < prev    next >
Text File  |  1999-10-02  |  7KB  |  337 lines

  1. /*
  2.     $VER: HTTXPlugin 3.0 (15.8.99) by Gabriele Favrin
  3.     Convert current document to text using HTTX
  4. */
  5.  
  6. options results
  7. options failat 999
  8.  
  9. if ~Show('L', 'rexxsupport.library') then do
  10.     call addlib('rexxsupport.library', 0, -30, 0)      
  11. end
  12.  
  13. parse ARG mode mode_data .
  14. parse SOURCE . . . resolved .
  15.  
  16. plugin_path=FindPath(resolved)
  17.  
  18. prefs_info=0
  19. prefs_preview=0
  20. prefs_report=1
  21. prefs_savedir="ram:"
  22. prefs_donotaskdir=0
  23. prefs_save_noheader=0
  24. prefs_print_noheader=0
  25. prefs_temppath="T:"
  26.  
  27. req_title = '"Save as text"'
  28. temp_out     = prefs_temppath"temp_HTTXP_Output.txt"
  29. temp_name = prefs_temppath"temp_HTTXP_file"
  30. temp_inc  = prefs_temppath"temp_HTTXP_header"
  31. del_list     = '"'prefs_temppath'temp_HTTXP_#?"'
  32. eol         = '0A'x
  33. trg         = ""
  34. force         = ""
  35. append     = ""
  36. string     = ""
  37. option     = ""
  38. converted = 0
  39.  
  40. Call ReadCfg()
  41.  
  42. if (mode == "TARGET") then do
  43.     trg="TARGET" mode_data
  44. end
  45.  
  46. 'Get URL VAR' doc_url trg
  47. if rc ~= 0 then do
  48.     'Request 'req_title' "No document displayed, save aborted." "_Ok"'
  49.     exit 0
  50. end
  51.  
  52. 'Get TITLE VAR' doc_title trg
  53. 'Get FRAMES ALL' trg 'STEM' frms
  54.  
  55. if (frms.0 = 0) then do
  56.     if getName() == 1 then do
  57.         Call SaveHTML()
  58.         Call Convert()
  59.         Call CleanUp(1)
  60.     end
  61. end
  62. else do
  63.     prefs_report=0
  64.  
  65.     'Request 'req_title' "Selected document contains frames." "Convert _All|_Select frame(s)|_Cancel"'
  66.     if rc == 5 then exit 0
  67.     option=result
  68.  
  69.     if option == 1 then do
  70.         if ~getName() then do
  71.             exit 0
  72.         end
  73.     end
  74.  
  75.     do ct=0 for (frms.0)+1
  76.         if ct > 0 then do
  77.             doc_url=frms.ct.url
  78.             doc_title=frms.ct.name
  79.             trg="TARGET" frms.ct.id
  80.         end
  81.  
  82.         if option == 1 then do
  83.             'Get FRAMESET' trg
  84.             if (result == 0) then do
  85.                 if converted then do
  86.                     drop append
  87.                     string=eol||"----"||eol
  88.                 end
  89.  
  90.                 Call SaveHTML()
  91.                 Call Convert()
  92.                 Call CleanUp(0)
  93.             end
  94.         end
  95.         else do
  96.             'Open "'doc_url'"'
  97.             'Wait "'doc_url'"'
  98.  
  99.             'Get FRAMESET'
  100.             if (result == 0) then do
  101.                 'Request 'req_title' "Convert this document?" "_Yes|_No|_Cancel"'
  102.                 if (rc == 5) then do
  103.                     Call CleanUp(1)
  104.                     if (ct > 0) then do
  105.                         'GO' ct 'BACK'
  106.                     end
  107.                     exit 0
  108.                 end
  109.  
  110.                 if result == 1 then do
  111.                     trg=""
  112.                     string=""
  113.                     if getName() == 1 then do
  114.                         Call SaveHTML()
  115.                         Call Convert()
  116.                         Call CleanUp(0)
  117.                     end
  118.                 end
  119.             end
  120.         end
  121.     end
  122.  
  123.     if option == 1 then do
  124.         if (prefs_preview == 1) then do
  125.             if (Preview() == 1) then do
  126.                 Call DisplayOutput()
  127.             end
  128.         end
  129.         else do
  130.             Call DisplayOutput()
  131.         end
  132.         Call CleanUp(1)
  133.     end
  134.     else do
  135.         if (option == 2) & (ct > 0) then do
  136.             'GO' ct-1 'BACK'
  137.         end
  138.         Call CleanUp(1)
  139.     end
  140. end
  141.  
  142. exit 0
  143.  
  144.  
  145. CleanUp:
  146.     parse ARG mode_all
  147.  
  148.     address command 'C:Delete >NIL: QUIET' del_list
  149.  
  150.     if mode_all == 1 then do
  151.         local_savedir=FindPath(save_file)
  152.  
  153.         if prefs_savedir ~= local_savedir then do
  154.             prefs_savedir=local_savedir
  155.             address REXX setclip('HTTXPlugin_savedir', prefs_savedir)
  156.         end
  157.     end
  158.     return
  159.  
  160.  
  161. SaveHTML:
  162.     'SaveAs "'temp_name'" NOICON' trg
  163.     if (rc ~= 0) then do
  164.         'Request 'req_title' "Error while saving HTML on temporary path*N *N'prefs_temppath'" "_Ok" NOWAIT'
  165.         exit 0
  166.     end
  167.     return
  168.  
  169.  
  170. GetName:
  171.     html_name=Translate(doc_url,,'`~"=#?/:;\*%()[]<>'||"'")
  172.     html_name=Word(html_name, Words(html_name))
  173.  
  174.     ext=lastpos('.', html_name)
  175.     if ext > 0 then
  176.         html_name=left(html_name, ext)'txt'
  177.     else
  178.         html_name=html_name'.txt'
  179.  
  180.     if prefs_donotaskdir = 0 then do
  181.         'RequestFile 'req_title' FILE="'prefs_savedir||html_name'" SAVEMODE'
  182.         if (rc == 5) then return 0
  183.         save_file=result
  184.  
  185.         if (Right(save_file, 1) == ":") | (Right(save_file, 1) == "/") then
  186.             save_file=save_file||html_name
  187.     end
  188.     else
  189.         save_file=prefs_savedir||html_name
  190.  
  191.     html_name=prefs_temppath||html_name
  192.     return 1
  193.  
  194.  
  195. Convert:
  196.     'Get MIME VAR' mtype trg
  197.     if (Upper(mtype) == "TEXT/HTML") then do
  198.         drop force
  199.     end
  200.     else do
  201.         force=""
  202.     end
  203.  
  204.     Call PrepareFile()
  205.  
  206.     res=httx()
  207.     if (res == 212) then do
  208.         'Request 'req_title' "File is not HTML, save anyway?" "_Save|_Cancel"'
  209.  
  210.         if (rc ~= 5) then do
  211.             address command 'C:Copy "'temp_name'" "'save_file'"'
  212.             prefs_report=0
  213.             res=0
  214.             Call DisplayOutput()
  215.         end
  216.     end
  217.     else do
  218.         if option ~= 1 then do
  219.             if (res ~= 0) then do
  220.                 del_list=del_list '"'save_file'"'
  221.                 if option ~= 2 then do
  222.                     Call DisplayOutput()
  223.                 end
  224.             end
  225.             else do
  226.                 if prefs_preview == 1 then do
  227.                     if Preview() == 1 then do
  228.                         if option ~= 2 then do
  229.                             Call DisplayOutput()
  230.                         end
  231.                     end
  232.                 end
  233.                 else do
  234.                     if option ~= 2 then do
  235.                         Call DisplayOutput()
  236.                     end
  237.                 end
  238.             end
  239.         end
  240.     end
  241.     converted=1
  242.     return
  243.  
  244.  
  245. httx:
  246.     address command '"'plugin_path'HTTX" >"'temp_out'" "'temp_name'" "'save_file'" INCLUDE="'temp_inc'" NOHEADER GETNOTE' force append
  247.     return rc
  248.  
  249.  
  250. DisplayOutput:
  251.     if (prefs_report == 1) then do
  252.         'Open "file:///'temp_out'" RELOAD'
  253.         'Wait "file:///'temp_out'"'
  254.     end
  255.     else do
  256.         if (res ~= 0) then do
  257.             address command 'sys:rexxc/rxset HTTXPlugin_fault `fault' res'`'
  258.             err=getclip(HTTXPlugin_fault)
  259.             'Request 'req_title' "Error 'err'" "_Ok" NOWAIT'
  260.         end
  261.     end
  262.     return
  263.  
  264.  
  265. Preview:
  266.     'Get SCREEN VAR' prefs_screen
  267.     address command 'SYS:Utilities/Multiview "'save_file'" pubscreen="'prefs_screen'"'
  268.     drop result
  269.  
  270.     if prefs_donotaskdir = 0 then
  271.         'Request 'req_title' "File converted." "_Ok (save)|_Delete file"'
  272.     else
  273.         'Request 'req_title' "File converted.*NSave as *"'save_file'*" ?" "_Ok (save)|_Delete file"'
  274.  
  275.     if (result ~= 1) then do
  276.         del_list=del_list '"'save_file'"'
  277.     end
  278.     return result
  279.  
  280.  
  281. PrepareFile:
  282.     if Open(fp, temp_inc, 'W') then do
  283.         if (prefs_save_noheader == 0) then do
  284.             if (prefs_info == 1) then do
  285.                 'Get INFO STEM' infos trg
  286.                 if (infos.0 > 0) then do
  287.                     string=string||eol||"Header:"||eol
  288.  
  289.                     do ctx=1 for infos.0
  290.                         if (infos.ctx.type ~= "CACHE") then do
  291.                             string=string||infos.ctx.value||eol
  292.                             if infos.ctx.type == "LINK" then string=string||eol||infos.ctx.url
  293.                         end
  294.                     end
  295.                 end
  296.             end
  297.  
  298.             string=string||eol||'URL  : 'doc_url||eol'Title: 'doc_title
  299.         end
  300.  
  301.         WriteCH(fp, string)
  302.         Call Close(fp)
  303.     end
  304.     return
  305.  
  306.  
  307. ReadCfg:
  308.     if Open(rcfg_fp, 'ENV:HTTXPlugin.config', 'R') then do
  309.         cfg=ReadLN(rcfg_fp)
  310.  
  311.         if Left(cfg, 6) == "prefs_" then do
  312.             interpret translate(strip(cfg), ';',' ')
  313.             prefs_savedir=Translate(prefs_savedir, ' ', ';')
  314.         end
  315.  
  316.         Call Close(rcfg_fp)
  317.     end
  318.  
  319.     if prefs_donotaskdir = 0 then do
  320.         prefs_savedir2=getclip('HTTXPlugin_savedir')
  321.         if prefs_savedir2 ~= "" then prefs_savedir=prefs_savedir2
  322.     end
  323.     return
  324.  
  325.  
  326. FindPath:
  327.     procedure
  328.  
  329.     parse ARG pathf
  330.     dir_pos=Max(lastpos('/', pathf), lastpos(':', pathf))
  331.     if dir_pos > 0    then do
  332.         return(left(pathf,dir_pos))
  333.     end
  334.     else do
  335.         return('')
  336.     end
  337.