Skip to content

Commit

Permalink
Merge pull request #26 from the-turk/analysis-BMkwKg
Browse files Browse the repository at this point in the history
Apply fixes from StyleCI
  • Loading branch information
the-turk committed Jul 16, 2021
2 parents f2e234c + 3f23200 commit 13a2538
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 78 deletions.
12 changes: 6 additions & 6 deletions extend.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,24 +9,24 @@

namespace TheTurk\MathRen;

use Flarum\Extend;
use Flarum\Api\Serializer\ForumSerializer;
use Flarum\Extend;

return [
(new Extend\Frontend('forum'))
->css(__DIR__ . '/less/forum.less')
->css(__DIR__.'/less/forum.less')
->js(__DIR__.'/js/dist/forum.js'),

(new Extend\Frontend('admin'))
->css(__DIR__ . '/less/admin.less')
->js(__DIR__ . '/js/dist/admin.js'),
->css(__DIR__.'/less/admin.less')
->js(__DIR__.'/js/dist/admin.js'),

(new Extend\Locales(__DIR__ . '/locale')),
(new Extend\Locales(__DIR__.'/locale')),

(new Extend\ApiSerializer(ForumSerializer::class))
->attributes(LoadSettings::class),

(new Extend\Formatter)
(new Extend\Formatter())
->configure(ConfigureTextFormatter::class),

// Provides `@config-copy-tex` less variable
Expand Down
32 changes: 16 additions & 16 deletions migrations/2021_07_15_000000_set_default_settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,21 +11,21 @@

return Migration::addSettings(
[
'the-turk-mathren.alias_block_delimiters' => '$$%e%$$,₺₺%e%₺₺',
'the-turk-mathren.alias_block_delimiters' => '$$%e%$$,₺₺%e%₺₺',
'the-turk-mathren.alias_inline_delimiters' => '\\(%e%\\)',
'the-turk-mathren.block_delimiters' => '[math]%e%[/math]',
'the-turk-mathren.color_is_text_color' => '0',
'the-turk-mathren.enable_copy_tex' => '1',
'the-turk-mathren.enable_fleqn' => '0',
'the-turk-mathren.enable_leqno' => '0',
'the-turk-mathren.enable_editor_buttons' => '1',
'the-turk-mathren.throw_on_error' => '0',
'the-turk-mathren.error_color' => '#cc0000',
'the-turk-mathren.inline_delimiters' => '[imath]%e%[/imath]',
'the-turk-mathren.max_expand' => '1000',
'the-turk-mathren.max_size' => '10',
'the-turk-mathren.min_rule_thickness' => '0.05',
'the-turk-mathren.output_mode' => 'htmlAndMathml',
'the-turk-mathren.aliases_as_primary' => '1',
'the-turk-mathren.block_delimiters' => '[math]%e%[/math]',
'the-turk-mathren.color_is_text_color' => '0',
'the-turk-mathren.enable_copy_tex' => '1',
'the-turk-mathren.enable_fleqn' => '0',
'the-turk-mathren.enable_leqno' => '0',
'the-turk-mathren.enable_editor_buttons' => '1',
'the-turk-mathren.throw_on_error' => '0',
'the-turk-mathren.error_color' => '#cc0000',
'the-turk-mathren.inline_delimiters' => '[imath]%e%[/imath]',
'the-turk-mathren.max_expand' => '1000',
'the-turk-mathren.max_size' => '10',
'the-turk-mathren.min_rule_thickness' => '0.05',
'the-turk-mathren.output_mode' => 'htmlAndMathml',
'the-turk-mathren.aliases_as_primary' => '1',
]
);
);
9 changes: 5 additions & 4 deletions src/ConfigureTextFormatter.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,14 +32,15 @@ public function __construct(Util $util)
}

/**
* Configure s9e/TextFormatter
* Configure s9e/TextFormatter.
*
* @param Configurator $config TextFormatter configurator.
*
* @see https://s9etextformatter.readthedocs.io/Plugins/BBCodes/Add_custom_BBCodes/
* @see https://s9etextformatter.readthedocs.io/Plugins/BBCodes/Use_template_parameters/
* @see https://s9etextformatter.readthedocs.io/Rules/Tag_rules/
* @see https://github.com/s9e/TextFormatter/blob/master/docs/JavaScript/Live_preview_attributes.md
*
* @return void
*/
public function __invoke(Configurator $config)
Expand All @@ -59,14 +60,14 @@ public function __invoke(Configurator $config)

