! Program to convert a View source file into a TeX source
! RMM 16/5/82
! Modded by JHB Sep 84
! Transfer from Layout to Tex in progress Jan 1989

!!%include "inc:util.imp"
!--
external predicate spec  EXISTS(string (255)file)
{apm} system string (8)fn spec  DATE
{apm} system string (5)fn spec  TIME
!--
begin 

const  integer  TT = 0;                  ! terminal I/O stream
const  integer  source = 1
const  integer  destination = 1
const  integer  stream error = 9;        ! event signalled on trouble

string  (255) input file,output file,a,b

integer  lines = 0
owninteger  blank=0 ;!Count of how many consecutive blank lines we've had
owninteger  sectionnos=0 ;!Flag to catch text before the first section.
                         ;!We insert  special section "Preamble".
owninteger  ttmode=0

string  (255) current line

! section stack

const  integer  max depth = 10

integer  array  sect no (1 : max depth);         ! section numbers
integer  TOS = 0;                                ! top of section stack

!
! SET DEFAULT
!

constinteger  nonfatal=0, fatal=1

routine  readline(string  (*) name  s)
   byte  c
   s=""
   cycle ; readsymbol(c); exit  if  c<' '; s=s.tostring(c); repeat 
end 

routine  printline(string  (255) s)
   printstring(s); newline
end 

routine  error(string  (255) text, integer  what)
   printline(text)
   stop  if  what = fatal
end 

routine  set default(string  (*) name  file, string  (7) extension)
   if  exists(file.extension) then  file = file.extension
end 


!
! PRINT MODDED
! Quotes special characters and converts runs of spaces to HSPACE.
! Note trailing spaces are discarded as a side-effect
! Spaces at start of line will usually indicate some specific formatting
! so force a line break.
!

routine  print modded (string  (255) line)
   integer  n,c,lastc, spcs
   predicate  all white(string  (*) name  s)
      integer  i
      true  if  s=""
      for  i=1,1,length(s) cycle 
         false  if  charno(s, i) # ' '
      repeat 
      true 
   end 

   printstring(line) and  return  if  ttmode#0
   if  all white(line) then  blank=blank+1 and  return 
   printline("¬section{Preamble}") and  sectionnos=1 and  c 
   blank=1 if  sectionnos=0
   spcs=0
   for  n = 1, 1, length (line) cycle 
      c = charno(line, n)

      if  c # ' ' and  spcs#0 start 
        if  spcs <= 2 start 
           space
        else 
           if  spcs=n-1 start  ;!Line is blank from start
              printstring("¬¬ ") if  blank=0
              printstring("{¬hspace*{")
           else 
              printstring(" {¬hspace{")
           finish 
           print(spcs/15,0,1); printstring(" in}} ")
        finish 
        spcs = 0
      finish 

      if  c = '$' or   c = '&' or  c = '%' or   c = '#' or  c 
      c = '_' or   c = '{' or  c = '}' start 
         printsymbol('¬')
         print symbol (c)
      elseif  c = '^'
         printstring("$¬hat{ }${}")
      elseif  c = '<'
         printstring("$<$")
      elseif  c = '¬'
         printstring("$¬backslash$")
      elseif  c = '>'
         printstring("$>$")
      elseif  c = ' ' and  ttmode=0
         spcs=spcs+1
      else 
         printsymbol(c)
      finish 
   repeat 
   blank=0
end ; ! of print modded

! +----------+
! |          |
! | GET LINE |
! |          |
! +----------+

routine  get line
   lines = lines + 1
   read line (current line)
end ; ! of get line

! +------------+
! |            |
! | INITIALISE |
! |            |
! +------------+

routine  initialise

   string  (255) param,left,right,title,author,title line
   integer  xx,yy

   on  stream error start 
      select output (TT)
      error("Fails: ".event_message, fatal)
   finish 

   input file = cli param
   output file = input file unless  input file -> input file.("/").output file
   if  output file -> output file.(".").b start ; finish 
   output file = output file.".tex"
   

   output file = input file if  output file = ""
   input file = "view:".input file unless  exists(input file)
   open input (source, input file)
   select input (source)

   set default (output file, ".tex")
   printline("Converting ".input file." to ".output file)
   open output (destination, output file)  
   select output (destination)

   ! some preliminary TeX options
   get line until  current line # ""; !title line
   !This should be a comment !{mmmmmmm} but it might be
   !                         !mmmmmmmm
   xx = 1
   xx=xx+1 if  charno(current line,1) = '!'
   xx=xx+1 if  charno(current line,2) = '{'
   yy = length(current line)
   yy=yy-1 if  charno(current line, length(current line)) = '}'
   title line = substring(current line,xx, yy)
   title=title line and  author = "APM Manual pages" unless  c 
   title line -> title.("/").author

   printline("¬documentstyle[a4,12pt]{article}")
   printline("¬begin{document}")
   printline("¬author{".author."}")
   printline("¬title{".title."}")
   printline("¬maketitle")
   printline("¬parskip .1 in")
   printline("¬setcounter{secnumdepth}{10}")
   printline("¬parindent 0in")

   ! initialise stack
   sect no (1) = 0
