How to get the current language name on code tag?

Discussion forum for Extension Writers regarding Extension Development.
Post Reply
WHK102
Registered User
Posts: 9
Joined: Wed Aug 15, 2018 8:40 pm

How to get the current language name on code tag?

Post by WHK102 » Mon Mar 25, 2019 12:01 am

Have a template with bbcode.html file, and it is the code writer line:

Code: Select all

<!-- BEGIN code_open --><div class="codebox"><p>{L_CODE}{L_COLON} <a href="#" onclick="selectCode(this); return false;">{L_SELECT_ALL_CODE}</a></p><pre><code><!-- END code_open -->
Need write the lang name in the code, like as:

Code: Select all

<!-- BEGIN code_open --><div class="codebox"><pre><code class="{LANG-NAME????}"><!-- END code_open -->
What is the template var for get the languaje name?, and it is secure for XSS attacks?

The post code tag use for example:

Code: Select all

[code=python]..
Need use this: https://highlightjs.org/usage/ in a programming forum.

Thanks.

User avatar
kinerity
Community Team Member
Community Team Member
Posts: 2228
Joined: Mon Sep 01, 2014 1:00 am
Location: sudo rm -rf /
Name: Kailey Truscott
Contact:

Re: How to get the current language name on code tag?

Post by kinerity » Mon Mar 25, 2019 12:59 am

Would a BBCode not work for you?

BBCode:

Code: Select all

[codebox={SIMPLETEXT}]{TEXT}[/codebox]
HTML:

Code: Select all

<div class="codebox"><p>{SIMPLETEXT} {L_CODE}{L_COLON} <a href="#" onclick="selectCode(this); return false;">{L_SELECT_ALL_CODE}</a></p><pre><code class="{SIMPLETEXT}">{TEXT}</code></pre></div>
BBCode.png
BBCode.png (5.54 KiB) Viewed 461 times
Kailey Truscott - Community Team

WHK102
Registered User
Posts: 9
Joined: Wed Aug 15, 2018 8:40 pm

Re: How to get the current language name on code tag?

Post by WHK102 » Tue Apr 02, 2019 6:57 pm

But, using a new bbc need make a new buttons on posting box? need hide the original code button?

User avatar
kinerity
Community Team Member
Community Team Member
Posts: 2228
Joined: Mon Sep 01, 2014 1:00 am
Location: sudo rm -rf /
Name: Kailey Truscott
Contact:

Re: How to get the current language name on code tag?

Post by kinerity » Tue Apr 02, 2019 7:53 pm

The problem is that phpBB does not pass a language variable for you to define different programming languages. It's best to go with a new BBCode if possible.

Scratch that - see if this works for you https://github.com/s9e/phpbb-ext-highlighter
Kailey Truscott - Community Team

User avatar
mrgoldy
Jr. Extension Validator
Posts: 1040
Joined: Tue Oct 06, 2009 7:34 pm
Location: The Netherlands
Name: Gijs

Re: How to get the current language name on code tag?

Post by mrgoldy » Tue Apr 02, 2019 8:44 pm

You can try the following line:

Code: Select all

<!-- BEGIN code_open -->
	<div class="codebox"><pre><code>
	<xsl:if test="@lang">
		<xsl:attribute name="class">
			<xsl:value-of select="@lang"/>
		</xsl:attribute>
	</xsl:if>		
<!-- END code_open -->
As the default code BBCode uses a slightly different approach ([code lang=<CODE_LANGUAGE>]<the code>[/code], I believe.
Not sure if the above xsl approach works, as the html element it applies to is not yet closed within this line.
Do note, that after making these changes you will have to purge the cache and perhaps even resubmit your post.
But all this would mean modifying bbcode.html, which means you're Custom coding. Are you or are we talking about extensions writing?

Otherwise, if you want to enable the HighlightJS Plugin that is build into the s9e Textformatter you have to create a listener:

Code: Select all

<?php

namespace vendor\extension\event;

use Symfony\Component\EventDispatcher\EventSubscriberInterface;

class listener implements EventSubscriberInterface
{
	/**
	 * Assign functions defined in this class to event listeners in the core.
	 *
	 * @static
	 * @return array
	 * @access public
	 */
	static public function getSubscribedEvents()
	{
		return array(
			'core.text_formatter_s9e_configure_after'	=> 'your_function_name',
		);
	}
	
	/**
	 * @event  core.text_formatter_s9e_configure_after
	 * @param  \phpbb\event\data	$event		The event object
	 * @return void
	 * @access public
	 */
	public function your_function_name($event)
	{
		// Get the BBCode configurator
		$configurator = $event['configurator'];
		
		$configurator->BBCodes->addFromRepository('CODE'); // re-enable code for highlighting
		
		$event['configurator'] = $configurator;
	}
}
phpBB Studio / ''Proud member of the Studio"

Post Reply

Return to “Extension Writers Discussion”