{NToL - News TO Layout}

{This program converts a news topic to a layout file}

%external %string (80) Copyright %alias "NEWS_(C)_RMM_85" = -
   "Copyright (C) 1985 Richard M. Marshall"

%include "Inc:Util.Imp"
%include "News.Inc"

%begin
   %byte Finished = No

   %constant %string Temp File = "$NToL_Temp$"

   %constant %integer Laser    = 2_10000000000000000000000000000000,
                      Contents = 2_01000000000000000000000000000000,
                      Confirm  = 2_00100000000000000000000000000000,
                      Page     = 2_00010000000000000000000000000000
   %own %integer Options = 0,
                 Message Number = 1,
                 Last Number = 0,
                 Message Length

   %own %string (255) Topic,
                      Output = ""

   {GET SUBJECT}

   %routine Get Subject (%string (*) %name From, Subject)
      %string (255) Line
      %byte Ch
      %integer I
      
      From = ""
      Subject = ""
      Line = ""
      %cycle
         Read Symbol (Ch)
         Message Length = Message Length - 1
         %if Ch = NL %start
            %if Line -> ("Subject: ") . Subject %start
            %else %if Line -> ("From: ") . From
            %finish
            Line = ""
         %else
            Line =  Line . To String (Ch) %unless Ch < ' ' %and -
                                                  Ch # 9 {TAB}
         %finish
      %repeat %until Message Length = 0 %or -
                     (Subject # "" %and -
                      From # "")
      %for I = 1, 1, Length (From) %cycle
         %if Char No (From, I) = ' ' %start
            Length (From) = I - 1
            %exit
         %finish
      %repeat
   %end {Get Subject}

   {-SET UP-}

   %routine Set Up
      %byte State

      {DALLAS}

      %routine Dallas

         %on 9 %start
            %stop
         %finish

         Define Param ("Topic - the name of the topic to be printed",
                       Topic,
                       PAM Major!PAM No Default)
         Define Param ("Output - the layout file, default <topic>.lay",
                       Output,
                       PAM Major!PAM New Group)
         Define Int Param ("First - number of first article to print",
                           Message Number,
                           PAM Major)
         Define Int Param ("Last - number of last article to print",
                           Last Number,
                           PAM Major)
         Define Boolean Params ("Laser,CONTents,CONFirm,Page",
                                Options,
                                0)
         Process Parameters (CLI Param)
         %if Topic = "ALL" %start
            Print String ("ALL is not valid for NToL")
            New Line
            %stop
         %finish
         Output = Topic . ".LAY" %if Output = ""
         %if Length (Output) > 12 %start
            Print String("Output Filename too long")
            New Line
            %stop
         %finish
      %end {Dallas}

      {LAYOUT HEADER}

      %routine Layout Header
         %if Options&Laser = 0 %start
            print string ("$A line=80");    new line
            print string ("$A pageno=1");   new line
            print string ("$A invert=0");   new line
            print string ("$A cap=0");      new line
            print string ("$A capsh=0");    new line
            print string ("$A und=0");    new line
         %else
            print string("$d ^=$a$r-0.25"); newline
            print string("$d %=$w$u"); newline
            print string("$a just=1; left=1""; line=6.5"""); newline
            print string("$f16=16 s=1.0"); newline
            print string("$16"); newline
            Print String ("$F26=26 S=1.0");  New Line
            Print String ("$D Heading=$26$H"); New Line
            Print String ("$D Italic=$26");  New Line
            print string("$a left=0.75"";nls=1.0"); new line
            print string("$d ^=$^"); new line
            print string("$d %=$%"); new line
         %finish
         print string ("$L0");           new line
      %end {Layout Header}

      {MAIN CODE OF SETUP}

      %on 3 %start
         Select Output (0)
         %if State = 1 %start
            Print String (Topic . " does not exist")
         %else
            Print String (Event_Message)
         %finish  
         New Line
         Finished = Yes
         %return
      %finish

      Home Host = Current Host
      News Host = Connect to Host(News Server)
      Select Host (Home Host)
      Dallas
      State = 1
      Select Host (News Host)
      %unless Open News File (Topic, 0) %start
         Print String (Topic . " is currently in use - try again later")
         New Line
         Finished = Yes
         %return
      %finish
      Message Number = Message Number - 1                {does a NEXT on it}
      Last Number = Last Message %if Last Number = 0     {defaulting}
      State = 2
      Select Host (Home Host)
      Open Output (1, Output)
      Select Output (1)
      Layout Header
      %if Options&Contents # 0 %start
         Open Output (3, Temp File)
         Select Output (3)
         Print String ("$N")
         New Line
         %if Options&Laser # 0 %start
            Print String ("$PageNo=0")
         %else
            Print String ("$A PageNo=0")
         %finish
         New Line
         Print String ("$B2 $L1UM")
         New Line
         Print String ("$Heading(") %if Options&Laser # 0
         Print String ("Contents of " . Topic)
         Print Symbol (')') %if Options&Laser # 0
         New Line
         Print String ("$B2")
         New Line
         Select Output (1)
      %finish
   %end {Set Up}

   {PRINT ARTICLE}

   %routine Print Article
      %string (255) Line
      %string (255) From, Subject

      {PRINT HEADING}

      %routine Print Heading
         %own %integer First = 1

         Select Host (Home Host)
         %if Options&Page # 0 %start
            %if First = 0 %start
               Print String ("$N")
               New Line
            %else
               First = 0
            %finish
         %else
            Print String ("$B2 $V5")
            New Line
         %finish
         Print String ("$L1UM")
         New Line
         Print String ("$Heading(") %if Options&Laser # 0
         %if Message Number = 1 %start
            Print String ("TOPIC ")
         %else
            Print String ("Article ")
            Write (Message Number, 0)
            Print String (" of ")
            Write (Last Message, 0)
            Print String (" in ")
         %finish
         Print String (Topic)
         Print String (")") %if Options&Laser # 0
         New Line
         Print String ("$B1")
         New Line
      %end {Print Heading}

      {SAFE PRINT SYMBOL}

      %routine Safe Print Symbol (%byte Ch)
         %own %byte Featuring = No,
                    Last Ch = 0

         %if Ch = 6 {ACK} %start
            Featuring = Yes
         %else %if Ch = 14 {SO}
            Featuring = No
         %else
            %if Featuring = Yes %and -
                (Last Ch = ' ' %or -
                 Last Ch = NL) %and -
                Ch > ' ' %start
               Print Symbol ('%')
            %finish
            Print Symbol ('$') %if Ch = '%' %or -
                                   Ch = '$'
            Print Symbol (Ch)
         %finish
         Last Ch = Ch
      %end {Safe Print Symbol}

      {PRINT TOP LINES}

      %routine Print Top Lines
         %byte Ch,
               Last Ch = 0,
               First

         {LINE START}

         %routine Line Start
            Select Host (News Host)
            First = Yes
            %return %if Next Symbol = '(' %or -
                        Next Symbol = NL
            Select Host (Home Host)
            %if Options&Laser # 0 %start
               Print String ("$Italic(")
            %else
               Print Symbol ('%')
            %finish
         %end {Line Start}

         {MAIN CODE OF PRINT TOP LINES}

         %if Message Number > 1 %start
            Line Start
            %cycle
               Select Host (News Host)
               Read Symbol (Ch)
               Select Host (Home Host)
               Message Length = Message Length - 1
               Print String (" $B0") %if Ch = NL
               Safe Print Symbol (Ch)
               %if Ch = NL %start
                  %exit %if Last Ch = NL
                  Line Start
               %else %if Ch = ':' %and -
                         First = Yes
                  {end of start of line!?}
                  %if Options&Laser # 0 %start
                     Print String (") $C1.5""")
                  %else
                     Print String (" $C14")
                  %finish
                  First = No
               %finish
               Last Ch = Ch
            %repeat
         %finish
         Print String ("$L0")
         New Line
      %end {Print Top Lines}

      {PRINT BODY}

      %routine Print Body
         %byte Ch

         %while Message Length > 0 %cycle
            Select Host (News Host)
            Read Symbol (Ch)
            Message Length = Message Length - 1
            Select Host (Home Host)
            Safe Print Symbol (Ch)
         %repeat
      %end {Print Body}

      {CONTENTS ENTRY}

      %routine Contents Entry
         %return %if Message Number = 1
         Select Output (3)
         %if Options&Laser # 0 %start
            Write (Message Number, 0)
            Print String ("$C1.5"" " . From . "$C3"" " . Subject . " $B0")
         %else
            Print String ("$C5 ")
            Write (Message Number, 0) 
            Print String ("$C10 " . From . "$C20 " . Subject . " $B0")
         %finish
         New Line
         Select Output (1)
      %end {Contents Entry}

      {CONFIRMED}

      %predicate Confirmed
         %byte Ch

         %on 9 %start
            Finished = Yes
            New Line
            %false
         %finish

         %true %if Options&Confirm = 0
         %if Message Number = 1 %start
            Select Output (0)
            Print String ("Introductory article")
         %else
            Select Output (0)
            Print String ("Article ")
            Write (Message Number, 0)
            Print String (" from " . From)
            Print String ("  Subject: " . Subject)
         %finish
         New Line
         Select Output (1)
         Prompt ("Print? [yn] ")
         Select Input (0)
         Read Symbol (Ch)
         Skip Symbol %if Ch # NL
         Select Input (News Stream)
         %if Ch = 'n' %or -
             CH = 'N' %start
            %false
         %else
            {reset position on message}
            %true
         %finish
      %end {Confirmed}

      {MAIN CODE OF PRINT ARTICLE}

      Select Host (News Host)
      Message Length = Move To Message (Message Number, Forwards)
      Finished = Yes %if Message Number >= Last Number
      %return %if Message Number > Last Number
      %if Message Length = 0 %start
         Finished = Yes
      %else 
         %if Options&(Confirm!Contents) # 0 %start
            Get Subject (From, Subject)
            Message Length = Move To Message (Message Number, No)
         %finish
         %if Confirmed %start
            Contents Entry %if Options&Contents # 0
            Print Heading
            Print Top Lines
            Print Body
         %finish
      %finish
   %end {Print Article}

   {CLOSE DOWN}

   %routine Close Down

      {SHUFFLE CONTENTS}

      %routine Shuffle Contents
         %byte Ch

         %on 9 %start
            Close Input
            Close Output
            Delete (Temp File)
            %return
         %finish

         Select Output (3)
         Close Output
         Open Input (3, Temp File)
         Select Input (3)
         Select Output (1)
         %cycle
            Read Symbol (Ch)
            Print Symbol (Ch)
         %repeat
      %end {Shuffle Contents}

      {MAIN CODE OF CLOSE DOWN}

      Disconnect Host (News Host)
      Select Host (Home Host)
      Shuffle Contents %if Options&Contents # 0
      Select Output(0)
   %end {Close Down}

   {MAIN CODE OF NTOL}

   %on %event 0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15 %start
      Close News File      { Safe, even if not open }
      Disconnect Host (News Host)
      Select Host (Home Host)
      %if Event_Event > 0 %start
         Newline
         Print String ("Screwed up at line: ")
         Write (Event_Line, 0)
         Newline
      %finish
      Signal (Event_Sub << 4 + Event_Event)      { Not literal indeed ! }
   %finish

   Set Up
   %while Finished = No %cycle
      Print Article
   %repeat

   Close Down
%end
