home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 35 Internet / 35-Internet.zip / makehtml.zip / MAKEHTML.CMD < prev    next >
OS/2 REXX Batch file  |  1996-10-25  |  16KB  |  360 lines

  1. /* MakeHTML.CMD -  Make HTML code for a set of download dirs */
  2. /* By Paul M Blais - pblais@visi.com - September 13, 1996 */
  3. CALL RxFuncAdd 'FileLoadFuncs' , 'FILEREXX' , 'FileLoadFuncs'
  4. CALL FileLoadFuncs
  5. CALL RxFuncAdd 'SysLoadFuncs','RexxUtil','SysLoadFuncs'
  6. CALL SysLoadFuncs
  7.  
  8. /* ======== This stuff should be examined before you run the script ====== */
  9. RootDir = 'F:\DOWNLOAD'     /* root of the library (UPDATE THIS) */
  10.  
  11. /* Controls for the HTML Pages Update these are required */
  12. MyHTML = 'INDEX.HTML'               /* default HTML file name */
  13. ReadMe = 'README'                   /* default readme file to be included AFTER the */
  14.                                     /* HTML header. No slashes, No path, May include an extension */
  15. BodyParm = 'BACKGROUND="/down.gif"' /* if you want a background or other <BODY> HTML parms */
  16. HomePage = '/welcome.html'          /* Your Web Site Home Page for the Home Icon in the footer */
  17. HomeSite = 'Paul Blais Downloads'   /* the name of you Web site download library for the */
  18.                                     /*   HTML Title it will have the dir subject ea appended */
  19.  
  20. /* HTML File for the main Root directory (Do NOT change) */
  21. RootPage = MakeHTMLPath(FILESPEC('path' , RootDir)||FILESPEC('name' , RootDir)||'/'||MyHTML)
  22.  
  23. /* Home Button for the footer */
  24. HomeButton = '<A HREF="'||HomePage||'"><IMG SRC="/ADMIN/SKYHOME.GIF" ALIGN="BOTTOM" ALT=" ">HOME PAGE</A>'
  25. /* Icon to retrun to previous library */
  26. BackButton = '<IMG SRC="/ICONS/BACK.GIF" ALIGN="BOTTOM" ALT=" ">'
  27. /* Icon to return to root library */
  28. MainButton = '<A HREF="'||RootPage||'"><IMG SRC="/ICONS/DIR.GIF" ALIGN="BOTTOM" ALT=" ">'
  29. /* Icon for related libraries */
  30. RelatedIcon = '<IMG SRC="/ADMIN/YELDOT.GIF" WIDTH=14 HEIGHT=14 ALIGN="ABSMIDDLE" ALT=" ">'
  31. /* Icons for member files */
  32. FileIcon = '<IMG SRC="/ICONS/DITDOT.GIF" WIDTH=14 HEIGHT=13 ALIGN="ABSMIDDLE" ALT=" ">'
  33.  
  34. /* ======== Program Starts Here ========== */
  35. SAY 'Creating HTML Pages'
  36. /* Init the forever loop for the first pass */
  37. FCount = 0                  /* Number of files in the current page */
  38. DCount = 0                  /* Dir count on the current page */
  39. TDCount = 1                 /* sequence number in the ToDO stem */
  40. CurrentDir = 0              /* sequence # of the current Dir in the ToDo stem */
  41. ToDo. = ''                  /* Stem for dirs to process */
  42. Handle = 0                  /* File handle for FileREXX */
  43. ToDo.0 = 1                  /* one dir in the hooper already (the root dir) */
  44.  
  45. /* get the subject from the root directory */
  46. TmpSubject = ""
  47. PathName = RootDir
  48. IF SysGetEA(RootDir , ".subject" , "TmpSubject") = 0 THEN DO
  49.     /* Parse out the first 4 bytes FM/2 prepends */
  50.     PARSE VAR TmpSubject 5 TmpSubject
  51.     /* Remove leading and trailing spaces */
  52.     TmpSubject = STRIP(TmpSubject)
  53.     /* If no .subject make one */
  54.     IF TmpSubject = "" THEN TmpSubject = '- No desc -'
  55. END  /* End Get EA */
  56. ELSE TmpSubject = '- No desc -'
  57.  
  58. /* Load the data for the root directory */
  59. ToDo.TDCount.dpath = PathName
  60. ToDo.TDCOunt.parent = '/'
  61. ToDo.TDCount.sb = TmpSubject
  62.  
  63. DO FOREVER  /* keep processing dirs til we get them all */
  64.   FCount = 0                  /* Number of files in the current page */
  65.   Files. =''                  /* Stem for file names */
  66.   Dirs.  = ''                 /* Stem for current dirs */
  67.   Files.0 = 0                 /* reinit the Files. stem var each time */
  68.   Dirs.0 = 0                  /* reinit the Dirs. stem var each time */
  69.   DCount = 0                  /* Dir count on the current page */
  70.   CurrentDir = CurrentDir + 1 /* pointer to the next dir to process */
  71.  
  72.   /* this is how we break the FOREVER LOOP */
  73.   /* we keep looking for more dirs and when there are no more we quit */
  74.   /* Note that we also keep adding to the ToDo. stem as we find subdirs */
  75.   IF CurrentDir > ToDo.0 THEN LEAVE
  76.  
  77.   /* get the next dir on the list to process */
  78.   PathName = ToDo.CurrentDir.dpath
  79.  
  80.   /* find all the files in the current dir */
  81.   Handle = 0   /* reset the handle each time we start */
  82.   DO Until Handle  = 0
  83.     err = FileMatchFile('File','Handle',PathName||'\',DIR,'SIZE|DATE')
  84.     IF err = 0 THEN DO
  85.  
  86.       /* Get Subject EA this will be the description or "- no desc" - is none found */
  87.       TmpSubject = ""
  88.       IF SysGetEA(PathName||'\'||File , ".subject" , "TmpSubject") = 0 THEN DO
  89.           /* Parse out the first 4 bytes FM/2 prepends */
  90.           PARSE VAR TmpSubject 5 TmpSubject
  91.           /* Remove leading and trailing spaces */
  92.           TmpSubject = STRIP(TmpSubject)
  93.           /* If no .subject make one */
  94.           IF TmpSubject = "" THEN TmpSubject = '- no desc -'
  95.       END  /* Get EA */
  96.       ELSE TmpSubject = '- no desc -'
  97.  
  98.       /* Filter out HTML files and any objects with a leading * in the subject EA */
  99.       /* The store dirs in the Dirs. stem and Files in the Files. stem */
  100.       IF \(File = MyHTML | File = ReadMe | SUBSTR(TmpSubject,1,1) = '*') THEN DO
  101.  
  102.         IF File.0 \= '' THEN DO  /* It's a file not a Dir */
  103.            /* File FCount  hOw many files in this dir */
  104.            FCount = FCount + 1
  105.            Files.0 = FCount
  106.  
  107.            /* Subject EA this will be the file description */
  108.            Files.FCount.sb = TmpSubject
  109.  
  110.            /* File Size calculation in either bytes, KB, or MB as required */
  111.            IF File.0 > 1024  THEN DO
  112.                 IF File.0 > 1024*1024  THEN DO
  113.                   Files.FCount.size = STRIP(FORMAT(File.0/(1024*1024) , 5 , 1)' MB')
  114.                 END
  115.                 ELSE Files.FCount.size = STRIP(FORMAT(File.0/1024 , 5 , 0)' KB')
  116.            END /* File.0 > 1024 */
  117.            ELSE Files.FCount.size = STRIP(File.0)
  118.  
  119.            /* File name fold to upper case always*/
  120.            Files.FCount.nm = TRANSLATE(File)
  121.            Files.FCount.html = MakeHTMLPath(PathName||'\'||File)
  122.  
  123.            /* File Date in MM-DD-YYYY format*/
  124.            Files.FCount.dt = WORD(File.1,1)'-'WORD(File.1,2)'-'WORD(File.1,3)
  125.  
  126.         END /* File.0 \= 0 a single file is added to the stem */
  127.         ELSE DO        /* It's a driectory not a file */
  128.                        /* zero length files will test to be a dir, this is a problem */
  129.           /* Increment the Dirs  DCount */
  130.           DCount = DCount + 1
  131.           Dirs.0 = DCount
  132.  
  133.           /* Subject EA */
  134.           Dirs.DCount.nm = TRANSLATE(File)
  135.           Dirs.DCount.html = MakeHTMLPath(PathName||'\'||File)
  136.  
  137.           TDCount = TDCount + 1
  138.           ToDo.0 = TDCount
  139.           ToDo.TDCount.dpath = PathName||'\'||File
  140.           ToDo.TDCOunt.parent = MakeHTMLPath(PathName'\')
  141.  
  142.           /* Parse the Subject for a attr flag */
  143.           /* If you need more flags start mucking with the code here */
  144.           Flag = SUBSTR(TmpSubject , 1 , 1) /* get the 1st char from the subject) */
  145.           SELECT
  146.             WHEN Flag = '#' THEN DO
  147.               ToDo.TDCount.attr = '#'
  148.               ToDo.TDCount.sb = SUBSTR(TmpSubject,2, LENGTH(TmpSubject)-1)
  149.               Dirs.DCount.sb = ToDo.TDCount.sb
  150.             END
  151.             OTHERWISE
  152.               ToDo.TDCount.attr = ''
  153.               ToDo.TDCount.sb = TmpSubject
  154.               Dirs.DCount.sb = TmpSubject
  155.           END  /* End Flag select */
  156.         END /* It's a DIR a new dir has been added to it's stem */
  157.       END /* Pass the filter test we skip those that don't pass */
  158.     END /* err = 0 get next file from the dir */
  159.  
  160.     /* opps had a problem reading files from the dir CHKDSK is appropriate */
  161.     ELSE IF err <> 18 THEN SAY 'Error Processing '||PathName||'\'||File||' Please run CHKDSK'
  162.   END /* Handle = 0 this directory has all the stuff we need to make an HTML page */
  163.  
  164.   /* for some reason this needs to be cleared, I don't know why */
  165.   err = FileMatchFile(0,Handle)
  166.  
  167.   /*==============================*/
  168.   /* Output the new HTML Page     */
  169.   /*==============================*/
  170.   handle = 0   /* Init the File Handle */
  171.  
  172.   /* Overwrite any esisting HTML pages */
  173.   /* Open a new HTML file without any file sharing allowed */
  174.   handle = FileOpen(ToDo.CurrentDir.dpath||'\'||MyHTML , 'wrs--' , 'on' , 0)
  175.   IF handle = 0 Then DO
  176.      SAY '!! Unable to create '||ToDo.CurrentDir.dpath||'\'||MyHTML
  177.      SAY 'handle = '||handle||' FileErr = '||FileErr
  178.      EXIT  /* Bail out */
  179.   END /* End Open File */
  180.  
  181.   /* ============== ouput standard Page header ===================== */
  182.   SELECT  /* there may be a totally empty directory (not counting an existing .HTML index file */
  183.     WHEN (Dirs.0 > 0) | (Files.0 > 0) THEN DO /* there are files or sub dirs to process */
  184.       /* write the HTML header */
  185.       err = Makeheader()
  186.  
  187.       /* Process the subdirectories */
  188.       IF Dirs.0 > 0 THEN DO /* there are sub dirs in this dir */
  189.         err = OutputDirs()
  190.       END /* end dirs processing */
  191.  
  192.       /* Process the files */
  193.       IF Files.0 > 0 THEN DO /* There are files in this dir to process */
  194.       /* setup before the files table */
  195.       err = OutputFiles()
  196.       /* any stuff after the files table goes here */
  197.       END /* End files processing */
  198.       /* make a standard page footer */
  199.       err = MakeFooter()
  200.     END /* End of dirs or files to process */
  201.  
  202.     OTHERWISE /* it's an empty directory */
  203.       /* Create a special Under Construction Page with header, the message below and a footer */
  204.       /* write the HTML header */
  205.       err = Makeheader()
  206.       err = FilePuts(handle,'<P>!!!! - Library '||ToDo.CurrentDir.sb||' is under construction.</P>')
  207.       /* make a standard page footer */
  208.       err = MakeFooter()
  209.   END
  210.   /* Post processing after the page is done */
  211.   SAY 'Created: '||ToDo.CurrentDir.dpath||'\'||MyHTML
  212.   err = FileClose(handle)
  213. END /* end forever */
  214. /* do any clean up or final processing before we leave.  All HTML pages are done */
  215. SAY 'All HTML Pages were made!'
  216. EXIT
  217.  
  218. /*============================= Functions and procedures ==============================*/
  219.  
  220. MakeHTMLPath:
  221. /* --------- Make an HTML path with forward slashes ----------*/
  222.   ARG HTMLPath
  223.   /* Strip off the drive letter */
  224.   HTMLPath = FILESPEC('path' , HTMLPath)FILESPEC('name' , HTMLPath)
  225.  
  226.   /* Replace all the "\" chars with a "/" char i.e. global search & replace */
  227.   Counter = 0
  228.   DO LENGTH(HTMLPath)
  229.     Counter = Counter + 1
  230.     IF COMPARE('\' , SUBSTR(HTMLPath , Counter , 1)) = 0
  231.     THEN HTMLPath = SUBSTR(HTMLPath , 1 , Counter-1)'/'SUBSTR(HTMLPath , Counter+1 , LENGTH(HTMLPath)-Counter)
  232.   END
  233.   RETURN HTMLPath
  234.  
  235. /*=============================== The following procedures define the HTML page =================== */
  236. /* all pages have a Header, the Dirs (if any), the Files (if any) and a footer */
  237.  
  238. Makeheader:
  239. /* ------------------ Make Page Header ---------------- */
  240.   err = FilePuts(handle,'<HTML> <HEAD>')
  241.   /* I test the err var on the first and last write only! */
  242.   IF err <> LENGTH('<HTML> <HEAD>')+2 THEN DO
  243.     Say 'Error writing to HTML File '||ToDo.CurrentDir.dpath||'\'||MyHTML
  244.     EXIT
  245.   END
  246.  
  247.   err = FilePuts(handle,'<TITLE>'||HomeSite||' : '||ToDo.CurrentDir.sb||'</TITLE></HEAD><BODY '||BodyParm||'>')
  248.   err = FilePuts(handle,'<P><H1>Downloads : <B>'||ToDo.CurrentDir.sb||'</B></H1></P>')
  249.   /* --- Insert any README text ----- */
  250.   err = AddTheReadme()
  251.   /* Thre is no previous dir on the root page */
  252.   IF CurrentDir \= 1 THEN DO
  253.     err = FilePuts(handle,'<P><A HREF="'||ToDo.CurrentDir.parent||MyHTML||'">'||BackButton||'<B>Parent Library</B></A></P>')
  254.   END
  255. RETURN 0
  256.  
  257. OutputDirs:
  258. /*----------------- Output the Dirs Stem -------------------*/
  259.   /* Init the table and legend */
  260.   err = FilePuts(handle,'<P><H2>'||RelatedIcon||'Related Libraries</H2></P>')
  261.   err = FilePuts(handle,'<TABLE BORDER=1 WIDTH=100%>')
  262.   err = FilePuts(handle,' <TR>')
  263.   err = FilePuts(handle,'   <TD><B>Related Library</B></TD>')
  264.   err = FilePuts(handle,'   <TD><B>Description</B></TD></TR>')
  265.  
  266.   /* make all the rows in the table */
  267.   DO x=1 TO Dirs.0
  268.     err = FilePuts(handle,' <TR>')
  269.     err = FilePuts(handle,'   <TD VALIGN=TOP>'||RelatedIcon||'<A HREF="'||Dirs.x.html||'/'||MyHTML'"><B>'||Dirs.x.nm||'</B></A></TD>')
  270.     err = FilePuts(handle,'   <TD VALIGN=TOP>'||Dirs.x.sb||'</TD></TR>')
  271.   END
  272.   /* Close the table */
  273.   err = FilePuts(handle,'</TABLE>')
  274. RETURN 0
  275.  
  276. OutputFiles:
  277. /*----------------- Output the Files Stem -------------------*/
  278.   /* Init the table and legend */
  279.   err = FilePuts(handle,'<P><H2>'||FileIcon||'Files in this Library</H2></P>')
  280.   err = FilePuts(handle,'<P>'||Files.0||' Files</P>')
  281.   err = FilePuts(handle,'<TABLE BORDER=1 WIDTH=100%>')
  282.   err = FilePuts(handle,' <TR>')
  283.   err = FilePuts(handle,'   <TD><B>File Name</B></TD>')
  284.   err = FilePuts(handle,'   <TD><B>Description</B></TD>')
  285.   err = FilePuts(handle,'   <TD ALIGN=RIGHT><B>Size</B></TD>')
  286.   /* Special ToDo.x.Attr flag for this dir (reverse order) */
  287.   IF ToDo.CurrentDir.attr \= '#' THEN err = FilePuts(handle,'   <TD ALIGN=RIGHT><B>Date</B></TD></TR>')
  288.   IF ToDo.CurrentDir.attr = '#'  THEN err = FilePuts(handle,'   </TR>')
  289.  
  290.   /* make all the rows in the table */
  291.   SELECT
  292.     /* set the loop params for processing all the files in Files. stem var */
  293.     When ToDo.CurrentDir.attr = '#' THEN DO
  294.     /* Special ToDo.x.Attr flag for this dir (reverse order) */
  295.     /* I also don't add a Date column either (see below)*/
  296.       Beg = Files.0
  297.       Endat = 1
  298.       DecBy = -1
  299.       END
  300.     /* Alpha Order normal default */
  301.     OTHERWISE
  302.       Beg = 1
  303.       Endat = Files.0
  304.       DecBy = 1
  305.   END
  306.  
  307.   DO x=Beg TO Endat BY DecBy
  308.     err = FilePuts(handle,' <TR>')
  309.     err = FilePuts(handle,'   <TD VALIGN=TOP>'||FileIcon||'<A HREF="'||Files.x.html||'"><B>'||Files.x.nm||'</B></A></TD>')
  310.     err = FilePuts(handle,'   <TD VALIGN=TOP>'||Files.x.sb||'</TD>')
  311.  
  312.     /* Special ToDo.x.Attr flag for this dir (reverse order) */
  313.     IF ToDo.CurrentDir.attr \= '#' THEN err = FilePuts(handle,'   <TD ALIGN=RIGHT VALIGN=TOP><I>'||Files.x.size||'</I></TD>')
  314.     IF ToDo.CurrentDir.attr \= '#' THEN err = FilePuts(handle,'   <TD ALIGN=RIGHT VALIGN=TOP><I>'||Files.x.dt||'</I></TD></TR>')
  315.  
  316.     IF ToDo.CurrentDir.attr  = '#' THEN err = FilePuts(handle,'   <TD ALIGN=RIGHT VALIGN=TOP><I>'||Files.x.size||'</I></TD></TR>')
  317.   END
  318.   /* Close the table */
  319.   err = FilePuts(handle,'</TABLE>')
  320. RETURN 0
  321.  
  322. MakeFooter:
  323. /* ------------------ Make Page footer ---------------- */
  324.   /* Add any standard links at the bottom of the page. You may want this different */
  325.  
  326.   /* add a link to the Home page and to the main Library page */
  327.   err = FilePuts(handle,'<P>'||HomeButton||' | '||MainButton||Todo.1.sb||' Library</A></P>')
  328.   /* put the current date as the last updated date, also use smaller font */
  329.   err = FilePuts(handle,'<P><FONT SIZE=-1>Last Updated : '||DATE('W')||' - '||DATE('N')||'</FONT></P>')
  330.  
  331.   /* You MUST have this in the footer duh! */
  332.   err = FilePuts(handle,'</BODY></HTML>')
  333.   /* I test the err var on the first and last write only! */
  334.   IF err <> LENGTH('</BODY></HTML>')+2 THEN DO
  335.     Say 'Error writing to HTML File '||ToDo.CurrentDir.dpath||'\'||MyHTML
  336.     EXIT
  337.   END
  338. RETURN 0
  339.  
  340. AddTheReadme:
  341. /* ----- Inserts any readme file into the HTML File after the header ------ */
  342. /* Readme should be in HTML format */
  343.   handle2 = 0 /* Init the handle */
  344.   handle2 = FileOpen(ToDo.CurrentDir.dpath||'\'||ReadMe)
  345.   IF handle2 = 0 Then RETURN 0 /* no handle means no file found */
  346.   /* If you want plain text you could insert a <PRE> here and a </PRE> at the end */
  347.   DO FOREVER
  348.     Line = FileGets(handle2, 't')
  349.     /* Detect End of File */
  350.     IF FileErr = 0 THEN LEAVE  /* EOF encountered */
  351.     /* write to the HTMl file */
  352.     err = FilePuts(handle, Line)
  353.   END
  354.   /* Close the README */
  355.   /* insert </PRE> here */
  356.   err = FileClose(handle2)
  357. RETURN 0
  358.  
  359.  
  360.