1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
* ***** BEGIN LICENSE BLOCK *****
* Version: MPL 1.1/GPL 2.0/LGPL 2.1
*
* The contents of this file are subject to the Mozilla Public License Version
* 1.1 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
* http://www.mozilla.org/MPL/
*
* Software distributed under the License is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
* for the specific language governing rights and limitations under the
* License.
*
* The Original Code is Mozilla Communicator client code, released
* March 31, 1998.
*
* The Initial Developer of the Original Code is
* Netscape Communications Corporation.
* Portions created by the Initial Developer are Copyright (C) 1998-1999
* the Initial Developer. All Rights Reserved.
*
* Contributor(s):
* Akkana Peck.
*
* Alternatively, the contents of this file may be used under the terms of
* either of the GNU General Public License Version 2 or later (the "GPL"),
* or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
* in which case the provisions of the GPL or the LGPL are applicable instead
* of those above. If you wish to allow use of your version of this file only
* under the terms of either the GPL or the LGPL, and not to allow others to
* use your version of this file under the terms of the MPL, indicate your
* decision by deleting the provisions above and replace them with the notice
* and other provisions required by the GPL or the LGPL. If you do not delete
* the provisions above, a recipient may use your version of this file under
* the terms of any one of the MPL, the GPL or the LGPL.
*
* ***** END LICENSE BLOCK ***** */
#include <ctype.h> // for isdigit()
#include "nsXPCOM.h"
#include "nsParserCIID.h"
#include "nsIParser.h"
#include "nsIHTMLContentSink.h"
#include "nsIContentSerializer.h"
#include "nsLayoutCID.h"
#include "nsIHTMLToTextSink.h"
#include "nsIComponentManager.h"
#include "nsIServiceManager.h"
#include "nsIComponentRegistrar.h"
#include "nsReadableUtils.h"
#include "nsCRT.h"
static NS_DEFINE_IID(kIParserIID, NS_IPARSER_IID);
static NS_DEFINE_CID(kParserCID, NS_PARSER_CID);
int
Compare(nsString& str, nsString& aFileName)
{
// Open the file in a Unix-centric way,
// until I find out how to use nsFileSpec:
char* filename = ToNewCString(aFileName);
FILE* file = fopen(filename, "r");
if (!file)
{
fprintf(stderr, "Can't open file %s", filename);
perror(" ");
delete[] filename;
return 2;
}
delete[] filename;
// Inefficiently read from the file:
nsString inString;
int c;
int index = 0;
int different = 0;
while ((c = getc(file)) != EOF)
{
inString.Append(PRUnichar(c));
// CVS isn't doing newline comparisons on these files for some reason.
// So compensate for possible newline problems in the CVS file:
if (c == '\n' && str[index] == '\r')
++index;
if (c != str[index++])
{
//printf("Comparison failed at char %d: generated was %d, file had %d\n",
// index, (int)str[index-1], (int)c);
different = index;
break;
}
}
if (file != stdin)
fclose(file);
if (!different)
return 0;
else
{
nsAutoString left;
str.Left(left, different);
char* cstr = ToNewUTF8String(left);
printf("Comparison failed at char %d:\n-----\n%s\n-----\n",
different, cstr);
Recycle(cstr);
return 1;
}
}
//----------------------------------------------------------------------
// Convert html on stdin to either plaintext or (if toHTML) html
//----------------------------------------------------------------------
nsresult
HTML2text(nsString& inString, nsString& inType, nsString& outType,
int flags, int wrapCol, nsString& compareAgainst)
{
nsresult rv = NS_OK;
nsString outString;
// Create a parser
nsIParser* parser;
rv = CallCreateInstance(kParserCID, &parser);
if (NS_FAILED(rv))
{
printf("Unable to create a parser : 0x%x\n", rv);
return NS_ERROR_FAILURE;
}
// Create the appropriate output sink
#ifdef USE_SERIALIZER
nsCAutoString progId(NS_CONTENTSERIALIZER_CONTRACTID_PREFIX);
progId.AppendWithConversion(outType);
// The syntax used here doesn't work
nsCOMPtr<nsIContentSerializer> mSerializer;
mSerializer = do_CreateInstance(NS_STATIC_CAST(const char *, progId));
NS_ENSURE_TRUE(mSerializer, NS_ERROR_NOT_IMPLEMENTED);
mSerializer->Init(flags, wrapCol);
nsCOMPtr<nsIHTMLContentSink> sink (do_QueryInterface(mSerializer));
if (!sink)
{
printf("Couldn't get content sink!\n");
return NS_ERROR_UNEXPECTED;
}
#else /* USE_SERIALIZER */
nsCOMPtr<nsIContentSink> sink;
if (!inType.EqualsLiteral("text/html")
|| !outType.EqualsLiteral("text/plain"))
{
char* in = ToNewCString(inType);
char* out = ToNewCString(outType);
printf("Don't know how to convert from %s to %s\n", in, out);
Recycle(in);
Recycle(out);
return NS_ERROR_FAILURE;
}
sink = do_CreateInstance(NS_PLAINTEXTSINK_CONTRACTID);
NS_ENSURE_TRUE(sink, NS_ERROR_FAILURE);
nsCOMPtr<nsIHTMLToTextSink> textSink(do_QueryInterface(sink));
NS_ENSURE_TRUE(textSink, NS_ERROR_FAILURE);
textSink->Initialize(&outString, flags, wrapCol);
#endif /* USE_SERIALIZER */
parser->SetContentSink(sink);
nsCOMPtr<nsIDTD> dtd;
if (inType.EqualsLiteral("text/html")) {
static NS_DEFINE_CID(kNavDTDCID, NS_CNAVDTD_CID);
dtd = do_CreateInstance(kNavDTDCID, &rv);
}
else
{
printf("Don't know how to deal with non-html input!\n");
return NS_ERROR_NOT_IMPLEMENTED;
}
if (NS_FAILED(rv))
{
printf("Couldn't create new HTML DTD: 0x%x\n", rv);
return rv;
}
parser->RegisterDTD(dtd);
rv = parser->Parse(inString, 0, NS_LossyConvertUCS2toASCII(inType), PR_FALSE, PR_TRUE);
if (NS_FAILED(rv))
{
printf("Parse() failed! 0x%x\n", rv);
return rv;
}
NS_RELEASE(parser);
if (compareAgainst.Length() > 0)
return Compare(outString, compareAgainst);
char* charstar = ToNewUTF8String(outString);
printf("Output string is:\n--------------------\n%s--------------------\n",
charstar);
delete[] charstar;
return NS_OK;
}
//----------------------------------------------------------------------
int main(int argc, char** argv)
{
nsString inType(NS_LITERAL_STRING("text/html"));
nsString outType(NS_LITERAL_STRING("text/plain"));
int wrapCol = 72;
int flags = 0;
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] [-f flags] [-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\
\n\
\tDefaults are -i text/html -o text/plain -f 0 -w 72 [stdin]\n",
progname);
exit(0);
case 'i':
if (argv[0][2] != '\0')
inType.AssignWithConversion(argv[0]+2);
else {
inType.AssignWithConversion(argv[1]);
--argc;
++argv;
}
break;
case 'o':
if (argv[0][2] != '\0')
outType.AssignWithConversion(argv[0]+2);
else {
outType.AssignWithConversion(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 'f':
if (isdigit(argv[0][2]))
flags = atoi(argv[0]+2);
else {
flags = atoi(argv[1]);
--argc;
++argv;
}
break;
case 'c':
if (argv[0][2] != '\0')
compareAgainst.AssignWithConversion(argv[0]+2);
else {
compareAgainst.AssignWithConversion(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;
nsresult ret;
{
nsCOMPtr<nsIServiceManager> servMan;
NS_InitXPCOM2(getter_AddRefs(servMan), nsnull, nsnull);
nsCOMPtr<nsIComponentRegistrar> registrar = do_QueryInterface(servMan);
NS_ASSERTION(registrar, "Null nsIComponentRegistrar");
registrar->AutoRegister(nsnull);
// Read in the string: very inefficient, but who cares?
nsString inString;
int c;
while ((c = getc(file)) != EOF)
inString.Append(PRUnichar(c));
if (file != stdin)
fclose(file);
ret = HTML2text(inString, inType, outType, flags, wrapCol, compareAgainst);
} // this scopes the nsCOMPtrs
// no nsCOMPtrs are allowed to be alive when you call NS_ShutdownXPCOM
nsresult rv = NS_ShutdownXPCOM( NULL );
NS_ASSERTION(NS_SUCCEEDED(rv), "NS_ShutdownXPCOM failed");
return ret;
}
|