home *** CD-ROM | disk | FTP | other *** search
/ Programmer 7500 / MAX_PROGRAMMERS.iso / CLIPPER / MISC / BIPL.ZIP / PROGS.ZIP / DETEX.ICN < prev    next >
Encoding:
Text File  |  1992-11-26  |  4.4 KB  |  170 lines

  1. ############################################################################
  2. #
  3. #    File:     detex.icn
  4. #
  5. #    Subject:  Program to strip LaTeX commands
  6. #
  7. #    Author:   Clinton L. Jeffery
  8. #
  9. #    Date:     September 8, 1990
  10. #
  11. ###########################################################################
  12. #
  13. #     Program that reads in documents written in the LaTeX typesetting
  14. #  language, and removes some of the common LaTeX commands to produce
  15. #  plain ASCII.  This program is not a full LaTeX parser, and output must
  16. #  typically be further edited by hand to produce an acceptable result.
  17.  
  18. global fin, fout, silent, date, bibliography
  19.  
  20. procedure main(args)
  21.     local cut, i, j
  22.  
  23.     initialize()
  24.     cut := ""
  25.  
  26.     if *args=0 then {
  27.     every write(\ (detex(!&input)))
  28.     } else {
  29.     i := 1
  30.     while (args[i][1] == "-") do {
  31.         case args[i] of {
  32.         "-silent": silent := 1
  33.         "-cut"   : cut := "-cut"
  34.         default  : write(&errout,"dont know option ",args[i])
  35.         }
  36.         i +:= 1
  37.     }
  38.     if /silent then write(&errout,"Detex version 1.10 executed on ",date)
  39.     if j := find(".tex",args[i]) then args[i][j:0] := ""
  40.     fin := open(args[i]||".tex","r") |
  41.         stop("detex: couldn't open ",args[i],".tex for reading")
  42.     fout := open(args[i]||".doc","w") |
  43.         stop("detex: couldn't open ",args[i],".doc for writing")
  44.     every write(fout,\ (detex(!fin)))
  45.     close(fin)
  46.     if \bibliography then {
  47.         fin := open(bibliography,"r") |
  48.         write(&errout,"detex: couldn't open ",
  49.               bibliography," for reading")
  50.         every write(fout,\ (debib(detex(!fin))))
  51.     }
  52.     close(fout)
  53.     }
  54. end
  55.  
  56. procedure initialize()
  57.     date := &dateline[find(",",&dateline)+2:0]
  58.     date := reverse(date)
  59.     date := date[find(":",date)+1:0]
  60.     date := date[many(&digits,date):0]
  61.     date := reverse(date)
  62. end
  63.  
  64. # strip comments.  so far we only strip entire-line comments
  65. procedure detex(s)
  66.     if *s>0 & s[1]=="%" then fail
  67.     return defootnote(deline(debrace(demacro(s))))
  68. end
  69.  
  70. #
  71. # remove footnotes and similar multiline entities
  72. # all footnotes are assumed to end in }. and this is processed after
  73. # all single-line entities were already processed
  74. #
  75. procedure defootnote(s)
  76.     if s == "}" then return ""
  77.     while s[find("\\footnote{",s) +: *"\\footnote{"] := " ("
  78.     while s[find("}.",s) +: *"}."] := ")."
  79.     return s
  80. end
  81. #
  82. # This routine handles macros that may appear anywhere on a line
  83. # Footnotes are translated into parentheses.
  84. # The close of all footnotes are assumed to be }.
  85. #
  86. procedure demacro(s)
  87.     local i
  88.  
  89.     while i := find("\\today",s) do {
  90.     s[i:i+*"\\today"] := date
  91.     }
  92.     while i := find("\\cite{",s) do {
  93.     s[i:i+*"\\cite{"] := "["
  94.     s[find("}",s,i)] := "]"
  95.     }
  96.     while s[find("\\linebreak",s) +: *"\\linebreak"] := ""
  97.     while s[find("\\/"|"\\\\"|"\\>",s) +: 2] := ""
  98.     while s[find("``"|"''",s) +: 2] := "\""
  99.     while s[ find("\\&"|"\\$"|"\\_",s) ] := ""
  100.     return s
  101. end
  102.  
  103. # extra help for .bbl files
  104. procedure debib(s)
  105.     local i
  106.  
  107.     while s[find("~",s)] := ""
  108.     while (i:=find("{",s))=(find("}",s)-2) do s[i+:3] := s[i+1]
  109.     while (i:=find("{",s))=(find("}",s)-3) do s[i+:4] := s[i+1]
  110.     return s
  111. end
  112.  
  113. # This procedure handles macros that comprise and entire line
  114. procedure deline(s)
  115.     local command, body
  116.  
  117.     if s[1] == "\\" then s ? {
  118.     move(1)
  119.     command := tab(many(&letters))
  120.     case command of {
  121.         "item": {
  122.         move(1) # past [
  123.         body := tab(upto(']'))
  124.         move(1)
  125.         return body || tab(0)
  126.         }
  127.         "bibitem": {
  128.         body := tab(upto(']')+1)
  129.         return body
  130.         }
  131.         "newblock": {
  132.         move(1)
  133.         body := tab(0)
  134.         return body
  135.         }
  136.         "bibliography": {
  137.         tab(upto('{')+1)
  138.         if not (body := tab(upto('}'))) then body := tab(0)
  139.         bibliography := body || ".bbl"
  140.         return trim(center("References",70))
  141.         }
  142.         "title" | "author" | "date" | "section" | "subsection" |
  143.         "subsubsection": {
  144.             tab(upto('{')+1) # handle both \section{ and \section*{
  145.             if not (body := tab(upto('}'))) then body := tab(0)
  146.             return trim(center(body,70))
  147.         }
  148.         default: return ""
  149.     }
  150.     } else
  151.     return s
  152. end
  153.  
  154. # This procedure removes braces which get inserted by common single-line
  155. # environments such as font changes
  156. procedure debrace(s)
  157.     local i, j
  158.  
  159.     s ||:= " "
  160.  
  161.     while i := find("{\\em "|"{\\tt ",s) do {
  162.     j := &null
  163.     every j := bal(&cset,'{','}',s,i,0) \ 2
  164.     if \j then {
  165.             s := s[1:i] || s[i+5:j-1] || s[j:0]
  166.     }
  167.     }
  168.     return trim(s)
  169. end
  170.