2023-06-20 23:18:53 +02:00
|
|
|
<?php
|
|
|
|
/*
|
2023-06-21 00:21:46 +02:00
|
|
|
Plugin Name: SyntaxHighlighter-NG
|
2023-06-20 23:18:53 +02:00
|
|
|
Version: 1.0
|
2023-06-21 00:21:46 +02:00
|
|
|
Plugin URI: https://git.la10cy.net/DeltaLima/flatpress-plugin-syntaxhighlighter-ng
|
|
|
|
Description: <a href="https://git.la10cy.net/DeltaLima/flatpress-plugin-syntaxhighlighter-ng/">SyntaxHighlighter-NG 1.0.0</a> (forked from <a href="https://forum.flatpress.org/viewtopic.php?p=1130&hilit=syntax+highlight#p1135">Arvid's forum post</a>, using now <a href="https://prismjs.com">prism.js</a>)
|
|
|
|
Author: 2005 NoWhereMan, 2023 DeltaLima
|
|
|
|
Author URI: https://deltalima.org
|
2023-06-20 23:18:53 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
function plugin_syntaxhighlighter_add($lang=null) {
|
|
|
|
static $languages = array();
|
|
|
|
|
|
|
|
$pdir=plugin_geturl('syntaxhighlighter');
|
|
|
|
|
2023-06-21 00:21:46 +02:00
|
|
|
// create array containing the used languages
|
2023-06-20 23:18:53 +02:00
|
|
|
$languages[] = "{$lang}";
|
2023-06-21 00:21:46 +02:00
|
|
|
// remove unique
|
2023-06-20 23:18:53 +02:00
|
|
|
$languages = array_unique($languages);
|
|
|
|
|
|
|
|
return $languages;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
function plugin_syntaxhighlighter_head() {
|
|
|
|
$pdir=plugin_geturl('syntaxhighlighter');
|
2023-06-21 00:21:46 +02:00
|
|
|
echo <<<PRISMJS
|
|
|
|
<!-- start of prism.js -->
|
2023-06-20 23:18:53 +02:00
|
|
|
|
2023-06-21 00:49:05 +02:00
|
|
|
<link rel="stylesheet" type="text/css" href="{$pdir}res/prism.okaidia.css" />
|
2023-06-20 23:18:53 +02:00
|
|
|
|
2023-06-21 00:21:46 +02:00
|
|
|
<!-- end of prism.js -->
|
|
|
|
PRISMJS;
|
2023-06-20 23:18:53 +02:00
|
|
|
|
|
|
|
}
|
|
|
|
add_action('wp_head', 'plugin_syntaxhighlighter_head');
|
|
|
|
|
|
|
|
|
|
|
|
function plugin_syntaxhighlighter_foot() {
|
|
|
|
|
2023-06-21 00:21:46 +02:00
|
|
|
// convert the returned array into an json one, to have an easier time
|
|
|
|
// giving it to the javascript below
|
2023-06-20 23:18:53 +02:00
|
|
|
$used_languages = json_encode(plugin_syntaxhighlighter_add());
|
|
|
|
|
2023-06-21 00:21:46 +02:00
|
|
|
$pdir=plugin_geturl('syntaxhighlighter');
|
|
|
|
// javascript part
|
|
|
|
echo <<<PRISMBOX
|
|
|
|
<!-- start of prism.js -->
|
2023-06-20 23:18:53 +02:00
|
|
|
|
2023-06-21 00:49:05 +02:00
|
|
|
<script type="text/javascript" src="{$pdir}res/prism.full.js"></script>
|
2023-06-21 00:21:46 +02:00
|
|
|
|
|
|
|
<!-- wrapping the content of pre html-tags into code-tags, as said in https://prismjs.com/index.html#basic-usage -->
|
2023-06-20 23:18:53 +02:00
|
|
|
<script type="text/javascript">
|
|
|
|
// wrap the content of <pre> elements into <code></code> for prismjs
|
|
|
|
|
2023-06-21 00:21:46 +02:00
|
|
|
// get an array of <pre></pre> elements
|
2023-06-20 23:47:09 +02:00
|
|
|
//var preEl = document.getElementsByTagName("pre");
|
2023-06-20 23:18:53 +02:00
|
|
|
|
|
|
|
// split used_languages list into array
|
|
|
|
let used_languages = $used_languages;
|
|
|
|
|
2023-06-21 00:49:05 +02:00
|
|
|
// iterate through all used_languages
|
2023-06-20 23:18:53 +02:00
|
|
|
for (let iUl = 0;iUl < used_languages.length; iUl++)
|
|
|
|
{
|
2023-06-21 00:49:05 +02:00
|
|
|
// handle [code] without language definition as "none"
|
|
|
|
// and we have to look for them up a bit different
|
|
|
|
if ( used_languages[iUl] == "" )
|
2023-06-20 23:18:53 +02:00
|
|
|
{
|
2023-06-21 00:49:05 +02:00
|
|
|
|
|
|
|
used_languages[iUl] == "none"
|
|
|
|
var preElements = document.querySelectorAll("pre:not([class])");
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
var preElements = document.querySelectorAll("pre." + used_languages[iUl]);
|
|
|
|
|
|
|
|
}
|
2023-06-20 23:47:09 +02:00
|
|
|
|
2023-06-21 00:49:05 +02:00
|
|
|
// iterate through all <pre>
|
2023-06-20 23:47:09 +02:00
|
|
|
for (let iEl = 0;iEl < preElements.length; iEl++)
|
|
|
|
{
|
|
|
|
org_html = preElements[iEl].innerHTML;
|
|
|
|
new_html = "<code class=\"language-" + used_languages[iUl] + "\">" + org_html + "</code>";
|
2023-06-21 00:21:46 +02:00
|
|
|
|
2023-06-20 23:47:09 +02:00
|
|
|
preElements[iEl].innerHTML = new_html;
|
|
|
|
}
|
2023-06-20 23:18:53 +02:00
|
|
|
|
2023-06-21 00:49:05 +02:00
|
|
|
}
|
2023-06-20 23:18:53 +02:00
|
|
|
</script>
|
|
|
|
|
2023-06-21 00:49:05 +02:00
|
|
|
<!-- end of prism.js -->
|
2023-06-21 00:21:46 +02:00
|
|
|
PRISMBOX;
|
2023-06-20 23:18:53 +02:00
|
|
|
}
|
|
|
|
add_action('wp_footer', 'plugin_syntaxhighlighter_foot');
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
?>
|