AcePerl
view release on metacpan or search on metacpan
acelib/texthelp.c view on Meta::CPAN
/* part specific to the text-help system, which uses freeOut
to print arsed HTML as plain text */
{
int i, len ;
char *cp, *start ;
static BOOL
MODE_PREFORMAT=FALSE,
MODE_HREF=FALSE,
MODE_HEADER=FALSE,
FOUND_NOBULLET_IN_LIST_NOINDENT=FALSE ;
static int itemNumber ;
static char *currentLink ;
switch (node->type)
{
case HTML_SECTION:
printTextSection (node->left) ;
if (node->right) printTextSection (node->right) ;
break ;
case HTML_COMMENT:
/* do nothing */
break ;
case HTML_DOC:
case HTML_HEAD:
case HTML_BODY:
if (node->left) printTextSection (node->left) ;
break ;
case HTML_TITLE:
for (i = 0; i < strlen(node->text)+4; ++i)
freeOutf ("*") ;
freeOutf ("\n* %s *\n", node->text) ;
for (i = 0; i < strlen(node->text)+4; ++i)
freeOutf ("*") ;
blankTextLine() ;
break ;
case HTML_HEADER:
{
MODE_HEADER = TRUE ;
indent = node->hlevel*2 ;
blankTextLine () ;
/* check, in case some bozo has done a thing like <H1></H1> */
if (node->left) printTextSection (node->left) ;
freeOutf ("\n") ;
for (i = 0; i < xPos; ++i)
{
if (i < indent) freeOutf (" ") ;
else freeOutf ("*") ;
}
blankTextLine () ;
MODE_HEADER = FALSE ;
}
break ;
case HTML_LIST:
if (node->lstyle == HTML_LIST_BULLET ||
node->lstyle == HTML_LIST_NUMBER)
indent += 2 ;
else if (node->lstyle == HTML_LIST_NOINDENT)
indent -= 2 ;
newTextLine () ;
itemNumber = 0 ;
/* a list might not have a leftnode (a list item) */
if (node->left) printTextSection (node->left) ;
if (node->lstyle == HTML_LIST_BULLET ||
node->lstyle == HTML_LIST_NUMBER)
indent -= 2 ;
else if (node->lstyle == HTML_LIST_NOINDENT)
indent += 2 ;
if (node->lstyle == HTML_LIST_NOINDENT &&
FOUND_NOBULLET_IN_LIST_NOINDENT)
{
indent -= 4 ;
FOUND_NOBULLET_IN_LIST_NOINDENT = FALSE ;
}
blankTextLine () ;
break ;
case HTML_LISTITEM:
++itemNumber ;
if (node->left)
{
if (node->lstyle == HTML_LIST_NOINDENT_NOBULLET)
{
/* if we are in a <DL> list and went to indentation
because of a <DD> item, a <DT> item brings back
the old indent-level (noindent for <DL>'s) */
if (FOUND_NOBULLET_IN_LIST_NOINDENT)
{
indent -= 6 ;
FOUND_NOBULLET_IN_LIST_NOINDENT = FALSE ;
newTextLine () ;
freeOutf (" ") ;
}
}
else
newTextLine () ;
if (node->lstyle == HTML_LIST_BULLET ||
node->lstyle == HTML_LIST_NOINDENT)
{
freeOutf ("* ") ;
indent += 2 ;
xPos = indent ;
}
else if (node->lstyle == HTML_LIST_NUMBER)
{
freeOutf ("%d. ", itemNumber) ;
indent += strlen(messprintf ("%d. ", itemNumber)) ;
xPos = indent ;
}
else if (node->lstyle == HTML_LIST_NOBULLET)
{
/* part of a <DL> noindented list, but a <DD>
item becomes indented, but no bullet */
/* if we come across the first NO_BULLET item, in
a LIST_NOINDENT, the LIST becomes indented */
if (!FOUND_NOBULLET_IN_LIST_NOINDENT)
{
indent += 6 ;
xPos = indent ;
freeOutf (" ") ;
fflush (stdout) ;
FOUND_NOBULLET_IN_LIST_NOINDENT = TRUE ;
}
}
printTextSection (node->left) ;
}
if (node->lstyle == HTML_LIST_BULLET ||
node->lstyle == HTML_LIST_NOINDENT)
{
indent -= 2 ;
}
else if (node->lstyle == HTML_LIST_NUMBER)
{
indent -= strlen(messprintf ("%d. ", itemNumber)) ;
}
else if (node->lstyle == HTML_LIST_NOBULLET)
{
if (!FOUND_NOBULLET_IN_LIST_NOINDENT)
indent -= 6 ;
}
if (node->right)
{
printTextSection (node->right) ;
}
break ;
case HTML_HREF:
if (node->link)
{
MODE_HREF = TRUE ;
currentLink = node->link ;
}
/* we have to check for leftnode, in case we have a thing
like <A HREF=...></A>. The HREF-node doesn't have a TEXT
node attached, and it would crash otherwise */
if (node->left)printTextSection (node->left) ;
if (node->link)
{
MODE_HREF = FALSE ;
currentLink = 0 ;
}
break ;
case HTML_TEXT:
cp = node->text ;
if (!MODE_PREFORMAT)
stripSpaces (node->text) ;
/* for MODE_PREFORMAT keeps all controls chars */
while (*cp)
{
len = 0 ;
start = cp ;
if (!MODE_PREFORMAT)
{
while (*cp && !isspace((int)*cp)) { ++(cp) ; ++len ; }
if (*cp) ++cp ; /* skip whitespace */
}
else
{
while (*cp && *cp != '\n') { ++(cp) ; ++len ; }
if (*cp)
{
++cp ; /* skip RETURN */
++len ; /* so we copy the RETURN into buf */
}
}
memset (buf, 0, 10000) ;
strncpy (buf, start, len) ;
buf[len] = 0 ;
/* linewrapping of words/lines longer than WINX */
if (strlen(buf) > WINX)
( run in 1.024 second using v1.01-cache-2.11-cpan-8f98c5d2c55 )