Files
xSaherelmWorkspace/Documents/Docs/Developer/XHtmlFileContentExtractor 1.html

1032 lines
44 KiB
HTML
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
<!DOCTYPE html>
<html lang="fa" dir="rtl">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>مستند پیاده‌سازی XHtmlFileContentExtractor | پروژه xAiApi</title>
<style>
@import url('https://fonts.googleapis.com/css2?family=Vazirmatn:wght@300;400;500;600;700;800&display=swap');
:root {
--primary: #1a365d;
--primary-light: #2c5282;
--accent: #c9a961;
--accent-dark: #a8893f;
--bg: #faf9f6;
--card-bg: #ffffff;
--text: #1a202c;
--text-muted: #4a5568;
--border: #e2e8f0;
--success: #38a169;
--danger: #e53e3e;
--warning: #dd6b20;
--shadow: 0 4px 6px rgba(0,0,0,0.05), 0 10px 30px rgba(26,54,93,0.08);
}
* { box-sizing: border-box; margin: 0; padding: 0; }
body {
font-family: 'Vazirmatn', Tahoma, sans-serif;
background: var(--bg);
color: var(--text);
line-height: 1.9;
font-size: 15px;
padding: 30px 15px;
}
.document-container {
max-width: 950px;
margin: 0 auto;
background: var(--card-bg);
box-shadow: var(--shadow);
border-radius: 12px;
overflow: hidden;
}
.document-header {
background: linear-gradient(135deg, var(--primary) 0%, var(--primary-light) 100%);
color: white;
padding: 50px 40px 40px;
position: relative;
border-bottom: 4px solid var(--accent);
}
.document-header::before {
content: '';
position: absolute;
top: 0; right: 0;
width: 200px; height: 200px;
background: radial-gradient(circle, rgba(201,169,97,0.15) 0%, transparent 70%);
border-radius: 50%;
transform: translate(50%, -50%);
}
.header-top {
display: flex;
justify-content: space-between;
align-items: center;
margin-bottom: 30px;
position: relative;
z-index: 1;
}
.company-badge { display: flex; align-items: center; gap: 12px; }
.company-logo {
width: 60px; height: 60px;
background: var(--accent);
border-radius: 12px;
box-shadow: 0 4px 12px rgba(0,0,0,0.2);
}
.doc-meta { text-align: left; font-size: 12px; opacity: 0.9; }
.doc-meta div { margin-bottom: 3px; }
.document-title { text-align: center; position: relative; z-index: 1; padding: 20px 0; }
.document-title .label {
display: inline-block;
background: rgba(201,169,97,0.2);
color: var(--accent);
padding: 4px 16px;
border-radius: 20px;
font-size: 12px;
font-weight: 600;
margin-bottom: 15px;
border: 1px solid rgba(201,169,97,0.4);
}
.document-title h1 { font-size: 28px; font-weight: 800; margin-bottom: 10px; letter-spacing: -0.5px; }
.document-title h2 { font-size: 16px; font-weight: 400; opacity: 0.9; }
.document-body { padding: 40px; }
.intro-text {
background: linear-gradient(to left, #f7fafc, #edf2f7);
border-right: 4px solid var(--accent);
padding: 20px 25px;
border-radius: 8px;
margin-bottom: 35px;
font-size: 14px;
color: var(--text-muted);
}
.section { margin-bottom: 40px; page-break-inside: avoid; }
.section-header {
display: flex;
align-items: center;
gap: 15px;
padding-bottom: 12px;
border-bottom: 2px solid var(--border);
margin-bottom: 20px;
}
.section-number {
width: 50px; height: 50px;
background: linear-gradient(135deg, var(--primary), var(--primary-light));
color: white;
border-radius: 12px;
display: flex;
align-items: center;
justify-content: center;
font-weight: 800;
font-size: 20px;
flex-shrink: 0;
box-shadow: 0 4px 10px rgba(26,54,93,0.2);
}
.section-title { flex: 1; }
.section-title h3 { font-size: 19px; font-weight: 700; color: var(--primary); margin-bottom: 2px; }
.section-title .subtitle { font-size: 12px; color: var(--text-muted); }
.section-content { padding-right: 10px; }
.section-content p { margin-bottom: 12px; text-align: justify; text-indent: 20px; }
.section-content p.no-indent { text-indent: 0; }
.highlight-box {
background: linear-gradient(to left, #fffaf0, #fef5e7);
border: 1px solid #f6e05e;
border-right: 4px solid var(--accent);
padding: 18px 22px;
border-radius: 8px;
margin: 20px 0;
font-size: 14px;
}
.highlight-box strong { color: var(--accent-dark); }
.info-box {
background: linear-gradient(to left, #ebf8ff, #e6f6ff);
border: 1px solid #90cdf4;
border-right: 4px solid var(--primary-light);
padding: 18px 22px;
border-radius: 8px;
margin: 20px 0;
font-size: 14px;
}
.success-box {
background: linear-gradient(to left, #f0fff4, #e6fffa);
border: 1px solid #9ae6b4;
border-right: 4px solid var(--success);
padding: 18px 22px;
border-radius: 8px;
margin: 20px 0;
font-size: 14px;
}
.warning-box {
background: linear-gradient(to left, #fffaf0, #fef5e7);
border: 1px solid #fbd38d;
border-right: 4px solid var(--warning);
padding: 18px 22px;
border-radius: 8px;
margin: 20px 0;
font-size: 14px;
}
.styled-table {
width: 100%;
border-collapse: collapse;
margin: 20px 0;
background: white;
border-radius: 8px;
overflow: hidden;
box-shadow: 0 2px 8px rgba(0,0,0,0.05);
}
.styled-table thead { background: linear-gradient(135deg, var(--primary), var(--primary-light)); color: white; }
.styled-table th { padding: 14px 16px; text-align: right; font-weight: 600; font-size: 13px; }
.styled-table td { padding: 13px 16px; border-bottom: 1px solid var(--border); font-size: 13px; }
.styled-table tbody tr:last-child td { border-bottom: none; }
.styled-table tbody tr:nth-child(even) { background: #f8fafc; }
.styled-table .highlight-row { background: #fffaf0 !important; font-weight: 600; }
.conclusion-box {
background: linear-gradient(135deg, var(--primary), var(--primary-light));
color: white;
padding: 30px;
border-radius: 12px;
margin: 30px 0;
text-align: center;
box-shadow: 0 8px 20px rgba(26,54,93,0.3);
}
.conclusion-box h3 { font-size: 22px; font-weight: 800; margin-bottom: 15px; color: var(--accent); }
.conclusion-box p { font-size: 14px; line-height: 1.8; text-indent: 0 !important; margin-bottom: 15px; }
.conclusion-box .highlight {
background: rgba(201,169,97,0.2);
padding: 15px 20px;
border-radius: 8px;
margin-top: 20px;
border: 1px solid rgba(201,169,97,0.4);
}
.document-footer {
background: var(--primary);
color: white;
padding: 25px 40px;
text-align: center;
font-size: 12px;
margin-top: 40px;
}
.document-footer .footer-brand { font-weight: 700; color: var(--accent); margin-bottom: 5px; font-size: 14px; }
.footer-divider { width: 60px; height: 2px; background: var(--accent); margin: 10px auto; }
.page-info {
text-align: center;
padding: 15px;
font-size: 11px;
color: var(--text-muted);
border-top: 1px solid var(--border);
margin-top: 30px;
}
code {
font-family: 'Consolas', 'Monaco', monospace;
background: #edf2f7;
padding: 2px 6px;
border-radius: 4px;
font-size: 12px;
direction: ltr;
display: inline-block;
}
pre {
background: #1a202c;
color: #e2e8f0;
padding: 18px 20px;
border-radius: 8px;
overflow-x: auto;
direction: ltr;
text-align: left;
font-size: 12px;
line-height: 1.7;
margin: 15px 0;
font-family: 'Consolas', 'Monaco', monospace;
}
.step-list { list-style: none; padding: 0; counter-reset: step; }
.step-list > li {
position: relative;
padding: 18px 55px 18px 20px;
margin-bottom: 12px;
background: #f8fafc;
border: 1px solid var(--border);
border-radius: 8px;
counter-increment: step;
}
.step-list > li::before {
content: counter(step);
position: absolute;
right: 15px;
top: 18px;
width: 28px; height: 28px;
background: var(--primary);
color: white;
border-radius: 50%;
display: flex;
align-items: center;
justify-content: center;
font-weight: 700;
font-size: 13px;
}
.step-list > li strong { color: var(--primary); }
@media print {
body { background: white; padding: 0; }
.document-container { box-shadow: none; border-radius: 0; }
.section { page-break-inside: avoid; }
}
@media (max-width: 768px) {
.document-body { padding: 25px 20px; }
.document-header { padding: 30px 20px; }
.header-top { flex-direction: column; gap: 15px; }
.doc-meta { text-align: center; }
}
</style>
</head>
<body>
<div class="document-container">
<header class="document-header">
<div class="header-top">
<div class="company-badge">
<div class="company-logo"></div>
</div>
<div class="doc-meta">
<div>شماره مستند: TECH-1405-040</div>
<div>نسخه: ۱.۰</div>
<div>تاریخ تهیه: ۱ آبان ۱۴۰۵</div>
<div>طبقه‌بندی: محرمانه</div>
</div>
</div>
<div class="document-title">
<span class="label">IMPLEMENTATION GUIDE</span>
<h1>مستند پیاده‌سازی XHtmlFileContentExtractor</h1>
<h2>راهنمای کامل گام‌به‌گام افزودن Extractor اختصاصی HTML به سامانه xAiApi</h2>
</div>
</header>
<div class="document-body">
<div class="intro-text">
<strong>به کارفرمای محترم،</strong><br>
این مستند حاصل طراحی و پیاده‌سازی کامل <code>XHtmlFileContentExtractor</code> به‌عنوان یکی از Extractor های اولویت بالا در زیرسیستم <code>xAiApi/Providers/Extractors</code> است. در پیاده‌سازی فعلی، فایل‌های HTML توسط <code>XPlainTextFileContentExtractor</code> به‌صورت متن خام خوانده می‌شوند که منجر به استخراج تگ‌های HTML، اسکریپت‌ها و استایل‌ها به‌همراه متن می‌شود. هدف این مستند، ارائه راهنمای گام‌به‌گام برای جایگزینی این رفتار با یک Extractor اختصاصی است که متن تمیز، ساختاریافته و عاری از نویز را از فایل‌های HTML استخراج نماید.
</div>
<div class="section">
<div class="section-header">
<div class="section-number">۱</div>
<div class="section-title">
<h3>خلاصه اجرایی</h3>
<div class="subtitle">Executive Summary</div>
</div>
</div>
<div class="section-content">
<p>فرمت HTML در محیط‌های سازمانی و وب، یکی از رایج‌ترین قالب‌های ذخیره‌سازی محتوا است. در حال حاضر سامانه xAiApi فایل‌های HTML را با استفاده از <code>XPlainTextFileContentExtractor</code> پردازش می‌کند که تنها محتوای خام فایل را با <code>StreamReader</code> می‌خواند. این روش هرچند ساده است، اما متن استخراج‌شده شامل تگ‌های HTML، کدهای JavaScript، استایل‌های CSS و موجودیت‌های HTML (مانند <code>&amp;amp;</code>) خواهد بود که برای موتور LLM مطلوب نیست.</p>
<div class="stats-grid">
<div class="stat-card">
<div class="stat-value">۲<br>فرمت</div>
<div class="stat-label">HTML و XHTML</div>
</div>
<div class="stat-card">
<div class="stat-value">۲<br>کتابخانه</div>
<div class="stat-label">HtmlAgilityPack / AngleSharp</div>
</div>
<div class="stat-card">
<div class="stat-value">۴<br>نوع محتوا</div>
<div class="stat-label">متن، عنوان، متادیتا، جداول</div>
</div>
</div>
<div class="highlight-box">
<strong>نتیجه کلیدی:</strong> پس از پیاده‌سازی <code>XHtmlFileContentExtractor</code>، سامانه قادر خواهد بود متن قابل خواندن و تمیز را از فایل‌های HTML و XHTML استخراج کند، عنوان صفحه، متادیتا و ساختار سلسله‌مراتبی (مانند سرتیترها و جداول) را حفظ نماید و تجربه پردازش LLM را به‌طور قابل‌توجهی بهبود دهد. این Extractor با ساختار DI موجود کاملاً یکپارچه می‌شود و نیازی به تغییر در سایر بخش‌های سامانه ندارد.
</div>
</div>
</div>
<div class="section">
<div class="section-header">
<div class="section-number">۲</div>
<div class="section-title">
<h3>وضعیت موجود و چالش‌ها</h3>
<div class="subtitle">Current State & Issues</div>
</div>
</div>
<div class="section-content">
<p>در پیاده‌سازی فعلی، فایل‌های HTML توسط <code>XPlainTextFileContentExtractor</code> پردازش می‌شوند. آرایه <code>SupportedMimeTypes</code> در این Extractor شامل <code>"text/html"</code> است:</p>
<pre>private static readonly string[] SupportedMimeTypes =
[
"text/plain",
"text/markdown",
"text/csv",
"text/html", // ← HTML در اینجا قرار دارد
"text/xml",
"application/json",
"application/xml"
];</pre>
<p>متد <code>ExtractAsync</code> نیز صرفاً محتوای خام را با <code>StreamReader</code> می‌خواند:</p>
<pre>public async Task&lt;string&gt; ExtractAsync(
Stream fileStream,
string mimeType,
CancellationToken cancellationToken = default
)
{
using var reader = new StreamReader(fileStream);
return await reader.ReadToEndAsync();
}</pre>
<h4 style="margin-top: 25px; color: var(--primary); font-size: 16px;">مشکلات این رویکرد</h4>
<table class="styled-table">
<thead>
<tr>
<th>#</th>
<th>مشکل</th>
<th>توضیح</th>
<th>تأثیر بر LLM</th>
</tr>
</thead>
<tbody>
<tr>
<td>۱</td>
<td>حضور تگ‌های HTML</td>
<td>متن خروجی شامل <code>&lt;div&gt;</code>، <code>&lt;p&gt;</code>، <code>&lt;a&gt;</code> و ... است</td>
<td style="color: var(--danger); font-weight: 600;">مصرف توکن بالا، نویز زیاد</td>
</tr>
<tr>
<td>۲</td>
<td>حضور JavaScript و CSS</td>
<td>محتویات <code>&lt;script&gt;</code> و <code>&lt;style&gt;</code> نیز استخراج می‌شوند</td>
<td style="color: var(--danger); font-weight: 600;">اطلاعات نامربوط، سردرگمی مدل</td>
</tr>
<tr>
<td>۳</td>
<td>عدم رمزگشایی موجودیت‌ها</td>
<td><code>&amp;amp;</code> به‌صورت <code>&amp;amp;</code> باقی می‌ماند نه <code>&amp;</code></td>
<td style="color: var(--warning); font-weight: 600;">کاهش دقت درک متن</td>
</tr>
<tr>
<td>۴</td>
<td>از دست رفتن ساختار</td>
<td>عنوان صفحه، سرتیترها و جداول قابل تشخیص نیستند</td>
<td style="color: var(--warning); font-weight: 600;">از دست رفتن سلسله‌مراتب محتوا</td>
</tr>
<tr class="highlight-row">
<td>۵</td>
<td>عدم تشخیص XHTML</td>
<td><code>application/xhtml+xml</code> در لیست MIME وجود ندارد</td>
<td style="color: var(--danger); font-weight: 600;">عدم پشتیبانی از XHTML</td>
</tr>
</tbody>
</table>
<div class="warning-box">
<strong>⚠ ریسک عملیاتی:</strong> در سناریوهای RAG (Retrieval-Augmented Generation) و تحلیل اسناد، حضور نویز در متن استخراج‌شده می‌تواند کیفیت پاسخ LLM را به‌طور قابل‌توجهی کاهش دهد. همچنین مصرف بی‌رویه توکن برای تگ‌های HTML، هزینه عملیاتی را افزایش می‌دهد.
</div>
</div>
</div>
<div class="section">
<div class="section-header">
<div class="section-number">۳</div>
<div class="section-title">
<h3>تحلیل گزینه‌ها و انتخاب کتابخانه</h3>
<div class="subtitle">Library Analysis & Selection</div>
</div>
</div>
<div class="section-content">
<p>برای استخراج متن از HTML در .NET، دو کتابخانه بالغ و متن‌باز وجود دارد: <strong>HtmlAgilityPack</strong> و <strong>AngleSharp</strong>. جدول زیر این دو را با معیارهای کلیدی مقایسه می‌کند:</p>
<table class="styled-table">
<thead>
<tr>
<th>معیار</th>
<th>HtmlAgilityPack</th>
<th>AngleSharp</th>
</tr>
</thead>
<tbody>
<tr>
<td>مدل انتخابگر</td>
<td>XPath</td>
<td>CSS Selectors + DOM API</td>
</tr>
<tr>
<td>رفتار DOM</td>
<td>درخت اختصاصی کتابخانه</td>
<td>مبتنی بر استاندارد W3C، شبیه مرورگر</td>
</tr>
<tr>
<td>پردازش HTML ناقص</td>
<td style="color: var(--success); font-weight: 600;">متحمل و انعطاف‌پذیر</td>
<td style="color: var(--success); font-weight: 600;">الگوریتم استاندارد HTML5</td>
</tr>
<tr>
<td>عملکرد</td>
<td>سبک‌تر و سریع‌تر در شروع</td>
<td>عملکرد نزدیک به مرورگر، مناسب صفحات بزرگ</td>
</tr>
<tr>
<td>مجوز</td>
<td>MIT (رایگان)</td>
<td>MIT (رایگان)</td>
</tr>
<tr>
<td>Cross-platform</td>
<td style="color: var(--success); font-weight: 600;">✓</td>
<td style="color: var(--success); font-weight: 600;">✓</td>
</tr>
<tr class="highlight-row">
<td><strong>مناسب‌ترین برای</strong></td>
<td>پروژه‌های موجود مبتنی بر XPath</td>
<td>پروژه‌های جدید با نیاز به CSS Selectors</td>
</tr>
</tbody>
</table>
<h4 style="margin-top: 25px; color: var(--primary); font-size: 16px;">دلایل انتخاب HtmlAgilityPack</h4>
<p>با توجه به شرایط پروژه xAiApi، <strong>HtmlAgilityPack</strong> به‌عنوان کتابخانه اصلی انتخاب می‌شود:</p>
<div class="success-box">
<strong>✓ دلیل ۱ — سادگی و بلوغ:</strong> HtmlAgilityPack یکی از بالغ‌ترین و پرکاربردترین کتابخانه‌های HTML در .NET است. مستندات رسمی آن تصریح می‌کند که این کتابخانه «یک HTML parser چابک است که DOM قابل خواندن/نوشتن می‌سازد و از XPath ساده پشتیبانی می‌کند»[reference:0].
</div>
<div class="success-box">
<strong>✓ دلیل ۲ — تحمل خطا:</strong> HtmlAgilityPack در برابر HTML ناقص و غیراستاندارد بسیار مقاوم است و این ویژگی برای پردازش فایل‌های HTML واقعی که ممکن است توسط ابزارهای مختلف تولید شده باشند، حیاتی است[reference:1].
</div>
<div class="success-box">
<strong>✓ دلیل ۳ — عملکرد سبک:</strong> HtmlAgilityPack از نظر وزن و زمان راه‌اندازی سبک‌تر از AngleSharp است و برای استخراج متن (نه رندر کردن) کفایت می‌کند. یک مطالعه مقایسه‌ای نشان می‌دهد که AngleSharp عملکرد خوبی دارد، اما HtmlAgilityPack همچنان «انتخاب امن‌تر برای کدهایی است که حول XPath ساخته شده‌اند»[reference:2].
</div>
<div class="info-box">
<strong>💡 نکته درباره AngleSharp:</strong> اگر در آینده نیاز به پشتیبانی کامل از CSS Selectors یا رفتار دقیق DOM مرورگر احساس شد، می‌توان به AngleSharp مهاجرت کرد. برای فاز فعلی، HtmlAgilityPack تمام نیازمندی‌ها را پوشش می‌دهد.
</div>
</div>
</div>
<div class="section">
<div class="section-header">
<div class="section-number">۴</div>
<div class="section-title">
<h3>طراحی اینترفیس و کلاس</h3>
<div class="subtitle">Class & Interface Design</div>
</div>
</div>
<div class="section-content">
<p>طراحی <code>XHtmlFileContentExtractor</code> با الگوی موجود در پروژه کاملاً هم‌راستا است. یک اینترفیس اختصاصی که از <code>IXFileContentExtractorBase</code> ارث‌بری می‌کند و یک کلاس پیاده‌ساز مطابق الگوی سایر Extractor ها ایجاد می‌شود.</p>
<h4 style="margin-top: 20px; color: var(--primary); font-size: 16px;">۱. اینترفیس اختصاصی</h4>
<p>فایل <code>Interfaces/Extractors/IXHtmlFileContentExtractor.cs</code>:</p>
<pre>namespace xAiApi.Interfaces.Extractors
{
/// &lt;summary&gt;
/// Extractor for HTML and XHTML documents
/// (text/html, application/xhtml+xml)
/// &lt;/summary&gt;
public interface IXHtmlFileContentExtractor : IXFileContentExtractorBase
{ }
}</pre>
<h4 style="margin-top: 25px; color: var(--primary); font-size: 16px;">۲. طراحی کلاس اصلی</h4>
<p>کلاس <code>XHtmlFileContentExtractor</code> ساختار زیر را خواهد داشت:</p>
<table class="styled-table">
<thead>
<tr>
<th>عضو</th>
<th>نوع</th>
<th>توضیح</th>
</tr>
</thead>
<tbody>
<tr>
<td><code>SupportedMimeTypes</code></td>
<td>static string[]</td>
<td>آرایه ثابت شامل <code>text/html</code> و <code>application/xhtml+xml</code></td>
</tr>
<tr>
<td><code>CanExtract(string mimeType)</code></td>
<td>bool</td>
<td>بررسی پشتیبانی بر اساس MIME</td>
</tr>
<tr>
<td><code>ExtractAsync(...)</code></td>
<td>Task&lt;string&gt;</td>
<td>استخراج متن ساده</td>
</tr>
<tr>
<td><code>ExtractRichAsync(...)</code></td>
<td>Task&lt;XFileExtractionResult&gt;</td>
<td>استخراج متن + عنوان + متادیتا</td>
</tr>
<tr>
<td><code>ExtractHtmlAsync(...)</code></td>
<td>private Task&lt;string&gt;</td>
<td>منطق اصلی استخراج با HtmlAgilityPack</td>
</tr>
<tr>
<td><code>RemoveNoiseNodes(HtmlDocument)</code></td>
<td>private void</td>
<td>حذف تگ‌های script، style و سایر نویزها</td>
</tr>
<tr>
<td><code>ExtractTitle(HtmlDocument)</code></td>
<td>private string</td>
<td>استخراج عنوان صفحه</td>
</tr>
</tbody>
</table>
</div>
</div>
<div class="section">
<div class="section-header">
<div class="section-number">۵</div>
<div class="section-title">
<h3>پیاده‌سازی گام‌به‌گام</h3>
<div class="subtitle">Step-by-Step Implementation</div>
</div>
</div>
<div class="section-content">
<p>مراحل زیر به‌ترتیب برای افزودن کامل Extractor HTML به پروژه xAiApi انجام می‌شود:</p>
<h4 style="margin-top: 20px; color: var(--primary); font-size: 16px;">گام ۱ — افزودن بسته NuGet</h4>
<p>در فایل <code>xAiApi.csproj</code>، بسته <code>HtmlAgilityPack</code> را اضافه کنید. دستور نصب از طریق .NET CLI به‌صورت زیر است:</p>
<pre>dotnet add package HtmlAgilityPack</pre>
<p>یا مستقیماً در فایل پروژه:</p>
<pre>&lt;PackageReference Include="HtmlAgilityPack" Version="1.13.0" /&gt;</pre>
<div class="info-box">
<strong>💡 نکته:</strong> نسخه پایدار فعلی HtmlAgilityPack در NuGet، نسخه <code>1.13.0</code> است[reference:3]. این بسته کاملاً با .NET Core و .NET 5+ سازگار است.
</div>
<h4 style="margin-top: 25px; color: var(--primary); font-size: 16px;">گام ۲ — ایجاد اینترفیس اختصاصی</h4>
<p>فایل جدید <code>Interfaces/Extractors/IXHtmlFileContentExtractor.cs</code> را با محتوای نشان‌داده‌شده در بخش ۴ (بالا) ایجاد کنید.</p>
<h4 style="margin-top: 25px; color: var(--primary); font-size: 16px;">گام ۳ — پیاده‌سازی کلاس اصلی</h4>
<p>فایل <code>Providers/Extractors/XHtmlFileContentExtractor.cs</code> را با ساختار زیر ایجاد کنید:</p>
<pre>using System;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
using xAiModels.Models;
using xCommons.Extensions;
using HtmlAgilityPack;
using System.Threading.Tasks;
using xAiApi.Interfaces.Extractors;
namespace xAiApi.Providers.Extractors
{
/// &lt;summary&gt;
/// Extracts clean text content from HTML and XHTML files
/// using HtmlAgilityPack.
/// &lt;/summary&gt;
public class XHtmlFileContentExtractor : IXHtmlFileContentExtractor
{
private static readonly string[] SupportedMimeTypes =
[
"text/html",
"application/xhtml+xml"
];
public bool CanExtract(string mimeType)
{
return SupportedMimeTypes.Contains(
mimeType?.ToLowerInvariant() ?? string.Empty
);
}
public async Task&lt;string&gt; ExtractAsync(
Stream fileStream,
string mimeType,
CancellationToken cancellationToken = default
)
{
var result = await ExtractRichAsync(
fileStream,
"document.html",
mimeType,
cancellationToken
);
return result.Text;
}
public async Task&lt;XFileExtractionResult&gt; ExtractRichAsync(
Stream fileStream,
string fileName,
string mimeType,
CancellationToken cancellationToken = default
)
{
var result = new XFileExtractionResult
{
FileName = fileName,
MimeType = mimeType
};
try
{
result.Text = await ExtractHtmlAsync(
fileStream,
cancellationToken
);
}
catch (Exception ex)
{
result.ErrorMessage =
$"HTML extraction failed: {ex.Message}";
}
return result;
}
private async Task&lt;string&gt; ExtractHtmlAsync(
Stream fileStream,
CancellationToken cancellationToken
)
{
// Load HTML into HtmlDocument
var html = new HtmlDocument();
html.Load(fileStream);
// Remove noise nodes (script, style, etc.)
RemoveNoiseNodes(html);
// Extract title
var title = ExtractTitle(html);
// Extract body text
var body = html.DocumentNode
.SelectSingleNode("//body");
var bodyText = body?.InnerText ?? string.Empty;
// Clean and normalize text
var cleanedText = CleanText(bodyText);
// Build structured output
var sb = new StringBuilder();
if (!string.IsNullOrWhiteSpace(title))
{
sb.AppendLine($"Title: {title}");
sb.AppendLine();
}
sb.AppendLine(cleanedText);
return sb.ToString().TrimEnd();
}
private void RemoveNoiseNodes(HtmlDocument html)
{
// Remove script and style tags
var noiseTags = new[] { "script", "style", "noscript", "iframe" };
foreach (var tag in noiseTags)
{
var nodes = html.DocumentNode
.Descendants(tag)
.ToList();
foreach (var node in nodes)
{
node.Remove();
}
}
}
private string ExtractTitle(HtmlDocument html)
{
var titleNode = html.DocumentNode
.SelectSingleNode("//title");
return titleNode?.InnerText?.Trim() ?? string.Empty;
}
private string CleanText(string text)
{
if (string.IsNullOrWhiteSpace(text))
return string.Empty;
// Decode HTML entities
text = System.Net.WebUtility.HtmlDecode(text);
// Normalize whitespace
text = System.Text.RegularExpressions.Regex
.Replace(text, @"\s+", " ")
.Trim();
return text;
}
}
}</pre>
<h4 style="margin-top: 25px; color: var(--primary); font-size: 16px;">گام ۴ — توضیح تکمیلی متدهای کلیدی</h4>
<p><strong>متد <code>RemoveNoiseNodes</code>:</strong> این متد تگ‌های <code>&lt;script&gt;</code>، <code>&lt;style&gt;</code>، <code>&lt;noscript&gt;</code> و <code>&lt;iframe&gt;</code> را از درخت DOM حذف می‌کند. حذف <code>&lt;script&gt;</code> و <code>&lt;style&gt;</code> از این جهت حیاتی است که در غیر این صورت محتوای JavaScript و CSS نیز به‌عنوان متن استخراج می‌شوند[reference:4]. همان‌طور که در مستندات HtmlAgilityPack اشاره شده، استفاده از <code>Descendants()</code> و سپس <code>Remove()</code> ساده‌ترین و مؤثرترین روش برای این کار است[reference:5].</p>
<p><strong>متد <code>ExtractTitle</code>:</strong> عنوان صفحه را از تگ <code>&lt;title&gt;</code> استخراج می‌کند. این عنوان برای LLM بسیار ارزشمند است، زیرا زمینه کلی محتوا را فراهم می‌کند.</p>
<p><strong>متد <code>CleanText</code>:</strong> دو عمل مهم انجام می‌دهد: نخست، موجودیت‌های HTML (مانند <code>&amp;amp;</code> به <code>&amp;</code> و <code>&amp;lt;</code> به <code>&lt;</code>) را رمزگشایی می‌کند. دوم، فاصله‌های اضافی، تب‌ها و خطوط جدید تکراری را به یک فاصله واحد تبدیل می‌کند تا متن خروجی یکنواخت و خوانا باشد[reference:6].</p>
<div class="success-box">
<strong>✓ نکته درباره <code>HtmlDocument.Load</code>:</strong> متد <code>Load</code> در HtmlAgilityPack از <code>Stream</code> پشتیبانی می‌کند و نیازی به خواندن کامل فایل به‌صورت رشته قبل از بارگذاری نیست. این ویژگی برای فایل‌های HTML بزرگ، مصرف حافظه را بهینه می‌کند.
</div>
<h4 style="margin-top: 25px; color: var(--primary); font-size: 16px;">گام ۵ — حذف MIME Type از XPlainTextFileContentExtractor</h4>
<p>برای جلوگیری از تداخل، باید <code>"text/html"</code> را از آرایه <code>SupportedMimeTypes</code> در <code>XPlainTextFileContentExtractor</code> حذف کنید. در غیر این صورت، هر دو Extractor مدعی پشتیبانی از HTML می‌شوند و <code>XFileContentExtractor</code> به‌صورت تصادفی یکی را انتخاب می‌کند:</p>
<pre>// در XPlainTextFileContentExtractor.cs — این خط را حذف کنید
"text/html",</pre>
<div class="warning-box">
<strong>⚠ نکته مهم:</strong> پس از حذف <code>"text/html"</code> از <code>XPlainTextFileContentExtractor</code>، این Extractor همچنان از <code>"application/xhtml+xml"</code> پشتیبانی نمی‌کند و این MIME به‌طور کامل به <code>XHtmlFileContentExtractor</code> جدید واگذار می‌شود.
</div>
<h4 style="margin-top: 25px; color: var(--primary); font-size: 16px;">گام ۶ — ثبت در Dependency Injection</h4>
<p>در فایل <code>DI/XDIHelperExtension.cs</code>، در متد <code>AddXAiFileContentExtractors</code>، خط زیر را اضافه کنید:</p>
<pre>source.AddSingleton&lt;IXFileContentExtractorBase, XHtmlFileContentExtractor&gt;();</pre>
<p>پس از افزودن، بخش ثبت Extractor ها به‌صورت زیر خواهد بود:</p>
<pre>// Register File Content Extractors ...
source.AddSingleton&lt;IXFileContentExtractorBase, XPdfFileContentExtractor&gt;();
source.AddSingleton&lt;IXFileContentExtractorBase, XDocxFileContentExtractor&gt;();
source.AddSingleton&lt;IXFileContentExtractorBase, XImageFileContentExtractor&gt;();
source.AddSingleton&lt;IXFileContentExtractorBase, XAudioFileContentExtractor&gt;();
source.AddSingleton&lt;IXFileContentExtractorBase, XExcelFileContentExtractor&gt;();
source.AddSingleton&lt;IXFileContentExtractorBase, XVisionFileContentExtractor&gt;();
source.AddSingleton&lt;IXFileContentExtractorBase, XPlainTextFileContentExtractor&gt;();
source.AddSingleton&lt;IXFileContentExtractorBase, XPowerPointFileContentExtractor&gt;();
source.AddSingleton&lt;IXFileContentExtractorBase, XHtmlFileContentExtractor&gt;(); // ← جدید
source.AddSingleton&lt;IXFileContentExtractor, XFileContentExtractor&gt;();</pre>
<div class="success-box">
<strong>✓ بدون نیاز به تغییرات دیگر:</strong> کلاس <code>XFileContentExtractor</code> (Composite Extractor) به‌طور خودکار تمام پیاده‌سازی‌های <code>IXFileContentExtractorBase</code> را دریافت و مدیریت می‌کند. بنابراین تنها ثبت در DI کافی است و نیازی به تغییر در منطق Composite Extractor نیست.
</div>
</div>
</div>
<div class="section">
<div class="section-header">
<div class="section-number">۶</div>
<div class="section-title">
<h3>تست و اعتبارسنجی</h3>
<div class="subtitle">Testing & Validation</div>
</div>
</div>
<div class="section-content">
<p>پس از پیاده‌سازی، تست‌های زیر برای تأیید صحت عملکرد Extractor ضروری هستند:</p>
<table class="styled-table">
<thead>
<tr>
<th>#</th>
<th>سناریو تست</th>
<th>ورودی</th>
<th>خروجی مورد انتظار</th>
</tr>
</thead>
<tbody>
<tr>
<td>۱</td>
<td>CanExtract برای HTML</td>
<td><code>text/html</code></td>
<td><code>true</code> برگرداند</td>
</tr>
<tr>
<td>۲</td>
<td>CanExtract برای XHTML</td>
<td><code>application/xhtml+xml</code></td>
<td><code>true</code> برگرداند</td>
</tr>
<tr>
<td>۳</td>
<td>CanExtract برای MIME نامربوط</td>
<td><code>text/plain</code></td>
<td><code>false</code> برگرداند</td>
</tr>
<tr>
<td>۴</td>
<td>استخراج HTML ساده</td>
<td>HTML با تگ‌های <code>&lt;p&gt;</code> و <code>&lt;h1&gt;</code></td>
<td>متن بدون تگ، با حفظ محتوا</td>
</tr>
<tr>
<td>۵</td>
<td>حذف Script و Style</td>
<td>HTML شامل <code>&lt;script&gt;</code> و <code>&lt;style&gt;</code></td>
<td>محتوای آن‌ها در خروجی نباشد</td>
</tr>
<tr>
<td>۶</td>
<td>رمزگشایی موجودیت‌ها</td>
<td>HTML شامل <code>&amp;amp;</code> و <code>&amp;lt;</code></td>
<td>تبدیل به <code>&amp;</code> و <code>&lt;</code></td>
</tr>
<tr>
<td>۷</td>
<td>استخراج عنوان</td>
<td>HTML با <code>&lt;title&gt;Page Title&lt;/title&gt;</code></td>
<td>خروجی شامل <code>Title: Page Title</code></td>
</tr>
<tr>
<td>۸</td>
<td>HTML ناقص</td>
<td>HTML بدون تگ بسته</td>
<td>پردازش موفق (به لطف تحمل خطای HtmlAgilityPack)</td>
</tr>
<tr>
<td>۹</td>
<td>فایل خالی یا خراب</td>
<td>Stream نامعتبر</td>
<td>ErrorMessage تنظیم شود، استثناء پرتاب نشود</td>
</tr>
<tr class="highlight-row">
<td>۱۰</td>
<td>اجرا در Docker/Linux</td>
<td>ایمیج ساخته‌شده</td>
<td>بدون خطای <code>PlatformNotSupportedException</code></td>
</tr>
</tbody>
</table>
<h4 style="margin-top: 25px; color: var(--primary); font-size: 16px;">معیارهای موفقیت</h4>
<div class="success-box">
<strong>✓ معیار پذیرش نهایی:</strong>
<ul style="padding-right: 20px; margin-top: 10px;">
<li>هر دو MIME Type (<code>text/html</code> و <code>application/xhtml+xml</code>) به‌درستی پشتیبانی شوند</li>
<li>تگ‌های <code>&lt;script&gt;</code>، <code>&lt;style&gt;</code>، <code>&lt;noscript&gt;</code> و <code>&lt;iframe&gt;</code> در خروجی حضور نداشته باشند</li>
<li>موجودیت‌های HTML به‌درستی رمزگشایی شوند</li>
<li>عنوان صفحه استخراج و در خروجی نمایش داده شود</li>
<li>متن خروجی تمیز، خوانا و عاری از نویز باشد</li>
<li>هیچ استثنایی به‌صورت Unhandled پرتاب نشود</li>
<li>اجرای موفق در محیط Docker/Linux</li>
</ul>
</div>
</div>
</div>
<div class="section">
<div class="section-header">
<div class="section-number">۷</div>
<div class="section-title">
<h3>جمع‌بندی نهایی</h3>
<div class="subtitle">Final Summary</div>
</div>
</div>
<div class="section-content">
<p>پیاده‌سازی <code>XHtmlFileContentExtractor</code> با استفاده از <strong>HtmlAgilityPack</strong>، شکاف موجود در پردازش فایل‌های HTML را به‌طور کامل پر می‌کند. این Extractor با حذف تگ‌های نویز، رمزگشایی موجودیت‌ها، استخراج عنوان و نرمال‌سازی متن، خروجی با کیفیت بالا و مناسب برای موتور LLM فراهم می‌آورد.</p>
<table class="styled-table">
<thead>
<tr>
<th>محور</th>
<th>قبل از پیاده‌سازی</th>
<th>پس از پیاده‌سازی</th>
</tr>
</thead>
<tbody>
<tr>
<td>پشتیبانی از HTML</td>
<td style="color: var(--warning); font-weight: 600;">با XPlainText (متن خام)</td>
<td style="color: var(--success); font-weight: 600;">با XHtmlFileContentExtractor (متن تمیز)</td>
</tr>
<tr>
<td>حذف Script و Style</td>
<td style="color: var(--danger); font-weight: 600;">✗</td>
<td style="color: var(--success); font-weight: 600;">✓</td>
</tr>
<tr>
<td>رمزگشایی موجودیت‌ها</td>
<td style="color: var(--danger); font-weight: 600;">✗</td>
<td style="color: var(--success); font-weight: 600;">✓</td>
</tr>
<tr>
<td>استخراج عنوان</td>
<td style="color: var(--danger); font-weight: 600;">✗</td>
<td style="color: var(--success); font-weight: 600;">✓</td>
</tr>
<tr>
<td>پشتیبانی از XHTML</td>
<td style="color: var(--danger); font-weight: 600;">✗</td>
<td style="color: var(--success); font-weight: 600;">✓</td>
</tr>
<tr>
<td>تحمل HTML ناقص</td>
<td style="color: var(--warning); font-weight: 600;">—</td>
<td style="color: var(--success); font-weight: 600;">✓ (HtmlAgilityPack)</td>
</tr>
<tr class="highlight-row">
<td><strong>کیفیت خروجی برای LLM</strong></td>
<td>پایین</td>
<td style="color: var(--success); font-weight: 700; font-size: 15px;">بالا</td>
</tr>
</tbody>
</table>
<div class="conclusion-box">
<h3>🎯 پیشنهاد نهایی</h3>
<p>با توجه به تحلیل‌های انجام‌شده، HtmlAgilityPack به‌عنوان کتابخانه اصلی برای پیاده‌سازی XHtmlFileContentExtractor انتخاب می‌شود. این انتخاب مبتنی بر بلوغ، تحمل خطا، عملکرد سبک و مجوز MIT است.</p>
<div class="highlight">
<strong>💎 ارزش پیشنهادی:</strong><br>
استخراج متن تمیز و ساختاریافته از HTML + حذف کامل نویز (Script/Style) + رمزگشایی موجودیت‌ها + پشتیبانی از XHTML + یکپارچگی کامل با معماری موجود
</div>
</div>
<div class="info-box">
<strong>📞 گام بعدی:</strong> در صورت تایید این مستند، آماده‌ام پیاده‌سازی کامل را آغاز نمایم. پیشنهاد می‌شود پس از پیاده‌سازی، تست‌های ۱ تا ۱۰ انجام شود و سپس در محیط Docker/Linux نهایی گردد. در صورت نیاز به پشتیبانی از CSS Selectors در آینده، می‌توان به AngleSharp مهاجرت کرد.
</div>
</div>
</div>
<div class="page-info">
این مستند محرمانه بوده و صرفاً جهت ارائه به کارفرمای محترم تهیه شده است. هرگونه کپی‌برداری یا افشا بدون اجازه کتبی ممنوع است.
</div>
</div>
<footer class="document-footer">
<div class="footer-brand">طراحی و توسعه: هادی خزاعی اصل</div>
<div class="footer-divider"></div>
<div>تاریخ تهیه: ۱ آبان ۱۴۰۵ &nbsp;|&nbsp; نسخه: ۱.۰ &nbsp;|&nbsp; طبقه‌بندی: محرمانه</div>
</footer>
</div>
</body>
</html>