File: html2text.C

package info (click to toggle)
html2text 1.3.2a-3
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 736 kB
  • ctags: 1,694
  • sloc: cpp: 9,311; yacc: 828; sh: 209; makefile: 144
file content (384 lines) | stat: -rw-r--r-- 11,355 bytes parent folder | download | duplicates (7)
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
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384

 /***************************************************************************/

/*
 * Portions Copyright (c) 1999 GMRS Software GmbH
 * Carl-von-Linde-Str. 38, D-85716 Unterschleissheim, http://www.gmrs.de
 * All rights reserved.
 *
 * Author: Arno Unkrig <arno@unkrig.de>
 */
 
/* This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License in the file COPYING for more details.
 */

 /***************************************************************************/

/*
 * Changes to version 1.2.2 were made by Martin Bayer <mbayer@zedat.fu-berlin.de>
 * Dates and reasons of modifications:
 * Fre Jun  8 19:00:26 CEST 2001: new image handling
 * Thu Oct  4 21:42:24 CEST 2001: ported to g++ 3.0, bugfix for '-' as synonym for STDIN
 * Mon Jul 22 13:48:26 CEST 2002: Made finaly reading from STDIN work.
 * Sat Sep 14 15:04:09 CEST 2002: Added plain ASCII output patch by Bela Lubkin
 * Wed Jul  2 22:08:45 CEST 2003: ported to g++ 3.3
 */
  
 /***************************************************************************/


#include <iostream>
#include <string.h>
#include <stdlib.h>

#include "html.h"
#include "HTMLControl.h"
#include "urlistream.h"
#include "format.h"

#define stringify(x) stringify2(x)
#define stringify2(x) #x

/* ------------------------------------------------------------------------- */

class MyParser : public HTMLControl {

public:
  enum { PRINT_AS_ASCII, UNPARSE, SYNTAX_CHECK };

  MyParser(
    urlistream &is_,
    bool       debug_scanner_,
    bool       debug_parser_,
    ostream    &os_,
    int        mode_,
    int        width_,
    const char *file_name_
  ) :
    HTMLControl(is_, debug_scanner_, debug_parser_),
    os(os_),
    mode(mode_),
    width(width_),
    file_name(file_name_)
  {}

private:
  /*virtual*/ void yyerror(char *);
  /*virtual*/ void process(const Document &);

  ostream &os;
  int     mode;
  int     width;
  string  file_name;
};

/*virtual*/ void
MyParser::yyerror(char *p)
{

  /*
   * Swallow parse error messages if not in "syntax check" mode.
   */
  if (mode != SYNTAX_CHECK && !strcmp(p, "parse error")) return;

  std::cerr
    << "File \""
    << file_name
    << "\", line "
    << current_line
    << ", column "
    << current_column
    << ": "
    << p
    << std::endl;
}

/*virtual*/ void
MyParser::process(const Document &document)
{
  switch (mode) {

  case PRINT_AS_ASCII:
    document.format(/*indent_left*/ 0, width, Area::LEFT, os);
    break;

  case UNPARSE:
    document.unparse(os, std::endl);
    break;

  case SYNTAX_CHECK:
    break;

  default:
    std::cerr << "??? Invalid mode " << mode << " ??? " << std::endl;
    exit(1);
    break;
  }
}

/* ------------------------------------------------------------------------- */

static const char *usage = "\
Usage:\n\
  html2text -help\n\
  html2text -version\n\
  html2text [ -unparse | -check ] [ -debug-scanner ] [ -debug-parser ] \\\n\
     [ -rcfile <file> ] [ -style ( compact | pretty ) ] [ -width <w> ] \\\n\
     [ -o <file> ] [ -nobs ] [ -ascii ] [ <input-url> ] ...\n\
Formats HTML document(s) read from <input-url> or STDIN and generates ASCII\n\
text.\n\
  -help          Print this text and exit\n\
  -version       Print program version and copyright notice\n\
  -unparse       Generate HTML instead of ASCII output\n\
  -check         Do syntax checking only\n\
  -debug-scanner Report parsed tokens on STDERR (debugging)\n\
  -debug-parser  Report parser activity on STDERR (debugging)\n\
  -rcfile <file> Read <file> instead of \"$HOME/.html2textrc\"\n\
  -style compact Create a \"compact\" output format (default)\n\
  -style pretty  Insert some vertical space for nicer output\n\
  -width <w>     Optimize for screen widths other than 79\n\
  -o <file>      Redirect output into <file>\n\
  -nobs          Do not use backspaces for boldface and underlining\n\
  -ascii         Use plain ASCII for output instead of ISO-8859-1\n\
";

