Files
xSaherelmWorkspace/Documents/Docs/Developer/XEpubFile Content Extractor.html
T

996 lines
43 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>مستند پیاده‌سازی XEpubFileContentExtractor | پروژه 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-060</div>
<div>نسخه: ۱.۰</div>
<div>تاریخ تهیه: ۲ آبان ۱۴۰۵</div>
<div>طبقه‌بندی: محرمانه</div>
</div>
</div>
<div class="document-title">
<span class="label">IMPLEMENTATION GUIDE</span>
<h1>مستند پیاده‌سازی XEpubFileContentExtractor</h1>
<h2>راهنمای کامل گام‌به‌گام افزودن Extractor اختصاصی EPUB به سامانه xAiApi</h2>
</div>
</header>
<div class="document-body">
<div class="intro-text">
<strong>به کارفرمای محترم،</strong><br>
این مستند حاصل طراحی و پیاده‌سازی کامل <code>XEpubFileContentExtractor</code> به‌عنوان یکی از Extractor های اولویت پایین در زیرسیستم <code>xAiApi/Providers/Extractors</code> است. فرمت EPUB (Electronic Publication) استاندارد باز کتاب‌های الکترونیکی است که توسط انتشارات دیجیتال، کتابخانه‌ها و سامانه‌های آموزشی به‌کار می‌رود. در حال حاضر سامانه xAiApi فاقد یک Extractor اختصاصی برای EPUB است و این فرمت‌ها در صورت ارسال، پردازش نمی‌شوند. هدف این مستند، ارائه راهنمای گام‌به‌گام برای پیاده‌سازی یک Extractor اختصاصی با استفاده از کتابخانه <strong>VersOne.Epub</strong> است که متن هر فصل کتاب را استخراج و در اختیار موتور LLM قرار دهد.
</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>فرمت EPUB یک استاندارد باز بر پایه ZIP و XHTML است که توسط سازمان W3C نگهداری می‌شود. هر فایل EPUB در واقع یک آرشیو ZIP است که شامل فایل <code>mimetype</code> با محتوای <code>application/epub+zip</code>، فایل <code>META-INF/container.xml</code> و مجموعه‌ای از فایل‌های XHTML است که محتوای کتاب را تشکیل می‌دهند[reference:0].</p>
<p>در حال حاضر هیچ Extractor اختصاصی برای EPUB در سامانه وجود ندارد. با پیاده‌سازی <code>XEpubFileContentExtractor</code> با استفاده از <strong>VersOne.Epub</strong>، سامانه قادر خواهد بود کتاب‌های الکترونیکی را در محیط‌های Docker/Linux پردازش کند، متن هر فصل را استخراج نماید و در اختیار موتور LLM قرار دهد.</p>
<div class="stats-grid">
<div class="stat-card">
<div class="stat-value">۱<br>فرمت</div>
<div class="stat-label">EPUB 2 و EPUB 3</div>
</div>
<div class="stat-card">
<div class="stat-value">۲<br>کتابخانه</div>
<div class="stat-label">VersOne.Epub + HtmlAgilityPack</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> پس از پیاده‌سازی این Extractor، سامانه xAiApi می‌تواند کتاب‌های الکترونیکی EPUB را به‌طور کامل پردازش کند، متن تمیز هر فصل را استخراج نماید و آن را برای تحلیل، خلاصه‌سازی و پاسخ‌گویی مبتنی بر RAG در اختیار LLM قرار دهد. این Extractor با ساختار DI موجود کاملاً یکپارچه می‌شود.
</div>
</div>
</div>
<div class="section">
<div class="section-header">
<div class="section-number">۲</div>
<div class="section-title">
<h3>معرفی فرمت EPUB</h3>
<div class="subtitle">EPUB Format Overview</div>
</div>
</div>
<div class="section-content">
<p>فرمت EPUB یک استاندارد باز برای کتاب‌های الکترونیکی است که توسط کنسرسیوم W3C نگهداری می‌شود. این فرمت از دو نسخه اصلی پشتیبانی می‌کند: <strong>EPUB 2</strong> (2.0، 2.0.1) و <strong>EPUB 3</strong> (3.0، 3.0.1، 3.1، 3.2، 3.3)[reference:1].</p>
<h4 style="margin-top: 25px; color: var(--primary); font-size: 16px;">ساختار داخلی EPUB</h4>
<table class="styled-table">
<thead>
<tr>
<th>بخش</th>
<th>توضیح</th>
</tr>
</thead>
<tbody>
<tr>
<td><code>mimetype</code></td>
<td>اولین فایل آرشیو ZIP؛ محتوای آن باید دقیقاً <code>application/epub+zip</code> باشد[reference:2]</td>
</tr>
<tr>
<td><code>META-INF/container.xml</code></td>
<td>نقطه ورود به فایل OPF (Package Document)</td>
</tr>
<tr>
<td>فایل OPF</td>
<td>متادیتای کتاب (عنوان، نویسنده)، فهرست فایل‌های محتوا و ترتیب خواندن (Spine)</td>
</tr>
<tr>
<td>فایل‌های XHTML</td>
<td>محتوای واقعی کتاب (فصل‌ها، بخش‌ها)</td>
</tr>
<tr>
<td>فایل NCX / Navigation</td>
<td>فهرست مطالب (Table of Contents)</td>
</tr>
</tbody>
</table>
<h4 style="margin-top: 25px; color: var(--primary); font-size: 16px;">MIME Type</h4>
<p>MIME Type رسمی ثبت‌شده برای EPUB، <code>application/epub+zip</code> است[reference:3]. این مقدار در فایل <code>mimetype</code> داخل آرشیو و همچنین در هدر <code>Content-Type</code> هنگام ارسال فایل استفاده می‌شود.</p>
<div class="info-box">
<strong>💡 نکته:</strong> در برخی سامانه‌ها ممکن است MIME Type به‌صورت <code>application/zip</code> یا <code>application/octet-stream</code> گزارش شود. در این حالت، تشخیص قطعی بر اساس محتوای فایل (وجود <code>mimetype</code> با مقدار <code>application/epub+zip</code>) انجام می‌شود. در فاز فعلی، <code>XEpubFileContentExtractor</code> فقط بر اساس MIME Type عمل می‌کند.
</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>برای پردازش EPUB در .NET، چندین گزینه وجود دارد. جدول زیر گزینه‌های اصلی را با معیارهای کلیدی مقایسه می‌کند:</p>
<table class="styled-table">
<thead>
<tr>
<th>کتابخانه</th>
<th>EPUB 2</th>
<th>EPUB 3</th>
<th>Cross-platform</th>
<th>مجوز</th>
<th>بلوغ</th>
<th>ارزیابی</th>
</tr>
</thead>
<tbody>
<tr class="highlight-row">
<td><strong>VersOne.Epub</strong></td>
<td style="color: var(--success); font-weight: 600;">✓</td>
<td style="color: var(--success); font-weight: 600;">✓</td>
<td style="color: var(--success); font-weight: 600;">✓</td>
<td>MIT (رایگان)</td>
<td>بالغ (فعال)</td>
<td>بهترین گزینه؛ پشتیبانی کامل از EPUB 2 و 3</td>
</tr>
<tr>
<td>EpubSharp</td>
<td style="color: var(--success); font-weight: 600;">✓</td>
<td style="color: var(--success); font-weight: 600;">✓</td>
<td style="color: var(--success); font-weight: 600;">✓</td>
<td>MPL-2.0</td>
<td>متوسط</td>
<td>قابلیت ویرایش محدود؛ نگهداری کمتر فعال</td>
</tr>
<tr>
<td>OfficeIMO.Reader.Epub</td>
<td style="color: var(--success); font-weight: 600;">✓</td>
<td style="color: var(--success); font-weight: 600;">✓</td>
<td style="color: var(--success); font-weight: 600;">✓</td>
<td>MIT</td>
<td>جدید</td>
<td>گزینه جایگزین؛ هم‌خانواده با OfficeIMO موجود</td>
</tr>
<tr>
<td>Aspose.HTML</td>
<td style="color: var(--success); font-weight: 600;">✓</td>
<td style="color: var(--success); font-weight: 600;">✓</td>
<td style="color: var(--success); font-weight: 600;">✓</td>
<td style="color: var(--danger); font-weight: 600;">تجاری</td>
<td>بالغ</td>
<td>لایسنس تجاری مانع استفاده است</td>
</tr>
</tbody>
</table>
<h4 style="margin-top: 25px; color: var(--primary); font-size: 16px;">دلایل انتخاب VersOne.Epub</h4>
<div class="success-box">
<strong>✓ دلیل ۱ — پشتیبانی کامل از استانداردهای EPUB:</strong> طبق مستندات رسمی، VersOne.Epub از EPUB 2 (2.0، 2.0.1) و EPUB 3 (3.0، 3.0.1، 3.1، 3.2، 3.3) پشتیبانی می‌کند و روی .NET Standard 1.3 به بالا (شامل .NET Core و .NET 5+) کار می‌کند[reference:4].
</div>
<div class="success-box">
<strong>✓ دلیل ۲ — مجوز MIT و Cross-platform:</strong> مجوز MIT استفاده تجاری بدون هزینه را ممکن می‌سازد و کتابخانه کاملاً Managed و Cross-platform است و در Docker/Linux بدون وابستگی Native کار می‌کند.
</div>
<div class="success-box">
<strong>✓ دلیل ۳ — الگوی رسمی برای استخراج متن:</strong> مستندات رسمی VersOne.Epub یک الگوی کامل برای استخراج متن ارائه می‌دهد: بارگذاری کتاب با <code>EpubReader.ReadBook</code>، پیمایش <code>book.ReadingOrder</code> و استخراج متن از هر <code>EpubTextContentFile.Content</code> با <strong>HtmlAgilityPack</strong>[reference:5]. این الگو دقیقاً همان چیزی است که برای پیاده‌سازی ما نیاز داریم.
</div>
<div class="success-box">
<strong>✓ دلیل ۴ — دسترسی به متادیتا و فهرست مطالب:</strong> این کتابخانه علاوه بر متن، دسترسی به <code>book.Title</code>، <code>book.Author</code> و <code>book.Navigation</code> (فهرست مطالب) را نیز فراهم می‌کند که برای ساخت خروجی ساختاریافته بسیار ارزشمند است[reference:6].
</div>
<div class="info-box">
<strong>💡 نکته درباره HtmlAgilityPack:</strong> VersOne.Epub خودش متن را از XHTML استخراج نمی‌کند. طبق مستندات رسمی، باید بسته <code>HtmlAgilityPack</code> را نیز نصب کنید و محتوای <code>EpubTextContentFile.Content</code> را با آن پارس نمایید[reference:7]. این بسته در حال حاضر در پروژه برای <code>XHtmlFileContentExtractor</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>طراحی <code>XEpubFileContentExtractor</code> با الگوی موجود در پروژه کاملاً هم‌راستا است. یک اینترفیس اختصاصی که از <code>IXFileContentExtractorBase</code> ارث‌بری می‌کند و یک کلاس پیاده‌ساز مطابق الگوی سایر Extractor ها ایجاد می‌شود.</p>
<h4 style="margin-top: 20px; color: var(--primary); font-size: 16px;">۱. اینترفیس اختصاصی</h4>
<p>فایل <code>Interfaces/Extractors/IXEpubFileContentExtractor.cs</code>:</p>
<pre>namespace xAiApi.Interfaces.Extractors
{
/// &lt;summary&gt;
/// Extractor for EPUB electronic publication format
/// (application/epub+zip)
/// &lt;/summary&gt;
public interface IXEpubFileContentExtractor : IXFileContentExtractorBase
{ }
}</pre>
<h4 style="margin-top: 25px; color: var(--primary); font-size: 16px;">۲. طراحی کلاس اصلی</h4>
<p>کلاس <code>XEpubFileContentExtractor</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>application/epub+zip</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>ExtractEpubAsync(...)</code></td>
<td>private Task&lt;string&gt;</td>
<td>منطق اصلی استخراج با VersOne.Epub</td>
</tr>
<tr>
<td><code>ExtractTextFromHtml(string)</code></td>
<td>private string</td>
<td>استخراج متن از XHTML با HtmlAgilityPack</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 EPUB به پروژه xAiApi انجام می‌شود:</p>
<h4 style="margin-top: 20px; color: var(--primary); font-size: 16px;">گام ۱ — افزودن بسته NuGet</h4>
<p>در فایل <code>xAiApi.csproj</code>، بسته <code>VersOne.Epub</code> را اضافه کنید. دستور نصب از طریق .NET CLI:</p>
<pre>dotnet add package VersOne.Epub</pre>
<p>یا مستقیماً در فایل پروژه:</p>
<pre>&lt;PackageReference Include="VersOne.Epub" Version="3.3.6" /&gt;</pre>
<div class="info-box">
<strong>💡 نکته:</strong> بسته <code>HtmlAgilityPack</code> از قبل در پروژه برای <code>XHtmlFileContentExtractor</code> وجود دارد و نیازی به افزودن مجدد نیست. اگر به هر دلیلی موجود نبود، با دستور <code>dotnet add package HtmlAgilityPack</code> نصب کنید[reference:8].
</div>
<h4 style="margin-top: 25px; color: var(--primary); font-size: 16px;">گام ۲ — ایجاد اینترفیس اختصاصی</h4>
<p>فایل جدید <code>Interfaces/Extractors/IXEpubFileContentExtractor.cs</code> را با محتوای نشان‌داده‌شده در بخش ۴ (بالا) ایجاد کنید.</p>
<h4 style="margin-top: 25px; color: var(--primary); font-size: 16px;">گام ۳ — پیاده‌سازی کلاس اصلی</h4>
<p>فایل <code>Providers/Extractors/XEpubFileContentExtractor.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 VersOne.Epub;
using HtmlAgilityPack;
using System.Threading.Tasks;
using xAiApi.Interfaces.Extractors;
namespace xAiApi.Providers.Extractors
{
/// &lt;summary&gt;
/// Extracts text content from EPUB electronic publication files
/// using VersOne.Epub and HtmlAgilityPack.
/// &lt;/summary&gt;
public class XEpubFileContentExtractor : IXEpubFileContentExtractor
{
private static readonly string[] SupportedMimeTypes =
[
"application/epub+zip"
];
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,
"book.epub",
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 ExtractEpubAsync(
fileStream,
cancellationToken
);
}
catch (Exception ex)
{
result.ErrorMessage =
$"EPUB extraction failed: {ex.Message}";
}
return result;
}
private async Task&lt;string&gt; ExtractEpubAsync(
Stream fileStream,
CancellationToken cancellationToken
)
{
// Copy to MemoryStream (VersOne.Epub requires seekable stream)
await using var ms = new MemoryStream();
await fileStream.CopyToAsync(ms, cancellationToken);
ms.Position = 0;
// Load EPUB book
EpubBook book = await EpubReader.ReadBookAsync(ms);
var sb = new StringBuilder();
// Extract metadata
if (!string.IsNullOrWhiteSpace(book.Title))
{
sb.AppendLine($"Title: {book.Title}");
}
if (!string.IsNullOrWhiteSpace(book.Author))
{
sb.AppendLine($"Author: {book.Author}");
}
sb.AppendLine();
// Extract chapters in reading order
var chapterIndex = 0;
foreach (EpubTextContentFile textContentFile in book.ReadingOrder)
{
if (cancellationToken.IsCancellationRequested)
break;
chapterIndex++;
sb.AppendLine($"[Chapter {chapterIndex}]");
var plainText = ExtractTextFromHtml(
textContentFile.Content
);
if (!string.IsNullOrWhiteSpace(plainText))
{
sb.AppendLine(plainText);
}
sb.AppendLine();
}
return sb.ToString().TrimEnd();
}
private string ExtractTextFromHtml(string html)
{
if (string.IsNullOrWhiteSpace(html))
{
return string.Empty;
}
var htmlDocument = new HtmlDocument();
htmlDocument.LoadHtml(html);
var sb = new StringBuilder();
var textNodes = htmlDocument.DocumentNode
.SelectNodes("//text()");
if (textNodes == null)
{
return string.Empty;
}
foreach (var node in textNodes)
{
var text = node.InnerText?.Trim();
if (!string.IsNullOrWhiteSpace(text))
{
sb.AppendLine(text);
}
}
return sb.ToString().TrimEnd();
}
}
}</pre>
<h4 style="margin-top: 25px; color: var(--primary); font-size: 16px;">گام ۴ — توضیح تکمیلی متدهای کلیدی</h4>
<p><strong>متد <code>ExtractEpubAsync</code>:</strong> این متد ابتدا محتوای EPUB را از <code>Stream</code> به <code>MemoryStream</code> کپی می‌کند، زیرا <code>EpubReader.ReadBookAsync</code> به استریم Seekable نیاز دارد. سپس با <code>EpubReader.ReadBookAsync(ms)</code> کتاب را بارگذاری می‌کند. متادیتای کتاب (عنوان و نویسنده) از <code>book.Title</code> و <code>book.Author</code> استخراج می‌شود[reference:9]. سپس با پیمایش <code>book.ReadingOrder</code> (که ترتیب صحیح خواندن فصل‌ها را بر اساس فایل Spine تعیین می‌کند)، محتوای هر فصل استخراج می‌شود[reference:10].</p>
<p><strong>متد <code>ExtractTextFromHtml</code>:</strong> این متد محتوای XHTML هر فصل را با <code>HtmlDocument.LoadHtml</code> بارگذاری می‌کند و سپس با <code>SelectNodes("//text()")</code> تمام گره‌های متنی را انتخاب می‌کند. این روش دقیقاً همان الگویی است که در مستندات رسمی VersOne.Epub توصیه شده است[reference:11]. متن هر گره با <code>InnerText.Trim()</code> استخراج و در <code>StringBuilder</code> تجمیع می‌شود.</p>
<div class="success-box">
<strong>✓ نکته درباره <code>book.ReadingOrder</code>:</strong> ویژگی <code>ReadingOrder</code> در <code>EpubBook</code>، مجموعه‌ای از <code>EpubTextContentFile</code> را به ترتیب صحیح خواندن برمی‌گرداند. این ترتیب از فایل OPF (به‌ویژه عنصر Spine) استخراج می‌شود و تضمین می‌کند که فصل‌ها به همان ترتیبی که نویسنده تعیین کرده، پردازش شوند.
</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, XEpubFileContentExtractor&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;IXFileContentExtractorBase, XRtfFileContentExtractor&gt;();
source.AddSingleton&lt;IXFileContentExtractorBase, XOpenDocumentFileContentExtractor&gt;();
source.AddSingleton&lt;IXFileContentExtractorBase, XEpubFileContentExtractor&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 برای EPUB</td>
<td><code>application/epub+zip</code></td>
<td><code>true</code> برگرداند</td>
</tr>
<tr>
<td>۲</td>
<td>CanExtract برای MIME نامربوط</td>
<td><code>application/pdf</code></td>
<td><code>false</code> برگرداند</td>
</tr>
<tr>
<td>۳</td>
<td>استخراج EPUB ساده</td>
<td>کتاب EPUB با ۳ فصل</td>
<td>متن هر ۳ فصل با برچسب <code>[Chapter N]</code></td>
</tr>
<tr>
<td>۴</td>
<td>استخراج متادیتا</td>
<td>EPUB با Title و Author</td>
<td>خروجی شامل <code>Title:</code> و <code>Author:</code></td>
</tr>
<tr>
<td>۵</td>
<td>ترتیب فصل‌ها</td>
<td>EPUB با فصل‌های نامرتب در ZIP</td>
<td>خروجی به ترتیب <code>ReadingOrder</code></td>
</tr>
<tr>
<td>۶</td>
<td>EPUB 2</td>
<td>کتاب با فرمت EPUB 2.0</td>
<td>پردازش موفق</td>
</tr>
<tr>
<td>۷</td>
<td>EPUB 3</td>
<td>کتاب با فرمت EPUB 3.3</td>
<td>پردازش موفق</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>application/epub+zip</code> به‌درستی پشتیبانی شود</li>
<li>متن تمام فصل‌ها به ترتیب <code>ReadingOrder</code> استخراج شود</li>
<li>متادیتای کتاب (عنوان و نویسنده) در خروجی حضور داشته باشد</li>
<li>هیچ استثنایی به‌صورت Unhandled پرتاب نشود</li>
<li>اجرای موفق در محیط Docker/Linux</li>
<li>یکپارچگی کامل با Composite Extractor و DI</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>XEpubFileContentExtractor</code> با استفاده از <strong>VersOne.Epub</strong> و <strong>HtmlAgilityPack</strong>، شکاف موجود در پردازش کتاب‌های الکترونیکی EPUB را به‌طور کامل پر می‌کند. این Extractor با پشتیبانی از EPUB 2 و EPUB 3، استخراج متادیتا و متن فصل‌ها به ترتیب صحیح، و کارکرد Cross-platform، خروجی با کیفیت بالا و مناسب برای موتور LLM فراهم می‌آورد.</p>
<table class="styled-table">
<thead>
<tr>
<th>محور</th>
<th>قبل از پیاده‌سازی</th>
<th>پس از پیاده‌سازی</th>
</tr>
</thead>
<tbody>
<tr>
<td>پشتیبانی از EPUB</td>
<td style="color: var(--danger); font-weight: 600;">✗</td>
<td style="color: var(--success); font-weight: 600;">✓</td>
</tr>
<tr>
<td>EPUB 2</td>
<td style="color: var(--danger); font-weight: 600;">✗</td>
<td style="color: var(--success); font-weight: 600;">✓</td>
</tr>
<tr>
<td>EPUB 3</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;">✓ (ReadingOrder)</td>
</tr>
<tr>
<td>Cross-platform</td>
<td style="color: var(--warning); font-weight: 600;">—</td>
<td style="color: var(--success); font-weight: 600;">✓</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>با توجه به تحلیل‌های انجام‌شده، VersOne.Epub به‌عنوان کتابخانه اصلی برای پیاده‌سازی XEpubFileContentExtractor انتخاب می‌شود. این انتخاب مبتنی بر پشتیبانی کامل از EPUB 2 و 3، مجوز MIT، بلوغ و الگوی رسمی استخراج متن است.</p>
<div class="highlight">
<strong>💎 ارزش پیشنهادی:</strong><br>
پوشش EPUB 2 و 3 + استخراج متادیتا + ترتیب صحیح فصل‌ها + Cross-platform + یکپارچگی کامل با معماری موجود
</div>
</div>
<div class="info-box">
<strong>📞 گام بعدی:</strong> در صورت تایید این مستند، آماده‌ام پیاده‌سازی کامل را آغاز نمایم. پیشنهاد می‌شود پس از پیاده‌سازی، تست‌های ۱ تا ۹ انجام شود و سپس در محیط Docker/Linux نهایی گردد. در صورت نیاز به استخراج فهرست مطالب (Navigation) در فاز بعدی، می‌توان از <code>book.Navigation</code> استفاده کرد.
</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>