Skip to content

Commit bece218

Browse files
committed
0.0.3 debug run passed
1 parent 2382e7d commit bece218

7 files changed

Lines changed: 16 additions & 49 deletions

File tree

.env

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
INSTALL_METHOD=remote
22
REMOTE_INSTALL_HOST=debug.dify.ai
33
REMOTE_INSTALL_PORT=5003
4-
REMOTE_INSTALL_KEY=89807907-f186-4f5a-a1d3-299bb28c2226
4+
REMOTE_INSTALL_KEY=b0895dc3-efe2-45d1-a8a7-39124d14ee46

manifest.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
version: 0.0.2
1+
version: 0.0.3
22
type: plugin
33
author: davidkhala
44
name: html_extractor

tests/html_test.py

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@ def test_html_extractor_parses_file(self):
2020
result = extractor.extract()
2121

2222
self.assertIsInstance(result, ExtractorResult)
23-
self.assertIn("Hello World", result.md_content)
24-
self.assertIn("test@example.com", result.md_content)
23+
self.assertIn("Hello World", result.text)
24+
self.assertIn("test@example.com", result.text)
2525

2626
def test_thei_page(self):
2727
html_content = """
@@ -79,16 +79,16 @@ def test_thei_page(self):
7979
</html>
8080
"""
8181

82-
no_footer = HtmlExtractor(html_content, "", "-").extract().md_content
82+
no_footer = HtmlExtractor(html_content, "", "-").extract().text
8383
self.assertNotIn("Copyright", no_footer)
84-
no_header = HtmlExtractor(html_content, "-", "").extract().md_content
84+
no_header = HtmlExtractor(html_content, "-", "").extract().text
8585
self.assertNotIn("THEi 设计与建筑系", no_header)
8686
def test_real_page(self):
8787
with open("tests/fixtures/Bachelor of Engineering (Honours) in Building Services Engineering - Technological and Higher Education Institute of Hong Kong.html", 'r', encoding='utf-8') as f:
8888
html_content = f.read()
8989
header_classes ="mobile-menu-wrapper, breadcrumbs-wrapper, popup-login-wrapper, toolbar"
9090
footer_classes = 'footer,header-info-swapper,thim-widget-button,elementor-widget-social-icons'
91-
clean = HtmlExtractor(html_content, header_classes, footer_classes).extract().md_content
91+
clean = HtmlExtractor(html_content, header_classes, footer_classes).extract().text
9292
self.assertNotIn("About", clean)
9393
self.assertNotIn("Copyright", clean)
9494
self.assertNotIn("Apply now", clean)
@@ -97,8 +97,6 @@ def test_real_page(self):
9797
self.assertIn("Fluid Mechanics", clean)
9898
self.assertIn("4,230", clean)
9999
print(clean)
100-
with open("output.txt", "w", encoding="utf-8") as f:
101-
f.write(clean)
102100

103101

104102
if __name__ == '__main__':

tools/__init__.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,5 @@ def _invoke(self, tool_parameters: dict[str, Any]) -> Generator[ToolInvokeMessag
1616
remove_footer = tool_parameters.get("footer_class")
1717

1818
extractor = HtmlExtractor(html_content, remove_header, remove_footer)
19-
20-
extractor_result = extractor.extract()
21-
yield self.create_text_message(extractor_result.md_content)
19+
t = extractor.extract().text
20+
yield self.create_text_message(t)

tools/document.py

Lines changed: 4 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,11 @@
11
from typing import Optional
22

3-
from dify_plugin.invocations.file import UploadFileResponse
4-
from pydantic import BaseModel, Field
5-
6-
7-
class ChildDocument(BaseModel):
8-
"""Class for storing a piece of text and associated metadata."""
9-
page_content: str
10-
vector: Optional[list[float]] = None
11-
"""Arbitrary metadata about the page content (e.g., source, relationships to other
12-
documents, etc.).
13-
"""
14-
metadata: dict = Field(default_factory=dict)
15-
16-
17-
class Document(ChildDocument):
18-
provider:Optional[str] = "davidkhala"
19-
children: Optional[list[ChildDocument]] = None
20-
21-
def to_dict(self) -> dict:
22-
return {
23-
"page_content": self.page_content,
24-
"vector": self.vector if self.vector is not None else None,
25-
"metadata": self.metadata,
26-
"provider": self.provider,
27-
"children": [child.model_dump() for child in self.children]
28-
if self.children
29-
else None,
30-
}
3+
from pydantic import BaseModel
314

325

336
class ExtractorResult(BaseModel):
347
"""Class for storing the result of an extractor."""
358

36-
md_content: str
37-
documents: Optional[list[Document]] = None
38-
img_list: Optional[list[UploadFileResponse]] = None
39-
origin_result: Optional[dict] = None
9+
text: str
10+
files: Optional[list] = None
11+
json: Optional[list[dict]] = None

tools/extractor_base.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,5 @@
33

44
class BaseExtractor(ABC):
55

6-
@abstractmethod
76
def extract(self):
8-
raise NotImplementedError
7+
...

tools/html_extractor.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ def __init__(self, html_content: str, remove_header: str, remove_footer: str):
1919

2020
def extract(self) -> ExtractorResult:
2121
return ExtractorResult(
22-
md_content=self._load_as_text(),
22+
text=self._load_as_text(),
2323
)
2424

2525
@staticmethod
@@ -45,7 +45,6 @@ def _load_as_text(self) -> str:
4545
tag.decompose()
4646
HtmlExtractor.class_remover(soup, self.remove_footer)
4747

48-
text = soup.get_text()
48+
text = soup.get_text(separator='\n')
4949
text = re.sub(r'\n{3,}', '\n\n', text)
50-
text = re.sub(r'(?<!\n)\n(?!\n)', '\n\n', text)
5150
return text.strip() if text else ""

0 commit comments

Comments
 (0)