end ; ! of intialise

! +--------------+
! |              |
! | PROCESS LINE |
! |              |
! +--------------+

routine  process line

   byte  name  ch
   string  (255) s

   !
   ! CHECKS
   !

   predicate  checks (string  (15) c)
      true  if  sub string (current line, 1, length (c) + 1) = "!" . c
      false 
   end ; ! of checks

   !
   ! MAIN CODE OF PROCESS LINE
   !

   if  current line="" start 
      newline; blank=1
      return 
   finish 
   ch == char no (current line, 1)
   if  ch = '!' start 
      ! may be a command line
      ch == char no (current line, 2)
      begin 

         integer  n
         switch  ctype (0 : '~')

         -> ctype (ch)

      ctype ('<'): ! start of section
         ch == char no (current line, 3)
         if  ch = '>' then  c 
            error("Fails: Subsiduary files not implemented", fatal)
         TOS = TOS + 1;                         ! a new section
         if  TOS = max depth then  c 
            error("Fails: Nesting too deep", fatal)
         sect no (TOS) = sect no (TOS) + 1
         sect no (TOS + 1) = 0
         sectionnos=1
         if  TOS = 1 start 
            print string ("¬section{")
         elseif  TOS = 2
            printstring("¬subsection{")
         else 
            printstring("¬subsubsection{")
         finish 
         print modded (sub string (current line, 3, length (current line)))
         printstring("}"); newline
         blank=1
         return 

      ctype ('>'): ! end of section
         if  TOS = 0 then  c 
            error("Fails: Unbalanced brackets", fatal)
         TOS = TOS - 1;                         ! pop section stack
         newline
         return 

      ctype ('T'): ! TITLE
         -> fall out unless  checks ("TITLE")
         if  lines > 1 then  c 
            error("Fails: TITLE out of context", fatal)
         print string ("¬section{")
         sectionnos=1
         print modded (sub string (current line, 8, length (current line)))
         printstring("}"); newline
         blank=1
         return 

      ctype ('{'): ! Comment
         s = sub string(current line, 3, length(current line)-1)
         if  charno(s,1) = '¬' start  ;!Embedded TeX Command
            if  s = "¬tt" then  printstring("¬small")
            if  s = "¬rm" then  printstring("¬end{verbatim}")
            print string(s); space
            if  s = "¬tt" then  printstring("¬begin{verbatim}")
            if  s = "¬tt" then  ttmode=1
            if  s = "¬rm" then  ttmode=0
            if  s = "¬rm" then  printstring(" ¬normalsize ")
         finish 
         return 

      ctype ('P'): ! PAGE
         -> fall out unless  checks ("PAGE")
!         newline;                             ! maybe
         return 

      ctype ('S'): ! STOP
         if  checks ("STOP") start 
            printstring("¬vspace(.75in} ".input file." printed on ".date." at ".time)
            printline("¬newpage")
            printline("¬tableofcontents")
            newline; print string ("¬end{document}")
            newline
            stop 
         else  if  checks ("SKIP")
            error("Fails: SKIP not implemented", fatal)
         finish 
!         -> fall out
      
      fall out:
      ctype (*):   ! anything else
         print modded (sub string (current line, 2, length (current line)))
         newline
      end 
   else 
      print modded (current line)
      newline
   finish 
end ; ! of process line

! +------------+
! |            |
! | EVENT TRAP |
! |            |
! +------------+

on  stream error start 
   if  event_sub = 3 start 
      select output (TT)
      print string ("Fails: ")
      print string (event_message)
      newline
   else 
      printstring("¬vspace{.75in} ".input file." printed on ".date." at ".time); newline
      newline
      printline("¬newpage")
      printline("¬tableofcontents")
      print string ("¬end{document}"); newline
   finish 
   stop 
finish 

! +-----------+
! |           |
! | MAIN CODE |
! |           |
! +-----------+

initialise
cycle 
   get line
   process line
repeat    

end 
end  of  file