Skip to content

Commit

Permalink
re-implementation of dynamic-resource-invocation (DRI)
Browse files Browse the repository at this point in the history
  • Loading branch information
AndiDittrich committed Aug 2, 2020
1 parent 8a95fd8 commit 748c6bc
Show file tree
Hide file tree
Showing 27 changed files with 415 additions and 143 deletions.
2 changes: 1 addition & 1 deletion .wp-skeleton.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"slug": "enlighter",
"name": "Enlighter",
"namespace": "Enlighter",
"version": "4.3-BETA1",
"version": "4.3.0-BETA1",
"license": "GPL-2.0",
"licenseFile": "LICENSE.txt",
"constants": {},
Expand Down
33 changes: 33 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,43 @@

### 4.3.0 ###

* Added: EnlighterJS `v3.4.0`

* Added: compatibility for `async/defer` script loading (configuration code is merged with the library) (optional)- feature requested [on WordPress.org Forums](https://wordpress.org/support/topic/enlighterjs-resources-not-loaded-yet/)
* Added: javascript based dynamic-resource-invocation to load the EnlighterJS assets dynamically when needed (client side)
* Added: dri compatibility for bbpress and dynamic content (jquery load, jetpack infinity scroll)
* Changed: renamed the "raw code" language label to "Plain text"
* Changed: EnlighterJS library (js) is loaded via cache file `cache/enlighterjs.min.js`
* Changed: js/css cache file generation is bound to `enqueue` hook (performance)
* Changed: `wp-skltn` library updated to **0.24.0**
* Changed: for compatibility reasons, the language-shortcodes are disabled by default (this only affects new installations)
* Changed: full semantic versioning tag is used to display the plugin version (may used by additional tools) - thanks to [roy-bongers on GitHub](https://github.com/EnlighterJS/Plugin.WordPress/issues/262)
* Changed: theme cache is only reloaded on Enlighter settings pages (performance) - thanks to [gdragon on Wordpress forums](https://wordpress.org/support/topic/problem-with-_transient_enlighter_userthemes-option/)
* Changed: EnlighterJS::getConfig has been changed to multidimension object to allow additional payloads (this also affects the initialization code)
* Removed: `EnlighterJS::dequeue` function (deprecated due to new DRI implementation)

* Added: keyword `k11` for annotations
* Added: keyword `x16` for css element selectors
* Added: contextual keywords to csharp - thanks to [mabako on GitHub](https://github.com/EnlighterJS/EnlighterJS/pull/112)
* Added: kotlin string template support
* Added: `r` language support (covered by generic ruleset)
* Added: MikroTik `RouterOS` language support (ros/mikrotik/switchos/routeros/mt)
* Added: pound style comment support to `php`
* Changed: css selector fragment highlighting is limited to the selector itself
* Changed: enhanced css unit parsing
* Changed: `java`, `scala`, `cpp` annotation token changed to `k11`
* Changed: added token `k11` to themes
* Changed: moved generic highlighting rules to `lang/rulesets/generic`
* Changed: `ampersandCleanup` is performed after html escape sequences to eliminate issues related to double unquoting #109
* Changed: toggle raw code label to "Plain text"
* Changed: allowed utf8 characters in `php` variable and function names (side effect of the php parser)
* Bugfix: vhdl single bit highlighting collided with attribute syntax - thanks to [tyriun on GitHub](https://github.com/EnlighterJS/EnlighterJS/issues/106)
* Bugfix: css classnames/ids with hyphens were not recognized
* Bugfix: XML mixins of single/double quotes in attributes failed #108
* Bugfix: dot char within XML attribute names not recognized
* Bugfix: kotlin raw string where not correctly parsed (wrong rule priority)
* Bugfix: highlighting color of `classic` theme not applied due to invalid selectors - thanks to [woolseyj on GitHub](https://github.com/EnlighterJS/EnlighterJS/issues/117)


### 4.2.0 ###

Expand Down
5 changes: 3 additions & 2 deletions Enlighter.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
Plugin Name: Enlighter - Customizable Syntax Highlighter
Plugin URI: https://enlighterjs.org
Description: all-in-one syntax highlighting solution
Version: 4.3-BETA1
Version: 4.3.0-BETA1
Author: Andi Dittrich
Author URI: https://andidittrich.com
License: GPL-2.0
Expand All @@ -16,7 +16,7 @@
// Plugin Bootstrap Operation
// AUTO GENERATED CODE - DO NOT EDIT !!!
define('ENLIGHTER_INIT', true);
define('ENLIGHTER_VERSION', '4.3-BETA1');
define('ENLIGHTER_VERSION', '4.3.0-BETA1');
define('ENLIGHTER_WPSKLTN_VERSION', '0.24.0');
define('ENLIGHTER_PHP_VERSION', '5.6');
define('ENLIGHTER_PLUGIN_TITLE', 'Enlighter - Customizable Syntax Highlighter');
Expand Down Expand Up @@ -55,6 +55,7 @@ function Enlighter_PhpEnvironmentError(){
require_once(ENLIGHTER_PLUGIN_PATH.'/modules/compatibility/Crayon.php');
require_once(ENLIGHTER_PLUGIN_PATH.'/modules/compatibility/GenericType1.php');
require_once(ENLIGHTER_PLUGIN_PATH.'/modules/compatibility/GenericType2.php');
require_once(ENLIGHTER_PLUGIN_PATH.'/modules/core/DynamicResourceInvocation.php');
require_once(ENLIGHTER_PLUGIN_PATH.'/modules/core/Enlighter.php');
require_once(ENLIGHTER_PLUGIN_PATH.'/modules/core/EnlighterJS.php');
require_once(ENLIGHTER_PLUGIN_PATH.'/modules/core/EnvironmentCheck.php');
Expand Down
64 changes: 64 additions & 0 deletions modules/core/DynamicResourceInvocation.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
<?php

namespace Enlighter;

use \Enlighter\skltn\ResourceManager as ResourceManager;
use Enlighter\skltn\CacheManager;
use Enlighter\EnlighterJS;

class DynamicResourceInvocation{

// stores the plugin config
private $_config;

// store enlighterjs object
private $_enlighterjs;

public function __construct($config, $enlighterjs){
// store local plugin config
$this->_config = $config;

// store enlighterjs instance
$this->_enlighterjs = $enlighterjs;
}

// generate EnlighterJS initilization code including wrapper
public function getInitializationCode(){

// retrieve config object
$enlighterjsConfig = $this->_enlighterjs->getConfig();

// resources to load
$resources = array();

// load EnlighterJS themes ?
if ($this->_config['enlighterjs-assets-themes']){
// cache file exists ?
$this->_enlighterjs->cacheCheckCSS();

// include local css file - use cache hash!
$resources[] = ResourceManager::getResourceUrl('cache/' . EnlighterJS::CSS_FILENAME, CacheManager::getCacheHash());
}

// load EnlighterJS library ?
if ($this->_config['enlighterjs-assets-js']){

// include plain js file
$resources[] = ENLIGHTER_PLUGIN_URL . '/resources/enlighterjs/enlighterjs.min.js';
}

// add resource links
$enlighterjsConfig['resources'] = $resources;

// @see resources/init/dri.init.js
$wrapper = '!function(e,n){var r='. json_encode($enlighterjsConfig);
$wrapper .= ',o=document.getElementsByTagName("head")[0],t=n&&(n.error||n.log)||function(){};e.EnlighterJSINIT=function(){!function(e,n){var r=0,l=null;function c(o){l=o,++r==e.length&&(!0,n(l))}e.forEach(function(e){switch(e.match(/\.([a-z]+)(?:[#?].*)?$/)[1]){case"js":var n=document.createElement("script");n.onload=function(){c(null)},n.onerror=c,n.src=e,n.async=!0,o.appendChild(n);break;case"css":var r=document.createElement("link");r.onload=function(){c(null)},r.onerror=c,r.rel="stylesheet",r.type="text/css",r.href=e,r.media="all",o.appendChild(r);break;default:t("Error: invalid file extension",e)}})}(r.resources,function(e){e?t("Error: failed to dynamically load EnlighterJS resources!",e):"undefined"!=typeof EnlighterJS?EnlighterJS.init(r.selectors.block,r.selectors.inline,r.options):t("Error: EnlighterJS resources not loaded yet!")})},(document.querySelector(r.selectors.block)||document.querySelector(r.selectors.inline))&&e.EnlighterJSINIT()}(window,console);';

return $wrapper;
}

// enqueue resources
public function enqueue(){
ResourceManager::enqueueDynamicScript($this->getInitializationCode());
}
}
77 changes: 42 additions & 35 deletions modules/core/EnlighterJS.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,32 +38,35 @@ public function __construct($config, $cacheManager, $themeCustomizer){
// generate the EnlighterJS related config object
public function getConfig(){
return array(
'indent' => $this->_config['enlighterjs-indent'],
'ampersandCleanup' => $this->_config['enlighterjs-ampersandcleanup'],
'linehover' => $this->_config['enlighterjs-linehover'],
'rawcodeDbclick' => $this->_config['enlighterjs-rawcodedbclick'],
'textOverflow' => $this->_config['enlighterjs-textoverflow'],
'linenumbers' => $this->_config['enlighterjs-linenumbers'],
'theme' => $this->_config['enlighterjs-theme'],
'language' => $this->_config['enlighterjs-language'],
'retainCssClasses' => $this->_config['enlighterjs-retaincss'],
'collapse' => false,
'toolbarOuter' => '',
'toolbarTop' => '{BTN_RAW}{BTN_COPY}{BTN_WINDOW}{BTN_WEBSITE}',
'toolbarBottom' => ''
'selectors' => array(
'block' => $this->_config['enlighterjs-selector-block'],
'inline' => $this->_config['enlighterjs-selector-inline']
),
'options' => array(
'indent' => $this->_config['enlighterjs-indent'],
'ampersandCleanup' => $this->_config['enlighterjs-ampersandcleanup'],
'linehover' => $this->_config['enlighterjs-linehover'],
'rawcodeDbclick' => $this->_config['enlighterjs-rawcodedbclick'],
'textOverflow' => $this->_config['enlighterjs-textoverflow'],
'linenumbers' => $this->_config['enlighterjs-linenumbers'],
'theme' => $this->_config['enlighterjs-theme'],
'language' => $this->_config['enlighterjs-language'],
'retainCssClasses' => $this->_config['enlighterjs-retaincss'],
'collapse' => false,
'toolbarOuter' => '',
'toolbarTop' => '{BTN_RAW}{BTN_COPY}{BTN_WINDOW}{BTN_WEBSITE}',
'toolbarBottom' => ''
)
);
}

// generate EnlighterJS initilization code including wrapper
public function getInitializationCode(){
// @see resources/init/enlighterjs.init.js
$wrapper = '!function(n,o){"undefined"!=typeof EnlighterJS?(n.EnlighterJSINIT=function(){';
$wrapper .= 'EnlighterJS.init('.
'"' . esc_attr($this->_config['enlighterjs-selector-block']) . '", ' .
'"' . esc_attr($this->_config['enlighterjs-selector-inline']) . '", ' .
json_encode($this->getConfig()) .
')';
$wrapper .= '})():(o&&(o.error||o.log)||function(){})("Error: EnlighterJS resources not loaded yet!")}(window,console);';
$wrapper = '!function(e,n){if("undefined"!=typeof EnlighterJS){';
$wrapper.= 'var o=' . json_encode($this->getConfig()) . ';';
$wrapper .= '(e.EnlighterJSINIT=function(){EnlighterJS.init(o.selectors.block,o.selectors.inline,o.options)})()}else{(n&&(n.error||n.log)||function(){})("Error: EnlighterJS resources not loaded yet!")}}(window,console);';

return $wrapper;
}

Expand All @@ -72,21 +75,17 @@ public function enqueue(){

// load EnlighterJS themes ?
if ($this->_config['enlighterjs-assets-themes']){
// cache file exists ?
if (!$this->_cacheManager->fileExists(self::CSS_FILENAME)){
$this->_themeCustomizer->generateCSS();
}
// cache file exists ? if not regenerate
$this->cacheCheckCSS();

// include local css file - use cache hash!
ResourceManager::enqueueStyle('enlighterjs', 'cache/' . self::CSS_FILENAME, array(), CacheManager::getCacheHash());
}

// load EnlighterJS library ?
if ($this->_config['enlighterjs-assets-js']){
// cache file exists ?
if (!$this->_cacheManager->fileExists(self::JS_FILENAME)){
$this->generateJS();
}
// cache file exists ? if not regenerate
$this->cacheCheckJS();

// include merged js file - use cache hash!
ResourceManager::enqueueScript('enlighterjs', 'cache/' . self::JS_FILENAME, array(), CacheManager::getCacheHash());
Expand All @@ -98,14 +97,12 @@ public function enqueue(){
}
}

// dequeue resources
public function dequeue(){
wp_dequeue_script('enlighterjs');
wp_dequeue_style('enlighterjs');
}

// generate js file
public function generateJS(){
public function cacheCheckJS(){
// file exists ? skip file generation
if ($this->_cacheManager->fileExists(self::JS_FILENAME)){
return;
}

// initialize js generator
$jsGenerator = new JsBuilder();
Expand All @@ -122,4 +119,14 @@ public function generateJS(){
// generate + store file
$this->_cacheManager->writeFile(self::JS_FILENAME, $jsGenerator->render());
}

public function cacheCheckCSS(){
// cache file exists ?
if ($this->_cacheManager->fileExists(self::CSS_FILENAME)){
return;
}

// generate css file
$this->_themeCustomizer->generateCSS();
}
}
4 changes: 3 additions & 1 deletion modules/core/LanguageManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ class LanguageManager{
// list of build-in languages
const LANGUAGES = array(
'generic' => 'Generic Highlighting',
'raw' => 'Plain text',
'abap' => 'ABAP',
'asm' => 'Generic Assembly',
'apache' => 'Apache httpd',
Expand Down Expand Up @@ -43,7 +44,8 @@ class LanguageManager{
'python' => 'Python',
'purebasic' => 'Purebasic',
'qml' => 'QML',
'raw' => 'RAW Code',
'r' => 'R',
'routeros' => 'RouterOS',
'ruby' => 'Ruby',
'rust' => 'Rust',
'scala' => 'SCALA',
Expand Down
52 changes: 31 additions & 21 deletions modules/core/ResourceLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use \Enlighter\editor\QuickTags as QuickTagsEditor;
use \Enlighter\extensions\Jetpack as JetpackExtension;
use \Enlighter\extensions\JQuery as JQueryExtension;
use \Enlighter\DynamicResourceInvocation;

class ResourceLoader{

Expand Down Expand Up @@ -143,34 +144,43 @@ public function backendSettingsCustomizer(){
// initialize the frontend
public function frontendEnlighter($contentProcessor){

// load enlighterjs resources
add_action('wp_enqueue_scripts', array($this->_enlighterjs, 'enqueue'), 50);
// dependency for extensions
$extensionDependency = 'enlighterjs';

// dynamic resource incovation active ?
if ($this->_config['dynamic-resource-invocation']){

// initialize DRI
$dri = new DynamicResourceInvocation($this->_config, $this->_enlighterjs);

// load enlighterjs resources loader
add_action('wp_enqueue_scripts', array($dri, 'enqueue'), 50);

// disable dependencies
$extensionDependency = null;

// standard wordpress assets loading via enqueue
}else{
// load enlighterjs resources
add_action('wp_enqueue_scripts', array($this->_enlighterjs, 'enqueue'), 50);
}

// load user themes ?
if ($this->_config['enlighterjs-assets-themes-external']){
add_action('wp_enqueue_scripts', array($this->_themeManager, 'enqueue'));
}

// load infinite scroll extension ?
if ($this->_config['ext-infinite-scroll']){
ResourceManager::enqueueDynamicScript(JetpackExtension::getInfiniteScrollCode(), 'enlighterjs');
}
add_action('wp_enqueue_scripts', function() use ($extensionDependency){
// load infinite scroll extension ?
if ($this->_config['ext-infinite-scroll']){
ResourceManager::enqueueDynamicScript(JetpackExtension::getInfiniteScrollCode(), $extensionDependency);
}

// load infinite scroll extension ?
if ($this->_config['ext-ajaxcomplete']){
ResourceManager::enqueueDynamicScript(JQueryExtension::getAjaxcompleteCode(), 'enlighterjs');
}
// load infinite scroll extension ?
if ($this->_config['ext-ajaxcomplete']){
ResourceManager::enqueueDynamicScript(JQueryExtension::getAjaxcompleteCode(), $extensionDependency);
}
}, 51);

// frontend resource optimization ?
if ($this->_config['dynamic-resource-invocation']){
// deregister footer scripts
add_action('wp_footer', function() use ($contentProcessor){
// enlighter codeblocks active within current page ?
if (!$contentProcessor->hasContent()){
// dequeue scripts
$this->_enlighterjs->dequeue();
}
}, 1);
}
}
}
2 changes: 1 addition & 1 deletion modules/extensions/JQuery.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,6 @@ class JQuery{

// @see resources/extensions/jquery-ajaxcomplete.js
public static function getAjaxcompleteCode(){
return '!function(e){"undefined"!=typeof EnlighterJSINIT&&"undefined"!=typeof jQuery&&jQuery(document).on("ajaxComplete",function(){e.setTimeout(function(){EnlighterJSINIT.apply(e)},180)})}(window);';
return '!function(e){"undefined"!=typeof jQuery&&jQuery(document).on("ajaxComplete",function(){"undefined"!=typeof EnlighterJSINIT&&e.setTimeout(function(){EnlighterJSINIT.apply(e)},180)})}(window);';
}
}
2 changes: 1 addition & 1 deletion modules/extensions/Jetpack.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,6 @@ class Jetpack{

// @see resources/extensions/jetpack-infinite-scroll.js
public static function getInfiniteScrollCode(){
return '!function(n){"undefined"!=typeof EnlighterJSINIT&&"undefined"!=typeof jQuery&&jQuery(document.body).on("post-load",function(){n.setTimeout(function(){EnlighterJSINIT.apply(n)},180)})}(window);';
return '!function(n){"undefined"!=typeof jQuery&&jQuery(document.body).on("post-load",function(){"undefined"!=typeof EnlighterJSINIT&&n.setTimeout(function(){EnlighterJSINIT.apply(n)},180)})}(window);';
}
}
Loading

0 comments on commit 748c6bc

Please sign in to comment.