#include "driver.h"
#include "stringclass.h"
#include "cset.h"
#if !defined(TRUE)
# define TRUE (1==1)
#endif
#if !defined(FALSE)
# define FALSE (1==0)
#endif
#include "html-text.h"
html_text::html_text (simple_output *op) :
stackptr(NULL), lastptr(NULL), out(op), space_emitted(TRUE),
current_indentation(-1), pageoffset(-1), linelength(-1)
{
}
html_text::~html_text ()
{
flush_text();
}
void html_text::end_tag (tag_definition *t)
{
switch (t->type) {
case I_TAG: out->put_string("</i>"); break;
case B_TAG: out->put_string("</b>"); break;
case P_TAG: out->put_string("</p>").nl().enable_newlines(FALSE); break;
case SUB_TAG: out->put_string("</sub>"); break;
case SUP_TAG: out->put_string("</sup>"); break;
case TT_TAG: out->put_string("</tt>"); break;
case PRE_TAG: out->put_string("</pre>");
if (! is_present(TABLE_TAG)) {
out->nl();
out->enable_newlines(TRUE);
}
break;
case SMALL_TAG: out->put_string("</small>"); break;
case BIG_TAG: out->put_string("</big>"); break;
case TABLE_TAG: issue_table_end(); break;
default:
error("unrecognised tag");
}
}
void html_text::issue_tag (char *tagname, char *arg)
{
if ((arg == 0) || (strlen(arg) == 0)) {
out->put_string(tagname);
out->put_string(">");
} else {
out->put_string(tagname);
out->put_string(" ");
out->put_string(arg);
out->put_string(">");
}
}
void html_text::start_tag (tag_definition *t)
{
switch (t->type) {
case I_TAG: issue_tag("<i", t->arg1); break;
case B_TAG: issue_tag("<b", t->arg1); break;
case P_TAG: issue_tag("\n<p", t->arg1);
out->enable_newlines(TRUE); break;
case SUB_TAG: issue_tag("<sub", t->arg1); break;
case SUP_TAG: issue_tag("<sup", t->arg1); break;
case TT_TAG: issue_tag("<tt", t->arg1); break;
case PRE_TAG: out->nl(); issue_tag("<pre", t->arg1);
out->enable_newlines(FALSE); break;
case SMALL_TAG: issue_tag("<small", t->arg1); break;
case BIG_TAG: issue_tag("<big", t->arg1); break;
case TABLE_TAG: issue_table_begin(t); break;
case BREAK_TAG: break;
default:
error("unrecognised tag");
}
}
int html_text::table_is_void (tag_definition *t)
{
if (linelength > 0) {
return( current_indentation*100/linelength <= 0 );
} else {
return( FALSE );
}
}
void html_text::issue_table_begin (tag_definition *t)
{
if (linelength > 0) {
int width=current_indentation*100/linelength;
if (width > 0) {
out->put_string("<table width=\"100%\" border=0 rules=\"none\" frame=\"void\"\n cols=\"2\" cellspacing=\"0\" cellpadding=\"0\">").nl();
out->put_string("<tr valign=\"top\" align=\"left\">").nl();
if ((t->arg1 == 0) || (strcmp(t->arg1, "") == 0))
out->put_string("<td width=\"").put_number(width).put_string("%\"></td>");
else {
out->put_string("<td width=\"").put_number(width).put_string("%\">").nl();
out->put_string(t->arg1).put_string("</td>");
t->arg1[0] = (char)0;
}
out->put_string("<td width=\"").put_number(100-width).put_string("%\">").nl();
}
}
}
void html_text::issue_table_end (void)
{
out->put_string("</td></table>").nl();
out->enable_newlines(TRUE);
}
void html_text::flush_text (void)
{
int notext=TRUE;
tag_definition *p=stackptr;
while (stackptr != 0) {
notext = (notext && (! stackptr->text_emitted));
if (! notext) {
end_tag(stackptr);
}
p = stackptr;
stackptr = stackptr->next;
free(p);
}
lastptr = NULL;
}
int html_text::is_present (HTML_TAG t)
{
tag_definition *p=stackptr;
while (p != NULL) {
if (t == p->type) {
return( TRUE );
}
p = p->next;
}
return( FALSE );
}
void html_text::push_para (HTML_TAG t, char *arg)
{
tag_definition *p=(tag_definition *)malloc(sizeof(tag_definition));
p->type = t;
p->arg1 = arg;
p->text_emitted = FALSE;
if (((t == P_TAG) || (t == PRE_TAG) || (t == TABLE_TAG)) &&
(lastptr != NULL)) {
if (((lastptr->type == TABLE_TAG) && (t == PRE_TAG)) ||
((lastptr->type == P_TAG) && (t == TABLE_TAG))) {
if (stackptr == lastptr) {
p->next = stackptr;
stackptr = p;
} else {
tag_definition *q = stackptr;
while (q->next != lastptr) {
q = q->next;
}
q->next = p;
p->next = lastptr;
}
} else {
lastptr->next = p;
lastptr = p;
p->next = NULL;
}
} else {
p->next = stackptr;
if (stackptr == NULL)
lastptr = p;
stackptr = p;
}
}
void html_text::do_indent (char *arg, int indent, int pageoff, int linelen)
{
if ((current_indentation != -1) &&
(pageoffset+current_indentation != indent+pageoff)) {
do_table(arg);
}
current_indentation = indent;
pageoffset = pageoff;
linelength = linelen;
}
void html_text::do_table (char *arg)
{
int in_pre = is_in_pre();
done_pre();
shutdown(TABLE_TAG); remove_break();
if (in_pre) {
do_pre();
}
push_para(TABLE_TAG, arg);
}
void html_text::done_table (void)
{
shutdown(TABLE_TAG);
space_emitted = TRUE;
}
void html_text::do_italic (void)
{
done_bold();
done_tt();
if (! is_present(I_TAG)) {
push_para(I_TAG, "");
}
}
void html_text::do_bold (void)
{
done_italic();
done_tt();
if (! is_present(B_TAG)) {
push_para(B_TAG, "");
}
}
void html_text::do_tt (void)
{
done_bold();
done_italic();
if ((! is_present(TT_TAG)) && (! is_present(PRE_TAG))) {
push_para(TT_TAG, "");
}
}
void html_text::do_pre (void)
{
done_bold();
done_italic();
done_tt();
(void)done_para();
if (! is_present(PRE_TAG)) {
push_para(PRE_TAG, "");
}
}
int html_text::is_in_pre (void)
{
return( is_present(PRE_TAG) );
}
int html_text::is_in_table (void)
{
return( is_present(TABLE_TAG) );
}
char *html_text::shutdown (HTML_TAG t)
{
char *arg=NULL;
if (is_present(t)) {
tag_definition *p =stackptr;
tag_definition *temp =NULL;
int notext =TRUE;
while ((stackptr != NULL) && (stackptr->type != t)) {
notext = (notext && (! stackptr->text_emitted));
if (! notext) {
end_tag(stackptr);
}
p = stackptr;
stackptr = stackptr->next;
if (stackptr == NULL)
lastptr = NULL;
p->next = temp;
temp = p;
}
if ((stackptr != NULL) && (stackptr->type == t)) {
if (stackptr->text_emitted) {
end_tag(stackptr);
}
if (t == P_TAG) {
arg = stackptr->arg1;
}
p = stackptr;
stackptr = stackptr->next;
if (stackptr == NULL)
lastptr = NULL;
free(p);
}
while (temp != NULL) {
push_para(temp->type, temp->arg1);
p = temp;
temp = temp->next;
free(p);
}
}
return( arg );
}
void html_text::done_bold (void)
{
shutdown(B_TAG);
}
void html_text::done_italic (void)
{
shutdown(I_TAG);
}
void html_text::done_sup (void)
{
shutdown(SUP_TAG);
}
void html_text::done_sub (void)
{
shutdown(SUB_TAG);
}
void html_text::done_tt (void)
{
shutdown(TT_TAG);
}
void html_text::done_pre (void)
{
shutdown(PRE_TAG);
}
void html_text::done_small (void)
{
shutdown(SMALL_TAG);
}
void html_text::done_big (void)
{
shutdown(BIG_TAG);
}
void html_text::check_emit_text (tag_definition *t)
{
if ((t != NULL) && (! t->text_emitted)) {
if (t->type == TABLE_TAG) {
if (table_is_void(t)) {
tag_definition *n = t->next;
remove_def(t);
check_emit_text(n);
} else {
if ((t->next != NULL) &&
(t->next->type == P_TAG) &&
((t->next->arg1 == 0) || strcmp(t->next->arg1, "") == 0)) {
check_emit_text(t->next->next);
} else {
check_emit_text(t->next);
}
t->text_emitted = TRUE;
start_tag(t);
}
} else {
check_emit_text(t->next);
t->text_emitted = TRUE;
start_tag(t);
}
}
}
void html_text::do_emittext (char *s, int length)
{
if ((! is_present(P_TAG)) && (! is_present(PRE_TAG)))
do_para("");
if (is_present(BREAK_TAG)) {
int text = remove_break();
check_emit_text(stackptr);
if (text) {
if (is_present(PRE_TAG)) {
out->nl();
} else {
out->put_string("<br>").nl();
}
}
} else {
check_emit_text(stackptr);
}
out->put_string(s, length);
space_emitted = FALSE;
}
void html_text::do_para (char *arg)
{
done_pre();
if (! is_present(P_TAG)) {
remove_sub_sup();
if ((arg != 0) && (strcmp(arg, "") != 0)) {
remove_tag(TABLE_TAG);
}
push_para(P_TAG, arg);
space_emitted = TRUE;
}
}
char *html_text::done_para (void)
{
space_emitted = TRUE;
return( shutdown(P_TAG) );
}
void html_text::do_space (void)
{
if (is_in_pre()) {
do_emittext("", 0);
} else {
do_para(done_para());
}
space_emitted = TRUE;
}
void html_text::do_break (void)
{
if (! is_present(PRE_TAG)) {
if (emitted_text()) {
if (! is_present(BREAK_TAG)) {
push_para(BREAK_TAG, "");
}
}
}
space_emitted = TRUE;
}
void html_text::do_newline (void)
{
if (is_present(PRE_TAG)) {
do_emittext("\n", 1);
space_emitted = TRUE;
}
}
int html_text::emitted_text (void)
{
return( ! space_emitted);
}
void html_text::emit_space (void)
{
if (space_emitted) {
if (is_present(PRE_TAG)) {
do_emittext(" ", 1);
}
} else {
out->space_or_newline();
space_emitted = TRUE;
}
}
void html_text::remove_def (tag_definition *t)
{
tag_definition *p = stackptr;
tag_definition *l = 0;
tag_definition *q = 0;
while ((p != 0) && (p != t)) {
l = p;
p = p->next;
}
if ((p != 0) && (p == t)) {
if (p == stackptr) {
stackptr = stackptr->next;
if (stackptr == NULL)
lastptr = NULL;
q = stackptr;
} else if (l == 0) {
error("stack list pointers are wrong");
} else {
l->next = p->next;
q = p->next;
if (l->next == NULL)
lastptr = l;
}
free(p);
}
}
void html_text::remove_tag (HTML_TAG tag)
{
tag_definition *p = stackptr;
while ((p != 0) && (p->type != tag)) {
p = p->next;
}
if ((p != 0) && (p->type == tag))
remove_def(p);
}
void html_text::remove_sub_sup (void)
{
if (is_present(SUB_TAG)) {
remove_tag(SUB_TAG);
}
if (is_present(SUP_TAG)) {
remove_tag(SUP_TAG);
}
if (is_present(PRE_TAG)) {
remove_tag(PRE_TAG);
}
}
int html_text::remove_break (void)
{
tag_definition *p = stackptr;
tag_definition *l = 0;
tag_definition *q = 0;
while ((p != 0) && (p->type != BREAK_TAG)) {
l = p;
p = p->next;
}
if ((p != 0) && (p->type == BREAK_TAG)) {
if (p == stackptr) {
stackptr = stackptr->next;
if (stackptr == NULL)
lastptr = NULL;
q = stackptr;
} else if (l == 0) {
error("stack list pointers are wrong");
} else {
l->next = p->next;
q = p->next;
if (l->next == NULL)
lastptr = l;
}
free(p);
}
while (q != 0) {
if (q->text_emitted) {
return( TRUE );
} else {
q = q->next;
}
}
return( FALSE );
}
void html_text::do_small (void)
{
if (is_present(BIG_TAG)) {
done_big();
} else {
push_para(SMALL_TAG, "");
}
}
void html_text::do_big (void)
{
if (is_present(SMALL_TAG)) {
done_small();
} else {
push_para(BIG_TAG, "");
}
}
void html_text::do_sup (void)
{
push_para(SUP_TAG, "");
}
void html_text::do_sub (void)
{
push_para(SUB_TAG, "");
}