last Documents and Module Updating ...

This commit is contained in:
2026-10-22 19:09:01 +03:30
parent 3143086526
commit bd8abbffc4
16 changed files with 9844 additions and 29795 deletions
+9
View File
@@ -517,6 +517,15 @@
"id": "171f43f3-ae73-49b7-9239-9f3fb695cc2b",
"title": "InProgress",
"cards": [
{
"id": "cf174349-2e72-442f-b4cb-c77b69286136",
"listId": "171f43f3-ae73-49b7-9239-9f3fb695cc2b",
"title": "Adding Other File Extractors ...",
"description": "Research About File Content Extraction and add Others ...",
"labels": [],
"checkboxes": [],
"comments": []
},
{
"id": "17bbcc83-4330-4631-a08a-521c99047ebc",
"listId": "171f43f3-ae73-49b7-9239-9f3fb695cc2b",
@@ -0,0 +1,450 @@
<!DOCTYPE html>
<html lang="fa" dir="rtl">
<head>
<meta charset="UTF-8"><meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>راهنمای گام‌به‌گام پیاده‌سازی Extended File Content Extractors | 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;--bg:#faf9f6;--text:#1a202c;--muted:#4a5568;--border:#e2e8f0;--success:#276749;--warning:#9c4221}
*{box-sizing:border-box}body{font-family:'Vazirmatn',Tahoma,sans-serif;background:var(--bg);color:var(--text);line-height:1.95;font-size:14px;margin:0;padding:24px}
.document-container{max-width:1100px;margin:auto;background:#fff;box-shadow:0 8px 35px #1a365d16;border-radius:12px;overflow:hidden}
.document-header{background:linear-gradient(135deg,var(--primary),var(--primary-light));color:white;padding:48px 42px 38px;border-bottom:4px solid var(--accent)}
.header-meta{display:flex;justify-content:space-between;gap:20px;font-size:12px;opacity:.9}.document-title{text-align:center;padding:30px 0 10px}.label{display:inline-block;color:var(--accent);border:1px solid #c9a96166;background:#c9a96122;border-radius:20px;padding:4px 15px;font-size:12px}
h1{font-size:28px;line-height:1.5;margin:14px 0 8px}h2{font-size:21px;color:var(--primary)}h3{font-size:19px;color:var(--primary);margin:0 0 3px}h4{font-size:16px;color:var(--primary);margin:24px 0 10px}.subtitle{font-size:12px;color:var(--muted)}
.document-body{padding:38px}.intro-text{background:linear-gradient(to left,#f7fafc,#edf2f7);border-right:4px solid var(--accent);padding:20px 24px;border-radius:8px;margin-bottom:30px}
.section{margin:0 0 42px;page-break-inside:avoid}.section-header{display:flex;gap:15px;align-items:center;border-bottom:2px solid var(--border);padding-bottom:13px;margin-bottom:18px}
.section-number{min-width:50px;height:50px;display:flex;align-items:center;justify-content:center;background:linear-gradient(135deg,var(--primary),var(--primary-light));color:white;border-radius:12px;font-weight:800;font-size:17px}
.section-content{padding-right:8px}.section-content p{text-align:justify;margin:8px 0 14px}.info-box,.warning-box,.success-box,.highlight-box{padding:16px 20px;border-radius:8px;margin:18px 0}.info-box{background:#ebf8ff;border:1px solid #90cdf4;border-right:4px solid var(--primary-light)}.warning-box{background:#fffaf0;border:1px solid #fbd38d;border-right:4px solid var(--warning)}.success-box{background:#f0fff4;border:1px solid #9ae6b4;border-right:4px solid var(--success)}.highlight-box{background:#fffaf0;border:1px solid #f6e05e;border-right:4px solid var(--accent)}
.steps{padding-right:24px}.steps li{padding:5px 0 8px;text-align:justify}.steps strong{color:var(--primary)}
pre{direction:ltr;text-align:left;white-space:pre-wrap;overflow-wrap:anywhere;background:#111827;color:#e5e7eb;border-radius:8px;padding:18px;font:12px/1.7 Consolas,Monaco,monospace;overflow-x:auto}code{font-family:Consolas,Monaco,monospace;background:#edf2f7;padding:2px 5px;border-radius:4px;direction:ltr;unicode-bidi:embed}
.toc{background:#f8fafc;border:1px solid var(--border);border-radius:10px;padding:22px 25px;margin:25px 0}.toc ol{columns:2;column-gap:35px}.toc li{break-inside:avoid;padding:3px 0}.toc a{color:var(--primary);text-decoration:none}.toc a:hover{text-decoration:underline}
table{border-collapse:collapse;width:100%;margin:18px 0}th{background:var(--primary);color:white;text-align:right}th,td{padding:10px 12px;border:1px solid var(--border);vertical-align:top}tr:nth-child(even){background:#f8fafc}
.footer{background:var(--primary);color:white;text-align:center;padding:24px;font-size:12px}.muted{color:var(--muted);font-size:12px}
@media(max-width:700px){body{padding:0}.document-body{padding:20px}.document-header{padding:30px 20px}.header-meta{flex-direction:column}.toc ol{columns:1}h1{font-size:23px}}
@media print{body{padding:0;background:white}.document-container{box-shadow:none}.document-header{-webkit-print-color-adjust:exact;print-color-adjust:exact}.section{break-inside:auto}pre{white-space:pre-wrap;break-inside:avoid}a{color:inherit}}
</style></head>
<body><div class="document-container">
<header class="document-header">
<div class="header-meta"><div><strong>xAiApi</strong><br>راهنمای فنی پیاده‌سازی</div><div>شماره مستند: TECH-1405-015<br>نسخه: 1.0<br>تاریخ: ۳۰ مهر ۱۴۰۵<br>طبقه‌بندی: محرمانه</div></div>
<div class="document-title"><span class="label">IMPLEMENTATION GUIDE</span><h1>راهنمای گام‌به‌گام پیاده‌سازی File Content Extractors</h1><div>شرح اجرایی ۱۳ Extractor پیشنهادی بر اساس قالب مستند «Extends File Content Extractors»</div></div>
</header>
<main class="document-body">
<div class="intro-text"><strong>هدف مستند</strong><p>این راهنما قالب بصری و چارچوب پیشنهادهای مستند مرجع را دنبال می‌کند و برای هر یک از ۱۳ Extractor پیشنهادی، دامنه، وابستگی، مراحل پیاده‌سازی، نمونه کد آغازین و آزمون‌های پذیرش را شرح می‌دهد. این متن راهنمای طراحی و پیاده‌سازی است؛ نمونه‌های کد باید با نسخه واقعی پکیج‌ها، interfaceها و مدل‌های موجود در پروژه تطبیق داده شوند.</p></div>
<section class="section"><div class="section-header"><div class="section-number">00</div><div class="section-title"><h3>راهنمای شروع و قرارداد مشترک</h3><div class="subtitle">Shared implementation contract</div></div></div>
<p>قبل از توسعه هر Extractor، قرارداد خروجی و رفتار مشترک را تثبیت کنید. هدف این است که Extractorها به‌جای پیاده‌سازی‌های ناهمگون، از یک مسیر قابل پیش‌بینی برای تشخیص نوع، استخراج، خطا و ثبت فراداده استفاده کنند.</p>
<h4>گام ۱ ـ قرارداد و مدل خروجی</h4>
<p>قرارداد پایه باید حداقل شامل <code>CanExtract</code>، شناسه پایدار <code>Key</code>، <code>Priority</code>، <code>SupportsFallback</code> و عملیات استخراج با <code>CancellationToken</code> باشد. برای خروجی غنی، متن، بلوک‌ها، جدول‌ها، تصاویر، فراداده و خطاهای جزئی را از هم جدا نگه دارید.</p>
<pre><code>public interface IXFileContentExtractor
{
string Key { get; }
int Priority { get; }
bool SupportsFallback { get; }
bool CanExtract(string mimeType);
Task&lt;XFileExtractionResult&gt; ExtractAsync(
Stream stream, string fileName, CancellationToken cancellationToken);
}</code></pre>
<h4>گام ۲ ـ ثبت Dependency Injection</h4>
<p>هر Extractor را با عمر سرویس متناسب با وابستگی‌هایش ثبت کنید. برای سرویس‌های بدون state معمولاً Singleton مناسب است؛ اشیای parser قابل‌استفاده مجدد یا مدل‌های غیر thread-safe را بدون بررسی thread-safety در singleton نگه ندارید.</p>
<pre><code>services.AddSingleton&lt;IXFileContentExtractor, XHtmlFileContentExtractor&gt;();
services.AddSingleton&lt;IXFileContentExtractor, XPowerPointFileContentExtractor&gt;();
// برای فهرست کامل، تمام ۱۳ پیاده‌سازی را ثبت کنید.</code></pre>
<h4>گام ۳ ـ انتخاب قطعی و Fallback</h4>
<p>Composite باید Extractorهای سازگار را بر اساس Priority مرتب کند. خطای واقعی، خروجی خالی یا خروجی ناکافی باید سیاست‌های متفاوت داشته باشند؛ Fallback را فقط برای خطاهای قابل‌بازیابی یا کیفیت ناکافی اجرا کنید و خطای امنیتی/محدودیت اندازه را با Extractor بعدی دور نزنید.</p>
<h4>گام ۴ ـ اعتبارسنجی و ایمنی</h4>
<ul><li>نوع فایل را با MIME، پسوند و magic bytes تا حد ممکن تطبیق دهید.</li><li>محدودیت اندازه ورودی، اندازه خروجی، زمان اجرا و عمق پردازش را اعمال کنید.</li><li>برای XML، DTD و resolver خارجی را غیرفعال کنید.</li><li>آرشیوها را روی فایل‌سیستم بدون کنترل مسیر استخراج نکنید.</li><li>فایل‌های Office یا کد را برای استخراج متن اجرا نکنید.</li><li>لغو عملیات باید به parser، OCR، FFmpeg و عملیات زیرمجموعه منتقل شود.</li></ul>
<div class="warning-box"><strong>نکته:</strong> کدهای نمونه در بخش‌های بعدی الگوی آغاز هستند، نه تضمین سازگاری مستقیم با نسخه خاص کتابخانه. قبل از ادغام، نسخه NuGet، امضای API و مدل خروجی پروژه را بررسی و build/test کنید.</div>
</section>
<section class="section"><div class="section-header"><div class="section-number">00A</div><div class="section-title"><h3>نقشه اولویت اجرا</h3><div class="subtitle">Implementation roadmap</div></div></div>
<table><thead><tr><th>مرحله</th><th>موارد</th><th>خروجی مورد انتظار</th></tr></thead><tbody>
<tr><td>فاز ۱</td><td>XPowerPoint، XHtml، XZipArchive</td><td>پوشش فرمت‌های پرکاربرد و تست‌های یکپارچگی</td></tr>
<tr><td>فاز ۲</td><td>XLegacyDoc، XRtf، XOpenDocument، XVideo، XEmail</td><td>گسترش پشتیبانی سازمانی و چندرسانه‌ای</td></tr>
<tr><td>فاز ۳</td><td>XEpub، XSubtitle، XSvg، XCode، XTiff</td><td>پوشش تخصصی و حفظ ساختار/فراداده</td></tr>
<tr><td>هم‌زمان</td><td>Priority/Fallback، MIME detector، محدودیت حجم، Cache، Streaming، Retry</td><td>پایداری، ایمنی و قابلیت استقرار</td></tr>
</tbody></table>
</section>
<section class="section" id="extractor-1">
<div class="section-header"><div class="section-number">01</div>
<div class="section-title"><h3>XPowerPointFileContentExtractor</h3>
<div class="subtitle">بالا | .pptx, .pptm, .ppsx, .ppsm, .potx, .potm؛ برای .ppt قدیمی مسیر جداگانه لازم است</div></div>
</div>
<div class="section-content">
<div class="info-box"><strong>هدف و دامنه:</strong> استخراج متن اسلایدها، عنوان، ترتیب اسلاید، جدول‌ها، یادداشت سخنران و فراداده‌های قابل دسترس؛ شمارش تصویر باید از استخراج واقعی تصویر تفکیک شود.<br><strong>وابستگی پیشنهادی:</strong> <code>DocumentFormat.OpenXml</code></div>
<h4>مراحل پیاده‌سازی</h4>
<ol class='steps'><li><strong>تعریف قرارداد:</strong> کلاس را با IXFileContentExtractor سازگار کنید؛ CanExtract را بر اساس MIMEهای شناخته‌شده و پسوندهای معتبر پیاده‌سازی کنید. فایل‌های ماکرو‌دار را صرفاً به‌صورت بسته Open XML بخوانید و هیچ ماکرویی اجرا نکنید.</li><li><strong>اعتبارسنجی ورودی:</strong> حداکثر اندازه، خالی نبودن Stream و لغو عملیات را بررسی کنید. از روی ContentType به‌تنهایی اعتماد نکنید؛ امضای ZIP/Open XML و پسوند را نیز کنترل کنید.</li><li><strong>بازکردن بسته:</strong> با PresentationDocument.Open روی Stream یا یک جریان محدودشده باز کنید. اگر کتابخانه نیازمند seek است، فایل را در حافظه یا فایل موقت با سقف اندازه کپی کنید.</li><li><strong>حفظ ترتیب واقعی اسلایدها:</strong> به‌جای اتکا به ترتیب SlideParts، فهرست Presentation.SlideIdList را پیمایش و هر RelationshipId را به SlidePart نگاشت کنید؛ اسلایدهای مخفی را طبق سیاست محصول نگه دارید یا حذف کنید.</li><li><strong>استخراج ساختاری متن:</strong> Shapeها و Drawing.Text را پیمایش کنید. متن هر Shape را جدا نگه دارید و از اتصال صرفاً سراسری متن پرهیز کنید. عنوان را با PlaceholderType=Title یا CenteredTitle تشخیص دهید، نه صرفاً نام Shape.</li><li><strong>استخراج جدول و یادداشت:</strong> جدول‌ها را سلول‌به‌سلول و با مختصات ردیف/ستون ثبت کنید. NotesSlidePart را جدا بخوانید تا متن یادداشت با متن اسلاید ادغام نشود. برای جلوگیری از تکرار، متن جدول را دوباره به‌عنوان متن عادی وارد نکنید.</li><li><strong>مدیریت رسانه:</strong> برای هر ImagePart، شناسه رابطه، MIME، نام/شناسه اسلاید و بایت تصویر را در XExtractedImage ذخیره کنید؛ اگر استخراج تصویر خارج از دامنه است، صریحاً فقط تعداد را اعلام کنید.</li><li><strong>خروجی و خطا:</strong> برای هر اسلاید یک رکورد ساخت‌یافته شامل شماره، عنوان، بلوک‌های متن، جدول‌ها، یادداشت‌ها و تصاویر بسازید. خرابی یک اسلاید را در خطاهای جزئی ثبت کنید، مگر آن‌که بسته کلی قابل خواندن نباشد.</li></ol>
<h4>نمونه کد / الگوی پیاده‌سازی</h4>
<p>نمونه زیر الگوی شروع است؛ نام دقیق overloadها و مدل خروجی باید با نسخه پکیج نصب‌شده و قرارداد واقعی پروژه تطبیق داده شود.</p>
<pre><code>using DocumentFormat.OpenXml.Packaging;
using A = DocumentFormat.OpenXml.Drawing;
using P = DocumentFormat.OpenXml.Presentation;
using var presentation = PresentationDocument.Open(stream, false);
var slideIds = presentation.PresentationPart!.Presentation.SlideIdList!
.Elements&lt;P.SlideId&gt;();
var orderedSlides = slideIds.Select(id =&gt;
presentation.PresentationPart.GetPartById(id.RelationshipId!) as SlidePart);
foreach (var slidePart in orderedSlides)
{
cancellationToken.ThrowIfCancellationRequested();
if (slidePart is null) continue;
var shapes = slidePart.Slide.CommonSlideData?.ShapeTree?
.Elements&lt;P.Shape&gt;() ?? Enumerable.Empty&lt;P.Shape&gt;();
foreach (var shape in shapes)
{
var text = string.Concat(shape.TextBody?
.Descendants&lt;A.Text&gt;().Select(t =&gt; t.Text) ?? Enumerable.Empty&lt;string&gt;());
// ذخیره متن به‌عنوان یک بلوک مستقل، همراه با شناسه/نوع Shape
}
}</code></pre>
<h4>آزمون‌های پذیرش</h4><p>اسلایدهای جابه‌جا‌شده، متن فارسی و انگلیسی، عنوان با نام Shape غیرمعمول، جدول، یادداشت، تصویر، اسلاید خالی، فایل خراب، فایل بسیار بزرگ و لغو وسط پردازش.</p>
</div>
</section>
<section class="section" id="extractor-2">
<div class="section-header"><div class="section-number">02</div>
<div class="section-title"><h3>XHtmlFileContentExtractor</h3>
<div class="subtitle">بالا | text/html, application/xhtml+xml؛ .html, .htm, .xhtml</div></div>
</div>
<div class="section-content">
<div class="info-box"><strong>هدف و دامنه:</strong> تبدیل HTML به متن تمیز و قابل استفاده برای جست‌وجو و LLM، با حفظ عنوان، هدینگ‌ها، لینک‌ها و ساختار فهرست‌ها.<br><strong>وابستگی پیشنهادی:</strong> <code>AngleSharp یا HtmlAgilityPack (یکی را استاندارد کنید)</code></div>
<h4>مراحل پیاده‌سازی</h4>
<ol class='steps'><li><strong>تشخیص و رمزگشایی:</strong> نوع فایل را اعتبارسنجی و encoding را از BOM، هدر/متادیتای charset و در نهایت UTF-8 تشخیص دهید. سقف اندازه ورودی را اعمال کنید.</li><li><strong>پارسیگ امن:</strong> HTML را با parser واقعی بخوانید؛ از Regex برای حذف تگ‌ها استفاده نکنید. پردازش را آفلاین انجام دهید و منابع خارجی، اسکریپت‌ها یا CSS را بارگیری نکنید.</li><li><strong>حذف بخش‌های غیرمحتوایی:</strong> script، style، noscript، template، nav، footer، header و عناصر مخفی را طبق سیاست محصول حذف کنید. برای صفحات مقاله‌ای ممکن است header یا nav حاوی اطلاعات مفید باشد؛ سیاست باید قابل تنظیم باشد.</li><li><strong>استخراج فراداده:</strong> title، meta description، language و canonical URL را فقط به‌عنوان فراداده استخراج کنید؛ URL را بدون درخواست شبکه ثبت کنید.</li><li><strong>نرمال‌سازی متن:</strong> فاصله‌های متوالی و خطوط خالی را کاهش دهید؛ بلوک‌ها را بر اساس h1–h6، p، li، blockquote، pre/code و table حفظ کنید.</li><li><strong>لینک و جدول:</strong> متن لینک را همراه href نسبی/مطلق ثبت کنید؛ href را resolve نکنید مگر base URI مشخص و بدون دسترسی شبکه. جدول را به رکوردهای ردیف/سلول تبدیل کنید.</li><li><strong>خروجی و محدودیت:</strong> متن ساده و بلوک‌های ساخت‌یافته را تولید کنید؛ طول خروجی، عمق DOM و تعداد گره‌ها را محدود کنید.</li></ol>
<h4>نمونه کد / الگوی پیاده‌سازی</h4>
<p>نمونه زیر الگوی شروع است؛ نام دقیق overloadها و مدل خروجی باید با نسخه پکیج نصب‌شده و قرارداد واقعی پروژه تطبیق داده شود.</p>
<pre><code>var config = Configuration.Default;
var context = BrowsingContext.New(config);
using var document = await context.OpenAsync(req =&gt; req.Content(html), cancellationToken);
document.QuerySelectorAll(&quot;script,style,noscript,template&quot;).Remove();
var title = document.Title;
var blocks = document.Body?.QuerySelectorAll(
&quot;h1,h2,h3,h4,h5,h6,p,li,blockquote,pre,td,th&quot;)
.Select(e =&gt; new { tag = e.LocalName, text = e.TextContent.Trim() })
.Where(x =&gt; !string.IsNullOrWhiteSpace(x.text))
.ToList();</code></pre>
<h4>آزمون‌های پذیرش</h4><p>HTML ناقص، charsetهای مختلف، محتوای فارسی، اسکریپت تزریقی، عناصر مخفی، جدول تو‌در‌تو، لینک نسبی، صفحه بدون Body و ورودی بسیار بزرگ.</p>
</div>
</section>
<section class="section" id="extractor-3">
<div class="section-header"><div class="section-number">03</div>
<div class="section-title"><h3>XZipArchiveFileContentExtractor</h3>
<div class="subtitle">بالا | .zip؛ با SharpCompress امکان .rar و .7z، مشروط به پشتیبانی نسخه و مجوز</div></div>
</div>
<div class="section-content">
<div class="info-box"><strong>هدف و دامنه:</strong> استخراج بازگشتی فایل‌های درون آرشیو و ارسال هر فایل به Composite Extractor، با دفاع در برابر Zip Bomb و مسیرهای مخرب.<br><strong>وابستگی پیشنهادی:</strong> <code>System.IO.Compression برای ZIP؛ SharpCompress برای RAR/7z</code></div>
<h4>مراحل پیاده‌سازی</h4>
<ol class='steps'><li><strong>تعریف سیاست آرشیو:</strong> حداکثر عمق تو‌در‌تو، تعداد فایل‌ها، اندازه هر ورودی، اندازه کل بازشده و نسبت فشرده‌سازی را در تنظیمات تعریف کنید.</li><li><strong>بازکردن بدون استخراج روی دیسک:</strong> ورودی‌ها را به‌صورت stream بخوانید؛ هرگز مسیر موجود در آرشیو را مستقیماً روی فایل‌سیستم استخراج نکنید.</li><li><strong>اعتبارسنجی نام‌ها:</strong> مسیرهای مطلق، ../، جداکننده‌های غیرعادی، نام‌های تکراری و ورودی‌های symlink را رد یا قرنطینه کنید. نام فایل صرفاً فراداده است.</li><li><strong>اعمال محدودیت‌ها:</strong> شمارنده‌های فایل، بایت بازشده، زمان و عمق recursion را در هر مرحله به‌روزرسانی کنید؛ قبل از خواندن کامل، از سقف عبور نکنید.</li><li><strong>پردازش بازگشتی:</strong> برای هر ورودی مجاز، نوع واقعی فایل را تشخیص دهید و ExtractRichAsync را با context شامل عمق و بودجه باقیمانده فراخوانی کنید. آرشیوهای تودرتو هم از همان محدودیت‌ها تبعیت کنند.</li><li><strong>جلوگیری از حلقه و تکرار:</strong> هش محتوا را برای شناسایی ورودی‌های تکراری به‌کار ببرید؛ سقف تعداد nesting و recursion اجباری باشد.</li><li><strong>گزارش نتیجه:</strong> هر نتیجه باید مسیر منطقی داخل آرشیو، MIME تشخیص‌داده‌شده، وضعیت، اندازه و خطاهای جزئی را حفظ کند. یک ورودی خراب نباید الزاماً کل آرشیو را شکست دهد.</li></ol>
<h4>نمونه کد / الگوی پیاده‌سازی</h4>
<p>نمونه زیر الگوی شروع است؛ نام دقیق overloadها و مدل خروجی باید با نسخه پکیج نصب‌شده و قرارداد واقعی پروژه تطبیق داده شود.</p>
<pre><code>// الگوی مفهومی؛ API دقیق را مطابق کتابخانه انتخابی تنظیم کنید.
foreach (var entry in archive.Entries)
{
cancellationToken.ThrowIfCancellationRequested();
if (entry.IsDirectory) continue;
if (!policy.TryReserve(entry.Size, entry.CompressedSize, depth))
throw new InvalidDataException(&quot;Archive limits exceeded&quot;);
await using var entryStream = entry.OpenEntryStream();
var detected = await typeDetector.DetectAsync(
entry.Key, entryStream, cancellationToken);
// پس از بازنشانی/Buffer کنترل‌شده stream:
var childResult = await composite.ExtractRichAsync(
detected.MimeType, entryStream, cancellationToken);
results.Add(new ArchiveEntryResult(entry.Key, childResult));
}</code></pre>
<h4>آزمون‌های پذیرش</h4><p>Zip Slip، فایل با اندازه بازشده بسیار بزرگ، nesting عمیق، ورودی خراب، فایل‌های تکراری، رمزگذاری‌شده، تعداد زیاد فایل و لغو پردازش.</p>
</div>
</section>
<section class="section" id="extractor-4">
<div class="section-header"><div class="section-number">04</div>
<div class="section-title"><h3>XLegacyDocFileContentExtractor</h3>
<div class="subtitle">متوسط | application/msword؛ .doc باینری قدیمی</div></div>
</div>
<div class="section-content">
<div class="info-box"><strong>هدف و دامنه:</strong> خواندن متن و ساختار پایه Word قدیمی بدون وابستگی به Microsoft Office یا COM.<br><strong>وابستگی پیشنهادی:</strong> <code>NPOI HWPF؛ جایگزین تجاری مانند Spire.Doc فقط پس از بررسی مجوز</code></div>
<h4>مراحل پیاده‌سازی</h4>
<ol class='steps'><li><strong>انتخاب کتابخانه:</strong> برای استقرار Cross-platform، NPOI را ارزیابی کنید. پشتیبانی از جدول‌ها، سربرگ/پابرگ، footnote و encoding را با فایل‌های واقعی تأیید کنید.</li><li><strong>اعتبارسنجی فایل:</strong> امضای OLE Compound File و محدودیت حجم را بررسی کنید؛ فایل .doc را صرفاً به‌خاطر پسوند قبول نکنید.</li><li><strong>بازکردن سند:</strong> جریان را به API HWPF بدهید؛ در صورت نیاز به seek از buffer محدود استفاده کنید.</li><li><strong>استخراج پاراگراف‌ها:</strong> پاراگراف‌ها را به ترتیب سند بخوانید؛ عنوان‌ها را با style name و outline level ثبت کنید، نه فقط طول یا بزرگی متن.</li><li><strong>استخراج جدول و بخش‌ها:</strong> جدول‌ها را به سلول‌های ساخت‌یافته تبدیل کنید. سربرگ، پابرگ، footnote/endnote را در صورت پشتیبانی کتابخانه در فیلدهای مجزا قرار دهید.</li><li><strong>محدودیت و خطا:</strong> فایل‌های رمزدار، خراب یا دارای ویژگی پشتیبانی‌نشده را با وضعیت روشن گزارش کنید. برای تبدیل به DOCX، سرویس تبدیل جداگانه تعریف کنید؛ تبدیل خام را با استخراج متن یکی نکنید.</li></ol>
<h4>نمونه کد / الگوی پیاده‌سازی</h4>
<p>نمونه زیر الگوی شروع است؛ نام دقیق overloadها و مدل خروجی باید با نسخه پکیج نصب‌شده و قرارداد واقعی پروژه تطبیق داده شود.</p>
<pre><code>// نمونه جهت‌گیری با NPOI.HWPF؛ جزئیات API به نسخه پکیج وابسته است.
using NPOI.HWPF;
using NPOI.HWPF.Extractor;
using var ms = new MemoryStream(bytes, writable: false);
using var doc = new HWPFDocument(ms);
var extractor = new WordExtractor(doc);
var paragraphs = extractor.ParagraphText
.Where(p =&gt; !string.IsNullOrWhiteSpace(p))
.Select((text, index) =&gt; new TextBlock(index, text.Trim()))
.ToList();</code></pre>
<h4>آزمون‌های پذیرش</h4><p>DOC با جدول، متن فارسی، چندبخشی، سند خراب، سند رمزدار، فایل OLE غیر Word و سند حجیم.</p>
</div>
</section>
<section class="section" id="extractor-5">
<div class="section-header"><div class="section-number">05</div>
<div class="section-title"><h3>XRtfFileContentExtractor</h3>
<div class="subtitle">متوسط | application/rtf؛ .rtf</div></div>
</div>
<div class="section-content">
<div class="info-box"><strong>هدف و دامنه:</strong> تبدیل متن RTF به HTML یا متن استاندارد، با حفظ پاراگراف، سبک‌های پایه و نویسه‌های Unicode.<br><strong>وابستگی پیشنهادی:</strong> <code>RtfPipe</code></div>
<h4>مراحل پیاده‌سازی</h4>
<ol class='steps'><li><strong>اعتبارسنجی ورودی:</strong> اندازه فایل و وجود ساختار ابتدایی RTF مانند {\\rtf را بررسی کنید؛ ورودی را از نظر encoding به رشته‌ای قابل‌خواندن تبدیل کنید.</li><li><strong>پارس با کتابخانه:</strong> از parser اختصاصی استفاده کنید؛ حذف control wordها با Regex باعث خرابی Unicode، گروه‌های تو‌در‌تو و جدول‌ها می‌شود.</li><li><strong>تبدیل به مدل میانی:</strong> اگر کتابخانه HTML تولید می‌کند، خروجی را با HTML parser امن دوباره پردازش کنید و به بلوک‌های متن، جدول و لینک تبدیل نمایید.</li><li><strong>مدیریت نویسه‌ها:</strong> escapeهای hex، \\uN و fallback characters را طبق قواعد کتابخانه پردازش کنید؛ دوباره‌کاری دستی روی خروجی parser انجام ندهید.</li><li><strong>خطا و منابع:</strong> حداکثر nesting و اندازه خروجی را کنترل کنید. RTF خراب باید خطای قابل‌تشخیص بدهد، نه اینکه بی‌صدا متن خالی برگرداند.</li></ol>
<h4>نمونه کد / الگوی پیاده‌سازی</h4>
<p>نمونه زیر الگوی شروع است؛ نام دقیق overloadها و مدل خروجی باید با نسخه پکیج نصب‌شده و قرارداد واقعی پروژه تطبیق داده شود.</p>
<pre><code>// نمونه با RtfPipe؛ نام overload را با نسخه نصب‌شده تطبیق دهید.
var html = RtfPipe.Rtf.ToHtml(rtfText);
var cleanText = htmlParser.ExtractText(html);
// بهتر است cleanText همراه بلوک‌ها و خطاهای تبدیل برگردانده شود.</code></pre>
<h4>آزمون‌های پذیرش</h4><p>RTF با متن فارسی/Unicode، جدول، بولد/ایتالیک، گروه تو‌در‌تو، escapeهای hex، RTF ناقص و خروجی حجیم.</p>
</div>
</section>
<section class="section" id="extractor-6">
<div class="section-header"><div class="section-number">06</div>
<div class="section-title"><h3>XOpenDocumentFileContentExtractor</h3>
<div class="subtitle">متوسط | .odt, .ods, .odp؛ MIMEهای رسمی OpenDocument</div></div>
</div>
<div class="section-content">
<div class="info-box"><strong>هدف و دامنه:</strong> پشتیبانی از اسناد OpenDocument شامل متن، صفحه‌گسترده و ارائه، با حفظ ساختار هر نوع.<br><strong>وابستگی پیشنهادی:</strong> <code>پارس ZIP/XML مستقیم یا کتابخانه ODF مانند AODL پس از ارزیابی پوشش واقعی</code></div>
<h4>مراحل پیاده‌سازی</h4>
<ol class='steps'><li><strong>تشخیص نوع داخلی:</strong> ODF را به‌عنوان بسته ZIP بررسی کنید و mimetype داخلی را بخوانید؛ این مقدار باید با نوع مورد انتظار سازگار باشد.</li><li><strong>اعتبارسنجی XML:</strong> XML را با DtdProcessing.Prohibit، XmlResolver=null و محدودیت اندازه/عمق بارگذاری کنید تا XXE و XML bomb مهار شود.</li><li><strong>ODT:</strong> content.xml را برای پاراگراف‌ها، headingها، فهرست‌ها، جدول‌ها و متن‌های تکرارشونده پردازش کنید. styleها را در صورت نیاز برای تشخیص عنوان به‌کار ببرید.</li><li><strong>ODS:</strong> جدول‌ها، sheetها، rowها و cellها را ساخت‌یافته استخراج کنید؛ تکرار ردیف/ستون را با سقف کنترل کنید و مقادیر فرمول و نمایش‌شده را از هم متمایز کنید.</li><li><strong>ODP:</strong> اسلایدها را طبق manifest/presentation structure مرتب کنید؛ متن، عنوان، جدول، یادداشت و تصاویر را به تفکیک اسلاید استخراج کنید.</li><li><strong>رسانه و فراداده:</strong> meta.xml و تصاویر داخلی را در صورت نیاز استخراج کنید؛ پیوندهای خارجی را دنبال نکنید.</li><li><strong>تست و سازگاری:</strong> برای هر زیرنوع ODT/ODS/ODP تست‌های جداگانه داشته باشید؛ از یک parser عمومی که همه محتوا را به متن تخت تبدیل کند پرهیز کنید.</li></ol>
<h4>نمونه کد / الگوی پیاده‌سازی</h4>
<p>نمونه زیر الگوی شروع است؛ نام دقیق overloadها و مدل خروجی باید با نسخه پکیج نصب‌شده و قرارداد واقعی پروژه تطبیق داده شود.</p>
<pre><code>using var zip = new ZipArchive(stream, ZipArchiveMode.Read, leaveOpen: true);
var contentEntry = zip.GetEntry(&quot;content.xml&quot;)
?? throw new InvalidDataException(&quot;ODF content.xml missing&quot;);
await using var contentStream = contentEntry.Open();
var settings = new XmlReaderSettings {
DtdProcessing = DtdProcessing.Prohibit,
XmlResolver = null,
MaxCharactersInDocument = options.MaxXmlCharacters
};
using var reader = XmlReader.Create(contentStream, settings);
// سپس براساس نوع ODT/ODS/ODP، namespaceها و عناصر استاندارد را پیمایش کنید.</code></pre>
<h4>آزمون‌های پذیرش</h4><p>ODT/ODS/ODP واقعی از چند ابزار، XML خراب، DTD خارجی، سلول/ردیف تکرارشونده، متن فارسی و تصویر جاسازی‌شده.</p>
</div>
</section>
<section class="section" id="extractor-7">
<div class="section-header"><div class="section-number">07</div>
<div class="section-title"><h3>XVideoFileContentExtractor</h3>
<div class="subtitle">متوسط | .mp4, .mkv, .webm, .avi, .mov</div></div>
</div>
<div class="section-content">
<div class="info-box"><strong>هدف و دامنه:</strong> استخراج فراداده ویدئو، جداکردن صوت و رونویسی گفتار؛ OCR فریم‌ها قابلیت جداگانه و اختیاری است.<br><strong>وابستگی پیشنهادی:</strong> <code>FFmpeg/FFprobe از طریق wrapper مانند FFMpegCore؛ Whisper برای transcription</code></div>
<h4>مراحل پیاده‌سازی</h4>
<ol class='steps'><li><strong>بررسی ابزار اجرایی:</strong> نسخه FFmpeg/FFprobe، مسیر باینری و قابلیت codecهای موردنیاز را در زمان راه‌اندازی بررسی کنید؛ نصب باینری را از کد برنامه جدا کنید.</li><li><strong>اعتبارسنجی و ایزوله‌سازی:</strong> پسوند/MIME و حجم را کنترل کنید؛ فایل را در محیط محدود پردازش کنید. هرگز آرگومان shell را با الحاق رشته‌های کنترل‌نشده بسازید.</li><li><strong>استخراج metadata:</strong> مدت، رزولوشن، frame rate، streamهای صوتی/تصویری، codec و زبان trackها را با FFprobe دریافت کنید.</li><li><strong>استخراج صوت:</strong> صوت را با تنظیمات نمونه‌برداری و کانال موردنیاز مدل ASR به WAV/PCM تبدیل کنید؛ فایل موقت را با نام تصادفی و پاک‌سازی تضمین‌شده بسازید.</li><li><strong>رونویسی:</strong> صوت را به قطعه‌های زمان‌دار تقسیم کنید؛ زبان را در صورت امکان تشخیص دهید و timestamp و confidence را حفظ کنید.</li><li><strong>پردازش چندرسانه‌ای اختیاری:</strong> در صورت نیاز، فریم‌ها را با فاصله زمانی و سقف تعداد استخراج و به Vision/OCR بدهید. این کار را از transcription جدا نگه دارید.</li><li><strong>محدودیت منابع:</strong> سقف مدت، رزولوشن، تعداد فریم، زمان CPU، حجم خروجی و فضای موقت تعریف کنید. cancellation باید فرایند FFmpeg و Whisper را واقعاً متوقف کند.</li></ol>
<h4>نمونه کد / الگوی پیاده‌سازی</h4>
<p>نمونه زیر الگوی شروع است؛ نام دقیق overloadها و مدل خروجی باید با نسخه پکیج نصب‌شده و قرارداد واقعی پروژه تطبیق داده شود.</p>
<pre><code>// الگوی کلی؛ API واقعی به نسخه FFMpegCore وابسته است.
var mediaInfo = await FFProbe.AnalyseAsync(inputPath);
var audioPath = tempFileProvider.Create(&quot;.wav&quot;);
await FFMpegArguments
.FromFileInput(inputPath)
.OutputToFile(audioPath, overwrite: true, options =&gt; options
.DisableVideo()
.AudioCodec(&quot;pcm_s16le&quot;)
.AudioChannels(1)
.AudioFrequency(16000))
.ProcessAsynchronously();
var transcript = await speechService.TranscribeAsync(
audioPath, cancellationToken);</code></pre>
<h4>آزمون‌های پذیرش</h4><p>ویدئوی بدون صدا، چند track صوتی، codec پشتیبانی‌نشده، فایل طولانی، فایل خراب، timestamp، قطع عملیات و مسیر فایل با کاراکترهای خاص.</p>
</div>
</section>
<section class="section" id="extractor-8">
<div class="section-header"><div class="section-number">08</div>
<div class="section-title"><h3>XEmailFileContentExtractor</h3>
<div class="subtitle">متوسط | .eml و .msg؛ MIME message/rfc822 برای EML</div></div>
</div>
<div class="section-content">
<div class="info-box"><strong>هدف و دامنه:</strong> استخراج فرستنده، گیرندگان، تاریخ، موضوع، متن، HTML، headers و پیوست‌ها؛ پیوست‌ها باید از pipeline موجود عبور کنند.<br><strong>وابستگی پیشنهادی:</strong> <code>MimeKit برای EML؛ برای MSG یک parser سازگار مانند MsgReader پس از ارزیابی مجوز</code></div>
<h4>مراحل پیاده‌سازی</h4>
<ol class='steps'><li><strong>تفکیک فرمت‌ها:</strong> EML را با MimeKit پردازش کنید؛ MSG قالب متفاوتی دارد و نباید به‌عنوان EML باز شود. انتخاب parser بر اساس تشخیص نوع انجام شود.</li><li><strong>پارسیگ ایمن:</strong> فایل خام را بدون ارسال شبکه‌ای بخوانید؛ headerهای تکراری، encoded-wordها، MIME multipart و charsetها را به parser بسپارید.</li><li><strong>استخراج سرآیندها:</strong> From، To، Cc، Bcc در صورت موجود بودن، Date، Subject، Message-Id و Reply-To را ساخت‌یافته نگه دارید. اطلاعات خصوصی را در log ثبت نکنید.</li><li><strong>بدنه متن:</strong> text/plain را ترجیح دهید؛ اگر فقط text/html وجود دارد از HTML extractor برای پاک‌سازی استفاده کنید. متن plain و HTML را بی‌دلیل دوبار به خروجی اضافه نکنید.</li><li><strong>پیوست‌ها:</strong> نام فایل را sanitize کنید و محتوا را با سقف اندازه و تعداد به Composite Extractor بدهید. فایل‌های inline مثل لوگوی امضا را طبق سیاست جدا کنید.</li><li><strong>thread و امنیت:</strong> در صورت نیاز References/In-Reply-To را استخراج کنید؛ لینک‌ها و تصاویر remote را بارگیری نکنید. فایل‌های رمزدار یا پیوست‌های محدودشده را گزارش کنید.</li><li><strong>خروجی:</strong> مدل ایمیل باید بدنه، فراداده و هر پیوست را به‌عنوان اجزای جداگانه نگه دارد.</li></ol>
<h4>نمونه کد / الگوی پیاده‌سازی</h4>
<p>نمونه زیر الگوی شروع است؛ نام دقیق overloadها و مدل خروجی باید با نسخه پکیج نصب‌شده و قرارداد واقعی پروژه تطبیق داده شود.</p>
<pre><code>using MimeKit;
using var stream = new MemoryStream(emlBytes, writable: false);
var message = await MimeMessage.LoadAsync(stream, cancellationToken);
var email = new EmailContent {
Subject = message.Subject,
From = message.From.ToString(),
To = message.To.ToString(),
Date = message.Date,
MessageId = message.MessageId,
TextBody = message.TextBody,
HtmlBody = message.HtmlBody
};
// پیوست‌ها را با محدودیت اندازه، نام امن و نوع تشخیص‌داده‌شده پردازش کنید.</code></pre>
<h4>آزمون‌های پذیرش</h4><p>ایمیل multipart، charset قدیمی، موضوع فارسی، headerهای ناقص، پیوست بزرگ، نام فایل مخرب، HTML remote image و پیام با چند پیوست.</p>
</div>
</section>
<section class="section" id="extractor-9">
<div class="section-header"><div class="section-number">09</div>
<div class="section-title"><h3>XEpubFileContentExtractor</h3>
<div class="subtitle">پایین | application/epub+zip؛ .epub</div></div>
</div>
<div class="section-content">
<div class="info-box"><strong>هدف و دامنه:</strong> استخراج متن کتاب با ترتیب صحیح فصل‌ها، عنوان‌ها، فراداده و در صورت نیاز اطلاعات محل فصل.<br><strong>وابستگی پیشنهادی:</strong> <code>VersOne.Epub یا ZIP/XML parser اختصاصی</code></div>
<h4>مراحل پیاده‌سازی</h4>
<ol class='steps'><li><strong>اعتبارسنجی بسته:</strong> EPUB را به‌عنوان ZIP بررسی کنید؛ container.xml و package document باید وجود داشته باشند.</li><li><strong>خواندن manifest و spine:</strong> از spine برای ترتیب خواندن بخش‌ها استفاده کنید؛ مرتب‌سازی فایل‌ها بر اساس نام مسیر ترتیب کتاب را تضمین نمی‌کند.</li><li><strong>استخراج فراداده:</strong> عنوان، نویسنده، زبان، شناسه و ناشر را از OPF استخراج کنید.</li><li><strong>خواندن XHTML:</strong> هر فصل را با HTML extractor پاک‌سازی کنید؛ محتوای navigation، پاورقی و یادداشت‌ها را در صورت نیاز حفظ کنید.</li><li><strong>حفظ منشأ:</strong> هر بلوک متن باید شناسه فصل، عنوان و ترتیب خود را داشته باشد تا citation و chunking بعدی ممکن شود.</li><li><strong>رسانه و محدودیت:</strong> تصاویر را اختیاری استخراج کنید؛ لینک‌های خارجی را دنبال نکنید و سقف فصل/اندازه/عمق را اعمال کنید.</li></ol>
<h4>نمونه کد / الگوی پیاده‌سازی</h4>
<p>نمونه زیر الگوی شروع است؛ نام دقیق overloadها و مدل خروجی باید با نسخه پکیج نصب‌شده و قرارداد واقعی پروژه تطبیق داده شود.</p>
<pre><code>// با VersOne.Epub، API دقیق به نسخه نصب‌شده وابسته است.
var book = await EpubReader.ReadBookAsync(stream);
var metadata = new {
book.Title, book.Author, book.Language, book.Identifier
};
foreach (var chapter in book.ReadingOrder)
{
cancellationToken.ThrowIfCancellationRequested();
var html = chapter.Content;
var text = htmlExtractor.Extract(html);
// chapter.Title + spine index + text را ذخیره کنید.
}</code></pre>
<h4>آزمون‌های پذیرش</h4><p>کتاب چندفصلی، ترتیب spine متفاوت از نام فایل، متن فارسی/RTL، فصل XHTML ناقص، کتاب بدون metadata و تصویر داخلی.</p>
</div>
</section>
<section class="section" id="extractor-10">
<div class="section-header"><div class="section-number">10</div>
<div class="section-title"><h3>XSubtitleFileContentExtractor</h3>
<div class="subtitle">پایین | .srt, .vtt, .ass, .ssa؛ .sub فقط با تشخیص نوع دقیق</div></div>
</div>
<div class="section-content">
<div class="info-box"><strong>هدف و دامنه:</strong> استخراج متن زیرنویس همراه timestamp، شماره cue و اطلاعات speaker/style در صورت وجود.<br><strong>وابستگی پیشنهادی:</strong> <code>Parser اختصاصی ساده و آزمون‌شده؛ برای فرمت‌های پیچیده parser اختصاصی مناسب‌تر است</code></div>
<h4>مراحل پیاده‌سازی</h4>
<ol class='steps'><li><strong>تشخیص فرمت:</strong> از پسوند به‌علاوه الگوی ساختاری استفاده کنید؛ .sub می‌تواند چند قالب متفاوت داشته باشد و نباید بدون تشخیص قبول شود.</li><li><strong>پارس timestamp:</strong> timestamp را به TimeSpan تبدیل کنید؛ ترتیب شروع/پایان و مدت منفی یا نامعتبر را اعتبارسنجی کنید.</li><li><strong>پارس cueها:</strong> SRT را با blank-line/cue separator و VTT را با header و cue identifiers پردازش کنید؛ خط‌های چندگانه داخل یک cue را حفظ کنید.</li><li><strong>فرمت‌های ASS/SSA:</strong> بخش Events و قالب ستون‌های Dialogue را بخوانید؛ ترتیب ستون‌ها را hard-code نکنید و overrideهای نمایشی را جدا نگه دارید.</li><li><strong>پاک‌سازی محتوا:</strong> markup را طبق سیاست حذف یا حفظ کنید؛ متن قابل نمایش را با timestamp و index در مدل ساخت‌یافته نگه دارید.</li><li><strong>خروجی و ادغام:</strong> خروجی transcript زمان‌دار تولید کنید؛ در صورت ادغام با ویدئو، تطبیق timestamp را در مرحله جداگانه انجام دهید.</li></ol>
<h4>نمونه کد / الگوی پیاده‌سازی</h4>
<p>نمونه زیر الگوی شروع است؛ نام دقیق overloadها و مدل خروجی باید با نسخه پکیج نصب‌شده و قرارداد واقعی پروژه تطبیق داده شود.</p>
<pre><code>public sealed record SubtitleCue(
int Index, TimeSpan Start, TimeSpan End, string Text);
foreach (var cueBlock in SplitCueBlocks(input))
{
cancellationToken.ThrowIfCancellationRequested();
var cue = ParseCue(cueBlock); // timestamp و متن چندخطی
if (cue.End &lt; cue.Start)
throw new InvalidDataException(&quot;Invalid cue time range&quot;);
cues.Add(cue);
}</code></pre>
<h4>آزمون‌های پذیرش</h4><p>timestamp با ساعت/میلی‌ثانیه، cue چندخطی، cue بدون شماره، BOM، WebVTT header، ASS با ترتیب ستون متفاوت و .sub مبهم.</p>
</div>
</section>
<section class="section" id="extractor-11">
<div class="section-header"><div class="section-number">11</div>
<div class="section-title"><h3>XSvgFileContentExtractor</h3>
<div class="subtitle">پایین | image/svg+xml؛ .svg</div></div>
</div>
<div class="section-content">
<div class="info-box"><strong>هدف و دامنه:</strong> استخراج title، desc و متن قابل مشاهده از SVG بدون اجرای script یا بارگیری منابع خارجی.<br><strong>وابستگی پیشنهادی:</strong> <code>System.Xml؛ برای render تصویری در صورت نیاز ImageSharp/SVG renderer جداگانه</code></div>
<h4>مراحل پیاده‌سازی</h4>
<ol class='steps'><li><strong>اعتبارسنجی XML:</strong> SVG را با XmlReader امن بخوانید؛ DTD و XmlResolver خارجی را غیرفعال کنید و محدودیت اندازه/عمق اعمال کنید.</li><li><strong>حذف عناصر خطرناک:</strong> script، foreignObject و event attributeها را برای استخراج محتوا نادیده بگیرید؛ هیچ SVG را در مرورگر یا renderer نامطمئن اجرا نکنید.</li><li><strong>استخراج متن:</strong> title، desc، text و tspan را با ترتیب DOM استخراج کنید؛ برای عناصر text، فاصله بین tspanها را با قواعد قابل تنظیم مدیریت کنید.</li><li><strong>فراداده و روابط:</strong> viewBox، width/height و role/aria-label را در صورت نیاز ثبت کنید؛ href خارجی را fetch نکنید.</li><li><strong>متن و تصویر را تفکیک کنید:</strong> این Extractor متن SVG را استخراج می‌کند؛ OCR از rasterized SVG یک مسیر مستقل است و فقط در صورت نیاز باید فراخوانی شود.</li><li><strong>خروجی و تست:</strong> بلوک‌های متن را با id/label عنصر حفظ کنید؛ XML نامعتبر را با خطای قابل‌تشخیص گزارش کنید.</li></ol>
<h4>نمونه کد / الگوی پیاده‌سازی</h4>
<p>نمونه زیر الگوی شروع است؛ نام دقیق overloadها و مدل خروجی باید با نسخه پکیج نصب‌شده و قرارداد واقعی پروژه تطبیق داده شود.</p>
<pre><code>var settings = new XmlReaderSettings {
DtdProcessing = DtdProcessing.Prohibit,
XmlResolver = null,
MaxCharactersInDocument = options.MaxXmlCharacters
};
using var reader = XmlReader.Create(svgStream, settings);
var doc = XDocument.Load(reader);
XNamespace svg = &quot;http://www.w3.org/2000/svg&quot;;
var textNodes = doc.Descendants()
.Where(e =&gt; e.Name == svg + &quot;title&quot; ||
e.Name == svg + &quot;desc&quot; ||
e.Name == svg + &quot;text&quot; ||
e.Name == svg + &quot;tspan&quot;)
.Select(e =&gt; new { Element = e.Name.LocalName, Text = e.Value.Trim() })
.Where(x =&gt; x.Text.Length &gt; 0);</code></pre>
<h4>آزمون‌های پذیرش</h4><p>SVG با title/desc، tspanهای متعدد، script، DTD، href خارجی، XML خراب، متن RTL و سند بسیار بزرگ.</p>
</div>
</section>
<section class="section" id="extractor-12">
<div class="section-header"><div class="section-number">12</div>
<div class="section-title"><h3>XCodeFileContentExtractor</h3>
<div class="subtitle">پایین | .cs, .py, .js, .ts, .java و سایر زبان‌های مصوب؛ MIME متنی</div></div>
</div>
<div class="section-content">
<div class="info-box"><strong>هدف و دامنه:</strong> استخراج کد و ساختار نحوی شامل namespace، type، method، import، comment و signature برای جست‌وجو و تحلیل کد.<br><strong>وابستگی پیشنهادی:</strong> <code>Roslyn برای C#؛ Tree-sitter یا parser مناسب زبان‌های دیگر</code></div>
<h4>مراحل پیاده‌سازی</h4>
<ol class='steps'><li><strong>allowlist زبان:</strong> extension به language parser نگاشت شود؛ extension ناشناخته به‌عنوان کد تحلیل نشود. تشخیص زبان فقط با MIME انجام نشود.</li><li><strong>رمزگشایی و اندازه:</strong> encoding و محدودیت اندازه/خطوط را بررسی کنید؛ فایل باینری را رد کنید.</li><li><strong>ساخت Syntax Tree:</strong> برای C# از Roslyn و برای زبان‌های دیگر parser متناظر استفاده کنید. اگر parser خطا دارد، متن خام را هم نگه دارید و وضعیت parse را گزارش دهید.</li><li><strong>استخراج ساختار:</strong> نوع‌ها، متدها، پارامترها، signature، namespace، imports و commentها را به رکوردهای جدا تبدیل کنید؛ بازه خط/ستون و نام فایل را حفظ کنید.</li><li><strong>حریم امنیتی:</strong> کد را اجرا، build یا package restore نکنید. استخراج باید صرفاً parsing ایستا باشد.</li><li><strong>chunking مبتنی بر نماد:</strong> به‌جای بریدن هر N کاراکتر، در مرز type/method/function قطعه‌سازی کنید؛ قطعه‌های خیلی بزرگ را با سقف توکن/کاراکتر خرد کنید.</li><li><strong>fallback:</strong> در زبان پشتیبانی‌نشده یا syntax خراب، متن خام را با برچسب زبان احتمالی برگردانید؛ شکست parser نباید منجر به اجرای کد شود.</li></ol>
<h4>نمونه کد / الگوی پیاده‌سازی</h4>
<p>نمونه زیر الگوی شروع است؛ نام دقیق overloadها و مدل خروجی باید با نسخه پکیج نصب‌شده و قرارداد واقعی پروژه تطبیق داده شود.</p>
<pre><code>// نمونه Roslyn برای C#
var tree = CSharpSyntaxTree.ParseText(
source, cancellationToken: cancellationToken);
var root = await tree.GetRootAsync(cancellationToken);
var declarations = root.DescendantNodes()
.OfType&lt;MemberDeclarationSyntax&gt;()
.Select(node =&gt; new {
Kind = node.Kind().ToString(),
Text = node.ToFullString(),
Span = node.Span,
Line = tree.GetLineSpan(node.Span).StartLinePosition.Line + 1
}).ToList();</code></pre>
<h4>آزمون‌های پذیرش</h4><p>فایل چندزبانه، syntax خراب، فایل خیلی بزرگ، commentهای Unicode، generated code، فایل باینری با پسوند .cs و جلوگیری از اجرای کد.</p>
</div>
</section>
<section class="section" id="extractor-13">
<div class="section-header"><div class="section-number">13</div>
<div class="section-title"><h3>XTiffFileContentExtractor</h3>
<div class="subtitle">پایین | image/tiff, image/tif؛ TIFF چندصفحه‌ای</div></div>
</div>
<div class="section-content">
<div class="info-box"><strong>هدف و دامنه:</strong> پردازش همه صفحات TIFF، اجرای OCR صفحه‌به‌صفحه و حفظ شماره صفحه و confidence.<br><strong>وابستگی پیشنهادی:</strong> <code>ImageSharp یا کتابخانه سازگار با TIFF؛ Tesseract برای OCR</code></div>
<h4>مراحل پیاده‌سازی</h4>
<ol class='steps'><li><strong>اعتبارسنجی تصویر:</strong> نوع واقعی، ابعاد، تعداد فریم/صفحه و حجم decode‌شده را محدود کنید؛ تصویر فشرده می‌تواند حافظه بسیار زیادی مصرف کند.</li><li><strong>بازکردن چندفریمی:</strong> TIFF را با API چندفریمی بخوانید؛ تنها فریم اول را پردازش نکنید. تعداد صفحات مجاز باید سقف داشته باشد.</li><li><strong>پیش‌پردازش:</strong> چرخش، deskew، grayscale و thresholding را در صورت نیاز و با نسخه خام قابل ردیابی اعمال کنید؛ پیش‌پردازش نباید جایگزین ورودی اصلی شود.</li><li><strong>OCR صفحه‌ای:</strong> برای هر صفحه زبان مناسب Tesseract را انتخاب کنید؛ متن، confidence و شماره صفحه را ذخیره کنید. زبان‌های موردنیاز باید در deployment نصب باشند.</li><li><strong>ادغام و خروجی:</strong> متن کل سند را با جداکننده صفحه بسازید، اما رکوردهای هر صفحه را جدا نگه دارید تا ارجاع به صفحه ممکن باشد.</li><li><strong>منابع و خطا:</strong> محدودیت پیکسل، حافظه، زمان OCR و cancellation اعمال کنید؛ خطای یک صفحه را می‌توان به‌صورت partial failure ثبت کرد.</li></ol>
<h4>نمونه کد / الگوی پیاده‌سازی</h4>
<p>نمونه زیر الگوی شروع است؛ نام دقیق overloadها و مدل خروجی باید با نسخه پکیج نصب‌شده و قرارداد واقعی پروژه تطبیق داده شود.</p>
<pre><code>// الگوی مفهومی؛ API دقیق چندفریمی به نسخه ImageSharp وابسته است.
using var image = await Image.LoadAsync(tiffStream, cancellationToken);
if (image.Frames.Count &gt; options.MaxPages)
throw new InvalidDataException(&quot;Too many TIFF pages&quot;);
for (var i = 0; i &lt; image.Frames.Count; i++)
{
cancellationToken.ThrowIfCancellationRequested();
using var page = image.Frames.CloneFrame(i);
var pageImage = PrepareForOcr(page);
var ocr = await ocrService.RecognizeAsync(pageImage, language, cancellationToken);
pages.Add(new PageText(i + 1, ocr.Text, ocr.Confidence));
}</code></pre>
<h4>آزمون‌های پذیرش</h4><p>TIFF تک‌صفحه‌ای و چندصفحه‌ای، رزولوشن بالا، صفحات چرخیده، صفحه خالی، زبان فارسی، فریم خراب و تعداد صفحات بیش از سقف.</p>
</div>
</section>
<section class="section"><div class="section-header"><div class="section-number">99</div><div class="section-title"><h3>چک‌لیست نهایی تحویل</h3><div class="subtitle">Definition of done</div></div></div>
<ul>
<li>Extractor با کلید یکتا، MIMEهای مستندشده و Priority مشخص ثبت شده باشد.</li>
<li>ورودی خراب، خالی، بیش‌ازحد بزرگ و ناسازگار به‌صورت کنترل‌شده مدیریت شود.</li>
<li>CancellationToken در تمام مسیرهای طولانی و عملیات فرزند رعایت شود.</li>
<li>خروجی rich ساخت‌یافته باشد و ترتیب/منشأ هر بخش را حفظ کند.</li>
<li>تست‌های unit، integration، regression و فایل‌های نمونه واقعی اضافه شده باشند.</li>
<li>رفتار Linux/Docker و وابستگی‌های native در CI یا container آزمایش شده باشد.</li>
<li>logها شامل شناسه عملیات و نوع خطا باشند، نه محتوای حساس فایل.</li>
<li>مجوز کتابخانه‌های انتخابی و باینری‌های جانبی بررسی و ثبت شده باشد.</li>
</ul>
<div class="success-box"><strong>معیار پذیرش نهایی:</strong> هر Extractor باید خروجی قابل‌تکرار، خطاهای قابل‌تشخیص، محدودیت منابع، آزمون‌های مستقل و سازگاری کامل با Composite داشته باشد. اضافه شدن پشتیبانی یک پسوند بدون آزمون محتوای واقعی، به‌تنهایی به معنی پشتیبانی کامل آن فرمت نیست.</div>
</section>
</main>
<footer class="footer"><strong>تهیه‌کننده: هادی خزاعی اصل</strong><br>پروژه xAiApi | نسخه ۱.۰ | مستند فنی محرمانه</footer>
</div></body></html>
@@ -0,0 +1,740 @@
<!DOCTYPE html>
<html lang="fa" dir="rtl">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>پیاده‌سازی کامل ExtractOdtAsync / ExtractOdsAsync / ExtractOdpAsync | 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-055</div>
<div>نسخه: ۱.۰</div>
<div>تاریخ تهیه: ۲ آبان ۱۴۰۵</div>
<div>طبقه‌بندی: محرمانه</div>
</div>
</div>
<div class="document-title">
<span class="label">IMPLEMENTATION DETAIL</span>
<h1>پیاده‌سازی کامل ExtractOdtAsync / ExtractOdsAsync / ExtractOdpAsync</h1>
<h2>کد نهایی و توضیحات فنی سه متد استخراج محتوا در XOpenDocumentFileContentExtractor</h2>
</div>
</header>
<div class="document-body">
<div class="intro-text">
<strong>به کارفرمای محترم،</strong><br>
این مستند تکمیل‌کننده مستند قبلی <code>XOpenDocumentFileContentExtractor</code> است و پیاده‌سازی کامل سه متد <code>ExtractOdtAsync</code>، <code>ExtractOdsAsync</code> و <code>ExtractOdpAsync</code> را ارائه می‌دهد. در مستند قبلی، ساختار کلی کلاس و ثبت در DI توضیح داده شد اما بدنه‌ی این سه متد به‌صورت کامل پیاده‌سازی نشده بود. در این مستند، با استفاده از API رسمی <strong>OfficeIMO.OpenDocument</strong>، هر سه متد به‌طور کامل پیاده‌سازی شده و نکات فنی مربوط به هر یک به تفصیل شرح داده می‌شود.
</div>
<div class="section">
<div class="section-header">
<div class="section-number">۱</div>
<div class="section-title">
<h3>مرور API مورد استفاده</h3>
<div class="subtitle">API Overview</div>
</div>
</div>
<div class="section-content">
<p>پیاده‌سازی این سه متد بر پایه کلاس‌های اصلی زیر از <strong>OfficeIMO.OpenDocument</strong> انجام می‌شود:</p>
<table class="styled-table">
<thead>
<tr>
<th>کلاس / متد</th>
<th>نوع سند</th>
<th>کاربرد</th>
</tr>
</thead>
<tbody>
<tr>
<td><code>OdtDocument.Load(Stream)</code></td>
<td>ODT (متن)</td>
<td>بارگذاری سند متنی از استریم[reference:0]</td>
</tr>
<tr>
<td><code>OdsDocument.Load(Stream)</code></td>
<td>ODS (صفحه‌گسترده)</td>
<td>بارگذاری سند صفحه‌گسترده از استریم[reference:1]</td>
</tr>
<tr>
<td><code>OdpPresentation.Load(Stream)</code></td>
<td>ODP (ارائه)</td>
<td>بارگذاری سند ارائه از استریم[reference:2]</td>
</tr>
<tr>
<td><code>OdsSheet</code></td>
<td>ODS</td>
<td>دسترسی به شیت‌ها، سلول‌ها و مقادیر[reference:3]</td>
</tr>
<tr>
<td><code>OdsSheet.GetValue(row, column)</code></td>
<td>ODS</td>
<td>خواندن مقدار یک سلول به‌صورت تایپ‌دار[reference:4]</td>
</tr>
</tbody>
</table>
<div class="info-box">
<strong>💡 نکته درباره OfficeIMO.Reader:</strong> کتابخانه OfficeIMO یک ماژول جداگانه به نام <code>OfficeIMO.Reader</code> نیز ارائه می‌دهد که یک «واسط یکپارچه استخراج فقط-خواندنی» برای تمام فرمت‌های Office است و از ODT، ODS و ODP نیز پشتیبانی می‌کند[reference:5][reference:6]. اگر هدف صرفاً استخراج متن باشد (بدون دسترسی به ساختار دقیق)، استفاده از Reader می‌تواند کد را ساده‌تر کند. اما در این پیاده‌سازی، از خود <code>OfficeIMO.OpenDocument</code> استفاده می‌شود تا کنترل بیشتری روی ساختار و قالب خروجی داشته باشیم.
</div>
</div>
</div>
<div class="section">
<div class="section-header">
<div class="section-number">۲</div>
<div class="section-title">
<h3>پیاده‌سازی کامل ExtractOdtAsync</h3>
<div class="subtitle">Full Implementation of ExtractOdtAsync</div>
</div>
</div>
<div class="section-content">
<p>این متد برای استخراج محتوا از فایل‌های ODT (سند متنی) استفاده می‌شود. با استفاده از <code>OdtDocument.Load</code> سند بارگذاری شده و سپس پاراگراف‌ها و جداول پیمایش می‌شوند. طبق API رسمی، <code>OdtDocument</code> متدهای <code>AddParagraph</code> و <code>AddTable</code> را برای ایجاد و همچنین امکان دسترسی به مجموعه‌های موجود را فراهم می‌کند[reference:7]. برای پیمایش، از propertyهای <code>Paragraphs</code> و <code>Tables</code> روی سند استفاده می‌شود (الگوی مشابه OfficeIMO.Word که در آن <code>document.Paragraphs</code> قابل پیمایش است[reference:8]).</p>
<pre>private async Task&lt;string&gt; ExtractOdtAsync(
Stream fileStream,
CancellationToken cancellationToken
)
{
// 1. کپی استریم به حافظه (OfficeIMO به استریم Seekable نیاز دارد)
await using var ms = new MemoryStream();
await fileStream.CopyToAsync(ms, cancellationToken);
ms.Position = 0;
// 2. بارگذاری سند ODT
using var document = OdtDocument.Load(ms);
var sb = new StringBuilder();
// 3. استخراج پاراگراف‌ها (متن اصلی سند)
foreach (var paragraph in document.Paragraphs)
{
if (cancellationToken.IsCancellationRequested)
break;
if (!string.IsNullOrWhiteSpace(paragraph.Text))
{
sb.AppendLine(paragraph.Text.Trim());
}
}
// 4. استخراج جداول
var tableIndex = 0;
foreach (var table in document.Tables)
{
if (cancellationToken.IsCancellationRequested)
break;
tableIndex++;
sb.AppendLine();
sb.AppendLine($"[Table {tableIndex}]");
foreach (var row in table.Rows)
{
var cellTexts = new List&lt;string&gt;();
foreach (var cell in row.Cells)
{
// هر سلول می‌تواند شامل چند پاراگراف باشد
var cellText = string.Join(
" ",
cell.Paragraphs
.Select(p =&gt; p.Text?.Trim())
.Where(t =&gt; !string.IsNullOrWhiteSpace(t))
);
cellTexts.Add(cellText);
}
sb.AppendLine($" | {string.Join(" | ", cellTexts)} |");
}
sb.AppendLine();
}
return sb.ToString().TrimEnd();
}</pre>
<h4 style="margin-top: 25px; color: var(--primary); font-size: 16px;">توضیح گام‌ها</h4>
<ul style="padding-right: 20px; margin: 10px 0;">
<li style="margin-bottom: 8px;"><strong>کپی استریم به حافظه:</strong> <code>OdtDocument.Load(Stream)</code> به استریمی نیاز دارد که قابلیت Seek داشته باشد. استریم‌های HTTP معمولاً Seekable نیستند، بنابراین ابتدا محتوا به یک <code>MemoryStream</code> کپی می‌شود.</li>
<li style="margin-bottom: 8px;"><strong>بارگذاری سند:</strong> <code>OdtDocument.Load(ms)</code> سند ODT را از استریم حافظه بارگذاری می‌کند[reference:9].</li>
<li style="margin-bottom: 8px;"><strong>پیمایش پاراگراف‌ها:</strong> <code>document.Paragraphs</code> مجموعه‌ای از <code>OdtParagraph</code> را برمی‌گرداند. هر <code>OdtParagraph</code> دارای ویژگی <code>Text</code> است که محتوای متنی آن را برمی‌گرداند. این الگو با OfficeIMO.Word هم‌راستا است که در آن <code>document.Paragraphs</code> قابل پیمایش است[reference:10].</li>
<li style="margin-bottom: 8px;"><strong>پیمایش جداول:</strong> <code>document.Tables</code> مجموعه‌ای از <code>OdtTable</code> را برمی‌گرداند. هر جدول شامل <code>Rows</code> و هر ردیف شامل <code>Cells</code> است. هر سلول می‌تواند شامل چند پاراگراف باشد که با <code>cell.Paragraphs</code> پیمایش می‌شوند. متن سلول‌ها با جداکننده <code> | </code> به هم متصل می‌شود تا ساختار جدول در خروجی متنی حفظ گردد.</li>
<li style="margin-bottom: 8px;"><strong>مدیریت Cancellation:</strong> در هر دو حلقه، <code>cancellationToken</code> بررسی می‌شود تا در صورت لغو درخواست، پردازش سریعاً متوقف شود.</li>
</ul>
</div>
</div>
<div class="section">
<div class="section-header">
<div class="section-number">۳</div>
<div class="section-title">
<h3>پیاده‌سازی کامل ExtractOdsAsync</h3>
<div class="subtitle">Full Implementation of ExtractOdsAsync</div>
</div>
</div>
<div class="section-content">
<p>این متد برای استخراج محتوا از فایل‌های ODS (صفحه‌گسترده) استفاده می‌شود. با استفاده از <code>OdsDocument.Load</code> سند بارگذاری شده و سپس شیت‌ها، ردیف‌ها و سلول‌ها پیمایش می‌شوند. طبق API رسمی، <code>OdsSheet</code> متد <code>GetValue(row, column)</code> را ارائه می‌دهد که مقدار تایپ‌دار یک سلول را بدون نیاز به گسترش ران‌های تکراری برمی‌گرداند[reference:11]. همچنین <code>OdsCellValue</code> دارای ویژگی <code>LexicalValue</code> است که مقدار سلول را به‌صورت رشته‌ای برمی‌گرداند[reference:12].</p>
<pre>private async Task&lt;string&gt; ExtractOdsAsync(
Stream fileStream,
CancellationToken cancellationToken
)
{
// 1. کپی استریم به حافظه
await using var ms = new MemoryStream();
await fileStream.CopyToAsync(ms, cancellationToken);
ms.Position = 0;
// 2. بارگذاری سند ODS
using var document = OdsDocument.Load(ms);
var sb = new StringBuilder();
// 3. پیمایش شیت‌ها
var sheetIndex = 0;
foreach (var sheet in document.Sheets)
{
if (cancellationToken.IsCancellationRequested)
break;
sheetIndex++;
sb.AppendLine($"--- Sheet: {sheet.Name} ---");
// 4. پیمایش ردیف‌ها و سلول‌ها
// از RowCount و ColumnCount برای تعیین محدوده استفاده می‌شود
for (long row = 0; row &lt; sheet.RowCount; row++)
{
if (cancellationToken.IsCancellationRequested)
break;
var cellValues = new List&lt;string&gt;();
for (long col = 0; col &lt; sheet.ColumnCount; col++)
{
// 5. خواندن مقدار سلول با GetValue
var value = sheet.GetValue(row, col);
var text = value?.LexicalValue?.Trim() ?? string.Empty;
cellValues.Add(text);
}
// 6. فقط ردیف‌های غیرخالی را اضافه کن
if (cellValues.Any(v =&gt; !string.IsNullOrWhiteSpace(v)))
{
sb.AppendLine(string.Join(" | ", cellValues));
}
}
sb.AppendLine();
}
return sb.ToString().TrimEnd();
}</pre>
<h4 style="margin-top: 25px; color: var(--primary); font-size: 16px;">توضیح گام‌ها</h4>
<ul style="padding-right: 20px; margin: 10px 0;">
<li style="margin-bottom: 8px;"><strong>بارگذاری سند:</strong> <code>OdsDocument.Load(ms)</code> سند ODS را از استریم حافظه بارگذاری می‌کند[reference:13].</li>
<li style="margin-bottom: 8px;"><strong>پیمایش شیت‌ها:</strong> <code>document.Sheets</code> مجموعه‌ای از <code>OdsSheet</code> را برمی‌گرداند. هر شیت دارای ویژگی <code>Name</code> است.</li>
<li style="margin-bottom: 8px;"><strong>تعیین محدوده ردیف‌ها و ستون‌ها:</strong> <code>OdsSheet</code> ویژگی‌های <code>RowCount</code> و <code>ColumnCount</code> را برای تعیین محدوده داده‌ها ارائه می‌دهد (در API رسمی، <code>Row</code> و <code>Column</code> به‌عنوان متدهای دسترسی ذکر شده‌اند و <code>RowCount</code>/<code>ColumnCount</code> به‌عنوان ویژگی‌های محدوده در دسترس هستند[reference:14]).</li>
<li style="margin-bottom: 8px;"><strong>خواندن مقدار سلول:</strong> <code>sheet.GetValue(row, col)</code> یک <code>OdsCellValue</code> برمی‌گرداند[reference:15]. ویژگی <code>LexicalValue</code> مقدار را به‌صورت رشته‌ای (همان‌طور که در XML ذخیره شده) برمی‌گرداند[reference:16].</li>
<li style="margin-bottom: 8px;"><strong>حذف ردیف‌های خالی:</strong> برای جلوگیری از تولید حجم زیادی از ردیف‌های خالی (که در صفحه‌گسترده‌ها رایج است)، تنها ردیف‌هایی اضافه می‌شوند که حداقل یک سلول غیرخالی داشته باشند.</li>
<li style="margin-bottom: 8px;"><strong>مدیریت Cancellation:</strong> در هر دو حلقه (شیت و ردیف) <code>cancellationToken</code> بررسی می‌شود.</li>
</ul>
<div class="warning-box">
<strong>⚠ نکته درباره RowCount/ColumnCount:</strong> اگر <code>RowCount</code> و <code>ColumnCount</code> در نسخه‌ی مورد استفاده شما به‌عنوان property در دسترس نباشند، می‌توانید از روش جایگزین زیر استفاده کنید:
<pre style="margin-top: 10px;">// روش جایگزین: پیمایش تا زمانی که با خطا مواجه شوید
long row = 0;
while (true)
{
try
{
var value = sheet.GetValue(row, 0);
if (value == null) break;
row++;
}
catch { break; }
}</pre>
اما روش <code>RowCount</code>/<code>ColumnCount</code> ترجیح داده می‌شود زیرا کارآمدتر است و از تولید استثناء جلوگیری می‌کند.
</div>
</div>
</div>
<div class="section">
<div class="section-header">
<div class="section-number">۴</div>
<div class="section-title">
<h3>پیاده‌سازی کامل ExtractOdpAsync</h3>
<div class="subtitle">Full Implementation of ExtractOdpAsync</div>
</div>
</div>
<div class="section-content">
<p>این متد برای استخراج محتوا از فایل‌های ODP (ارائه) استفاده می‌شود. با استفاده از <code>OdpPresentation.Load</code> سند بارگذاری شده و سپس اسلایدها و اشکال (به‌ویژه جعبه‌های متن) پیمایش می‌شوند. طبق API رسمی، <code>OdpPresentation</code> متد <code>AddSlide</code> را برای ایجاد اسلاید جدید ارائه می‌دهد[reference:17] و جعبه‌های متن با <code>OdpTextBox</code> نمایش داده می‌شوند[reference:18].</p>
<pre>private async Task&lt;string&gt; ExtractOdpAsync(
Stream fileStream,
CancellationToken cancellationToken
)
{
// 1. کپی استریم به حافظه
await using var ms = new MemoryStream();
await fileStream.CopyToAsync(ms, cancellationToken);
ms.Position = 0;
// 2. بارگذاری سند ODP
using var presentation = OdpPresentation.Load(ms);
var sb = new StringBuilder();
// 3. پیمایش اسلایدها
var slideIndex = 0;
foreach (var slide in presentation.Slides)
{
if (cancellationToken.IsCancellationRequested)
break;
slideIndex++;
sb.AppendLine($"[Slide {slideIndex}]");
var textParts = new List&lt;string&gt;();
// 4. پیمایش اشکال هر اسلاید و استخراج متن
foreach (var shape in slide.Shapes)
{
// جعبه‌های متن معمولاً از نوع OdpTextBox هستند
if (shape is OdpTextBox textBox
&amp;&amp; !string.IsNullOrWhiteSpace(textBox.Text))
{
textParts.Add(textBox.Text.Trim());
}
// سایر اشکالی که ممکن است متن داشته باشند
else if (shape is OdpShape odpShape
&amp;&amp; !string.IsNullOrWhiteSpace(odpShape.Text))
{
textParts.Add(odpShape.Text.Trim());
}
}
// 5. نوشتن متن‌های استخراج‌شده
if (textParts.Count &gt; 0)
{
sb.AppendLine("Content:");
foreach (var text in textParts)
{
sb.AppendLine($" - {text}");
}
}
sb.AppendLine();
}
return sb.ToString().TrimEnd();
}</pre>
<h4 style="margin-top: 25px; color: var(--primary); font-size: 16px;">توضیح گام‌ها</h4>
<ul style="padding-right: 20px; margin: 10px 0;">
<li style="margin-bottom: 8px;"><strong>بارگذاری سند:</strong> <code>OdpPresentation.Load(ms)</code> سند ODP را از استریم حافظه بارگذاری می‌کند[reference:19].</li>
<li style="margin-bottom: 8px;"><strong>پیمایش اسلایدها:</strong> <code>presentation.Slides</code> مجموعه‌ای از <code>OdpSlide</code> را برمی‌گرداند.</li>
<li style="margin-bottom: 8px;"><strong>پیمایش اشکال:</strong> <code>slide.Shapes</code> مجموعه‌ای از اشکال را برمی‌گرداند. جعبه‌های متن با کلاس <code>OdpTextBox</code> نمایش داده می‌شوند و ویژگی <code>Text</code> آن‌ها محتوای متنی را برمی‌گرداند[reference:20]. برای پوشش سایر انواع اشکال متنی، از کلاس پایه <code>OdpShape</code> و ویژگی <code>Text</code> آن نیز استفاده می‌شود.</li>
<li style="margin-bottom: 8px;"><strong>مدیریت Cancellation:</strong> در حلقه اسلایدها <code>cancellationToken</code> بررسی می‌شود.</li>
</ul>
<div class="info-box">
<strong>💡 نکته درباره یادداشت‌های سخنران:</strong> طبق مستندات <code>ReaderOpenDocumentOptions</code>، گزینه‌ای برای «شامل کردن یادداشت‌های سخنران ODP» وجود دارد[reference:21]. اگر نیاز به استخراج یادداشت‌های سخنران دارید، می‌توانید از <code>OfficeIMO.Reader</code> با تنظیمات مناسب استفاده کنید. در پیاده‌سازی فعلی که مستقیماً از <code>OdpPresentation</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">Summary & Final Notes</div>
</div>
</div>
<div class="section-content">
<p>سه متد <code>ExtractOdtAsync</code>، <code>ExtractOdsAsync</code> و <code>ExtractOdpAsync</code> با استفاده از API رسمی <strong>OfficeIMO.OpenDocument</strong> پیاده‌سازی شدند. جدول زیر خلاصه‌ای از رویکرد هر متد را نشان می‌دهد:</p>
<table class="styled-table">
<thead>
<tr>
<th>متد</th>
<th>کلاس بارگذاری</th>
<th>رویکرد استخراج</th>
<th>خروجی</th>
</tr>
</thead>
<tbody>
<tr>
<td><code>ExtractOdtAsync</code></td>
<td><code>OdtDocument</code></td>
<td>پیمایش <code>Paragraphs</code> و <code>Tables</code></td>
<td>متن پاراگراف‌ها + جداول pipe-separated</td>
</tr>
<tr>
<td><code>ExtractOdsAsync</code></td>
<td><code>OdsDocument</code></td>
<td>پیمایش <code>Sheets</code> و <code>GetValue</code></td>
<td>متن سلول‌ها با جداکننده <code> | </code></td>
</tr>
<tr>
<td><code>ExtractOdpAsync</code></td>
<td><code>OdpPresentation</code></td>
<td>پیمایش <code>Slides</code> و <code>Shapes</code></td>
<td>متن جعبه‌های متن هر اسلاید</td>
</tr>
</tbody>
</table>
<div class="success-box">
<strong>✓ نکات کلیدی برای اجرا:</strong>
<ul style="padding-right: 20px; margin-top: 10px;">
<li>همیشه ابتدا استریم ورودی را به یک <code>MemoryStream</code> کپی کنید، زیرا OfficeIMO به استریم Seekable نیاز دارد.</li>
<li>در تمام حلقه‌ها <code>cancellationToken</code> را بررسی کنید تا از هدر رفتن منابع در صورت لغو درخواست جلوگیری شود.</li>
<li>خروجی هر متد به‌صورت <code>string</code> است که در فیلد <code>Text</code> از <code>XFileExtractionResult</code> قرار می‌گیرد.</li>
<li>مدیریت خطا در سطح <code>ExtractRichAsync</code> انجام می‌شود و خطاها در <code>ErrorMessage</code> ثبت می‌گردند.</li>
</ul>
</div>
<div class="conclusion-box">
<h3>🎯 وضعیت نهایی</h3>
<p>با پیاده‌سازی این سه متد، <code>XOpenDocumentFileContentExtractor</code> قادر خواهد بود هر سه فرمت اصلی ODF (ODT، ODS، ODP) را به‌طور کامل پردازش کند و متن تمیز و ساختاریافته را برای موتور LLM فراهم آورد.</p>
<div class="highlight">
<strong>💎 پوشش نهایی:</strong><br>
ODT (متن + جداول) + ODS (صفحه‌گسترده با سلول‌های تایپ‌دار) + ODP (ارائه با جعبه‌های متن)
</div>
</div>
<div class="info-box">
<strong>📞 گام بعدی:</strong> پس از جایگذاری این کدها در فایل <code>XOpenDocumentFileContentExtractor.cs</code>، تست‌های بخش ۶ مستند قبلی (سناریوهای ۱ تا ۹) را اجرا کنید. در صورت بروز هرگونه مغایرت در نام‌گذاری propertyها (مانند <code>RowCount</code> یا <code>ColumnCount</code>)، با مراجعه به API Reference رسمی OfficeIMO.OpenDocument اصلاحات جزئی لازم را اعمال نمایید.
</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>
File diff suppressed because it is too large Load Diff
@@ -0,0 +1,966 @@
<!DOCTYPE html>
<html lang="fa" dir="rtl">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>راهنمای یکپارچه‌سازی OfficeIMO در XPowerPointFileContentExtractor | پروژه 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-035</div>
<div>نسخه: ۱.۰</div>
<div>تاریخ تهیه: ۱ آبان ۱۴۰۵</div>
<div>طبقه‌بندی: محرمانه</div>
</div>
</div>
<div class="document-title">
<span class="label">INTEGRATION GUIDE</span>
<h1>یکپارچه‌سازی OfficeIMO در XPowerPointFileContentExtractor</h1>
<h2>راهنمای گام‌به‌گام تکمیل پشتیبانی از فرمت‌های قدیمی PowerPoint در سامانه xAiApi</h2>
</div>
</header>
<div class="document-body">
<div class="intro-text">
<strong>به کارفرمای محترم،</strong><br>
این مستند حاصل بررسی و طراحی راه‌حل برای تکمیل پیاده‌سازی <code>XPowerPointFileContentExtractor</code> در پروژه xAiApi است. پس از مواجهه با محدودیت کتابخانه NPOI در پردازش فرمت‌های قدیمی PowerPoint (`.ppt`, `.pot`, `.pps`)، کتابخانه <strong>OfficeIMO.PowerPoint</strong> به‌عنوان یک راه‌حل متن‌باز، رایگان (MIT) و Cross-platform انتخاب شده است. این مستند مراحل نصب، یکپارچه‌سازی با کد موجود، بازنویسی متد <code>ExtractLegacyAsync</code> و ملاحظات استقرار در Docker/Linux را به‌صورت گام‌به‌گام شرح می‌دهد.
</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>پیاده‌سازی اولیه <code>XPowerPointFileContentExtractor</code> با استفاده از <code>DocumentFormat.OpenXml</code> برای فرمت‌های مدرن (`.pptx`, `.ppsx`, `.potx`, `.pptm`, `.ppsm`, `.potm`) با موفقیت انجام شده است. اما برای سه فرمت قدیمی (`.ppt`, `.pot`, `.pps`)، کتابخانه NPOI (حتی با بسته ScratchPad) کلاس سطح بالای <code>HSLFSlideShow</code> را ارائه نمی‌دهد و پیاده‌سازی را با شکست مواجه کرده است.</p>
<p>کتابخانه <strong>OfficeIMO.PowerPoint</strong> با مجوز MIT، بدون وابستگی به Microsoft Office یا COM، و با پشتیبانی صریح از فرمت‌های قدیمی PowerPoint 97-2003، جایگزین مناسب و کاملاً رایگانی است که می‌تواند این شکاف را پر کند.</p>
<div class="highlight-box">
<strong>نتیجه کلیدی:</strong> با جایگزینی بخش Legacy در <code>XPowerPointFileContentExtractor</code> با OfficeIMO.PowerPoint، پوشش کامل ۹ فرمت PowerPoint (۶ فرمت مدرن + ۳ فرمت قدیمی) بدون هیچ وابستگی تجاری و با قابلیت اجرا در Docker/Linux محقق می‌شود.
</div>
<table class="styled-table">
<thead>
<tr>
<th>محور</th>
<th>وضعیت فعلی</th>
<th>پس از یکپارچه‌سازی OfficeIMO</th>
</tr>
</thead>
<tbody>
<tr>
<td>پشتیبانی از فرمت‌های مدرن (.pptx و ...)</td>
<td style="color: var(--success); font-weight: 600;">✓ با OpenXml</td>
<td style="color: var(--success); font-weight: 600;">✓ بدون تغییر</td>
</tr>
<tr>
<td>پشتیبانی از فرمت‌های قدیمی (.ppt, .pot, .pps)</td>
<td style="color: var(--danger); font-weight: 600;">✗ (NPOI ناقص)</td>
<td style="color: var(--success); font-weight: 600;">✓ با OfficeIMO</td>
</tr>
<tr>
<td>مجوز</td>
<td>—</td>
<td style="color: var(--success); font-weight: 600;">MIT (رایگان)</td>
</tr>
<tr>
<td>Cross-platform (Linux/Docker)</td>
<td>—</td>
<td style="color: var(--success); font-weight: 600;">✓ پشتیبانی رسمی</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>
</div>
<div class="section">
<div class="section-header">
<div class="section-number">۲</div>
<div class="section-title">
<h3>معرفی OfficeIMO.PowerPoint</h3>
<div class="subtitle">OfficeIMO.PowerPoint Overview</div>
</div>
</div>
<div class="section-content">
<p>OfficeIMO یک خانواده از کتابخانه‌های .NET برای کار با اسناد Office است که به‌صورت کامل بدون نیاز به Microsoft Office یا COM کار می‌کند. ماژول <code>OfficeIMO.PowerPoint</code> به‌طور اختصاصی برای ایجاد و ویرایش فایل‌های PowerPoint طراحی شده و طبق مستندات رسمی، از فرمت‌های قدیمی نیز پشتیبانی می‌کند.</p>
<div class="info-box">
<strong>💡 نکته کلیدی:</strong> طبق مستندات رسمی OfficeIMO، این کتابخانه “از زیرمجموعه‌های مستندشده فرمت‌های قدیمی <code>.ppt</code>، <code>.pot</code> و <code>.pps</code> پشتیبانی می‌کند” و می‌تواند محتوای پشتیبانی‌شده را به مدل قابل ویرایش وارد کند[reference:0]. همچنین پلتفرم‌های Linux و Docker به‌طور رسمی پشتیبانی می‌شوند[reference:1].
</div>
<h4 style="margin-top: 25px; color: var(--primary); font-size: 16px;">مزایای کلیدی برای پروژه xAiApi</h4>
<table class="styled-table">
<thead>
<tr>
<th>ویژگی</th>
<th>توضیح</th>
<th>ارزش برای پروژه</th>
</tr>
</thead>
<tbody>
<tr>
<td>مجوز MIT</td>
<td>رایگان برای استفاده تجاری</td>
<td style="color: var(--success); font-weight: 600;">بدون هزینه لایسنس</td>
</tr>
<tr>
<td>COM-free</td>
<td>بدون نیاز به نصب Office</td>
<td style="color: var(--success); font-weight: 600;">مناسب Docker/Linux</td>
</tr>
<tr>
<td>پشتیبانی از .ppt/.pot/.pps</td>
<td>پردازش فرمت‌های قدیمی 97-2003</td>
<td style="color: var(--success); font-weight: 600;">تکمیل پوشش فرمت‌ها</td>
</tr>
<tr>
<td>API سطح بالا</td>
<td>مدل شیءگرا مشابه OpenXml</td>
<td style="color: var(--success); font-weight: 600;">یادگیری و استفاده آسان</td>
</tr>
<tr>
<td>پشتیبانی از Linux/Docker</td>
<td>اجرا در محیط‌های کانتینری</td>
<td style="color: var(--success); font-weight: 600;">استقرار بدون تغییر</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">Installation & Setup</div>
</div>
</div>
<div class="section-content">
<h4 style="margin-top: 20px; color: var(--primary); font-size: 16px;">گام ۱ — افزودن بسته NuGet</h4>
<p>در فایل <code>xAiApi.csproj</code>، بسته <code>OfficeIMO.PowerPoint</code> را اضافه کنید. دستور نصب از طریق .NET CLI به‌صورت زیر است[reference:2]:</p>
<pre>dotnet add package OfficeIMO.PowerPoint</pre>
<p>یا مستقیماً در فایل پروژه:</p>
<pre>&lt;PackageReference Include="OfficeIMO.PowerPoint" Version="..." /&gt;</pre>
<div class="info-box">
<strong>💡 نکته:</strong> بسته <code>DocumentFormat.OpenXml</code> که برای فرمت‌های مدرن استفاده می‌شود، از قبل در پروژه وجود دارد و نیازی به تغییر آن نیست. همچنین بسته‌های NPOI مربوط به PowerPoint (از جمله <code>ScratchPad.NPOI.HSLF</code>) را می‌توانید حذف کنید.
</div>
<h4 style="margin-top: 25px; color: var(--primary); font-size: 16px;">گام ۲ — حذف using های NPOI</h4>
<p>در فایل <code>XPowerPointFileContentExtractor.cs</code>، خطوط زیر را حذف کنید:</p>
<pre>// حذف شوند
using NPOI.HSLF.UserModel;
using NPOI.HSLF;</pre>
<h4 style="margin-top: 25px; color: var(--primary); font-size: 16px;">گام ۳ — افزودن using های OfficeIMO</h4>
<p>خطوط زیر را به بالای فایل اضافه کنید:</p>
<pre>using OfficeIMO.PowerPoint;
using OfficeIMO.PowerPoint.LegacyPpt;</pre>
<div class="success-box">
<strong>✓ نتیجه این بخش:</strong> پس از این سه گام، پروژه آماده است تا متد <code>ExtractLegacyAsync</code> با استفاده از OfficeIMO بازنویسی شود. بخش <code>ExtractOpenXmlAsync</code> و سایر بخش‌های کد (مانند تشخیص MIME و ثبت در 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">Integration with Existing Code</div>
</div>
</div>
<div class="section-content">
<p>ساختار کلی کلاس <code>XPowerPointFileContentExtractor</code> پس از یکپارچه‌سازی به‌صورت زیر خواهد بود. تنها متد <code>ExtractLegacyAsync</code> بازنویسی می‌شود و بقیه اجزا دست‌نخورده باقی می‌مانند.</p>
<h4 style="margin-top: 20px; color: var(--primary); font-size: 16px;">ساختار نهایی کلاس</h4>
<pre>public class XPowerPointFileContentExtractor : IXPowerPointFileContentExtractor
{
// ۱. آرایه MIME Type ها (بدون تغییر)
private static readonly string[] SupportedMimeTypes = [ ... ];
// ۲. CanExtract (بدون تغییر)
public bool CanExtract(string mimeType) { ... }
// ۳. ExtractAsync (بدون تغییر)
public async Task&lt;string&gt; ExtractAsync(...) { ... }
// ۴. ExtractRichAsync (بدون تغییر)
public async Task&lt;XFileExtractionResult&gt; ExtractRichAsync(...) { ... }
// ۵. ExtractOpenXmlAsync (بدون تغییر — برای ۶ فرمت مدرن)
private async Task&lt;string&gt; ExtractOpenXmlAsync(...) { ... }
// ۶. ExtractLegacyAsync (بازنویسی با OfficeIMO — برای ۳ فرمت قدیمی)
private async Task&lt;string&gt; ExtractLegacyAsync(...) { ... }
// ۷. متدهای کمکی OpenXml (بدون تغییر)
private static string GetSlideTitle(...) { ... }
private static string GetSpeakerNotes(...) { ... }
private static string ExtractOpenXmlTables(...) { ... }
}</pre>
<div class="highlight-box">
<strong>نکته مهم:</strong> تشخیص اینکه کدام متد فراخوانی شود، همچنان توسط متد <code>ExtractRichAsync</code> و بر اساس MIME Type انجام می‌شود. متدهای <code>IsOpenXmlFormat</code> و <code>IsLegacyFormat</code> بدون تغییر باقی می‌مانند.
</div>
</div>
</div>
<div class="section">
<div class="section-header">
<div class="section-number">۵</div>
<div class="section-title">
<h3>بازنویسی متد ExtractLegacyAsync با OfficeIMO</h3>
<div class="subtitle">Rewriting ExtractLegacyAsync with OfficeIMO</div>
</div>
</div>
<div class="section-content">
<p>متد جدید <code>ExtractLegacyAsync</code> از <code>PowerPointPresentation.Load</code> برای بارگذاری فایل‌های قدیمی استفاده می‌کند و سپس با پیمایش اسلایدها و شکل‌ها، متن را استخراج می‌کند.</p>
<pre>private async Task&lt;string&gt; ExtractLegacyAsync(
Stream fileStream,
CancellationToken cancellationToken)
{
var sb = new StringBuilder();
// ۱. کپی استریم به MemoryStream (OfficeIMO به استریم Seekable نیاز دارد)
await using var ms = new MemoryStream();
await fileStream.CopyToAsync(ms, cancellationToken);
ms.Position = 0;
// ۲. بارگذاری ارائه با OfficeIMO
// طبق مستندات، Load به‌طور خودکار فرمت .ppt/.pot/.pps را تشخیص می‌دهد
using var presentation = PowerPointPresentation.Load(ms);
// ۳. پیمایش اسلایدها
var slideIndex = 0;
foreach (var slide in presentation.Slides)
{
if (cancellationToken.IsCancellationRequested) break;
slideIndex++;
sb.AppendLine($"[Slide {slideIndex}]");
// ۴. استخراج متن از شکل‌های اسلاید
var textParts = new List&lt;string&gt;();
var imageCount = 0;
foreach (var shape in slide.Shapes)
{
// اگر شکل دارای متن است
if (shape is PowerPointTextBox textBox)
{
if (!string.IsNullOrWhiteSpace(textBox.Text))
{
textParts.Add(textBox.Text.Trim());
}
}
// اگر شکل از نوع LegacyPptShape باشد (در صورت دسترسی مستقیم)
else if (shape is LegacyPptShape legacyShape)
{
if (!string.IsNullOrWhiteSpace(legacyShape.Text))
{
textParts.Add(legacyShape.Text.Trim());
}
if (legacyShape.Picture != null)
{
imageCount++;
}
}
}
// ۵. نوشتن متن‌های استخراج‌شده
if (textParts.Count &gt; 0)
{
sb.AppendLine("Content:");
foreach (var text in textParts)
{
sb.AppendLine($" - {text}");
}
}
// ۶. شمارش تصاویر (در صورت وجود)
if (imageCount &gt; 0)
{
sb.AppendLine($"[Images: {imageCount}]");
}
sb.AppendLine();
}
return sb.ToString().TrimEnd();
}</pre>
<div class="warning-box">
<strong>⚠ نکته درباره API دقیق:</strong> ساختار ارائه‌شده بر اساس الگوی مستندات OfficeIMO (<code>presentation.Slides</code> و <code>slide.Shapes</code>) و کلاس <code>LegacyPptShape</code> که دارای ویژگی <code>Text</code> است، طراحی شده است[reference:3][reference:4]. در صورت تفاوت جزئی در نام‌گذاری کلاس‌ها (مانند <code>PowerPointTextBox</code>)، با مراجعه به API Reference رسمی OfficeIMO، اصلاحات جزئی لازم خواهد بود. با این حال، منطق کلی (بارگذاری با <code>Load</code>، پیمایش <code>Slides</code> و <code>Shapes</code>، و استخراج <code>Text</code>) صحیح است.
</div>
<h4 style="margin-top: 25px; color: var(--primary); font-size: 16px;">گزینه جایگزین: تبدیل به PPTX و استفاده از OpenXml</h4>
<p>اگر ترجیح می‌دهید تمام پردازش متنی را با <code>DocumentFormat.OpenXml</code> انجام دهید، OfficeIMO یک متد تبدیل درون‌خطی ارائه می‌دهد:</p>
<pre>// تبدیل .ppt به .pptx با OfficeIMO
PowerPointPresentation.Convert("input.ppt", "output.pptx");
// سپس فایل output.pptx را با OpenXml پردازش کنید</pre>
<p>این روش به‌ویژه زمانی مفید است که بخواهید از <code>ExtractOpenXmlAsync</code> موجود (که یادداشت‌ها و جداول را نیز استخراج می‌کند) برای فایل‌های قدیمی تبدیل‌شده استفاده کنید[reference:5]. اما توجه داشته باشید که تبدیل ممکن است بخشی از وفاداری ساختاری را از دست بدهد.</p>
<div class="info-box">
<strong>💡 توصیه:</strong> برای حفظ سادگی و یکپارچگی، استفاده مستقیم از <code>PowerPointPresentation.Load</code> و استخراج متن (روش اول) پیشنهاد می‌شود. روش تبدیل به‌عنوان یک گزینه پشتیبان در دسترس است.
</div>
</div>
</div>
<div class="section">
<div class="section-header">
<div class="section-number">۶</div>
<div class="section-title">
<h3>ملاحظات استقرار در Docker و Linux</h3>
<div class="subtitle">Docker & Linux Deployment Considerations</div>
</div>
</div>
<div class="section-content">
<p>OfficeIMO به‌طور رسمی از Linux و Docker پشتیبانی می‌کند[reference:6]. با این حال، برای اطمینان از عملکرد صحیح، نکات زیر باید در Dockerfile رعایت شوند:</p>
<table class="styled-table">
<thead>
<tr>
<th>مورد</th>
<th>توضیح</th>
<th>الزامی؟</th>
</tr>
</thead>
<tbody>
<tr>
<td>نصب فونت‌ها</td>
<td>برای پردازش صحیح متن و اندازه‌گیری، فونت‌های فارسی و لاتین باید در ایمیج موجود باشند</td>
<td style="color: var(--warning); font-weight: 600;">توصیه‌شده</td>
</tr>
<tr>
<td>libgdiplus (در صورت استفاده از PDF)</td>
<td>اگر از قابلیت‌های PDF در OfficeIMO استفاده می‌کنید، ممکن است لازم باشد</td>
<td style="color: var(--warning); font-weight: 600;">بسته به سناریو</td>
</tr>
<tr>
<td>پشتیبانی NativeAOT</td>
<td>OfficeIMO با NativeAOT سازگار است (اختیاری)</td>
<td style="color: var(--success); font-weight: 600;">اختیاری</td>
</tr>
</tbody>
</table>
<h4 style="margin-top: 25px; color: var(--primary); font-size: 16px;">نمونه Dockerfile</h4>
<pre>FROM mcr.microsoft.com/dotnet/aspnet:8.0 AS base
WORKDIR /app
EXPOSE 80
# نصب فونت‌های فارسی و لاتین
RUN apt-get update && apt-get install -y \
fonts-dejavu \
fonts-farsiweb \
&& rm -rf /var/lib/apt/lists/*
FROM mcr.microsoft.com/dotnet/sdk:8.0 AS build
WORKDIR /src
COPY ["xAiApi/xAiApi.csproj", "xAiApi/"]
RUN dotnet restore "xAiApi/xAiApi.csproj"
COPY . .
WORKDIR "/src/xAiApi"
RUN dotnet build "xAiApi.csproj" -c Release -o /app/build
FROM build AS publish
RUN dotnet publish "xAiApi.csproj" -c Release -o /app/publish
FROM base AS final
WORKDIR /app
COPY --from=publish /app/publish .
ENTRYPOINT ["dotnet", "xAiApi.dll"]</pre>
<div class="success-box">
<strong>✓ نکته:</strong> طبق مستندات OfficeIMO، این کتابخانه “برای تولید اسناد بدون COM در کانتینرهای Linux، GitHub Actions و Azure DevOps مناسب است” و نیازی به نصب Microsoft Office یا LibreOffice ندارد[reference:7].
</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>پس از یکپارچه‌سازی، تست‌های زیر برای اطمینان از صحت عملکرد ضروری هستند:</p>
<table class="styled-table">
<thead>
<tr>
<th>#</th>
<th>سناریو</th>
<th>ورودی</th>
<th>خروجی مورد انتظار</th>
</tr>
</thead>
<tbody>
<tr>
<td>۱</td>
<td>استخراج .ppt ساده</td>
<td>فایل .ppt با ۳ اسلاید متنی</td>
<td>متن هر ۳ اسلاید با برچسب [Slide N]</td>
</tr>
<tr>
<td>۲</td>
<td>استخراج .pps</td>
<td>فایل Slide Show قدیمی</td>
<td>مانند .ppt</td>
</tr>
<tr>
<td>۳</td>
<td>استخراج .pot</td>
<td>فایل قالب قدیمی</td>
<td>مانند .ppt</td>
</tr>
<tr>
<td>۴</td>
<td>فایل .ppt با تصویر</td>
<td>اسلاید حاوی تصویر</td>
<td>شمارش تصویر در خروجی ([Images: N])</td>
</tr>
<tr>
<td>۵</td>
<td>فایل .pptx (رگرسیون)</td>
<td>فایل مدرن</td>
<td>همچنان با OpenXml و بدون تغییر پردازش شود</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>بدون خطای PlatformNotSupportedException</td>
</tr>
</tbody>
</table>
<div class="info-box">
<strong>💡 معیار پذیرش:</strong> پس از اجرای موفق تست‌های ۱ تا ۷، می‌توان تأیید کرد که یکپارچه‌سازی OfficeIMO به‌درستی انجام شده و سامانه xAiApi آماده پردازش تمام ۹ فرمت PowerPoint در محیط Docker/Linux است.
</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>با یکپارچه‌سازی <strong>OfficeIMO.PowerPoint</strong> در کنار کد موجود، شکاف پشتیبانی از فرمت‌های قدیمی PowerPoint به‌طور کامل و رایگان پر می‌شود. این راه‌حل:</p>
<ul style="padding-right: 20px; margin: 15px 0;">
<li style="margin-bottom: 8px;"><strong>رایگان و متن‌باز است</strong> (مجوز MIT) و هیچ هزینه لایسنسی ندارد.</li>
<li style="margin-bottom: 8px;"><strong>در Docker و Linux کار می‌کند</strong> و نیازی به Microsoft Office یا COM ندارد.</li>
<li style="margin-bottom: 8px;"><strong>با ساختار موجود پروژه سازگار است</strong> و تنها متد <code>ExtractLegacyAsync</code> بازنویسی می‌شود.</li>
<li style="margin-bottom: 8px;"><strong>پوشش کامل ۹ فرمت PowerPoint</strong> (۶ مدرن + ۳ قدیمی) را فراهم می‌کند.</li>
</ul>
<table class="styled-table">
<thead>
<tr>
<th>محور</th>
<th>قبل از یکپارچه‌سازی</th>
<th>پس از یکپارچه‌سازی</th>
</tr>
</thead>
<tbody>
<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(--warning); font-weight: 600;">مطرح بود</td>
<td style="color: var(--success); font-weight: 600;">حذف شد</td>
</tr>
<tr>
<td>قابلیت اجرا در Docker/Linux</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>آمادگی تولیدی</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>با توجه به تحلیل‌های انجام‌شده، OfficeIMO.PowerPoint بهترین گزینه رایگان برای تکمیل پشتیبانی از فرمت‌های قدیمی PowerPoint در پروژه xAiApi است.</p>
<div class="highlight">
<strong>💎 ارزش پیشنهادی:</strong><br>
پوشش کامل ۹ فرمت PowerPoint + مجوز MIT رایگان + استقرار Cross-platform + یکپارچگی با کد موجود + بدون وابستگی تجاری
</div>
</div>
<div class="info-box">
<strong>📞 گام بعدی:</strong> در صورت تایید این مستند، آماده‌ام کد کامل و نهایی <code>XPowerPointFileContentExtractor</code> را با یکپارچه‌سازی OfficeIMO و اصلاحات لازم ارائه کنم تا مستقیماً جایگزین نسخه فعلی شود.
</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>
File diff suppressed because it is too large Load Diff
@@ -0,0 +1,996 @@
<!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>
File diff suppressed because it is too large Load Diff
File diff suppressed because it is too large Load Diff
File diff suppressed because it is too large Load Diff
@@ -1,5 +1,5 @@
### FILE: C:\Users\hkhazaeeasl\Documents\Projets\XProjects\xSaherelmWorkspace\Modules\xAiService\Constants\ConfigurationNodeNames.cs
### FILE: C:\Users\saherelm\Documents\Projects\xSaherelmWorkspace\Modules\xAiService\Constants\ConfigurationNodeNames.cs
namespace xAiService.Constants
{
@@ -9,7 +9,7 @@ namespace xAiService.Constants
}
}
### FILE: C:\Users\hkhazaeeasl\Documents\Projets\XProjects\xSaherelmWorkspace\Modules\xAiService\Constants\XAiServiceConstants.cs
### FILE: C:\Users\saherelm\Documents\Projects\xSaherelmWorkspace\Modules\xAiService\Constants\XAiServiceConstants.cs
namespace xAiService.Constants
{
@@ -28,7 +28,7 @@ namespace xAiService.Constants
}
}
### FILE: C:\Users\hkhazaeeasl\Documents\Projets\XProjects\xSaherelmWorkspace\Modules\xAiService\DI\XDIHelperExtension.cs
### FILE: C:\Users\saherelm\Documents\Projects\xSaherelmWorkspace\Modules\xAiService\DI\XDIHelperExtension.cs
namespace xAiService.DI
{
@@ -36,7 +36,7 @@ namespace xAiService.DI
{ }
}
### FILE: C:\Users\hkhazaeeasl\Documents\Projets\XProjects\xSaherelmWorkspace\Modules\xAiService\Extensions\XAiServiceExtensions.cs
### FILE: C:\Users\saherelm\Documents\Projects\xSaherelmWorkspace\Modules\xAiService\Extensions\XAiServiceExtensions.cs
using xCommons.Extensions;
using xExceptions.Constants;
@@ -83,7 +83,7 @@ namespace xAiService.Extensions
}
}
### FILE: C:\Users\hkhazaeeasl\Documents\Projets\XProjects\xSaherelmWorkspace\Modules\xAiService\Helpers\VectorHelper.cs
### FILE: C:\Users\saherelm\Documents\Projects\xSaherelmWorkspace\Modules\xAiService\Helpers\VectorHelper.cs
using System;
using xCommons.Extensions;
File diff suppressed because it is too large Load Diff
+1
View File
@@ -0,0 +1 @@
.\ContextGenerator.ps1 .\xAiApi; .\ContextGenerator.ps1 .\Modules\xAiModels; .\ContextGenerator.ps1 .\Modules\xAiService
+1 -1
Submodule xAiApi updated: 877627714c...f39cde0d09