.oO SearXNG Developer Documentation Oo.
Loading...
Searching...
No Matches
searx.utils.HTMLTextExtractor Class Reference
Inheritance diagram for searx.utils.HTMLTextExtractor:
Collaboration diagram for searx.utils.HTMLTextExtractor:

Public Member Functions

 __init__ (self)
None handle_starttag (self, str tag, list[tuple[str, str|None]] attrs)
None handle_endtag (self, str tag)
 is_valid_tag (self)
None handle_data (self, str data)
None handle_charref (self, str name)
None handle_entityref (self, str name)
 get_text (self)
None error (self, str message)

Public Attributes

list result = []
list tags = []

Detailed Description

Internal class to extract text from HTML

Definition at line 90 of file utils.py.

Constructor & Destructor Documentation

◆ __init__()

searx.utils.HTMLTextExtractor.__init__ ( self)

Definition at line 93 of file utils.py.

93 def __init__(self):
94 HTMLParser.__init__(self)
95 self.result: list[str] = []
96 self.tags: list[str] = []
97

Member Function Documentation

◆ error()

None searx.utils.HTMLTextExtractor.error ( self,
str message )

Definition at line 140 of file utils.py.

140 def error(self, message: str) -> None:
141 # error handle is needed in <py3.10
142 # https://github.com/python/cpython/pull/8562/files
143 raise AssertionError(message)
144
145

◆ get_text()

searx.utils.HTMLTextExtractor.get_text ( self)

Definition at line 137 of file utils.py.

137 def get_text(self):
138 return ''.join(self.result).strip()
139

References result.

◆ handle_charref()

None searx.utils.HTMLTextExtractor.handle_charref ( self,
str name )

Definition at line 121 of file utils.py.

121 def handle_charref(self, name: str) -> None:
122 if not self.is_valid_tag():
123 return
124 if name[0] in ('x', 'X'):
125 codepoint = int(name[1:], 16)
126 else:
127 codepoint = int(name)
128 self.result.append(chr(codepoint))
129

References is_valid_tag(), and result.

Here is the call graph for this function:

◆ handle_data()

None searx.utils.HTMLTextExtractor.handle_data ( self,
str data )

Definition at line 116 of file utils.py.

116 def handle_data(self, data: str) -> None:
117 if not self.is_valid_tag():
118 return
119 self.result.append(data)
120

References is_valid_tag(), and result.

Here is the call graph for this function:

◆ handle_endtag()

None searx.utils.HTMLTextExtractor.handle_endtag ( self,
str tag )

Definition at line 103 of file utils.py.

103 def handle_endtag(self, tag: str) -> None:
104 if not self.tags:
105 return
106
107 if tag != self.tags[-1]:
108 self.result.append(f"</{tag}>")
109 return
110
111 self.tags.pop()
112

References result, searx.result_types.paper.Paper.tags, and tags.

◆ handle_entityref()

None searx.utils.HTMLTextExtractor.handle_entityref ( self,
str name )

Definition at line 130 of file utils.py.

130 def handle_entityref(self, name: str) -> None:
131 if not self.is_valid_tag():
132 return
133 # codepoint = htmlentitydefs.name2codepoint[name]
134 # self.result.append(chr(codepoint))
135 self.result.append(name)
136

References is_valid_tag(), and result.

Here is the call graph for this function:

◆ handle_starttag()

None searx.utils.HTMLTextExtractor.handle_starttag ( self,
str tag,
list[tuple[str, str | None]] attrs )

Definition at line 98 of file utils.py.

98 def handle_starttag(self, tag: str, attrs: list[tuple[str, str | None]]) -> None:
99 self.tags.append(tag)
100 if tag == 'br':
101 self.result.append(' ')
102

References result, searx.result_types.paper.Paper.tags, and tags.

◆ is_valid_tag()

searx.utils.HTMLTextExtractor.is_valid_tag ( self)

Definition at line 113 of file utils.py.

113 def is_valid_tag(self):
114 return not self.tags or self.tags[-1] not in _BLOCKED_TAGS
115

References searx.result_types.paper.Paper.tags, and tags.

Referenced by handle_charref(), handle_data(), and handle_entityref().

Here is the caller graph for this function:

Member Data Documentation

◆ result

searx.utils.HTMLTextExtractor.result = []

◆ tags

list searx.utils.HTMLTextExtractor.tags = []

Definition at line 96 of file utils.py.

Referenced by handle_endtag(), handle_starttag(), and is_valid_tag().


The documentation for this class was generated from the following file:
  • /home/andrew/Documents/code/public/searxng/searx/utils.py