Many changes to make output test really useful. Fix Windows makefile. Add some sample test input files

git-svn-id: svn://10.0.0.236/trunk@49318 18797224-902f-48f8-a5cc-f745e15eee43
This commit is contained in:
akkana%netscape.com
1999-09-29 20:11:07 +00:00
parent 8e3610bfe2
commit ca522033c5
12 changed files with 486 additions and 54 deletions

View File

@@ -20,12 +20,15 @@
* Contributor(s): Akkana Peck.
*/
#include <ctype.h> // for isdigit()
#include "nsParserCIID.h"
#include "nsIParser.h"
#include "nsHTMLContentSinkStream.h"
#include "nsHTMLToTXTSinkStream.h"
#include "nsIComponentManager.h"
#include "CNavDTD.h"
#include "nsXIFDTD.h"
extern "C" void NS_SetupRegistry();
@@ -44,27 +47,26 @@ static NS_DEFINE_IID(kParserCID, NS_PARSER_IID);
// Interface IID's
static NS_DEFINE_IID(kIParserIID, NS_IPARSER_IID);
nsresult
Compare(nsString& str, nsString& filename)
{
printf("Sorry, don't know how to compare yet\n");
char* charstar = str.ToNewUTF8String();
printf("Output string is: %s\n-------------------- \n", charstar);
delete[] charstar;
return NS_ERROR_NOT_IMPLEMENTED;
}
//----------------------------------------------------------------------
// Convert html on stdin to either plaintext or (if toHTML) html
//----------------------------------------------------------------------
nsresult HTML2text(int toHTML)
nsresult
HTML2text(nsString& inString, nsString& inType, nsString& outType, int wrapCol, nsString& compareAgainst)
{
nsresult rv = NS_OK;
nsString inString;
nsString outString;
// Read in the string from the file: very inefficient, but who cares?
char c;
while ((c = getchar()) != EOF)
inString += c;
#if 0
printf("Input string is: %s\n-------------------- \n",
inString.ToNewCString());
printf("------------------------------------\n");
#endif
// Create a parser
nsIParser* parser;
rv = nsComponentManager::CreateInstance(kParserCID, nsnull,
@@ -78,11 +80,11 @@ nsresult HTML2text(int toHTML)
nsIHTMLContentSink* sink = nsnull;
// Create the appropriate output sink
if (toHTML)
if (outType == "text/html")
rv = NS_New_HTML_ContentSinkStream(&sink, &outString, 0);
else // default to plaintext
rv = NS_New_HTMLToTXT_SinkStream(&sink, &outString, 72, 2);
rv = NS_New_HTMLToTXT_SinkStream(&sink, &outString, wrapCol, 2);
if (NS_FAILED(rv))
{
@@ -92,7 +94,10 @@ nsresult HTML2text(int toHTML)
parser->SetContentSink(sink);
nsIDTD* dtd = nsnull;
rv = NS_NewNavHTMLDTD(&dtd);
if (inType == "text/xif")
rv = NS_NewXIFDTD(&dtd);
else
rv = NS_NewNavHTMLDTD(&dtd);
if (NS_FAILED(rv))
{
printf("Couldn't create new HTML DTD: 0x%x\n", rv);
@@ -101,32 +106,138 @@ nsresult HTML2text(int toHTML)
parser->RegisterDTD(dtd);
rv = parser->Parse(inString, 0, "text/html", PR_FALSE, PR_TRUE);
char* inTypeStr = inType.ToNewCString();
rv = parser->Parse(inString, 0, inTypeStr, PR_FALSE, PR_TRUE);
delete[] inTypeStr;
if (NS_FAILED(rv))
{
printf("Parse() failed! 0x%x\n", rv);
return rv;
}
printf("Output string is: %s\n-------------------- \n",
outString.ToNewCString());
NS_IF_RELEASE(dtd);
NS_IF_RELEASE(sink);
NS_RELEASE(parser);
return rv;
if (compareAgainst.Length() > 0)
return Compare(outString, compareAgainst);
char* charstar = outString.ToNewUTF8String();
printf("Output string is:\n--------------------\n%s\n--------------------\n",
charstar);
delete[] charstar;
return NS_OK;
}
//----------------------------------------------------------------------
int main(int argc, char** argv)
{
nsString inType ("text/html");
nsString outType ("text/plain");
int wrapCol = 72;
nsString compareAgainst;
// Skip over progname arg:
const char* progname = argv[0];
--argc; ++argv;
// Process flags
while (argc > 0 && argv[0][0] == '-')
{
switch (argv[0][1])
{
case 'h':
printf("\
Usage: %s [-i intype] [-o outtype] [-w wrapcol] [-c comparison_file] infile\n\
\tIn/out types are mime types (e.g. text/html)\n\
\tcomparison_file is a file against which to compare the output\n\
\t (not yet implemented\n\
\tDefaults are -i text/html -o text/plain -w 72 [stdin]\n",
progname);
exit(0);
case 'i':
if (argv[0][2] != '\0')
inType = argv[0]+2;
else {
inType = argv[1];
--argc;
++argv;
}
break;
case 'o':
if (argv[0][2] != '\0')
outType = argv[0]+2;
else {
outType = argv[1];
--argc;
++argv;
}
break;
case 'w':
if (isdigit(argv[0][2]))
wrapCol = atoi(argv[0]+2);
else {
wrapCol = atoi(argv[1]);
--argc;
++argv;
}
break;
case 'c':
if (argv[0][2] != '\0')
compareAgainst = argv[0]+2;
else {
compareAgainst = argv[1];
--argc;
++argv;
}
break;
}
++argv;
--argc;
}
FILE* file = 0;
if (argc > 0) // read from a file
{
// Open the file in a Unix-centric way,
// until I find out how to use nsFileSpec:
file = fopen(argv[0], "r");
if (!file)
{
fprintf(stderr, "Can't open file %s", argv[0]);
perror(" ");
exit(1);
}
}
else file = stdin;
nsComponentManager::AutoRegister(nsIComponentManager::NS_Startup, 0);
NS_SetupRegistry();
if (argc > 1 && argv[1][0] == '-' && argv[1][1] == 'h')
HTML2text(1);
else
HTML2text(0);
// Read in the string: very inefficient, but who cares?
nsString inString;
char c;
while ((c = getc(file)) != EOF)
inString += c;
if (file != stdin)
fclose(file);
#if 0
printf("Input string is: %s\n-------------------- \n",
inString.ToNewCString());
printf("------------------------------------\n");
#endif
printf("inType = '%s', outType = '%s'\n", inType.ToNewCString(), outType.ToNewCString());
HTML2text(inString, inType, outType, wrapCol, compareAgainst);
return 0;
}