Guidance
指路人
g.yi.org
Guidance Forums / Reginald Rexx / Use REXX to reformat text file as HTML

Register 
注册
Search 搜索
首页 
Home Home
Software
Upload

  
Forum List • Thread List • Reply • Refresh • New Topic • Search • Previous • Next First 1 Last
Message1. Use REXX to reformat text file as HTML
#3742
Posted by: 2004-04-09 05:55:24
I have 59 text files of varying sizes that I need to turn into HTML web pages.  I know there are lots of tools to help me do this but I'm floundering a bit with REXX.
Message2.
#3777
Posted by: Jeff Glatt 2004-04-09 08:05:23 Last edited by: Jeff Glatt 2007-06-26 15:12:51 (Total edited 3 times)
The minimum things that an HTML page needs are the HTML and BODY tags. I typically use some basic HTML formatting, so I'd just go with something like:
/* Searches for all .txt files under a directory tree,
 * and creates an HTM page from each.
 */
LIBRARY rexxgui

/* Get the directory that we'll search. Use REXX GUI's directory browser */

/* Store the directory in the variable directoryName */
directoryname = ''

/* Present the dialog */
err = guifile('directoryName', 'BROWSE|DRIVES', 'Where to find text files:')

/* Check for success */
IF err == '' THEN DO

   /* Convert the text files */
   err = dohtml(directoryname, 1)
   IF err \== "" THEN SAY err

END
RETURN

/* Finds the names of all files that end in extension of .TXT and makes
 * a HTM file from each text file. It does a recursive directory search
 * from the passed directory name.
 *
 * Returns an empty string if successful, or an error message.
 *
 * err = DoHtml(directoryName, 1)
 * IF err \== "" THEN SAY err
 */

dohtml: PROCEDURE EXPOSE

/* DO UNTIL no more items or an error */
DO UNTIL err \== ""

   /* Get the name of the next item inside of this dir */
   err = MATCHNAME(ARG(2), 'name', ARG(1) || '\*.*', 'DSNHCRAT', 'NS')

   /* No error? */
   IF err == "" THEN DO

      /* Is this a sub-dir? If so, its size will be an empty string */
      IF name.0 == "" THEN DO

         /* Enumerate all items in this sub-dir */
         err = dohtml(ARG(1) || '\' || NAME, ARG(2) + 1)
         IF err \== "" THEN SIGNAL badnames

      END

      /* It's a file. Check its extension for TXT */
      ELSE DO
         UPPER NAME
         IF RIGHT(NAME, 4) == ".TXT" THEN DO

            /* Create a HTM filename */
            htmname = EDITNAME(ARG(1) || '\' || NAME, '*.htm')

            STREAM(htmname, 'C', 'OPEN WRITE REPLACE')

            /* Write an opening HTML header */
            LINEOUT(htmname, "<HTML><HEAD><TITLE>" || NAME || "</TITLE></HEAD><BODY TEXT=BLACK BGCOLOR=WHITE><PRE>")

            /* Read in the text file */
            LOADTEXT('lines.', ARG(1) || '\' || name)

            /* Write it out to the HTML file */
            LOADTEXT('lines.', htmname, 'S')

            /* Write a closing HTML header */
            LINEOUT(htmname, "</PRE></BODY></HTML>")

            /* Close the file */
            STREAM(htmname, 'C', 'CLOSE')
         END
      END
   END

   CATCH NOTREADY
      RETURN STREAM(htmname, 'D')
END

/* If we enumerated all items successfully, return an empty string */
IF err == 'DONE' THEN err = ""

badnames:
/* Return an empty string if success, or an error message */
RETURN err
The HTM file produced above will not "flow" when you resize the browser window. If you want the text to flow, you'll need to do something fancier. Remove the PRE and /PRE tags, and go through the loaded text file inserting a P tag wherever a new paragraph should begin.
Forum List • Thread List • Reply • Refresh • New Topic • Search • Previous • Next First 1 Last
掌柜推荐
 
 
 
 
 
 
 
 
 
 
 
 
© Fri 2024-3-29  Guidance Laboratory Inc.
Email:webmaster1g.yi.org Hits:0