社区文档构建进行中,欢迎编辑。社区答疑群(非官方):717421103

全站通知:

帮助:解析函数/bidi

来自WIKI实验室WIKI_BWIKI_哔哩哔哩
跳到导航 跳到搜索

Bidi是一个解析函数。帮助:解析函数页列出了所有解析函数和魔术字的说明。

bidi

涉及从右到左书写的文字时可使用,正确显示多种书写方向的文本。MediaWiki原生支持。

根据内容自动增加方向性控制字符(从左到右U+202A,从右到左U+202B)。

语法

{{bidi:文本}}

示例

{{bidi:text transform}}‪‪text transform‬‬
{{bidi:كتابة عربية}}‫كتابة عربية‬
{{bidi:كتابة عربيةabc def}}‫كتابة عربيةabc def‬

底层代码

/** mediawiki-1.37.0\includes\parser\CoreParserFunctions.php
 * @param Parser $parser
 * @param string $text
 * @return string
 */
public static function bidi( $parser, $text = '' ) {
	return $parser->getFunctionLang()->embedBidi( $text );
}


/** mediawiki-1.37.0\includes\language\Language.php
 * Unicode directional formatting characters, for embedBidi()
 */
private const LRE = "\u{202A}"; // U+202A LEFT-TO-RIGHT EMBEDDING
private const RLE = "\u{202B}"; // U+202B RIGHT-TO-LEFT EMBEDDING
private const PDF = "\u{202C}"; // U+202C POP DIRECTIONAL FORMATTING

/** mediawiki-1.37.0\includes\language\Language.php
 * Wraps argument with unicode control characters for directionality safety
 *
 * This solves the problem where directionality-neutral characters at the edge of
 * the argument string get interpreted with the wrong directionality from the
 * enclosing context, giving renderings that look corrupted like "(Ben_(WMF".
 *
 * The wrapping is LRE...PDF or RLE...PDF, depending on the detected
 * directionality of the argument string, using the BIDI algorithm's own "First
 * strong directional codepoint" rule. Essentially, this works round the fact that
 * there is no embedding equivalent of U+2068 FSI (isolation with heuristic
 * direction inference). The latter is cleaner but still not widely supported.
 *
 * @param string $text Text to wrap
 * @return string Text, wrapped in LRE...PDF or RLE...PDF or nothing
 */
public function embedBidi( $text = '' ) {
	$dir = self::strongDirFromContent( $text );
	if ( $dir === 'ltr' ) {
		// Wrap in LEFT-TO-RIGHT EMBEDDING ... POP DIRECTIONAL FORMATTING
		return self::LRE . $text . self::PDF;
	}
	if ( $dir === 'rtl' ) {
		// Wrap in RIGHT-TO-LEFT EMBEDDING ... POP DIRECTIONAL FORMATTING
		return self::RLE . $text . self::PDF;
	}
	// No strong directionality: do not wrap
	return $text;
}
代码逻辑:
  • 根据文本方向性,嵌入控制显示方向的Unicode控制字符。
  • 文本方向的判断基于 strongDirFromContent 函数,它使用了结构为/(?:[LTR字符]|RTL字符)/u的正则表达式,检测文本中第一个具有强方向性的字符并返回其方向性。

实际用例

  • BWiki托管的wiki中,暂未发现使用案例。