CMTV
Well-known member
- Affected version
- 2.0
I tried to follow an algorithm of adding custom language for
It is not working.
I tried to figure what is going wrong and here is what I found. I will use Scala language here as an example.
The proposed method is working fine if we set "scala" directly in
So this code will properly color all scala code blocks:
This means there is a problem with
The problem appear at
So the problem is within the
At the end of this file there is an interesting
Conclusion
Custom languages (like 'scala') are not working because they can not pass through
So this needs to be fixed I think.
[code]
bbcode proposed by @Chris D .It is not working.
I tried to figure what is going wrong and here is what I found. I will use Scala language here as an example.
The proposed method is working fine if we set "scala" directly in
data-lang="{{$language ?: ''}}"
attribute in bb_code_tag_code
template.So this code will properly color all scala code blocks:
HTML:
<pre class="bbCodeCode" dir="ltr" data-xf-init="code-block" data-lang="scala"><code>{$content}</code></pre>
This means there is a problem with
$language
parameter (in fact, it is empty) that gets passed in bb_code_tag_code
template.The problem appear at
renderTagCode(...)
function in XF\BbCode\Renderer\Html.php
:
PHP:
public function renderTagCode(array $children, $option, array $tag, array $options)
{
$content = $this->renderSubTree($children, $options);
// a bit like ltrim, but only remove blank lines, not leading tabs on the first line
$content = preg_replace('#^([ \t]*\r?\n)+#', '', $content);
$content = rtrim($content);
/** @var \XF\Data\CodeLanguage $codeLanguages */
$codeLanguages = \XF::app()->data('XF:CodeLanguage');
$allowedLanguages = $codeLanguages->getSupportedLanguages(true);
///////////////////////////////////////////////////
// HERE WE DO HAVE "scala" IN $language variable
///////////////////////////////////////////////////
$language = strtolower(preg_replace('#[^a-z0-9_-]#i', '-', $option));
if (isset($allowedLanguages[$language]))
{
$config = $allowedLanguages[$language];
}
else
{
/////////////////////////////////////////////////
// HERE $language FOR "scala" is set to ''
/////////////////////////////////////////////////
$config = [];
$language = '';
}
if (!is_array($this->allowedCodeLanguages))
{
$this->allowedCodeLanguages = preg_split('/\r?\n/', \XF::options()->allowedCodeLanguages);
}
if (!in_array($language, $this->allowedCodeLanguages))
{
$language = '';
}
return $this->getRenderedCode($content, $language, $config);
}
So the problem is within the
XF\Data\CodeLanguage.php
file. All available languages are simply hardcoded there...
PHP:
$languages = [
'apacheconf' => [],
'bash' => [
'modes' => 'shell'
],
'c' => [
'modes' => 'clike',
'mime' => 'text/x-csrc'
],
'clike' => [
'modes' => 'clike',
'mime' => 'text/x-csrc'
],
/* ... */
At the end of this file there is an interesting
if
:
PHP:
if ($filterDisabled)
{
$enabledLanguages = preg_split('/\r?\n/', \XF::options()->allowedCodeLanguages);
$languages = array_intersect_key($languages, array_flip($enabledLanguages));
}
return $languages;
Conclusion
Custom languages (like 'scala') are not working because they can not pass through
array_intersect_key
function. They do not pass through because $filterDisabled
is set to true
when calling getSupportedLanguages(true)
in Html.php
. Setting it to false
will not work because in that case getSupportedLanguages(false)
will simply return hardcoded languages.So this needs to be fixed I think.