Skip to content

Commit

Permalink
Merge pull request #4 from OxCom/optional-format
Browse files Browse the repository at this point in the history
Optional formatting
  • Loading branch information
OxCom authored Aug 22, 2021
2 parents 3bbb4d8 + 98fe3ed commit 06aa601
Show file tree
Hide file tree
Showing 10 changed files with 119 additions and 24 deletions.
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
.idea
vendor/
composer.lock

15 changes: 9 additions & 6 deletions acp/main_module.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,20 +40,23 @@ public function main($id, $mode)

$theme = $request->variable('oxcom_phpbbch_theme', 'none');
$langs = $request->variable('oxcom_phpbbch_langs', ['none']);
$bFExt = $request->variable('oxcom_phpbbch_bh_format_ext', 'N');

$this->config->set('oxcom_phpbbch_theme', $theme);
$this->config->set('oxcom_phpbbch_format_only', $bFExt);
$this->updateLanguages($langs);

trigger_error($this->lang('ACP_PHPBBCH_SETTING_SAVED') . adm_back_link($this->u_action));
}

$template->assign_vars([
'ACP_PHPBBCH_LANGUAGES' => \oxcom\phpbbch\core\settings::getLanguages(),
'ACP_PHPBBCH_THEMES' => \oxcom\phpbbch\core\settings::getThemes(),
'ACP_PHPBBCH_THEME' => $this->config['oxcom_phpbbch_theme'],
'ACP_PHPBBCH_LANGS' => $this->getSelectedLanguages(),
'ACP_PHPBBCH_VERSION' => $this->config['oxcom_phpbbch_version'],
'U_ACTION' => $this->u_action,
'ACP_PHPBBCH_LANGUAGES' => \oxcom\phpbbch\core\settings::getLanguages(),
'ACP_PHPBBCH_THEMES' => \oxcom\phpbbch\core\settings::getThemes(),
'ACP_PHPBBCH_THEME' => $this->config['oxcom_phpbbch_theme'],
'ACP_PHPBBCH_LANGS' => $this->getSelectedLanguages(),
'ACP_PHPBBCH_VERSION' => $this->config['oxcom_phpbbch_version'],
'ACP_PHPBBCH_BEHAVIOUR_EXT_ONLY' => $this->config['oxcom_phpbbch_format_only'],
'U_ACTION' => $this->u_action,
]);
}

Expand Down
22 changes: 17 additions & 5 deletions adm/style/acp_phpbbch_body.html
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,7 @@ <h1>{L_SETTINGS}</h1>
<dl>
<dt>
<label for="oxcom_phpbbch_theme">{L_ACP_PHPBBCH_THEME}</label>
<p>
Please visit this site to get more information about themes
<a href="https://highlightjs.org/" target="_blank" title="HighlightJS">HighlightJS</a>
</p>
<p>{L_ACP_PHPBBCH_THEME_D} <a href="https://highlightjs.org/" target="_blank" title="HighlightJS">HighlightJS</a></p>
</dt>
<dd>
<select class="select" id="oxcom_phpbbch_theme" name="oxcom_phpbbch_theme">
Expand All @@ -23,6 +20,20 @@ <h1>{L_SETTINGS}</h1>
</dd>
</dl>

