File: lex-html.c

package info (click to toggle)
bow 19991122-4
  • links: PTS
  • area: main
  • in suites: woody
  • size: 2,544 kB
  • ctags: 2,987
  • sloc: ansic: 38,660; lisp: 1,072; makefile: 594; perl: 492; yacc: 149; sh: 91
file content (127 lines) | stat: -rw-r--r-- 3,615 bytes parent folder | download
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
/* A lexer will special features for handling HTML. */

/* Copyright (C) 1997, 1999 Andrew McCallum

   Written by:  Andrew Kachites McCallum <mccallum@cs.cmu.edu>

   This file is part of the Bag-Of-Words Library, `libbow'.

   This library is free software; you can redistribute it and/or
   modify it under the terms of the GNU Library General Public License
   as published by the Free Software Foundation, version 2.
   
   This library 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
   Library General Public License for more details.

   You should have received a copy of the GNU Library General Public
   License along with this library; if not, write to the Free Software
   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111, USA */

#include <bow/libbow.h>
#include <ctype.h>		/* for tolower() */

#define PARAMS (bow_default_lexer_parameters)

int
bow_lexer_html_get_raw_word (bow_lexer *self, bow_lex *lex, 
			     char *buf, int buflen)
{
  int byte;			/* characters read from the FP */
  int wordlen;			/* number of characters in the word so far */
  int html_bracket_nestings = 0;

  assert (lex->document_position <= lex->document_length);

  /* Ignore characters until we get an beginning character. */
  do
    {
      byte = lex->document[lex->document_position++];
      if (byte == 0)
	{
	  if (html_bracket_nestings)
	    bow_verbosify (bow_verbose,
			   "Found unterminated `<' in HTML\n");
	  lex->document_position--;
	  return 0;
	}
      if (byte == '<')
	{
	  if (html_bracket_nestings)
	    bow_verbosify (bow_verbose, 
			   "Found nested '<' in HTML\n");
	  html_bracket_nestings = 1;
	}
      else if (byte == '>')
	{
	  if (html_bracket_nestings == 0)
	    bow_verbosify (bow_verbose,
			   "Found `>' outside HTML token\n");
	  html_bracket_nestings = 0;
	}
    }
  while (html_bracket_nestings || !PARAMS->true_to_start (byte));

  /* Add the first alphabetic character to the word. */
  buf[0] = (bow_lexer_case_sensitive) ? byte : tolower (byte);

  /* Add all the satisfying characters to the word - stripping out all HTML
     markup.  "<FONT SIZE=+2>R</FONT>ainbow " becomes "Rainbow" */
  for (wordlen = 1; wordlen < buflen; wordlen++)
    {
      byte = lex->document[lex->document_position++];
      if (byte == 0)
	{
	  lex->document_position--;
	  break;
	}
      if (!PARAMS->false_to_end (byte) && html_bracket_nestings == 0)
	{
	  lex->document_position--;
	  break;
	}
      if (byte == '<')
	{
	  if (html_bracket_nestings)
	    bow_verbosify (bow_verbose, 
			   "Found nested '<' in HTML\n");
	  html_bracket_nestings = 1;
	}
      if (byte == '>')
	{
	  if (html_bracket_nestings == 0)
	    bow_verbosify (bow_verbose,
			   "Found `>' outside HTML token\n");
	  html_bracket_nestings = 0;
	}
      if (html_bracket_nestings == 0)
	buf[wordlen] = tolower (byte);
    }

  assert (lex->document_position <= lex->document_length);

  if (wordlen >= buflen)
    bow_error ("Encountered word longer than buffer length=%d", buflen);

  /* Terminate it. */
  buf[wordlen] = '\0';

  return wordlen;
}


/* A lexer that ignores all HTML directives, ignoring all characters
   between angled brackets: < and >. */
const bow_lexer _bow_html_lexer =
{
  sizeof (bow_lex),
  NULL,
  bow_lexer_simple_open_text_fp,
  bow_lexer_simple_open_str,
  bow_lexer_simple_get_word,
  bow_lexer_html_get_raw_word,
  bow_lexer_next_postprocess_word,
  bow_lexer_simple_close
};
const bow_lexer *bow_html_lexer = &_bow_html_lexer;