/*REXX*/
     'ISREDIT MACRO(PARMS)'
      IF PARMS = 'H' THEN
        DO
          ERRLINE = "FLOWTXT.SPF PARMS: FLOWTXT C = EACH PARA SEP BY BLANK LINE   FLOWTXT P = EACH LINE IS PARA   FLOWTXT NOPARM = BEST GUESS ON PARAGRAPHS"
          'ISREDIT RESET'
          "ISREDIT LINE_BEFORE " 1 "= NOTELINE  '"ERRLINE"'"
          EXIT
        END
/* TRACE I */
/*----------------------------------------------------------------------------
  PARMS:
   FLOWTXT   = Firgures out paragraph endings
   FLOWTXT P = Each line is a paragraph
   FLOWTXT C = Each paragraph is separated by a blank line

  This program is used to "reflow" text that may have imbedded control
  characters to make such text suitable for conversion to web pages or palm doc
  EBooks.

  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.

  Use MS Works word prosessor or MS WORD to save text in MSDOS Text line
  line break format when processing captured text (from html cut and paste) or
  RTF.

  23 August 2003
  Mark Mcdonald
----------------------------------------------------------------------------*/
/*--- Get Arguments(If Any)---*/
       OPT1 = TRANSLATE(PARMS)
/*--- If source has not been saved, save it ---*/
       'ISREDIT (MODIFIED) = DATA_CHANGED'
       IF (MODIFIED=YES) THEN
         'ISREDIT SAVE'
/*--- Retrieve Path/File Name of Source File ---*/
       'ISREDIT (FILENAME) = DATASET'
       FILENAME = TRANSLATE(FILENAME)
/*--- Retrieve Member Name of Source File ---*/
       MEMNAME = SUBSTR(FILENAME,1,POS('.',FILENAME) -1)
/*--- Set Outfile Name with txt ---*/
       INFILE = FILENAME
       OUTFILE = MEMNAME"2.TXT"
/*--- Zero out File Just in Case ---*/
       ADDRESS DOS 'ECHO S > 'OUTFILE
/*--- Init Variables ---*/
       T = ""
       TH= ""
       TRC = 0
       tcc = 0
       PBLANKFLAG = 0
/*--- Take Care of RTF Format Strings ---*/
  if text = "libertar" then do
       'ISREDIT CHANGE "\ldblquote" x"22" ALL'
       'ISREDIT CHANGE X"2220" X"22" ALL'
       'ISREDIT CHANGE "\rdblquote" x"22" ALL'
       'ISREDIT CHANGE X"2022" X"22" ALL'
       'ISREDIT CHANGE "\par"  " " ALL'
       'ISREDIT CHANGE "\tab"  " " ALL'
       'ISREDIT CHANGE "\rquote" x"27" ALL'
       'ISREDIT CHANGE X"2720" X"27" ALL'
       'ISREDIT CHANGE "__" x"0D0A0D0A" ALL'
       'ISREDIT SAVE'
 end
/*--- Main Loop ---*/
       DO WHILE LINES(INFILE) > 0
/*       --- Get Line from File ---*/
         T = ""
         T = LINEIN(INFILE)
/*       --- Translate Special Characters? ---*/
if text = "lib" then do
         T = TRANSLATE(T,'""',X2C(93)X2C(94))
         T = TRANSLATE(T,"'",X2C(92))
         T = TRANSLATE(T,"-",X2C(B7))
         T = TRANSLATE(T,"",X2C(A0))
         T = TRANSLATE(T,"e",X2C(E8))
         T = TRANSLATE(T,"LF",X2C(9C))
         T = TRANSLATE(T,"e",X2C(8A))
         T = TRANSLATE(T,"c",X2C(87))
         T = TRANSLATE(T,"ae",X2C(91))
         T = TRANSLATE(T," ",X2C(0C))
         T = TRANSLATE(T,"",X2C(A9))
         T = TRANSLATE(T,"",X2C(AD))
         T = TRANSLATE(T,"",X2C(95))
end
         T = TRANSLATE(T,"'",X2C(91))
         T = TRANSLATE(T,"'",X2C(92))
         T = TRANSLATE(T,"-",X2C(97))
         T = TRANSLATE(T,'""',X2C(93)X2C(94))
/*       --- Clean Text - Meaning Each Blank Line Does Delineate a New Paragraph ---*/
         IF OPT1 = "C" THEN DO
            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
/*       --- New Paragraph? Use Argument to Indicate Each Line is a Complete Paragraph ---*/
         IF OPT1 = "P" THEN DO
            IF WORDS(T) > 0 THEN DO
               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 ---*/
       'ISREDIT BROWSE 'OUTFILE
       EXIT(0)