/*REXX*/
/* TRACE I */
/*--- FLOWTXT C \TEMP\BOOKNAME.TXT ------------------------------------------------------------------- 23 August 2003 - Mark McDonald ----*/
  PARMS:  FLOWTXT ? = Firgures out paragraph endings
          FLOWTXT P = Each line is a paragraph
          FLOWTXT C = Each paragraph is separated by a blank line
  EXAMPLE:  FLOWTXT P \TEMP\EBOOK.TXT
  Used to "reflow" text that may have imbedded control characters to make such text suitable for  conversion to web
  pages, EPUP, etc. (PalmDoc Smallest size - Readable on Android Using CoolReader).

  Each paragraph becomes one lone line with one space between words and sentences. Suitable for PALM-DOC converters and HTML Text. Removes
  line-break characters replacing with one space.   Removes hanging hyphen at end of line.  Very fast - can process 26,000 lines in about
  10 seconds.  Save Text in MSDOS Text line line break format when processing captured text (from html cut and paste) or RTF.  Set to call
  SPFPRO for Last Minute Editing of TXT, Rex waits for SPFPRO to exit, then pocesses text and DOCREADER to create PalmDOC (PDB) document.
  Note that DOCREADER can load PDB files and save them as text allowing processimg into another DOC format.
------------------------------------------------------------------------------------------------------------------------------------------*/
     ARG PARMS
       PARMS = TRANSLATE(PARMS)                                              ;/*Convert Args/Parms to Upper Case*/
       FILENAME = WORD(PARMS,2)                                              ;/*2nd Word is Original Text File*/
       MEMNAME = SUBSTR(FILENAME,1,POS('.',FILENAME) -1)                     ;/*File Name Without .EXT*/
       INFILE = FILENAME                                                     ;/*Name of Text File to Process*/
       'C:\SPFPRO\SPFPRO.EXE /E 'FILENAME                                    ;/*Call SPFPRO and Wait Until it Exits*/
       OUTFILE = MEMNAME"2.TXT"                                              ;/*Set Name of New Output Text File*/
       T = ""; TH= ""; TRC = 0; tcc = 0; PBLANKFLAG = 0                      ;/*Init Vars*/
       DO WHILE LINES(INFILE) > 0                                            ;/*Loop Until End of Input File*/
         T = ""; T = LINEIN(INFILE)                                          ;/*Get Line from Input File*/
         T = TRANSLATE(T,"*",X2C(07))                                        ;/*Translate Various Chars IF Present*/
         T = TRANSLATE(T,"'",X2C(91))
         T = TRANSLATE(T," ",X2C(FA))
         T = TRANSLATE(T,"'",X2C(92))
         T = TRANSLATE(T,"-",X2C(97))
         T = TRANSLATE(T,'""',X2C(93)X2C(94))
         IF OPT1 = "?" THEN OPT1 = ""                                        ;/*Let Rexx Figure out Paragraphing*/
         IF OPT1 = "C" THEN DO                                               ;/*Each Blank Line Means a New Paragraph*/
            IF WORDS(T) < 1 THEN DO
               IF PBLANKFLAG = 0 THEN DO
                  TRC = LINEOUT(OUTFILE,TH)
                  TRC = LINEOUT(OUTFILE," ")
                  TH = ''
                  PBLANKFLAG = 1
               END
            END
            ELSE DO
               TH = TH' 'SPACE(T,1," ")
               PBLANKFLAG = 0
            END
            ITERATE
         END
         IF OPT1 = "P" THEN DO                                               ;/*Each Line IS a New Paragraph*/
            IF WORDS(T) > 0 THEN DO                                          ;/*What you Get From MS Word Save Txt MSDOS (Best FMT*/
               TH = SPACE(T,1," ")
               TH = STRIP(TH)
               TRC = LINEOUT(OUTFILE,TH)
               TRC = LINEOUT(OUTFILE," ")
               TH = ""
               T = ""
            END
            ITERATE
         END
/*       --- One Word Only? ---*/
         IF WORDS(T) = 1 & RIGHT(STRIP(T),1) <> "." & RIGHT(STRIP(T),1) <> '"' THEN DO
            T = "  "T
         END
/*       --- Start a New Paragraph? ---*/
         IF (LEFT(T,1) = ' ' & WORDS(T) > 0) | WORDS(T) < 1 | RIGHT(STRIP(T),1) = '.' | LEFT(T,1) = '#' | RIGHT(STRIP(T),2) = '."' | RIGHT(STRIP(T),2) = '?"' | RIGHT(STRIP(T),2) = '!"' | LEFT(STRIP(T),1) = '*' | RIGHT(STRIP(T),1) = '*' | LEFT(STRIP(T),1) = '"' THEN
           DO
/*           --- Hanging Sentence? Used When Paragrph Indent is Not Present ---*/
             IF LEFT(T,1) <> ' ' & ( RIGHT(STRIP(T),1) = '.' | RIGHT(STRIP(T),2) = '."' | RIGHT(STRIP(T),2) = '?"' | RIGHT(STRIP(T),2) = '!"' | RIGHT(STRIP(T),1) = '*') THEN
                DO
                  TH = ' 'TH' 'SPACE(T,1," ")
                  T = ""
             END
            IF WORDS(TH) > 0 THEN DO
               TRC = LINEOUT(OUTFILE,STRIP(TH))
               TH = " "
               TRC = LINEOUT(OUTFILE," ")
            END
         END
/*       --- Make Word Spacing Consistent ---*/
         IF WORDS(T) > 0 THEN T = SPACE(T,1," ")
/*       --- Embedded Page Marker? ---*/
/*       IF POS("[PAGE ",TRANSLATE(T),1) > 0 THEN   */
/*         DO                                       */
/*           SP = POS("[PAGE ",TRANSLATE(T),1)      */
/*           EP = POS("]",T,SP)                     */
/*           TH = TH""LEFT(T,SP-1)                  */
/*           TH = SUBSTR(T,EP+1)                    */
/*         END                                      */
/*       --- Hyphen is Present? ---*/
         IF (RIGHT(T,2) <> "--") & (RIGHT(T,1) = "-") THEN
           DO
/*             T = LEFT(T,LENGTH(T)-1)  */
           END
         ELSE DO
             T = T" "
           END
/*       --- Concatenate Data ---*/
         TH = TH" "T
       END
/*     -- CLOSE THE FILE ---*/
       T = LINEOUT(OUTFILE)
/*--- Browse the Output File ---*/
       '\U\DOCREADER.EXE 'OUTFILE                                    ;/*Use DOCREADER to Create PDB EBook*/
       EXIT(0)