home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 35 Internet / 35-Internet.zip / dir2html.zip / dir2html.cmd < prev    next >
OS/2 REXX Batch file  |  1996-03-06  |  15KB  |  388 lines

  1. /*   Directory to HTML Convertor
  2.      Copyright (C) 1996   Helge Walter
  3.      
  4.      This program is free software; you can redistribute it and/or
  5.      modify it under the terms of the GNU General Public License
  6.      as published by the Free Software Foundation; either version 2
  7.      of the License, or any later version.
  8.      
  9.      This program is distributed in the hope that it will be useful,
  10.      but WITHOUT ANY WARRANTY; without even the implied warranty of
  11.      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12.      GNU General Public License for more details.
  13.      
  14.      You should have received a copy of the GNU General Public License
  15.      along with this program; if not, write to the Free Software
  16.      Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  17.  
  18.      Contact me at hwa@master.hrz.th-zwickau.de or
  19.                    Helge.Walter@banyan.th-zwickau.de
  20.       or via snail-mail at
  21.                    Helge Walter
  22.                    Friedrichstr. 15
  23.                    08124 Cainsdorf
  24.                    Germany
  25. */
  26.  
  27. Parse Arg CmdLine
  28. Call InitKonst
  29. If Words(CmdLine)<1 then Do
  30.    Call Intro
  31.    Call Usage
  32. end  /* Do */
  33. Else Do
  34.    Call ParseOptions
  35.    Call Intro
  36.    Call WriteHeader
  37.    Call ProcessFiles
  38.    Call WriteFooter
  39. End /* Do */
  40. Exit
  41.  
  42. InitKonst:
  43.    Version="v1.5"
  44.    OutFile=1
  45.    Subjects=2
  46.    Description=3
  47.    Server=4
  48.    LowerCase=5
  49.    Files=6
  50.    Debug=7
  51.    DirDescription=8
  52.    IndexFile=9
  53.    ResponseFile=10
  54.    Title=11
  55.    Heading=12
  56.    PreFace=13
  57.    PostScriptum=14
  58.    IndexHint=15
  59.    HTMLBody=16
  60.    CGIMode=17
  61.    Options.CGIMode=0
  62.    EinTab="ABCDEFGHIJKLMNOPQRSTUVWXYZ"
  63.    AusTab="abcdefghijklmnopqrstuvwxyz"
  64.    ResponseLevel=0
  65.    Return
  66.  
  67. Intro: Procedure Expose Version CGIMode Options.CGIMode
  68.    If \(Options.CGIMode) then Do
  69.       Say "Directory To HTML Convertor "Version", Copyright (C) 1996 Helge Walter"
  70.       Say
  71.    end  /* Do */
  72.    Return
  73.  
  74. SysFailure: Procedure
  75.    Parse Arg ErrDescr,RetCode
  76.    Say "Error "ErrDescr". System returns "RetCode
  77.    If Pos(":", RetCode) Then Parse Value RetCode With Verbal":"RC
  78.    Else RC=RetCode
  79.    Say SysGetMessage(RC)
  80.    Return
  81.  
  82. StringTranslate:
  83.    Parse Arg Kette
  84.    EndKette=""
  85.    Do While Pos("%", Kette)<>0
  86.       Posi=Pos("%", Kette)
  87.       EndKette=EndKette""Left(Kette, Posi-1)
  88.       Command=SubStr(Kette, Posi+1, 1)
  89.       Kette=SubStr(Kette, Posi+2)
  90.       Select
  91.          when Command="%" then Addi="%"
  92.          when Command="d" then Addi=Date("Normal")
  93.          when Command="t" then Addi=Time("Normal")
  94.          when Command="i" then Addi='<A HREF="'Options.Server''Options.IndexFile'">'Options.IndexFile'</A>'
  95.          when Command="v" then Addi="<EM>DIR2HTML "Version"</EM>"
  96.          when Command="f" then Addi=Options.Files
  97.       otherwise
  98.          Addi=""
  99.       end  /* select */
  100.       EndKette=EndKette""Addi
  101.    end /* do */
  102.    EndKette=EndKette""Kette
  103.    Return EndKette
  104.  
  105. AddToZip:
  106.    Parse Arg Dateiens
  107.    If Options.Debug Then Say "Adding files "Dateiens" to file "Options.IndexFile
  108.    "@Zip -k9q "Options.IndexFile" "Dateiens
  109.    Return
  110.  
  111. LoadResponse:
  112.    Parse Arg RPF
  113.    If Options.Debug then Say "Processing responsefile "RPF"..."
  114.    RC=Stream(RPF, "Command", "Open Read")
  115.    If RC<>"READY:" then Do
  116.       If Options.Debug then Call SysFailure "opening file "RPF, RC
  117.       Else Say "Error opening responsefile ... skipping"
  118.    end /* Do */
  119.    Else Do
  120.       Do While Lines(RPF)
  121.          Zeile = Strip(LineIn(RPF))
  122.          Parse Value Zeile With FOption Rest
  123.          Select
  124.             when FOption="-o" then Parse Value Rest With Options.OutFile Rest
  125.             when FOption="-s" then Options.Subjects=1
  126.             when FOption="-d" then Options.Description=1
  127.             when FOption="-p" then Parse Value Rest With Options.Server Rest
  128.             when FOption="-l" then Options.LowerCase=1
  129.             when FOption="-debug" then Options.Debug=1
  130.             when FOption="-f" then Options.DirDescription=1
  131.             when FOption="-i" then Do
  132.                Parse Value Rest With Options.IndexFile Rest
  133.                If Pos(".", FileSpec("Name", Options.IndexFile))=0 then Do
  134.                   Options.IndexFile = Options.IndexFile".zip"
  135.                end  /* Do */
  136.             end /* Do */
  137.             when FOption="-r" then Do
  138.                ResponseLevel=ResponseLevel+1
  139.                ResponseName.ResponseLevel=RPF
  140.                Call LoadResponse Rest
  141.                RPF=ResponseName.ResponseLevel
  142.                ResponseLevel=ResponseLevel-1
  143.             end  /* Do */
  144.             when FOption="-title" then Options.Title=Rest
  145.             when FOption="-heading" then Options.Heading=Rest
  146.             when FOption="-files" then Options.Files=Rest
  147.             when FOption="-index" then Options.IndexHint=Rest
  148.             when FOption="-body" then Options.HTMLBody=Rest
  149.             when FOption="-begin" then Do
  150.                Rest=Strip(Rest)
  151.                If Rest="preface" then Options.PreFace=""
  152.                else If Rest="postscriptum" then Options.PostScriptum=""
  153.                else Say "Unknown environment "Rest" ignored"
  154.                Zeile=Strip(LineIn(RPF))
  155.                Do While (Zeile<>"-end")&(Lines(RPF))
  156.                   If Rest="preface" then Options.PreFace=Options.PreFace" "Zeile
  157.                   else If Rest="postscriptum" then Options.PostScriptum=Options.PostScriptum" "Zeile
  158.                   Zeile=Strip(LineIn(RPF))
  159.                end /* do */
  160.             end  /* Do */
  161.             when Left(FOption,1)=";" then Nop
  162.          otherwise
  163.             Say "Unknown option "FOption" ignored"
  164.          end  /* select */
  165.       end /* do */
  166.       Call Stream RPF, "Command", "Close"
  167.    end  /* Do */
  168.    Return
  169.  
  170. Usage: Procedure
  171.    Say "Usage: DIR2HTML [<Options>] <Filemask>"
  172.    Say
  173.    Say "----> Options ----------------------------------------------------------------"
  174.    Say " -s             use filedescription stored in .SUBJECT EA"
  175.    Say " -d             use filedescription stored in DESCRIPT.ION file"
  176.    Say " -f             use FILE_ID.DIZ to describe directory"
  177.    Say " -o <filename>  produce the file <filename> instead of the default index.htm"
  178.    Say " -p <server>    use fully qualified path to server <server>. Normally DIR2HTML"
  179.    Say "                stores only the filename in the created anchors. But:"
  180.    Say "                DIR2HTLM -p http://www.th-zwickau.de/~hwa/midis *.mid"
  181.    Say "                produces anchors like this:"
  182.    Say '                <a href="http://www.th-zwickau.de/~hwa/midis/rebyell.mid">'
  183.    Say " -l             convert filenames to lower case. Usefull if you create the"
  184.    Say "                index-file on a Os/2-FAT system but your www-server is a unix-"
  185.    Say "                machine with lower case filenames."
  186.    Say " -i <filename>  creates a Info-Zip file containing all indexed files"
  187.    Say " -r <filename>  uses a response file instead of command line options, herewith"
  188.    Say "                you can create language specific documents"
  189.    Say " -cgi           use the special WWW mode. So you can use DIR2HTML as a cgi-bin"
  190.    Say "                It uses dir2html.rc as response file"
  191.    Say " -debug         gives a verbose description of actions done by DIR2HTML"
  192.    Say "*** Press Enter to continue ***"
  193.    Pull EnterKey
  194.    Say
  195.    Say "DIR2HTML comes with ABSOLUTELY NO WARRANTY.  This is free software,"
  196.    Say "and you are welcome to redistribute it under certain conditions; read"
  197.    Say "the file COPYING for details."
  198.    Return
  199.  
  200. ParseOptions:
  201.    Options.OutFile="index.htm"
  202.    Options.Subjects=0
  203.    Options.Description=0
  204.    Options.Server=""
  205.    Options.LowerCase=0
  206.    Options.Files="*"
  207.    Options.Debug=0
  208.    Options.DirDescription=0
  209.    Options.IndexFile=""
  210.    Options.ResponseFile=""
  211.    Options.Title="Listing Of Files %f"
  212.    Options.Heading=Options.Title
  213.    Options.PreFace=""
  214.    Options.PostScriptum="Last updated %d %t"
  215.    Options.IndexHint="To download a collection of all indexed files try %i."
  216.    Options.HTMLBody=""
  217.    Options.CGIMode=0
  218.    Flag=1
  219.    Do While Flag
  220.       Parse Value CmdLine With Option CmdLine
  221.       Select
  222.          when Option="-o" then Do
  223.             Parse Value CmdLine With Options.OutFile CmdLine
  224.             If Options.CGIMode then Options.OutFile=""
  225.          end  /* Do */
  226.          when Option="-s" then Options.Subjects=1
  227.          when Option="-d" then Options.Description=1
  228.          when Option="-p" then Parse Value CmdLine With Options.Server CmdLine
  229.          when Option="-l" then Options.LowerCase=1
  230.          when Option="-debug" then Options.Debug=1
  231.          when Option="-f" then Options.DirDescription=1
  232.          when Option="-cgi" then Do
  233.             Options.CGIMode=1
  234.             Options.OutFile=""
  235.          end  /* Do */
  236.          when Option="-i" then Do
  237.             Parse Value CmdLine With Options.IndexFile CmdLine
  238.             If Pos(".", FileSpec(Options.IndexFile))=0 then Do
  239.                Options.IndexFile = Options.IndexFile".zip"
  240.             end  /* Do */
  241.          end /* Do */
  242.          when Option="-r" then Do
  243.             Parse Value CmdLine With Options.ResponseFile CmdLine
  244.             Call LoadResponse Options.ResponseFile
  245.          end /* Do */
  246.       otherwise
  247.          If SubStr(Option, 1, 1)="-" then Say "Unknown option "Option" ignored"
  248.          else Do
  249.             if Option<>"" then Options.Files=Option" "CmdLine
  250.             Flag=0
  251.          end  /* Do */
  252.       end  /* select */
  253.    end /* do */
  254.    If Options.Debug then Do
  255.       Say "DIR2HTML produces file "Options.OutFile" while processing "Options.Files
  256.       Say "Subjects processing is "Options.Subjects
  257.       Say "Description processing is "Options.Description
  258.       Say "Describe directory is "Options.DirDescription
  259.       Say "Serverpath is '"Options.Server"'"
  260.       Say "Lowercase conversion is "Options.LowerCase
  261.       Say "Using indexfile '"Options.IndexFile"'"
  262.       Say "Using responsefile '"Options.ResponseFile"'"
  263.    end  /* Do */
  264.    If Options.Debug Then Say "Loading RexxUtil..."
  265.    /* Load RexxUtil.DLL */
  266.    CALL RxFuncAdd 'SysLoadFuncs','Rexxutil','SysLoadFuncs'
  267.    CALL SysLoadFuncs
  268. Return
  269.  
  270. WriteHeader:
  271.    If \(Options.CGIMode) then Do
  272.       If Options.Debug then Say "Deleting file "Options.OutFile" if existing..."
  273.       Call SysFileDelete Options.OutFile
  274.    end  /* Do */
  275.    If Options.IndexFile<>"" then Do
  276.       If Options.Debug then Say "Deleting file "Options.IndexFile" if existing..."
  277.       Call SysFileDelete Options.IndexFile
  278.    end  /* Do */
  279.    If \Options.CGIMode then Do
  280.       If Options.Debug then Say "Opening file "Options.OutFile
  281.       RC=Stream(Options.OutFile, "Command", "Open Write");
  282.       If RC<>"READY:" then Do
  283.          If Options.Debug then Call SysFailure "opening file "Options.OutFile,RC
  284.          else Say "Cannot open file "Options.OutFile
  285.          Exit
  286.       end  /* Do */
  287.    end  /* Do */
  288.    If Options.Debug then Say "Writing HTML-Header..."
  289.    Call LineOut Options.OutFile, "<HTML>"
  290.    Call LineOut Options.OutFile, "<TITLE>"StringTranslate(Options.Title)"</TITLE>"
  291.    If Options.HTMLBody="" then Call LineOut Options.OutFile, "<BODY>"
  292.    else Call LineOut Options.OutFile, "<BODY "Options.HTMLBody">"
  293.    Call LineOut Options.OutFile, "<H1>"StringTranslate(Options.Heading)"</H1>"
  294.    If Options.PreFace<>"" then Call LineOut Options.OutFile, StringTranslate(Options.PreFace)"<BR>"
  295.    If Options.DirDescription then Do
  296.       If Options.Debug then Say "Inserting FILE_ID.DIZ..."
  297.       RC=Stream("file_id.diz", "Command", "Open Read");
  298.       If RC<>"READY:" then Do
  299.          If Options.Debug then Do
  300.             Call SysFailure "opening file",RC
  301.             Say "Skipping directory description..."
  302.          end  /* Do */
  303.       End /* Do */
  304.       else Do
  305.          Call LineOut Options.OutFile, "<PRE>"
  306.          Do While Lines("file_id.diz")
  307.             Call LineOut Options.OutFile, LineIn("file_id.diz")
  308.          End /* Do */
  309.          Call LineOut Options.OutFile, "</PRE>"
  310.       end  /* Do */
  311.       RC=Stream("file_id.diz", "Command", "Close")
  312.       Call AddToZip "file_id.diz"
  313.    end  /* Do */
  314.    Call LineOut Options.OutFile, "<HR>"
  315.    Call LineOut Options.OutFile, "<UL>"
  316.    If Options.Description then Call AddToZip "descript.ion"
  317. Return
  318.  
  319. WriteFooter:
  320.    If Options.Debug then Say "Writing HTML-Footer..."
  321.    Call LineOut Options.OutFile, "</UL>"
  322.    If Options.IndexFile<>"" then Call LineOut Options.OutFile, StringTranslate(Options.IndexHint)
  323.    Call LineOut Options.OutFile, "<HR>"
  324.    If Options.PostScriptum<>"" Then Call LineOut Options.OutFile, StringTranslate(Options.PostScriptum)
  325.    Call LineOut Options.OutFile, "<HR>This page was automagically created by <EM>DIR2HTML "Version"</EM> written by Helge Walter"
  326.    If \Options.CGIMode then Do
  327.       If Options.Debug then Say "Closing file "Options.OutFile"..."
  328.       RC=Stream(Options.OutFile, "Command", "Close");
  329.    end  /* Do */
  330. Return
  331.  
  332. ProcessFiles:
  333.    Do Until Words(Options.Files)=0
  334.       Parse Value Options.Files With Current Options.Files
  335.       If Options.Debug then Say "Retrieving directory listing matching "Current"..."
  336.       RC=SysFileTree(Current, Dateien, "F");
  337.       If RC<>0 then Do
  338.          If Options.Debug then Call SysFailure "retrieving dirlist",RC
  339.          else Say "Error retrieving directory listing."
  340.       end  /* Do */
  341.       else Do Counter=1 TO Dateien.0
  342.          Call ProcessFile Dateien.Counter
  343.       end  /* Do */
  344.       If Options.IndexFile<>"" Then Call AddToZip Current
  345.    end /* Do */
  346. Return
  347.  
  348. ProcessFile:
  349.    Parse Arg Datei
  350.    Descript = ""
  351.    Parse Value Datei With DDate DTime DSize DAttr DUName
  352.    DUName = Strip(DUName)
  353.    DName = FileSpec("Name", DUName)
  354.    If Options.Debug then Say "Processing file "DUName"..."
  355.    If Options.Subjects then Do
  356.       If Options.Debug then Say "Reading Subject..."
  357.       RC=SysGetEA(DUName, ".SUBJECT", "DESCRIPT")
  358.       Parse Var Descript 5 Descript
  359.       If Options.Debug then Say "Evaluated subject is "Descript
  360.       If RC<>0 then Do
  361.          If Options.Debug then Call SysFailure "reading subject",RC
  362.          Descript=""
  363.       end  /* Do */
  364.    end  /* Do */
  365.    If Options.Description then Do
  366.       If Options.Debug then Say "Reading DESCRIPT.ION..."
  367.       RC=SysFileSearch(DName, "descript.ion", Zeilen)
  368.       If RC<>0 then Do
  369.          If Options.Debug then Call SysFailure "searching thru DESCRIPT.ION",RC
  370.       end  /* Do */
  371.       else Do
  372.          If Zeilen.0=0 then Do
  373.             If Options.Debug then Say "Unable to find "DName" in DESCRIPT.ION. Skipping..."
  374.          end  /* Do */
  375.          else Do
  376.             Descript=SubWord(Zeilen.1, 2)
  377.          end  /* Do */
  378.       end  /* Do */
  379.    end  /* Do */
  380.    If Options.LowerCase then DName=Translate(DName, AusTab, EinTab)
  381.    If Descript="" then Descript=DName
  382.    Zeile='<LI><A HREF="'Options.Server''DName'">'Descript'</A> ('DSize' Bytes)'
  383.    Call LineOut Options.OutFile, Zeile
  384.    Drop Descript
  385.    Drop Zeilen.
  386. Return
  387. 
  388.