API
يوفر shikiphp نقطتي دخول: الواجهة الثابتة Shikiphp للحالات الشائعة، ونسخة Highlighter لكل ما عدا ذلك (مخرجات HAST، وقواعد لغوية وقوالب مخصصة، ومتغيرات القوالب، وفحص الحزمة).
واجهة Shikiphp
Shikiphp\Shikiphp واجهة ثابتة رقيقة فوق أداة إبراز مضمنة تُنشأ عند الحاجة وتُخزَّن مؤقتًا.
Shikiphp::codeToHtml
public static function codeToHtml(string $code, array $options): string
يحلل الرموز في $code ويعرض كتلة <pre class="shiki …"> HTML كاملة. تتضمن مصفوفة الخيارات دائمًا lang، إضافة إلى theme أو خريطة themes، وأي من خيارات العرض.
use Shikiphp\Shikiphp;
echo Shikiphp::codeToHtml($code, ['lang' => 'php', 'theme' => 'github-dark']);
Shikiphp::codeToTokens
/** @return list<list<ThemedToken>> */
public static function codeToTokens(string $code, array $options): array
يعيد شبكة الرموز ثنائية الأبعاد كما هي (قائمة داخلية لكل سطر) من كائنات
Shikiphp\Render\ThemedToken بدلًا من HTML.
use Shikiphp\Shikiphp;
$lines = Shikiphp::codeToTokens($code, ['lang' => 'js', 'theme' => 'nord']);
foreach ($lines as $line) {
foreach ($line as $token) {
// $token->content, $token->color, $token->fontStyle,
// $token->bgColor, $token->htmlStyle, $token->offset
}
}
Shikiphp::codeToTokensBase
/** @return list<list<ThemedToken>> */
public static function codeToTokensBase(string $code, array $options): array
اسم بديل لـcodeToTokens يطابق الاسم codeToTokensBase الذي تستخدمه Shiki.
Shikiphp::codeToTokensResult
public static function codeToTokensResult(string $code, array $options): TokensResult
يعيد النتيجة الغنية التي تحاكي codeToTokens ذات المستوى الأعلى في Shiki: شبكة الرموز
بالإضافة إلى الألوان المحسومة واسم القالب ونمط الجذر للقوالب المزدوجة وحالة
القواعد اللغوية النهائية. راجع TokensResult أدناه.
use Shikiphp\Shikiphp;
$result = Shikiphp::codeToTokensResult($code, [
'lang' => 'php',
'theme' => 'github-dark',
]);
$result->tokens; // list<list<ThemedToken>>
$result->fg; // "#e1e4e8"
$result->bg; // "#24292e"
$result->themeName; // "github-dark"
$result->grammarState; // GrammarState
Shikiphp::getLastGrammarState
public static function getLastGrammarState(string $code, array $options): GrammarState
يحلل الرموز في $code ويعيد حالة القواعد اللغوية التي وصل إليها في النهاية (getLastGrammarState لدى Shiki). مرّرها مجددًا عبر خيار grammarState لمتابعة تحليل جزء لاحق من الحالة نفسها — وهو مفيد للإبراز التدريجي. يرمي خطأً إذا كان lang هو 'ansi' (فلا توجد حالة قواعد لغوية في ANSI).
use Shikiphp\Shikiphp;
$state = Shikiphp::getLastGrammarState("/* unterminated comment", [
'lang' => 'js',
'theme' => 'nord',
]);
$html = Shikiphp::codeToHtml("still inside the comment */", [
'lang' => 'js',
'theme' => 'nord',
'grammarState' => $state,
]);
Shikiphp::highlighter
public static function highlighter(): Highlighter
تعيد Highlighter المجمّع المشترك، وتنشئه عند الاستخدام الأول. استخدم هذا للوصول إلى أساليب النسخة أدناه.
Shikiphp::use وShikiphp::reset
public static function use(Highlighter $highlighter): void
public static function reset(): void
يستبدل use أداة إبراز Highlighter مخصصة (مثل أداة حمّلت إليها قواعد لغوية أو قوالب مخصصة) لتفويض الواجهة إليها. يمسح reset النسخة المخزنة مؤقتًا، بحيث تعيد المكالمة التالية إنشاء أداة الإبراز المضمنة الافتراضية.
Highlighter
تتضمن Shikiphp\Highlighter الواجهة الكاملة. احصل على النسخة المضمنة المشتركة باستخدام Shikiphp::highlighter()، أو أنشئ واحدة صراحةً:
use Shikiphp\Highlighter;
$highlighter = Highlighter::createBundled();
تطبّق codeToHtml وcodeToTokens وcodeToTokensBase وcodeToTokensResult وgetLastGrammarState بالتواقيع نفسها للواجهة، إضافة إلى ما يلي.
Highlighter::codeToHast
public function codeToHast(string $code, array $options): \Shikiphp\Hast\Element
ينشئ شجرة HAST pre.shiki > code > (span.line > span)* التي تنشئها Shiki، لتكون جاهزة للتصفح أو التعديل أو التسلسل بنفسك. وهذا ما تسلسله codeToHtml داخليًا.
use Shikiphp\Shikiphp;
$hast = Shikiphp::highlighter()->codeToHast($code, [
'lang' => 'rust',
'theme' => 'nord',
]);
Highlighter::codeToTokensWithThemes
/** @return list<list<ThemedTokenWithVariants>> */
public function codeToTokensWithThemes(string $code, array $options): array
يحلل الرموز مرة لكل قالب ويدمج النتيجة في variants لكل رمز، مفهرسة بمفتاح القالب، محاكيًا codeToTokensWithThemes لدى Shiki. يتطلب خريطة themes.
use Shikiphp\Shikiphp;
$lines = Shikiphp::highlighter()->codeToTokensWithThemes($code, [
'lang' => 'ts',
'themes' => ['light' => 'github-light', 'dark' => 'github-dark'],
]);
$lines[0][0]->content; // token text
$lines[0][0]->variants['light']; // ThemedTokenStyle for the light theme
$lines[0][0]->variants['dark']; // ThemedTokenStyle for the dark theme
Highlighter::loadGrammar وHighlighter::loadTheme
public function loadGrammar(
array $rawTmLanguage,
?string $langId = null,
array $aliases = [],
array $embedded = [],
): void
public function loadTheme(array $rawTheme): void
سجّل قواعد TextMate لغوية أو قالب VS Code مخصصًا من JSON مفكوك الترميز. راجع اللغات والقوالب المخصصة.
استكشاف الحزمة
/** @return list<string> */ public function loadedLanguages(): array
/** @return list<string> */ public function loadedThemes(): array
/** @return list<string> */ public function bundledLanguages(): array
/** @return list<string> */ public function bundledThemes(): array
تعرض loadedLanguages/loadedThemes كل المعرّفات المتاحة حاليًا (المضمنة وأي معرّفات مخصصة سجّلتها). وتعرض bundledLanguages/bundledThemes المعرّفات المضمنة فقط. راجع اللغات والقوالب.
كائنات القيمة
ThemedToken
Shikiphp\Render\ThemedToken مقطع نصي واحد منسق:
final class ThemedToken
{
public readonly string $content;
public readonly ?string $color;
public readonly int $fontStyle; // FontStyle bitmask
public readonly ?string $bgColor;
public readonly ?string $htmlStyle; // pre-built inline style (dual themes / ANSI)
public readonly int $offset; // UTF-16 code-unit offset into the source
}
fontStyle قناع بتات من Shikiphp\Theme\FontStyle (NONE وITALIC وBOLD وUNDERLINE وSTRIKETHROUGH).
TokensResult
Shikiphp\TokensResult هي القيمة المعادة من codeToTokensResult:
final class TokensResult
{
public readonly array $tokens; // list<list<ThemedToken>>
public readonly ?string $fg; // resolved foreground
public readonly ?string $bg; // resolved background
public readonly ?string $themeName; // single id, or "shiki-themes …"
public readonly string|false|null $rootStyle; // dual-theme root style (or null)
public readonly ?GrammarState $grammarState;
}
GrammarState
يحتوي Shikiphp\GrammarState على مكدس قواعد المحلل، مفهرسًا باسم القالب، لمتابعة الإبراز من نقطة وسيطة.
final class GrammarState
{
public readonly string $lang;
public function theme(): string; // first theme name
public function themes(): array; // list<string>
public function getScopes(?string $theme = null): array; // scopes in effect, innermost first
public function withTheme(string $theme): self;
}