<dl>
<dt>
<label>{L_ACP_PHPBBCH_BEHAVIOUR}</label>
<p>{L_ACP_PHPBBCH_BEHAVIOUR_D}</p>
</dt>
<dd>
<input type="checkbox" class="checkbox"
id="oxcom_phpbbch_bh_format_ext"
name="oxcom_phpbbch_bh_format_ext"
value="Y"
{% if ACP_PHPBBCH_BEHAVIOUR_EXT_ONLY == 'Y' %}checked="checked"{% endif %}
/>&nbsp;<label for="oxcom_phpbbch_bh_format_ext">{L_ACP_PHPBBCH_BEHAVIOUR_EXT_ONLY}</label>
</dd>
</dl>
<dl>
<dt>
<label>{L_ACP_PHPBBCH_LANGUAGES}</label>
Expand All @@ -36,10 +47,11 @@ <h1>{L_SETTINGS}</h1>
<dd>
{% for key,title in ACP_PHPBBCH_LANGUAGES %}
<input type="checkbox" class="checkbox"
id="oxcom_phpbbch_langs_i_{{ key }}"
name="oxcom_phpbbch_langs[]"
value="{{ key }}"
{% if key in ACP_PHPBBCH_LANGS %} checked="checked" {% endif %}
/>&nbsp;{{ title }}
/>&nbsp;<label for="oxcom_phpbbch_langs_i_{{ key }}">{{ title }}</label>
<br />
{% endfor %}
</dd>
Expand Down
9 changes: 7 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"type": "phpbb-extension",
"description": "This is an extension for PHPBB 3.1 that allows to highlight the code",
"homepage": "https://github.com/oxcom/phpbbch",
"version": "1.1.0",
"version": "1.0.1",
"keywords": ["phpbb", "extension", "highlight"],
"license": "MIT",
"authors": [
Expand All @@ -18,8 +18,13 @@
"php": ">=5.6",
"composer/installers": "~1.4.0"
},
"autoload-dev": {
"psr-4": {
"oxcom\\phpbbch\\": ""
}
},
"require-dev": {
"phpbb/epv": "0.0.*"
"phpunit/phpunit": "^9.5.8"
},
"extra": {
"display-name": "PHPBB Code Highlight",
Expand Down
15 changes: 6 additions & 9 deletions event/main.php
Original file line number Diff line number Diff line change
Expand Up @@ -78,16 +78,17 @@ public function initExtension($event)
$enabled = $this->getSelectedLanguages();

foreach ($available as $key => $name) {
if (in_array($key, $enabled, true)) {
if (\in_array($key, $enabled, true)) {
continue;
}

unset($available[$key]);
}

$this->template->assign_vars([
'PHPBBCH_ALLOWED_LANGS' => array_keys($available),
'PHPBBCH_POST_HELP_LANGS' => $available,
'PHPBBCH_ALLOWED_LANGS' => \array_keys($available),
'PHPBBCH_POST_HELP_LANGS' => $available,
'PHPBBCH_BEHAVIOUR_EXT_ONLY' => $this->config['oxcom_phpbbch_format_only'],
]);
}

Expand All @@ -99,20 +100,16 @@ public function initExtension($event)
*/
protected function getSelectedLanguages()
{
$config = $this->config;

$allowed = \oxcom\phpbbch\core\settings::getLanguages();
$allowed = array_keys($allowed);

// get list of selected languages
$enabled = array_filter($allowed, function ($key) use ($config) {
$key = 'oxcom_phpbbch_lang_' . $key;
return \array_filter($allowed, function ($key) {
$key = 'oxcom_phpbbch_lang_'.$key;
$status = $this->config[$key];

return !empty($status) && $status === main_module::ENUM_ALLOWED_YES;
});

return $enabled;
}

/**
Expand Down
5 changes: 5 additions & 0 deletions language/en/phpbbch.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,11 @@

'ACP_PHPBBCH_LANGUAGES' => 'Languages',
'ACP_PHPBBCH_THEME' => 'Theme',
'ACP_PHPBBCH_THEME_D' => 'Please visit this site to get more information about themes',
'ACP_PHPBBCH_BEHAVIOUR' => 'Behaviour',
'ACP_PHPBBCH_BEHAVIOUR_D' => 'Customize plugin behaviour.',

'ACP_PHPBBCH_BEHAVIOUR_EXT_ONLY' => 'Force formatting only code blocks created by this extension only.',

'PHPBBCH_POST_TAB_TITLE' => 'Syntax Highlight',
'PHPBBCH_POST_TAB_HELP' => 'Please find list of available languages',
Expand Down
41 changes: 41 additions & 0 deletions migrations/format_only_ext.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php

namespace oxcom\phpbbch\migrations;

class format_only_ext extends \phpbb\db\migration\migration
{
/**
* If our config variable already exists in the db
* skip this migration.
*/
public function effectively_installed()
{
return isset($this->config['oxcom_phpbbch_format_only']);
}

/**
* This migration depends on phpBB's v314 migration
* already being installed.
*/
static public function depends_on()
{
return [
'\phpbb\db\migration\data\v31x\v314',
'\oxcom\phpbbch\migrations\add_module',
];
}

public function update_data()
{
return [
['config.add', ['oxcom_phpbbch_format_only', 'N']],
];
}

public function revert_data()
{
return [
['config.remove', ['oxcom_phpbbch_format_only']],
];
}
}
4 changes: 3 additions & 1 deletion styles/all/template/js/init.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@ $(function () {
tabReplace: ' ' // 4 spaces
});

$('code')
var selector = window.oxcom_phpbbch_config.behavior.format_only_ext ? "code[data-bind=\'phpbbch-code\']" : 'code';

$(selector)
.each(function (i, block) {
if ($(block).data('bind') !== 'phpbbch-code') {
var content = $(block).html();
Expand Down
8 changes: 8 additions & 0 deletions styles/all/template/phpbbch_init.html
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
<script type="text/javascript">
window.oxcom_phpbbch_config = {
behavior: {
format_only_ext: {{ PHPBBCH_BEHAVIOUR_EXT_ONLY == 'Y' ? 'true' : 'false' }}
}
};
</script>

{% INCLUDEJS '@oxcom_phpbbch/js/higlightjs/highlight.min.js' %}

{% for lang in PHPBBCH_ALLOWED_LANGS %}
Expand Down
20 changes: 19 additions & 1 deletion tests/FileMapTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,31 @@

namespace oxcom\phpbbch\tests;

class FileMapTest
use oxcom\phpbbch\core\settings;
use PHPUnit\Framework\TestCase;

class FileMapTest extends TestCase
{
const LANG_JS_PATH = './../styles/all/template/js/higlightjs/languages';
const LANG_THEME_PATH = './../styles/all/theme/higlightjs';

public function testTheme()
{
foreach (settings::getLanguages() as $file => $name) {
$path = __DIR__.'/'.self::LANG_JS_PATH.'/'.$file.'.min.js';
$path = \realpath($path);

self::assertTrue(\file_exists($path), "File not found for language $name");
}
}

public function testLanguage()
{
foreach (settings::getThemes() as $file => $name) {
$path = __DIR__.'/'.self::LANG_THEME_PATH.'/'.$file.'.min.css';
$path = \realpath($path);

self::assertTrue(\file_exists($path), "File not found for theme $name");
}
}
}

0 comments on commit 06aa601

Please sign in to comment.