int use_iso8859 = 1;

int
main(int argc, char **argv)
{
  if (argc == 2 && !strcmp(argv[1], "-help")) {
    std::cout
      << "This is html2text, version " stringify(VERSION) << std::endl
      << std::endl
      << usage;
    exit(0);
  }

  if (argc == 2 && !strcmp(argv[1], "-version")) {
    std::cout
      << "This is html2text, version " stringify(VERSION) << std::endl
      << std::endl
      << "The latest version can be found at http://userpage.fu-berlin.de/~mbayer/tools/" << std::endl
      << std::endl
      << "This program is distributed in the hope that it will be useful, but WITHOUT" << std::endl
      << "ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS" << std::endl
      << "FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details." << std::endl
      << std::endl;
    exit(0);
  }

  bool       mode              = MyParser::PRINT_AS_ASCII;
  bool       debug_scanner     = false;
  bool       debug_parser      = false;
  const char *home             = getenv("HOME");
  string     rcfile            = string(home ? home : "") + "/.html2textrc";
  const char *style            = "compact";
  int        width             = 79;
  const char *output_file_name = "-";
  bool       use_backspaces    = true;

  int i;
  for (i = 1; i < argc && argv[i][0] == '-' && argv[i][1]; i++) {
    const char *arg = argv[i];

    if (!strcmp(arg, "-unparse"      )) { mode = MyParser::UNPARSE;      } else
    if (!strcmp(arg, "-check"        )) { mode = MyParser::SYNTAX_CHECK; } else
    if (!strcmp(arg, "-debug-scanner")) { debug_scanner = true;          } else
    if (!strcmp(arg, "-debug-parser" )) { debug_parser = true;           } else
    if (!strcmp(arg, "-rcfile"       )) { rcfile = argv[++i];            } else
    if (!strcmp(arg, "-style"        )) { style = argv[++i];             } else
    if (!strcmp(arg, "-width"        )) { width = atoi(argv[++i]);       } else
    if (!strcmp(arg, "-o"            )) { output_file_name = argv[++i];  } else
    if (!strcmp(arg, "-nobs"         )) { use_backspaces = false;        } else
    if (!strcmp(arg, "-ascii"        )) { use_iso8859 = false;           } else
    {
      std::cerr
	<< "Unrecognized command line option \""
	<< arg
	<< "\", try \"-help\"."
	<< std::endl;
      exit(1);
    }
  }
  if (i > argc) {
    std::cerr
      << "Error: Required parameter after \""
      << argv[argc - 1]
      << "\" missing."
      << std::endl;
    exit(1);
  }

  const char *const *input_urls;
  int        number_of_input_urls;

  if (i >= argc) {
    static const char *const x = "-";
    input_urls = &x;
    number_of_input_urls = 1;
  } else {
    input_urls = argv + i;
    number_of_input_urls = argc - i;
  }

  /*
   * Set up formatting: First, set some formatting properties depending on
   * the "-style" command line option.
   */
  if (!strcmp(style, "compact")) {
    ;
  } else
  if (!strcmp(style, "pretty")) {

    /*
     * The "pretty" style was kindly supplied by diligent user Rolf Niepraschk.
     */
    static const struct {
      const char *key;
      const char *value;
    } properties[] = {
      { "OL.TYPE",                  "1" },
      { "OL.vspace.before",         "1" },
      { "OL.vspace.after",          "1" },
      { "OL.indents",               "5" },
      { "UL.vspace.before",         "1" },
      { "UL.vspace.after",          "1" },
      { "UL.indents",               "2" },
      { "DL.vspace.before",         "1" },
      { "DL.vspace.after",          "1" },
      { "DT.vspace.before",         "1" },
      { "DIR.vspace.before",        "1" },
      { "DIR.indents",              "2" },
      { "MENU.vspace.before",       "1" },
      { "MENU.vspace.after",        "1" },
      { "DT.indent",                "2" },
      { "DD.indent",                "6" },
      { "HR.marker",                "-" },
      { "H1.prefix",                ""  },
      { "H2.prefix",                ""  },
      { "H3.prefix",                ""  },
      { "H4.prefix",                ""  },
      { "H5.prefix",                ""  },
      { "H6.prefix",                ""  },
      { "H1.suffix",                ""  },
      { "H2.suffix",                ""  },
      { "H3.suffix",                ""  },
      { "H4.suffix",                ""  },
      { "H5.suffix",                ""  },
      { "H6.suffix",                ""  },
      { "H1.vspace.before",         "2" },
      { "H2.vspace.before",         "1" },
      { "H3.vspace.before",         "1" },
      { "H4.vspace.before",         "1" },
      { "H5.vspace.before",         "1" },
      { "H6.vspace.before",         "1" },
      { "H1.vspace.after",          "1" },
      { "H2.vspace.after",          "1" },
      { "H3.vspace.after",          "1" },
      { "H4.vspace.after",          "1" },
      { "H5.vspace.after",          "1" },
      { "H6.vspace.after",          "1" },
      { "TABLE.vspace.before",      "1" },
      { "TABLE.vspace.after",       "1" },
      { "CODE.vspace.before",       "0" },
      { "CODE.vspace.after",        "0" },
      { "BLOCKQUOTE.vspace.before", "1" },
      { "BLOCKQUOTE.vspace.after",  "1" },
      { "PRE.vspace.before",        "1" },
      { "PRE.vspace.after",         "1" },
      { "PRE.indent.left",          "2" },
      { "IMG.replace.noalt",        ""  },
      { "IMG.alt.prefix",           " " },
      { "IMG.alt.suffix",           " " },
      { 0, 0 }
    }, *p;
    for (p = properties; p->key; ++p) {
      Formatting::setProperty(p->key, p->value);
    }
  } else {
    std::cerr
      << "Unknown style \""
      << style
      << "\" specified -- try \"-help\"."
      << std::endl;
    ::exit(1);
  }

  {
    std::ifstream ifs(rcfile.c_str());
    if (!ifs.rdbuf()->is_open()) ifs.open("/etc/html2textrc");
    if (ifs.rdbuf()->is_open()) {
      Formatting::loadProperties(ifs);
    }
  }

  /*
   * Set up printing.
   */
  Area::use_backspaces = use_backspaces;

  ostream  *osp;
  std::ofstream ofs;

  if (!strcmp(output_file_name, "-")) {
    osp = &std::cout;
  } else {
    ofs.open(output_file_name, std::ios::out);
    if (!ofs) {
      std::cerr
        << "Could not open output file \""
        << output_file_name
        << "\"."
        << std::endl;
    exit(1);
    }
    osp = &ofs;
  }

  for (i = 0; i < number_of_input_urls; ++i) {
    const char *input_url = input_urls[i];

    if (number_of_input_urls != 1) {
      *osp << "###### " << input_url << " ######" << std::endl;
    }

    istream    *isp;
    urlistream uis;

    uis.open(input_url);
    if (!uis.is_open()) {
      std::cerr
        << "Opening input URL \""
	<< input_url
        << "\": "
        << uis.open_error()
        << std::endl;
      exit(1);
    }

    MyParser parser(
      uis,
      debug_scanner,
      debug_parser,
      *osp,
      mode,
      width,
      input_url
    );

    if (parser.yyparse() != 0) exit(1);
  }

  return 0;
}

/* ------------------------------------------------------------------------- */