Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
sciwhiz12 committed Apr 8, 2021
0 parents commit 45a7ac7
Show file tree
Hide file tree
Showing 9 changed files with 1,187 additions and 0 deletions.
661 changes: 661 additions & 0 deletions LICENSE

Large diffs are not rendered by default.

69 changes: 69 additions & 0 deletions PrismHighlight.class.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
<?php

class PrismHighlight {

public static function onBeforePageDisplay(OutputPage &$out, Skin &$skin) {
$out->addModules('ext.PrismHighlight');
return true;
}

public static function onParserFirstCallInit(Parser &$parser) {
global $wgHighlightTags;

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

return true;
}

public static function renderSyntaxhighlight($in, $param = array(), $parser = null, $frame = false) {
global $wgLangMapping;

// get the language
//<syntaxhighlight lang="bash">
//</syntaxhighlight>
$lang = isset($param['lang']) ? $param['lang'] : '';

$highlightClass = 'code2highlight';
if ($lang == 'nohighlight')
{
$highlightClass = 'nohighlight';
$lang = '';
}

// map lang if necessary
if (array_key_exists($lang, $wgLangMapping)) {
$lang = $wgLangMapping[$lang];
}

// class
$htmlAttribs['class'] = isset($param['class']) ? $param['class'] . ' ' . $highlightClass : $highlightClass;
if (!empty($lang)) {
$htmlAttribs['class'] .= " lang-$lang";
}
// id
if (isset( $param['id']))
{
$htmlAttribs['id'] = $param['id'];
}

$code = htmlspecialchars(trim($in));

// inline ?
//<syntaxhighlight lang="bash" inline></syntaxhighlight>
$inline = isset($param['inline']);

if ($inline) {
$htmlAttribs['style'] = 'display: inline;';
$out = Html::rawElement('code', $htmlAttribs, $code);
return $out;
}
else {
$out = Html::rawElement('pre', $htmlAttribs, $code);
return $out;
}
}

}
8 changes: 8 additions & 0 deletions PrismHighlight.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php

if ( function_exists( 'wfLoadExtension' ) ) {
wfLoadExtension( 'PrismHighlight' );
return true;
} else {
die( 'This version of the PrismHighlight extension requires MediaWiki 1.35+' );
}
50 changes: 50 additions & 0 deletions README
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
== Prism Integration ==

This extension allows source code to be syntax highlighted on wiki pages.

This works as a drop-in replacement for the Highlightjs Integration by Nikus Pokus.

== Requirements ==

This version of the extension has been tested with Prism 1.23.0 and MediaWiki
1.35.1.

== Installation ==

Add this line to your LocalSettings.php:

wfLoadExtension( 'PrismHighlight' );

By default, this extension will use a bundled copy of Prism 1.23.0 with the
following customizations:

* Plugins: Line Highlight, Line Numbers, Match braces, Diff Highlight
* Languages: Markup + HTML + XML + SVG + MathML + SSML + Atom + RSS, CSS,
C-like, JavaScript, ABNF, ANTLR4, Bash + Shell, Batch, BNF + RBNF, C, Clojure,
CSS Extras, CSV, Diff, EBNF, Git, GLSL, Groovy, .ignore + .gitignore + .hgignore
+ .npmignore, Ini, Java, JavaDoc, JavaDoc-like, Java stack trace, JSON + Web
App Manifest, JSON5, Kotlin + Kotlin Script, Less, Lisp, Lua, Markdown, Markup
templating, PHP, PHPDoc, PHP Extras, PowerShell, .properties, Python, Sass (Sass),
Sass (Scss), Scala, Shell session, SQL, TOML, YAML
* Theme: Tomorrow Night

If you wish to use a different copy of the library, download it from the PrismJS
website (https://prismjs.com) and replace the files in the 'prism' folder.

== Usage ==

You can use "syntaxhighlight" or "source" elements for code blocks.

The following attributes can be used:
* lang; Defines the language
* inline; Formats the source code to be inline, as part of a paragraph.
* class; Defines extra HTML classes to be added.
* id; Defines the id attribute of the resulting HTML output.

== Manual Configuration ==

You may edit the 'extension.js' file to adjust the configuration of the extension, in
the "config" area.

* HighlightTags; The tags which will be processed for syntax highlighting.
* LangMapping; Custom mappings of language keys to other language keys.
13 changes: 13 additions & 0 deletions custom.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
/* no .hljs because it has to work even if the lang doesn't exist */
pre.code2highlight,
pre.nohighlight {
/* override: pre, .mw-code { padding: 1em; */
/* same as: .hljs { padding: 0.5em; */
padding: 0.5em;
}

code.code2highlight.hljs {
/* override: .hljs { padding: 0.5em; */
/* same as: code { padding: 1px 4px; */
padding: 1px 4px;
}
52 changes: 52 additions & 0 deletions extension.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
{
"name": "Prism Highlight",
"version": "1.0",
"author": "[https://www.mediawiki.org/wiki/User:SciWhiz12 SciWhiz12], forgecommunitywiki",
"url": "https://github.com/forgecommunitywiki/PrismHighlight",
"license-name": "AGPL-3.0",
"description": "Allows to use the client-side syntax highlighter [https://prismjs.com Prism] in MediaWiki; derived from the [https://www.mediawiki.org/wiki/Extension:Highlightjs_Integration highlight.js Integration extension by Nikus Pokus]",
"type": "other",
"require": {
"MediaWiki": ">= 1.35"
},
"Hooks": {
"ParserFirstCallInit": [
"PrismHighlight::onParserFirstCallInit"
],
"BeforePageDisplay": [
"PrismHighlight::onBeforePageDisplay"
]
},
"AutoloadClasses": {
"PrismHighlight": "PrismHighlight.class.php"
},
"ResourceModules": {
"ext.PrismHighlight": {
"scripts": [
"prism/prism.js",
"init.js"
],
"styles": [
"prism/prism.css",
"custom.css"
],
"targets": [ "desktop", "mobile" ]
}
},
"ResourceFileModulePaths": {
"localBasePath": ""
},
"config": {
"HighlightTags": [
"syntaxhighlight",
"source"
],
"LangMapping": {
"tsql": "sql",
"mysql": "sql",
"xaml": "xml",
"mediawiki": "markdown"
}
},
"manifest_version": 1
}
5 changes: 5 additions & 0 deletions init.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
$(document).ready(function() {
$('pre.code2highlight, code.code2highlight, pre.mw-code').each(function(i, block) {
Prism.highlightElement(block);
});
});
Loading

0 comments on commit 45a7ac7

Please sign in to comment.