home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / cenvi23.zip / HTMLXLAT.CMM < prev    next >
Text File  |  1995-12-13  |  5KB  |  195 lines

  1. // HTMLxlat.cmm - CEnvi tool to convert an HTML document into
  2. // ver.1          the same document as produced by CMMCGI
  3. //                using the TINYHTML.LIB library.
  4.  
  5. Instructions()
  6. {
  7.    puts("\nHTMLxlat - Translate HTML document into form creatable with CMMCGI\a")
  8.    puts("")
  9.    puts("USAGE: CEnvi HTMLxlat.cmm <HTMLinput> <cmmOutput>")
  10.    puts("WHERE: HTMLinput - Name of existing HTML document")
  11.    puts("       cmmOutput - Path for new Cmm document")
  12.    puts("")    
  13.    puts("EXAMPLE: CEnviW HTMLxlat.cmm HANGMAN.HTM HANGMAN.CMM")
  14.    puts("")
  15.    exit(EXIT_FAILURE);
  16. }
  17.  
  18. Error(FormatS)
  19. {
  20.    va_start(valist,FormatS);
  21.    vprintf(FormatS,valist);
  22.    va_end(valist);
  23.    printf("\a\n");
  24.    exit(EXIT_FAILURE);
  25. }
  26.  
  27. main(argc,argv)
  28. {
  29.    if ( 3 != argc )
  30.       Instructions();
  31.  
  32.    source = ReadInputFile(argv[1]);
  33.    WriteOutputFile(argv[2],source);
  34.    return EXIT_SUCCESS;
  35. }
  36.  
  37. ReadInputFile(FileSpec) // read input into huge buffer
  38. {
  39.    if ( !(fp = fopen(FileSpec,"rb")) )
  40.       Error("Could not read input file \"%s\"",FileSpec);
  41.    fseek(fp,0,SEEK_END);
  42.    len = ftell(fp);
  43.    fseek(fp,0,SEEK_SET);
  44.    fread(ret,len,fp);
  45.    fclose(fp);
  46.    return ret;
  47. }
  48.  
  49. WriteOutputFile(FileSpec,src)
  50. {
  51.    if ( !(fp = fopen(FileSpec,"wt")) )
  52.       Error("Could not create output file \"%s\"",FileSpec);
  53.    // always start with the same lines
  54.    fprintf(fp,"//%s - CMMCGI output file created by HTMLXLAT\n\n",FileSpec);
  55.    fprintf(fp,"#include <tinyhtml.lib>\n\n");
  56.  
  57.    // read in source up to <HTML>
  58.    if ( !(html = strstr(src,"<HTML")) )
  59.       Error("Couldn't find beginning \"<HTML\" tag in %s.",FileSpec);
  60.  
  61.    // write plain text beginning before the HTML section
  62.    fprintf(fp,"CGIOut(\"");
  63.    WriteDoubleQuotedText(fp,src,html-src);
  64.    fprintf(fp,"\");\n\n");
  65.    
  66.    // start the CGIOut() statement
  67.    fprintf(fp,"CGIOut(\n");
  68.    
  69.    // start with this tag, and all that follow
  70.    WriteTag(fp,1,html);
  71.    
  72.    // finally end the CGIOut() statement
  73.    fprintf(fp,"\n);");
  74.    
  75.    fclose(fp);
  76. }
  77.  
  78. SkipWhiteSpace(text) // remove whitespace from beginning and end
  79. {
  80.    while ( text[0] && strchr(" \r\n\t",text[0]) )
  81.       text++;
  82.    // remove whitespace from end
  83.    len = strlen(text);
  84.    while( len  &&  strchr(" \r\n\t",text[len-1]) )
  85.       len--;
  86.    text[len] = 0;
  87. }
  88.  
  89. EscapeChars = "\\\r\n\t\'\"\a\b";
  90. EscapeXlat  = `\\\r\n\t\'\"\a\b`;
  91.  
  92. IndentLevel(fp,level)
  93. {
  94.    fprintf(fp,"%*s",4*level,"");
  95. }
  96.  
  97. WriteDoubleQuotedText(fp,src,len) // output each character, in escape-sequence if needed
  98. {
  99.    memcpy(s,src,len);
  100.    do {   
  101.       seq = s + strcspn(s,EscapeChars);
  102.       fwrite(s,seq-s,fp);
  103.       if ( seq[0] ) {
  104.          // write out esapce sequence
  105.          fprintf(fp,"\\%c",EscapeXlat[1 + 2 * (strchr(EscapeChars,seq[0])-EscapeChars)]);
  106.          s = seq + 1;
  107.       }
  108.    } while ( seq[0] );
  109. }
  110.  
  111. WriteTextString(fp,text,len)
  112. {
  113.    memcpy(s,text,len);
  114.    if ( !strchr(s,'`') )
  115.       fprintf(fp,"`%s`",s);
  116.    else {
  117.       fprintf(fp,"\"");
  118.       WriteDoubleQuotedText(fp,s,len);
  119.       fprintf(fp,"\"");
  120.    }
  121. }
  122.  
  123. WriteTag(fp,level,src)
  124. {
  125.    assert( '<' == (s = src)[0] );
  126.    s++;   
  127.    // get the name of this tag
  128.    len = strcspn(s," >");
  129.    end = s[len];
  130.    if ( len == 0  ||  !strchr(" >",end) )
  131.       Error("No tag found at \"%.30s\"",src);
  132.    memcpy(TagName,s,len);
  133.    
  134.    IndentLevel(fp,level);
  135.    
  136.    // find the end tag
  137.    sprintf(EndTagTemplate,"</%s>",TagName);
  138.    EndTag = strstr(s,EndTagTemplate);
  139.  
  140.    // if not end tag and no attributes, just print the text
  141.    if ( !EndTag  &&  end == '>' ) {
  142.       fprintf(fp,"\"<%s>\"",TagName);
  143.       src = s + len + 1 ;
  144.    } else {
  145.       // this tag has attributes and/or an EndTag
  146.       fprintf(fp,"tag(\"%s\"",TagName);
  147.       if ( end == '>' ) {
  148.          // no attributes
  149.          fprintf(fp,EndTag ? ",NULL" : ")" );
  150.          s += len + 1;
  151.       } else {
  152.          // append the attributes
  153.          EndBrace = strchr(s+len,'>');
  154.          if ( !EndBrace )
  155.             Error("No end for tag at \"%.30s\"",src);
  156.          fprintf(fp,",");
  157.          WriteTextString(fp,s+len+1,EndBrace-(s+len+1));
  158.          if ( !EndTag )
  159.             fprintf(fp,")");
  160.          s = EndBrace + 1;
  161.       }
  162.  
  163.       if ( !EndTag ) {
  164.          src = s;
  165.       } else {
  166.          src = EndTag + strlen(EndTagTemplate);
  167.          EndTag[0] = 0;
  168.          // append each line read
  169.          SkipWhiteSpace(s);
  170.          while( s[0] ) {
  171.             if ( s[0] == '<' ) {
  172.                fprintf(fp,",\n");
  173.                WriteTag(fp,level+1,s);
  174.             } else {
  175.                // write plain text
  176.                len = strcspn(s,"\r\n<");
  177.                memcpy(text,s,len);
  178.                s += len;
  179.                SkipWhiteSpace(text);
  180.                if ( text[0] ) {
  181.                   fprintf(fp,",\n");
  182.                   IndentLevel(fp,level+1);
  183.                   WriteTextString(fp,text,len);
  184.                }
  185.             }
  186.             SkipWhiteSpace(s);
  187.          }
  188.          fprintf(fp,"\n");
  189.          IndentLevel(fp,level);
  190.          fprintf(fp,")");
  191.       }
  192.    }
  193.    SkipWhiteSpace(src);
  194. }
  195.