Merge "Add parser tests about building table with {{!}}"
[lhc/web/wiklou.git] / includes / parser / CoreTagHooks.php
1 <?php
2 /**
3 * Tag hooks provided by MediaWiki core
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
19 *
20 * @file
21 * @ingroup Parser
22 */
23
24 /**
25 * Various tag hooks, registered in Parser::firstCallInit()
26 * @ingroup Parser
27 */
28 class CoreTagHooks {
29 /**
30 * @param Parser $parser
31 * @return void
32 */
33 public static function register( $parser ) {
34 global $wgRawHtml;
35 $parser->setHook( 'pre', array( __CLASS__, 'pre' ) );
36 $parser->setHook( 'nowiki', array( __CLASS__, 'nowiki' ) );
37 $parser->setHook( 'gallery', array( __CLASS__, 'gallery' ) );
38 if ( $wgRawHtml ) {
39 $parser->setHook( 'html', array( __CLASS__, 'html' ) );
40 }
41 }
42
43 /**
44 * Core parser tag hook function for 'pre'.
45 * Text is treated roughly as 'nowiki' wrapped in an HTML 'pre' tag;
46 * valid HTML attributes are passed on.
47 *
48 * @param string $text
49 * @param array $attribs
50 * @param Parser $parser
51 * @return string HTML
52 */
53 public static function pre( $text, $attribs, $parser ) {
54 // Backwards-compatibility hack
55 $content = StringUtils::delimiterReplace( '<nowiki>', '</nowiki>', '$1', $text, 'i' );
56
57 $attribs = Sanitizer::validateTagAttributes( $attribs, 'pre' );
58 return Xml::openElement( 'pre', $attribs ) .
59 Xml::escapeTagsOnly( $content ) .
60 '</pre>';
61 }
62
63 /**
64 * Core parser tag hook function for 'html', used only when
65 * $wgRawHtml is enabled.
66 *
67 * This is potentially unsafe and should be used only in very careful
68 * circumstances, as the contents are emitted as raw HTML.
69 *
70 * Uses undocumented extended tag hook return values, introduced in r61913.
71 *
72 * @param string $content
73 * @param array $attributes
74 * @param Parser $parser
75 * @throws MWException
76 * @return array
77 */
78 public static function html( $content, $attributes, $parser ) {
79 global $wgRawHtml;
80 if ( $wgRawHtml ) {
81 return array( $content, 'markerType' => 'nowiki' );
82 } else {
83 throw new MWException( '<html> extension tag encountered unexpectedly' );
84 }
85 }
86
87 /**
88 * Core parser tag hook function for 'nowiki'. Text within this section
89 * gets interpreted as a string of text with HTML-compatible character
90 * references, and wiki markup within it will not be expanded.
91 *
92 * Uses undocumented extended tag hook return values, introduced in r61913.
93 *
94 * @param string $content
95 * @param array $attributes
96 * @param Parser $parser
97 * @return array
98 */
99 public static function nowiki( $content, $attributes, $parser ) {
100 $content = strtr( $content, array( '-{' => '-&#123;', '}-' => '&#125;-' ) );
101 return array( Xml::escapeTagsOnly( $content ), 'markerType' => 'nowiki' );
102 }
103
104 /**
105 * Core parser tag hook function for 'gallery'.
106 *
107 * Renders a thumbnail list of the given images, with optional captions.
108 * Full syntax documented on the wiki:
109 *
110 * https://www.mediawiki.org/wiki/Help:Images#Gallery_syntax
111 *
112 * @todo break Parser::renderImageGallery out here too.
113 *
114 * @param string $content
115 * @param array $attributes
116 * @param Parser $parser
117 * @return string HTML
118 */
119 public static function gallery( $content, $attributes, $parser ) {
120 return $parser->renderImageGallery( $content, $attributes );
121 }
122 }