// get the class name that the expression will be wrapped with
$className = $delimiter['display'] === true ? 'block' : 'inline';

// will be passed into KaTeX options
$displayMode = $delimiter['display'] === true;

// generate KaTeX options
$options
= \json_encode(Arr::add($katexOptions, 'displayMode', $displayMode));

// add custom BBCode
$config->BBCodes->addCustom(
$delimiter['left'].'{TEXT}'.$delimiter['right'],
Expand Down
67 changes: 32 additions & 35 deletions src/Helpers/Util.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,13 @@ public function __construct(SettingsRepositoryInterface $settings)
{
$this->settings = $settings;
}

/**
* This method will make prefixed calls easier.
*
* @param string $key The setting key that will be fetched from the db.
* @param string $default The default value if there is no setting record.
*
*
* @return string
*/
public function get(string $key, string $default = ''): string
Expand All @@ -51,69 +51,66 @@ public function get(string $key, string $default = ''): string
* `displayMode` will be set on `ConfigureTextFormatter` class.
*
* @see https://katex.org/docs/autorender.html
*
* @return array
*/
public function getKatexOptions(): array
{
return [
'fleqn' => \boolval($this->get('enable_fleqn')),
'leqno' => \boolval($this->get('enable_leqno')),
'output' => $this->get('output_mode'),
'throwOnError' => \boolval($this->get('throw_on_error')),
'errorColor' => $this->get('error_color'),
'fleqn' => \boolval($this->get('enable_fleqn')),
'leqno' => \boolval($this->get('enable_leqno')),
'output' => $this->get('output_mode'),
'throwOnError' => \boolval($this->get('throw_on_error')),
'errorColor' => $this->get('error_color'),
'minRuleThickness' => \floatval($this->get('min_rule_thickness')),
'maxSize' => \floatval($this->get('max_size')),
'maxExpand' => \intval($this->get('max_expand')),
'macros' => \json_decode('{'.$this->get('macros').'}'),
'colorIsTextColor' => \boolval($this->get('color_is_text_color'))
'maxSize' => \floatval($this->get('max_size')),
'maxExpand' => \intval($this->get('max_expand')),
'macros' => \json_decode('{'.$this->get('macros').'}'),
'colorIsTextColor' => \boolval($this->get('color_is_text_color')),
];
}

/**
* Create a classes array that will be used to wrap expressions.
*
*
* @return array
*/
public function getClasses(): array
{
return [
// class for block expressions [displayMode: true]
'block' => 'mathren-block',

// class for inline expressions [displayMode: false]
'inline' => 'mathren-inline'
'inline' => 'mathren-inline',
];
}

/**
* Create a delimiters array that categorized by their type.
*
*
* @return array
*/
private function _getDelimitersByType(): array
{
return [
'block' =>
$this->_commaToArray($this->get('block_delimiters')),
'inline' =>
$this->_commaToArray($this->get('inline_delimiters')),
'aliasBlock' =>
$this->_commaToArray($this->get('alias_block_delimiters')),
'aliasInline' =>
$this->_commaToArray($this->get('alias_inline_delimiters'))
'block' => $this->_commaToArray($this->get('block_delimiters')),
'inline' => $this->_commaToArray($this->get('inline_delimiters')),
'aliasBlock' => $this->_commaToArray($this->get('alias_block_delimiters')),
'aliasInline' => $this->_commaToArray($this->get('alias_inline_delimiters')),
];
}

/**
* This function creates an array of delimiters to be used in various places.
* Its elements are also an array and looks like this:
* [[left] => '[math]', [right] => '[/math]', [display] => true]
* ^ they're created by `$this->_setOptions();`
* ^ they're created by `$this->_setOptions();`.
*
* @param string $type The delimiter type that we're creating an array for.
* Accepted types are `bbcode` and `alias`. If not set,
* returning array will include all delimiters.
*
*
* @return array
*/
public function getDelimitersWithOptions(string $type): array
Expand All @@ -140,7 +137,7 @@ public function getDelimitersWithOptions(string $type): array
}

