扩展:CSS

From MediaWiki.org
Jump to navigation Jump to search
This page is a translated version of the page Extension:CSS and the translation is 36% complete.

Other languages:
Bahasa Indonesia • ‎Deutsch • ‎English • ‎català • ‎dansk • ‎español • ‎français • ‎italiano • ‎polski • ‎português • ‎português do Brasil • ‎sicilianu • ‎suomi • ‎中文 • ‎日本語
MediaWiki扩展手册
OOjs UI icon advanced.svg
CSS
发布状态: 稳定版
实现 Parser function
描述 A parser-function for adding CSS files, article or inline rules to articles
作者 Aran Dunkley, Rusty Burchfield
最新版本 3.5.0 (2017-06-06)
MediaWiki 1.25+
数据更新
许可协议 GNU General Public License 2.0 or later
下载
例子 Organised Design
使用的钩子
ParserFirstCallInit
RawPageViewBeforeOutput
翻译CSS扩展如果在translatewiki.net可用
检查使用和版本矩阵。
问题 开放的工作 · 报告错误

CSS擴展允許將CSS樣式表載入到特定文章中。CSS樣式表可以是另一個文檔,也可以是直接在解析器函數中定義的規則。

用法

例如,如果你有一個名為「MyStyles.css」的CSS樣式表,它們是用於共筆頁「MyFancyUserPage」的樣式,你可以為後者添加以下解析器函數語法:

{{#css:MyStyles.css}}

再者,如果「MyStyles.css」是位於/wiki/skins目錄之下的檔案,那麼路徑應該也被寫進去,如下所示。請注意,檔案路徑必須是帶有文件根目錄斜杠的絕對路徑,以將其與檔名區分開來。

{{#css:/skins/MyStyles.css}}

或者,CSS規則可以直接包含在解析器函數(inline)中,如下例所示:

{{#css:
  body {
    background: yellow;
    font-size: 20pt;
    color: red;
  }
}}

安装

  • 下载文件,并将其放置在您extensions/文件夹中的CSS目录内。
  • 将下列代码放置在您的LocalSettings.php的底部:
    wfLoadExtension( 'CSS' );
    
  • Yes 完成 – 在您的wiki上导航至Special:Version,以验证扩展已成功安装。

致使用MediaWiki 1.29或更早版本的用户:

上面的说明介绍的是安装此扩展的新方法,它使用wfLoadExtension() 如果您需要在早期版本(MediaWiki 1.29和更早版本)中安装此扩展,而不是wfLoadExtension( 'CSS' );,您需要使用:

require_once "$IP/extensions/CSS/CSS.php";

Configuration

Optionally, you can set the base URL for external files.

$wgCSSPath = false;  # Default, relative to $wgScriptPath
$wgCSSPath = '';  # Relative to the root of your server
$wgCSSPath = 'https://example.org/stylesheets';  # Relative to a different site

Use as a PageCSS replacement

There is a prior Extension:PageCSS (now archived) which used <css> and </css> tags instead of the #css: parser function. This extension can do most of what those extensions provide, but the syntax is incompatible. One way to avoid breaking existing pages which still use the old <css> tags is to use both this Extension:CSS and Extension:NewPageCSS on the same wiki.

An alternate solution (if you want to use only this extension, but have existing content which expects Extension:PageCSS) is to save this stub function as a PHP file and include it from your LocalSettings.php to remap <css> to #css:

<?php

// Stub to remap <css></css> tags to {{#css:}} parser function extension

// For use when installing http://www.mediawiki.org/wiki/Extension:CSS to replace Extension:NewPageCSS on existing wikis

$wgHooks['ParserFirstCallInit'][] = 'wfCSSParserStubInit';
 
// Hook our callback function into the parser
function wfCSSParserStubInit( Parser $parser ) {

        // When the parser sees the <css> tag, it executes the stub wfCSSTagRender function (below) to invoke the {{#css:}} parser
        $parser->setHook( 'css', 'wfCSSTagRender' );
        return true;
}
// Execute <css> tag
function wfCSSTagRender( $input, array $args, Parser $parser, PPFrame $frame ) {

        // Stub function, just redirect the user-provided input text blindly to the {{#css:}} parser function to re-parse it as wikitext
        $output = $parser->recursiveTagParse('{{#css:' . $input . '}}', $frame);
        return $output;
}

At this point, the prior PageCSS (or NewPageCSS) extensions may be removed from your configuration; <css> now goes to #css: and (if you have this CSS extension already up and running) will behave the way any of the other CSS extensions always have.

Problems with changing the color or background of a specific page

Using the parser function to use the CSS from another page does not work to change the color or background of the page, however, using the parser function on a single page by itself does allow the background to be changed. To overcome this problem, it is possible to load the CSS through transclusion, which does not cause the problem of failing to load the change to the page background.

错误

讨论页

参见