Files
xSaherelmWorkspace/Documents/Docs/Developer/HTML File Content Extractor.html
T

1003 lines
48 KiB
HTML
Raw 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 style="font-family: monospace; background: #edf2f7; padding: 2px 6px; border-radius: 4px;">XHtmlFileContentExtractor</code> به‌عنوان یکی از Extractor های اولویت بالا در زیرسیستم <code style="font-family: monospace; background: #edf2f7; padding: 2px 6px; border-radius: 4px;">xAiApi/Providers/Extractors</code> است. در حال حاضر فرمت‌های HTML توسط <code style="font-family: monospace;">XPlainTextFileContentExtractor</code> به‌صورت خام (شامل تمام تگ‌ها و اسکریپت‌ها) پردازش می‌شوند. هدف این مستند، جایگزینی این رفتار با یک Extractor اختصاصی است که متن تمیز، قابل فهم و عاری از تگ‌های HTML را برای موتور LLM استخراج می‌کند. تمام مراحل از انتخاب کتابخانه تا ثبت در DI و تست، به‌صورت گام‌به‌گام شرح داده شده است.
</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 (`.html`، `.htm`، `.xhtml`) در محیط‌های سازمانی و وب بسیار پرکاربرد هستند. اما پردازش خام این فایل‌ها توسط <code style="font-family: monospace;">XPlainTextFileContentExtractor</code> باعث می‌شود تگ‌های HTML، کدهای CSS و جاوااسکریپت نیز به خروجی وارد شوند و کیفیت پاسخ LLM را کاهش دهند. یک Extractor اختصاصی HTML با استفاده از یک پارسر DOM، تنها متن قابل مشاهده (Visible Text) را استخراج می‌کند و تگ‌های غیرمحتوا را حذف می‌نماید.</p>
<div class="stats-grid" style="display: grid; grid-template-columns: repeat(3, 1fr); gap: 15px; margin: 25px 0;">
<div class="stat-card" style="background: linear-gradient(135deg, var(--primary), var(--primary-light)); color: white; padding: 20px; border-radius: 10px; text-align: center; box-shadow: 0 4px 12px rgba(26,54,93,0.2);">
<div class="stat-value" style="font-size: 24px; font-weight: 800; margin-bottom: 5px; color: var(--accent); line-height: 1.3;">۲<br>MIME</div>
<div class="stat-label" style="font-size: 12px; opacity: 0.9;">پوشش text/html و xhtml</div>
</div>
<div class="stat-card" style="background: linear-gradient(135deg, var(--primary), var(--primary-light)); color: white; padding: 20px; border-radius: 10px; text-align: center; box-shadow: 0 4px 12px rgba(26,54,93,0.2);">
<div class="stat-value" style="font-size: 24px; font-weight: 800; margin-bottom: 5px; color: var(--accent); line-height: 1.3;">AngleSharp</div>
<div class="stat-label" style="font-size: 12px; opacity: 0.9;">کتابخانه Cross-platform و استاندارد W3C</div>
</div>
<div class="stat-card" style="background: linear-gradient(135deg, var(--primary), var(--primary-light)); color: white; padding: 20px; border-radius: 10px; text-align: center; box-shadow: 0 4px 12px rgba(26,54,93,0.2);">
<div class="stat-value" style="font-size: 24px; font-weight: 800; margin-bottom: 5px; color: var(--accent); line-height: 1.3;">۸<br>گام</div>
<div class="stat-label" style="font-size: 12px; opacity: 0.9;">از نصب تا تست نهایی</div>
</div>
</div>
<div class="highlight-box">
<strong>نتیجه کلیدی:</strong> پس از پیاده‌سازی این Extractor، سامانه xAiApi قادر خواهد بود فایل‌های HTML را با استخراج متن تمیز و ساختاریافته پردازش کند. این Extractor با ساختار DI و Composite Extractor موجود کاملاً یکپارچه می‌شود و MIME Type های <code style="font-family: monospace;">text/html</code> و <code style="font-family: monospace;">application/xhtml+xml</code> را پوشش می‌دهد. توجه: MIME Type <code style="font-family: monospace;">text/html</code> در حال حاضر توسط <code style="font-family: monospace;">XPlainTextFileContentExtractor</code> نیز پوشش داده می‌شود؛ این تداخل با اصلاح 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">Library Selection & Architecture Decision</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>MIT (رایگان)</td>
<td style="color: var(--success); font-weight: 600;">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>
<td>استخراج متن تمیز</td>
<td>نیاز به حذف دستی <code>script</code> و <code>style</code></td>
<td style="color: var(--success); font-weight: 600;">✓ <code>TextContent</code> مستقیماً متن قابل مشاهده را برمی‌گرداند</td>
</tr>
<tr>
<td>پیروی از استاندارد W3C</td>
<td style="color: var(--warning); font-weight: 600;">△ رفتار شبیه‌سازی‌شده</td>
<td style="color: var(--success); font-weight: 600;">✓ پارسر منطبق با W3C</td>
</tr>
<tr>
<td>پشتیبانی از CSS Selectors</td>
<td style="color: var(--warning); font-weight: 600;">فقط XPath</td>
<td style="color: var(--success); font-weight: 600;">✓ CSS Selectors و LINQ</td>
</tr>
<tr>
<td>حذف خودکار محتوای غیرقابل مشاهده</td>
<td style="color: var(--danger); font-weight: 600;">✗</td>
<td style="color: var(--success); font-weight: 600;">✓ <code>TextContent</code> اسکریپت و استایل را شامل نمی‌شود</td>
</tr>
<tr class="highlight-row">
<td><strong>ارزیابی نهایی</strong></td>
<td>مناسب برای پردازش‌های سبک</td>
<td style="color: var(--success); font-weight: 700;">انتخاب‌شده برای این پروژه</td>
</tr>
</tbody>
</table>
<div class="success-box">
<strong>✓ دلیل انتخاب AngleSharp:</strong> ویژگی <code style="font-family: monospace;">TextContent</code> در AngleSharp به‌طور خودکار محتوای درون تگ‌های <code style="font-family: monospace;">&lt;script&gt;</code> و <code style="font-family: monospace;">&lt;style&gt;</code> را از خروجی حذف می‌کند و تنها متن قابل مشاهده برای کاربر را برمی‌گرداند. این ویژگی دقیقاً همان رفتاری است که برای استخراج تمیز HTML نیاز داریم و نیاز به پیاده‌سازی دستی منطق حذف تگ‌ها را از بین می‌برد. همچنین AngleSharp از استاندارد W3C پیروی می‌کند و در محیط‌های Docker/Linux بدون هیچ وابستگی Native کار می‌کند.
</div>
<div class="info-box">
<strong>💡 نکته:</strong> اگرچه HtmlAgilityPack نیز گزینه‌ای معتبر است، اما نیاز به کد اضافی برای حذف <code style="font-family: monospace;">script</code> و <code style="font-family: monospace;">style</code> دارد که ریسک خطا و پیچیدگی نگهداری را افزایش می‌دهد. AngleSharp با <code style="font-family: monospace;">TextContent</code> این کار را به‌صورت بومی انجام می‌دهد.
</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>طراحی این Extractor با الگوی موجود در پروژه کاملاً هم‌راستا است. یک اینترفیس اختصاصی که از <code style="font-family: monospace;">IXFileContentExtractorBase</code> ارث‌بری می‌کند و یک کلاس پیاده‌ساز مطابق الگوی <code style="font-family: monospace;">XDocxFileContentExtractor</code> و <code style="font-family: monospace;">XPowerPointFileContentExtractor</code> ایجاد می‌شود.</p>
<h4 style="margin-top: 20px; color: var(--primary); font-size: 16px;">۱. اینترفیس اختصاصی</h4>
<p>فایل <code style="font-family: monospace; background: #edf2f7; padding: 2px 6px; border-radius: 4px;">Interfaces/Extractors/IXHtmlFileContentExtractor.cs</code>:</p>
<pre>namespace xAiApi.Interfaces.Extractors
{
/// &lt;summary&gt;
/// Extractor for HTML 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>
<table class="styled-table">
<thead>
<tr>
<th>عضو</th>
<th>نوع</th>
<th>توضیح</th>
</tr>
</thead>
<tbody>
<tr>
<td><code style="font-family: monospace;">SupportedMimeTypes</code></td>
<td>static string[]</td>
<td>شامل <code>text/html</code> و <code>application/xhtml+xml</code></td>
</tr>
<tr>
<td><code style="font-family: monospace;">CanExtract(string mimeType)</code></td>
<td>bool</td>
<td>بررسی پشتیبانی بر اساس MIME</td>
</tr>
<tr>
<td><code style="font-family: monospace;">ExtractAsync(...)</code></td>
<td>Task&lt;string&gt;</td>
<td>استخراج متن ساده</td>
</tr>
<tr>
<td><code style="font-family: monospace;">ExtractRichAsync(...)</code></td>
<td>Task&lt;XFileExtractionResult&gt;</td>
<td>استخراج متن + عنوان + متادیتا</td>
</tr>
<tr>
<td><code style="font-family: monospace;">ExtractHtmlAsync(...)</code></td>
<td>private Task&lt;string&gt;</td>
<td>منطق اصلی استخراج با AngleSharp</td>
</tr>
<tr>
<td><code style="font-family: monospace;">GetDocumentTitle(...)</code></td>
<td>private static string</td>
<td>استخراج عنوان از تگ <code>&lt;title&gt;</code></td>
</tr>
</tbody>
</table>
<div class="warning-box">
<strong>⚠ نکته مهم درباره تداخل MIME Type:</strong> MIME Type <code style="font-family: monospace;">text/html</code> در حال حاضر توسط <code style="font-family: monospace;">XPlainTextFileContentExtractor</code> نیز پوشش داده می‌شود. در <code style="font-family: monospace;">XFileContentExtractor.GetExtractors</code>، انتخاب بر اساس اولین Extractor ثبت‌شده در DI انجام می‌شود و اولویت‌بندی وجود ندارد. برای اطمینان از انتخاب صحیح <code style="font-family: monospace;">XHtmlFileContentExtractor</code>، باید MIME Type <code style="font-family: monospace;">text/html</code> از آرایه <code style="font-family: monospace;">SupportedMimeTypes</code> در <code style="font-family: monospace;">XPlainTextFileContentExtractor</code> حذف شود. این اصلاح در گام ۷ (به‌روزرسانی 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">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 style="font-family: monospace; background: #edf2f7; padding: 2px 6px; border-radius: 4px;">xAiApi.csproj</code>، بسته زیر را اضافه کنید:</p>
<pre>&lt;PackageReference Include="AngleSharp" Version="1.1.2" /&gt;</pre>
<p>یا از طریق .NET CLI:</p>
<pre>dotnet add package AngleSharp</pre>
<div class="info-box">
<strong>💡 نکته:</strong> AngleSharp هیچ وابستگی Native ندارد و در تمام محیط‌های Linux، macOS و Windows کار می‌کند. نیازی به نصب بسته اضافی برای Docker نیست.
</div>
<h4 style="margin-top: 25px; color: var(--primary); font-size: 16px;">گام ۲ — ایجاد اینترفیس اختصاصی</h4>
<p>فایل <code style="font-family: monospace; background: #edf2f7; padding: 2px 6px; border-radius: 4px;">Interfaces/Extractors/IXHtmlFileContentExtractor.cs</code> را با محتوای نشان‌داده‌شده در بخش ۳ ایجاد کنید.</p>
<h4 style="margin-top: 25px; color: var(--primary); font-size: 16px;">گام ۳ — پیاده‌سازی کلاس اصلی</h4>
<p>فایل <code style="font-family: monospace; background: #edf2f7; padding: 2px 6px; border-radius: 4px;">Providers/Extractors/XHtmlFileContentExtractor.cs</code> را ایجاد کنید:</p>
<pre>using System;
using System.IO;
using System.Linq;
using AngleSharp;
using System.Text;
using System.Threading;
using AngleSharp.Html.Parser;
using xAiModels.Models;
using System.Threading.Tasks;
using xAiApi.Interfaces.Extractors;
namespace xAiApi.Providers.Extractors
{
/// &lt;summary&gt;
/// Extracts clean text content from HTML documents
/// (text/html, application/xhtml+xml) using AngleSharp.
/// &lt;/summary&gt;
public class XHtmlFileContentExtractor : IXHtmlFileContentExtractor
{
/// &lt;summary&gt;
/// Supported MIME Types ...
/// &lt;/summary&gt;
private static readonly string[] SupportedMimeTypes =
[
"text/html",
"application/xhtml+xml"
];
/// &lt;summary&gt;
/// Check if this extractor supports the specified MIME type ...
/// &lt;/summary&gt;
public bool CanExtract(string mimeType)
{
return SupportedMimeTypes.Contains(
mimeType?.ToLowerInvariant() ?? string.Empty
);
}
/// &lt;summary&gt;
/// Extract text content from file stream ...
/// &lt;/summary&gt;
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;
}
/// &lt;summary&gt;
/// Extract content from stream as Rich Result ...
/// &lt;/summary&gt;
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
{
// Copy to MemoryStream (AngleSharp requires seekable stream)
await using var ms = new MemoryStream();
await fileStream.CopyToAsync(ms, cancellationToken);
ms.Position = 0;
// Read HTML content with encoding detection
using var reader = new StreamReader(ms, Encoding.UTF8, true);
var html = await reader.ReadToEndAsync();
// Parse HTML with AngleSharp
var parser = new HtmlParser();
using var document = await parser.ParseDocumentAsync(
html, cancellationToken
);
// Extract document title
var title = document.Title?.Trim();
if (!string.IsNullOrWhiteSpace(title))
{
result.Text = $"Title: {title}{Environment.NewLine}{Environment.NewLine}";
}
// Extract visible body text
// TextContent excludes <script> and <style> contents
var bodyText = document.Body?.TextContent ?? string.Empty;
// Normalize whitespace
bodyText = NormalizeWhitespace(bodyText);
// Append body text to result
result.Text += bodyText;
}
catch (OperationCanceledException)
{
throw;
}
catch (Exception ex)
{
result.ErrorMessage =
$"HTML extraction failed: {ex.Message}";
}
return result;
}
// Private helpers ...
}
}</pre>
<h4 style="margin-top: 25px; color: var(--primary); font-size: 16px;">گام ۴ — پیاده‌سازی متدهای کمکی</h4>
<pre>/// &lt;summary&gt;
/// Normalize whitespace in extracted text
/// (collapse multiple spaces/newlines, trim lines)
/// &lt;/summary&gt;
private static string NormalizeWhitespace(string text)
{
if (string.IsNullOrWhiteSpace(text))
{
return string.Empty;
}
// Split into lines, trim each, remove empty lines
var lines = text
.Split(new[] { '\r', '\n' }, StringSplitOptions.RemoveEmptyEntries)
.Select(line => line.Trim())
.Where(line => line.Length > 0);
return string.Join(Environment.NewLine, lines);
}</pre>
<div class="info-box">
<strong>💡 نکته درباره <code style="font-family: monospace;">TextContent</code>:</strong> ویژگی <code style="font-family: monospace;">TextContent</code> در AngleSharp تمام متن‌های گره‌های فرزند را به‌صورت بازگشتی برمی‌گرداند، اما محتوای درون <code style="font-family: monospace;">&lt;script&gt;</code> و <code style="font-family: monospace;">&lt;style&gt;</code> را شامل نمی‌شود. این دقیقاً همان رفتار مطلوب است. همچنین تگ‌های <code style="font-family: monospace;">&lt;head&gt;</code> (شامل عنوان و متادیتا) نیز در <code style="font-family: monospace;">Body.TextContent</code> لحاظ نمی‌شوند و ما عنوان را جداگانه استخراج می‌کنیم.
</div>
<h4 style="margin-top: 25px; color: var(--primary); font-size: 16px;">گام ۵ — ثبت در Dependency Injection</h4>
<p>در فایل <code style="font-family: monospace; background: #edf2f7; padding: 2px 6px; border-radius: 4px;">DI/XDIHelperExtension.cs</code>، متد <code style="font-family: monospace;">AddXAiFileContentExtractors</code> را به‌صورت زیر به‌روزرسانی کنید:</p>
<pre>public static void AddXAiFileContentExtractors(
this IServiceCollection source
)
{
// 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, XPowerPointFileContentExtractor&gt;();
source.AddSingleton&lt;IXFileContentExtractorBase, XHtmlFileContentExtractor&gt;(); // ← جدید
source.AddSingleton&lt;IXFileContentExtractorBase, XPlainTextFileContentExtractor&gt;();
source.AddSingleton&lt;IXFileContentExtractor, XFileContentExtractor&gt;();
}</pre>
<div class="warning-box">
<strong>⚠ نکته مهم درباره ترتیب ثبت:</strong> <code style="font-family: monospace;">XHtmlFileContentExtractor</code> باید <strong>پیش از</strong> <code style="font-family: monospace;">XPlainTextFileContentExtractor</code> ثبت شود. با این حال، از آنجا که <code style="font-family: monospace;">XFileContentExtractor</code> از <code style="font-family: monospace;">FirstOrDefault</code> برای انتخاب استفاده می‌کند و ترتیب <code style="font-family: monospace;">IEnumerable</code> دریافتی از DI معمولاً با ترتیب ثبت مطابقت دارد، ثبت HTML قبل از PlainText انتخاب صحیح را تضمین می‌کند. اما راه‌حل قطعی، حذف MIME Type متداخل از PlainText است (گام ۷).
</div>
<h4 style="margin-top: 25px; color: var(--primary); font-size: 16px;">گام ۶ — (اختیاری) افزودن تنظیمات پیکربندی</h4>
<p>در صورت نیاز به کنترل رفتار استخراج، می‌توانید بخش پیکربندی اضافه کنید:</p>
<pre>{
"AiApiConfiguration": {
"Html": {
"IncludeTitle": true,
"IncludeMetaTags": false,
"MaxContentLength": 0
}
}
}</pre>
<p>این تنظیمات در فاز فعلی اختیاری هستند و در صورت عدم وجود، مقادیر پیش‌فرض (شامل عنوان، بدون متادیتا، بدون محدودیت طول) استفاده می‌شود.</p>
<h4 style="margin-top: 25px; color: var(--primary); font-size: 16px;">گام ۷ — به‌روزرسانی <code style="font-family: monospace;">XPlainTextFileContentExtractor</code> برای رفع تداخل MIME</h4>
<p>MIME Type <code style="font-family: monospace;">text/html</code> باید از آرایه <code style="font-family: monospace;">SupportedMimeTypes</code> در <code style="font-family: monospace;">XPlainTextFileContentExtractor</code> حذف شود:</p>
<pre>// در XPlainTextFileContentExtractor.cs
private static readonly string[] SupportedMimeTypes =
[
"text/plain",
"text/markdown",
"text/csv",
// "text/html", ← حذف شود
"text/xml",
"application/json",
"application/xml"
];</pre>
<div class="success-box">
<strong>✓ نتیجه این اصلاح:</strong> پس از حذف <code style="font-family: monospace;">text/html</code> از PlainText، هیچ تداخل MIME Type وجود ندارد و <code style="font-family: monospace;">XFileContentExtractor</code> به‌طور قطعی <code style="font-family: monospace;">XHtmlFileContentExtractor</code> را برای فایل‌های HTML انتخاب می‌کند. این راه‌حل، وابستگی به ترتیب ثبت در DI را از بین می‌برد و رفتار قطعی و قابل پیش‌بینی ایجاد می‌کند.
</div>
<h4 style="margin-top: 25px; color: var(--primary); font-size: 16px;">گام ۸ — بیلد و اجرای تست‌ها</h4>
<p>پس از اعمال تغییرات:</p>
<ol style="padding-right: 20px; margin: 10px 0;">
<li style="margin-bottom: 8px;">پروژه را بیلد کنید: <code style="font-family: monospace;">dotnet build</code></li>
<li style="margin-bottom: 8px;">تست‌های واحد مربوط به Extractor را اجرا کنید</li>
<li style="margin-bottom: 8px;">سامانه را در محیط Docker/Linux اجرا کنید و یک فایل HTML را از طریق endpoint <code style="font-family: monospace;">ExtractContent</code> ارسال کنید</li>
<li style="margin-bottom: 8px;">صحت خروجی (متن تمیز بدون تگ، شامل عنوان) را بررسی کنید</li>
</ol>
</div>
</div>
<div class="section">
<div class="section-header">
<div class="section-number">۵</div>
<div class="section-title">
<h3>رفتار استخراج و قالب خروجی</h3>
<div class="subtitle">Extraction Behavior & Output Format</div>
</div>
</div>
<div class="section-content">
<p>خروجی این Extractor به‌گونه‌ای طراحی شده است که برای موتور LLM خوانا و ساختاریافته باشد. ترتیب و رفتار استخراج به‌صورت زیر است:</p>
<table class="styled-table">
<thead>
<tr>
<th>#</th>
<th>محتوا</th>
<th>رفتار</th>
<th>نمونه خروجی</th>
</tr>
</thead>
<tbody>
<tr>
<td>۱</td>
<td>عنوان سند</td>
<td>از تگ <code>&lt;title&gt;</code> استخراج می‌شود</td>
<td><code>Title: صفحه اصلی</code></td>
</tr>
<tr>
<td>۲</td>
<td>متن بدنه</td>
<td>از <code>Body.TextContent</code> (بدون script/style)</td>
<td>متن قابل مشاهده صفحه</td>
</tr>
<tr>
<td>۳</td>
<td>نرمال‌سازی فاصله‌ها</td>
<td>حذف خطوط خالی و فاصله‌های اضافی</td>
<td>خطوط تمیز و مرتب</td>
</tr>
<tr>
<td>۴</td>
<td>تگ‌های حذف‌شده</td>
<td><code>&lt;script&gt;</code>، <code>&lt;style&gt;</code>، <code>&lt;head&gt;</code> (به‌جز عنوان)</td>
<td>—</td>
</tr>
</tbody>
</table>
<h4 style="margin-top: 25px; color: var(--primary); font-size: 16px;">نمونه خروجی</h4>
<pre>Title: راهنمای محصول
این یک پاراگراف نمونه است که متن قابل مشاهده صفحه را نشان می‌دهد.
لیست زیر شامل موارد مهم است:
- مورد اول
- مورد دوم
- مورد سوم
این متن از یک تگ div دیگر است.</pre>
<div class="highlight-box">
<strong>نکته درباره محتوای غیرقابل مشاهده:</strong> AngleSharp با استفاده از <code style="font-family: monospace;">TextContent</code> به‌طور خودکار محتوای درون <code style="font-family: monospace;">&lt;script&gt;</code> و <code style="font-family: monospace;">&lt;style&gt;</code> را حذف می‌کند. عناصر مخفی با CSS (مانند <code style="font-family: monospace;">display:none</code>) در <code style="font-family: monospace;">TextContent</code> حضور دارند زیرا این ویژگی صرفاً به ساختار DOM نگاه می‌کند، نه به استایل بصری. در صورتی که نیاز به حذف عناصر مخفی بصری باشد، می‌توان از افزونه <code style="font-family: monospace;">AngleSharp.Css</code> و متد <code style="font-family: monospace;">GetInnerText</code> استفاده کرد. برای فاز فعلی، رفتار <code style="font-family: monospace;">TextContent</code> کافی و مناسب است.
</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><code>CanExtract</code> برای MIME های پشتیبانی‌شده</td>
<td><code>text/html</code>، <code>application/xhtml+xml</code></td>
<td>هر دو <code>true</code></td>
</tr>
<tr>
<td>۲</td>
<td><code>CanExtract</code> برای MIME نامربوط</td>
<td><code>application/pdf</code></td>
<td><code>false</code></td>
</tr>
<tr>
<td>۳</td>
<td>استخراج HTML ساده</td>
<td>فایل HTML با تگ‌های <code>p</code>، <code>h1</code>، <code>ul</code></td>
<td>متن تمیز بدون تگ</td>
</tr>
<tr>
<td>۴</td>
<td>حذف script و style</td>
<td>HTML حاوی کد جاوااسکریپت و CSS</td>
<td>هیچ کد JS یا CSS در خروجی نباشد</td>
</tr>
<tr>
<td>۵</td>
<td>استخراج عنوان</td>
<td>HTML با تگ <code>&lt;title&gt;عنوان تست&lt;/title&gt;</code></td>
<td>خروجی شامل <code>Title: عنوان تست</code></td>
</tr>
<tr>
<td>۶</td>
<td>HTML بدون عنوان</td>
<td>HTML بدون تگ <code>title</code></td>
<td>خروجی فاقد خط <code>Title:</code> باشد</td>
</tr>
<tr>
<td>۷</td>
<td>HTML خالی یا بدشکل</td>
<td>رشته خالی یا HTML نامعتبر</td>
<td><code>ErrorMessage</code> تنظیم شود یا خروجی خالی</td>
</tr>
<tr>
<td>۸</td>
<td>XHTML</td>
<td>فایل <code>.xhtml</code> با MIME <code>application/xhtml+xml</code></td>
<td>مانند HTML معمولی پردازش شود</td>
</tr>
<tr>
<td>۹</td>
<td>حذف تگ‌های تودرتو</td>
<td>HTML با ساختار پیچیده <code>div</code> تو در تو</td>
<td>فقط متن قابل مشاهده بدون تکرار</td>
</tr>
<tr class="highlight-row">
<td>۱۰</td>
<td>اجرا در Docker/Linux</td>
<td>ایمیج ساخته‌شده</td>
<td>بدون خطای <code>PlatformNotSupportedException</code></td>
</tr>
</tbody>
</table>
<div class="success-box">
<strong>✓ معیار پذیرش نهایی:</strong>
<ul style="padding-right: 20px; margin-top: 10px;">
<li>تمام ۱۰ سناریو پاس شوند</li>
<li>MIME Type <code style="font-family: monospace;">text/html</code> منحصراً به <code style="font-family: monospace;">XHtmlFileContentExtractor</code> برسد (پس از اصلاح PlainText)</li>
<li>خروجی برای موتور LLM خوانا و فاقد تگ‌های HTML باشد</li>
<li>اجرای موفق در محیط Docker Linux</li>
<li>هیچ استثنایی به‌صورت Unhandled پرتاب نشود</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 style="font-family: monospace;">XHtmlFileContentExtractor</code> با استفاده از کتابخانه <strong>AngleSharp</strong>، پوشش کاملی از فرمت‌های HTML و XHTML ارائه می‌دهد. این Extractor با حذف خودکار محتوای <code style="font-family: monospace;">&lt;script&gt;</code> و <code style="font-family: monospace;">&lt;style&gt;</code> و استخراج متن قابل مشاهده، کیفیت پردازش فایل‌های وب در سامانه xAiApi را به‌طور قابل توجهی ارتقاء می‌دهد. مشکل تداخل MIME Type با <code style="font-family: monospace;">XPlainTextFileContentExtractor</code> نیز با حذف <code style="font-family: monospace;">text/html</code> از آن Extractor حل می‌شود و انتخاب قطعی <code style="font-family: monospace;">XHtmlFileContentExtractor</code> را تضمین می‌کند.</p>
<table class="styled-table">
<thead>
<tr>
<th>محور</th>
<th>وضعیت قبل</th>
<th>وضعیت بعد</th>
</tr>
</thead>
<tbody>
<tr>
<td>پردازش HTML</td>
<td style="color: var(--danger); font-weight: 600;">خام (شامل تگ‌ها)</td>
<td style="color: var(--success); font-weight: 600;">متن تمیز و ساختاریافته</td>
</tr>
<tr>
<td>حذف script/style</td>
<td style="color: var(--danger); font-weight: 600;">✗</td>
<td style="color: var(--success); font-weight: 600;">✓ خودکار توسط AngleSharp</td>
</tr>
<tr>
<td>استخراج عنوان</td>
<td style="color: var(--danger); font-weight: 600;">✗</td>
<td style="color: var(--success); font-weight: 600;">✓ از تگ &lt;title&gt;</td>
</tr>
<tr>
<td>تداخل MIME Type</td>
<td style="color: var(--danger); font-weight: 600;">دارد (با PlainText)</td>
<td style="color: var(--success); font-weight: 600;">حل شد (حذف از PlainText)</td>
</tr>
<tr>
<td>Cross-platform</td>
<td>—</td>
<td style="color: var(--success); font-weight: 600;">✓ کامل (AngleSharp)</td>
</tr>
<tr class="highlight-row">
<td><strong>آمادگی تولیدی</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>با توجه به طراحی ارائه‌شده، این Extractor آماده پیاده‌سازی است و تمام مراحل در این مستند به‌صورت گام‌به‌گام شرح داده شده است. پیشنهاد می‌شود ابتدا گام‌های ۱ تا ۵ (ایجاد و ثبت Extractor) اعمال شود و سپس گام ۷ (حذف MIME متداخل) برای تضمین انتخاب قطعی انجام گیرد.</p>
<div class="highlight">
<strong>💎 ارزش پیشنهادی:</strong><br>
استخراج متن تمیز از HTML + حذف خودکار script/style + استخراج عنوان + پشتیبانی از XHTML + استقرار Cross-platform + یکپارچگی کامل با Composite Extractor
</div>
</div>
<div class="info-box">
<strong>📞 گام بعدی:</strong> در صورت تایید این مستند، آماده‌ام پیاده‌سازی را آغاز کرده و کد کامل و نهایی <code style="font-family: monospace;">XHtmlFileContentExtractor</code> را به‌همراه اصلاح <code style="font-family: monospace;">XPlainTextFileContentExtractor</code> و به‌روزرسانی DI ارائه کنم.
</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>