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
|
/* Copyright 2020-2023 Fabian Groffen <grobian@gentoo.org>
*
* 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.
*/
#include "HTMLDriver.h"
#include "HTMLControl.h"
#include "HTMLParser.hh"
#include "iconvstream.h"
HTMLDriver::HTMLDriver(HTMLControl& c,
iconvstream& os_,
bool& enable_links_,
int& width_,
int& mode_,
bool& debug_parser) :
enable_links(enable_links_),
control(c),
trace_parsing(debug_parser),
width(width_),
mode(mode_),
os(os_)
{
links = new OrderedList;
links->items.reset(new list<auto_ptr<ListItem>>);
links->nesting = 0;
};
HTMLDriver::~HTMLDriver()
{
if (links != nullptr)
delete links;
}
int HTMLDriver::parse()
{
html2text::HTMLParser parse(*this);
parse.set_debug_level(trace_parsing);
return parse();
}
int HTMLDriver::lex(html2text::HTMLParser::semantic_type * const lval)
{
return control.htmlparser_yylex(lval);
}
void HTMLDriver::process(const Document& document)
{
switch (mode) {
case PRINT_AS_ASCII:
if (enable_links && links->items->size() > 0) {
Heading *h = new Heading;
PCData *d = new PCData;
h->level = 6;
d->text = "References";
list<auto_ptr<Element>> *data = new list<auto_ptr<Element>>;
data->push_back(auto_ptr<Element>(d));
h->content.reset(data);
document.body.content->push_back(auto_ptr<Element>(h));
document.body.content->push_back(auto_ptr<Element>(links));
links = nullptr; /* now taken by auto_ptr */
}
document.format(/*indent_left*/ 0, width, Area::LEFT, os);
break;
case SYNTAX_CHECK:
break;
default:
std::cerr << "??? Invalid mode " << mode << " ??? " << std::endl;
exit(1);
break;
}
}
bool HTMLDriver::read_cdata(const char *terminal, string *ret)
{
return control.read_cdata(terminal, ret);
}
void HTMLDriver::yyerror(const char *msg)
{
return control.htmlparser_yyerror(msg);
}
|