Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Document, optimise and update #1

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions .idea/php.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

62 changes: 40 additions & 22 deletions PrismHighlight.class.php
Original file line number Diff line number Diff line change
@@ -1,34 +1,53 @@
<?php

class PrismHighlight {

public static function onBeforePageDisplay(OutputPage &$out, Skin &$skin) {
class PrismHighlight
{
/**
* @return true
*/
public static function onBeforePageDisplay(OutputPage &$out, Skin &$skin): bool
{
$out->addModules('ext.PrismHighlight');

return true;
}

public static function onParserFirstCallInit(Parser &$parser) {
/**
* @return true
*/
public static function onParserFirstCallInit(Parser &$parser): bool
{
global $wgHighlightTags;

foreach ($wgHighlightTags as $tag) {
// $parser->setHook( tag, array( class, method ) );
$parser->setHook($tag, array('PrismHighlight', 'renderSyntaxhighlight'));
}
$parser->setHook($tag, ['PrismHighlight', 'renderSyntaxhighlight']);
}

return true;
}
return true;
}

public static function renderSyntaxhighlight($in, $param = array(), $parser = null, $frame = false) {
/**
* @param string $in
* @param array $param
* @param mixed|null $parser
* @param bool $frame
*
* @return string HTML
*/
public static function renderSyntaxhighlight(string $in, array $param = array(), $parser = null, $frame = false): string
{
global $wgLangMapping;

// get the language
//<syntaxhighlight lang="bash">
//</syntaxhighlight>
$lang = isset($param['lang']) ? $param['lang'] : '';
/**
* Get the language
* E.g.: <syntaxhighlight lang="bash"></syntaxhighlight> would return "bash"
* @var string $lang
*/
$lang = $param['lang'] ?? '';

$highlightClass = 'code2highlight';
if ($lang == 'nohighlight')
{
if ($lang === 'nohighlight') {
$highlightClass = 'nohighlight';
$lang = '';
}
Expand All @@ -39,13 +58,14 @@ public static function renderSyntaxhighlight($in, $param = array(), $parser = nu
}

// class
/** @var array<string> $htmlAttribs */
$htmlAttribs['class'] = isset($param['class']) ? $param['class'] . ' ' . $highlightClass : $highlightClass;
if (!empty($lang)) {
$htmlAttribs['class'] .= " lang-$lang";
}

// id
if (isset( $param['id']))
{
if (isset($param['id'])) {
$htmlAttribs['id'] = $param['id'];
}

Expand All @@ -58,12 +78,10 @@ public static function renderSyntaxhighlight($in, $param = array(), $parser = nu
if ($inline) {
$htmlAttribs['style'] = 'display: inline;';
$out = Html::rawElement('code', $htmlAttribs, $code);
return $out;
}
else {
} else {
$out = Html::rawElement('pre', $htmlAttribs, $code);
return $out;
}
}

return $out;
}
}
6 changes: 3 additions & 3 deletions PrismHighlight.php
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
<?php

if ( function_exists( 'wfLoadExtension' ) ) {
wfLoadExtension( 'PrismHighlight' );
if (function_exists('wfLoadExtension')) {
wfLoadExtension('PrismHighlight');
return true;
} else {
die( 'This version of the PrismHighlight extension requires MediaWiki 1.35+' );
die('This version of the PrismHighlight extension requires MediaWiki 1.35+');
}
2 changes: 1 addition & 1 deletion README → README.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,4 +48,4 @@ You may edit the 'extension.js' file to adjust the configuration of the extensio
the "config" area.

* HighlightTags; The tags which will be processed for syntax highlighting.
* LangMapping; Custom mappings of language keys to other language keys.
* LangMapping; Custom mappings of language keys to other language keys.
13 changes: 8 additions & 5 deletions init.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
$(document).ready(function() {
$('pre.code2highlight, code.code2highlight, pre.mw-code').each(function(i, block) {
Prism.highlightElement(block);
});
});
"use strict";
$(document).ready(
function () {
$('pre.code2highlight, code.code2highlight, pre.mw-code').each((i, block) => {
Prism.highlightElement(block);
});
}
);
Loading