$displayMode = in_array($key, ['block', 'aliasBlock']) ? true : false;

foreach ($delimiterArray as $delimiter) {
$delimitersWithOptions = array_merge(
$delimitersWithOptions,
Expand All @@ -156,9 +153,9 @@ public function getDelimitersWithOptions(string $type): array
array_push(
$delimitersWithOptions,
[
'left' => '\\\begin{'.$environment.'}',
'right' => '\\\end{'.$environment.'}',
'display' => true
'left' => '\\\begin{'.$environment.'}',
'right' => '\\\end{'.$environment.'}',
'display' => true,
]
);
}
Expand All @@ -171,11 +168,11 @@ public function getDelimitersWithOptions(string $type): array
/**
* Creates delimiter list with options (left, right, display).
*
* @param string $syntax The delimiter syntax that
* @param string $syntax The delimiter syntax that
* also including the expression placeholder.
* i.e. `[math]%e%[/math]`
* @param bool $displayMode To check whether it's a block or inline delimiter.
*
*
* @return array
*/
private function _setOptions(string $syntax, bool $displayMode = false): array
Expand All @@ -191,9 +188,9 @@ private function _setOptions(string $syntax, bool $displayMode = false): array
array_push(
$r,
[
'left' => $left,
'right' => $right,
'display' => $displayMode
'left' => $left,
'right' => $right,
'display' => $displayMode,
]
);

Expand All @@ -204,7 +201,7 @@ private function _setOptions(string $syntax, bool $displayMode = false): array
* Converts comma seperated list into an array.
*
* @param string $list The list to be seperated.
*
*
* @return array
*/
private function _commaToArray(string $list): array
Expand Down
27 changes: 10 additions & 17 deletions src/LoadSettings.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public function __construct(Util $util)

/**
* Get the setting values from the database and make them available in the forum.
*
*
* @return array
*/
public function __invoke(): array
Expand All @@ -40,28 +40,21 @@ public function __invoke(): array
$aliasDelimiters = $this->util->getDelimitersWithOptions('alias');

return [
'mathren.katex_options' => $this->util->getKatexOptions(),
'mathren.enable_editor_buttons' =>
\boolval($this->util->get('enable_editor_buttons')),
'mathren.aliases_as_primary' =>
\boolval($this->util->get('aliases_as_primary')),
'mathren.enable_copy_tex' =>
\boolval($this->util->get('enable_copy_tex')),
'mathren.katex_options' => $this->util->getKatexOptions(),
'mathren.enable_editor_buttons' => \boolval($this->util->get('enable_editor_buttons')),
'mathren.aliases_as_primary' => \boolval($this->util->get('aliases_as_primary')),
'mathren.enable_copy_tex' => \boolval($this->util->get('enable_copy_tex')),

// Get type-specific delimiters.
'mathren.bbcode_delimiters' => $bbCodeDelimiters,
'mathren.alias_delimiters' => $aliasDelimiters,
'mathren.alias_delimiters' => $aliasDelimiters,

// Set primiary delimiters.
// These will be the first delimiters those declared in delimiters list.
'mathren.primary_block_delimiter' =>
Arr::first($bbCodeDelimiters, fn($val) => $val['display'] === true),
'mathren.primary_inline_delimiter' =>
Arr::first($bbCodeDelimiters, fn($val) => $val['display'] === false),
'mathren.primary_block_delimiter_alias' =>
Arr::first($aliasDelimiters, fn($val) => $val['display'] === true),
'mathren.primary_inline_delimiter_alias' =>
Arr::first($aliasDelimiters, fn($val) => $val['display'] === false),
'mathren.primary_block_delimiter' => Arr::first($bbCodeDelimiters, fn ($val) => $val['display'] === true),
'mathren.primary_inline_delimiter' => Arr::first($bbCodeDelimiters, fn ($val) => $val['display'] === false),
'mathren.primary_block_delimiter_alias' => Arr::first($aliasDelimiters, fn ($val) => $val['display'] === true),
'mathren.primary_inline_delimiter_alias' => Arr::first($aliasDelimiters, fn ($val) => $val['display'] === false),
];
}
}

0 comments on commit 13a2538

Please sign in to comment.