3.2 Text Parser/Renderer tutorial

Discussion forum for Extension Writers regarding Extension Development.
User avatar
mrgoldy
Former Team Member
Posts: 1394
Joined: Tue Oct 06, 2009 7:34 pm
Location: The Netherlands
Name: Gijs
Contact:

3.2 Text Parser/Renderer tutorial

Post by mrgoldy »

Hiya,

I've been trying to code a 3.2.x extension. Currently I'm using the generate_text_for-line to display text from my extension's tables.
However, as I am writing it for 3.2 exclusively and not for < 3.1, I thought I might aswell use the new text formatter.

Been trying lots of things to get it to work but I can't truly figure it out.
- What are the dependencies/services I have to include?
- What is the way to prepare text for storage, edit, display?
- How do I include the options to disable bbcodes/smilies/etc ..

I've been reading a lot on Area51, New Text Reparser and 3.2.x Docs in general. The 'New Text Reparser'-doc pretty much only tells you how to update/reparse your old tables with cron jobs.

Basically what I'm looking/asking for is a tutorial or example along these lines Parsing Text.
Preferably with one example of services (- '@text_formatter' ?)
and the construct in the controller (__construct(\phpbb\textformatter $text_formatter) ?)

Thanks in advance and sorry for the incompetence,
Last edited by mrgoldy on Mon Nov 27, 2017 10:40 am, edited 1 time in total.
phpBB Studio / Member of the Studio

Contributing: You can do it too! Including testing Pull Requests (PR).
phpBB Development and Testing made easy.
User avatar
Kailey
Community Team Leader
Community Team Leader
Posts: 3726
Joined: Mon Sep 01, 2014 1:00 am
Location: sudo rm -rf /
Name: Kailey Snay
Contact:

Re: 3.2 Text Reparser tutorial

Post by Kailey »

I haven't tried with my extensions yet, but have you had a look at the TextFormatter docs? The BBCode bundle and Unparsing sections look extremely relevant.
Kailey Snay - Community Team Leader
Knowledge Base | Documentation | Community rules

If you have any questions about the rules/customs of this website, feel free to send me a PM.
User avatar
mrgoldy
Former Team Member
Posts: 1394
Joined: Tue Oct 06, 2009 7:34 pm
Location: The Netherlands
Name: Gijs
Contact:

Re: 3.2 Text Reparser tutorial

Post by mrgoldy »

Thanks for the pointers, I'll have a closer look at it and fiddle with it today.
Once again, thank you.
phpBB Studio / Member of the Studio

Contributing: You can do it too! Including testing Pull Requests (PR).
phpBB Development and Testing made easy.
User avatar
mrgoldy
Former Team Member
Posts: 1394
Joined: Tue Oct 06, 2009 7:34 pm
Location: The Netherlands
Name: Gijs
Contact:

Re: 3.2 Text Reparser tutorial

Post by mrgoldy »

Okay, I think I've figured most of it out.
However, there is still one thing I do not yet get.

When a user is posting/editing something, you use the \phpbb\textformatter\s9e\parser and can set the 'standard' disable/enable on bbcodes/smilies/magicurl's. Then parse it and save it to the database. This way it is properly saved in the database and outputted nicely with the renderer.
But then when I change a setting in my extension (ie. Disable BBCodes), so no BBCodes are allowed within my extension, there is no way to check for this setting with the \phpbb\textformatter\s9e\renderer? I have to reparse (edit and submit) posts for it to take effect? I can only find the set_viewsmilies-option.

Thanks in advance,
phpBB Studio / Member of the Studio

Contributing: You can do it too! Including testing Pull Requests (PR).
phpBB Development and Testing made easy.
User avatar
Kailey
Community Team Leader
Community Team Leader
Posts: 3726
Joined: Mon Sep 01, 2014 1:00 am
Location: sudo rm -rf /
Name: Kailey Snay
Contact:

Re: 3.2 Text Reparser tutorial

Post by Kailey »

JoshyPHP would be best to answer that question.
Kailey Snay - Community Team Leader
Knowledge Base | Documentation | Community rules

If you have any questions about the rules/customs of this website, feel free to send me a PM.
User avatar
RMcGirr83
Former Team Member
Posts: 22011
Joined: Wed Jun 22, 2005 4:33 pm
Location: Your display
Name: Rich McGirr

Re: 3.2 Text Reparser tutorial

Post by RMcGirr83 »

joshyphp wrote: JoshyPHP would be best to answer that question.
Former Modifications/Extensions Team Member | My extensions | github | All requests for support via PM will be ignored
Appreciate the extensions/mods/support then buy me a beer Image
User avatar
JoshyPHP
Code Contributor
Posts: 1288
Joined: Mon Jul 11, 2011 12:28 am

Re: 3.2 Text Reparser tutorial

Post by JoshyPHP »

Not really. The reparser doesn't do anything that editing/reposting something doesn't. It won't change options and there's no user interface to trigger it outside of CLI.
I wrote the library that handles markup in phpBB 3.2+.
User avatar
mrgoldy
Former Team Member
Posts: 1394
Joined: Tue Oct 06, 2009 7:34 pm
Location: The Netherlands
Name: Gijs
Contact:

Re: 3.2 Text Reparser tutorial

Post by mrgoldy »

I'm not quite sure what you mean? The post is stored in the database, with the parser having BBCodes enabled. The renderer only puts it out the way it is stored in the database. So while I change my setting, the post in the database isn't altered and BBCodes are still being displayed.
With the generate_text_for_display you could check again for the BBCode option, whether it was enabled or not.
So now if I want to have that post without BBCodes, I have to edit and submit it again?

And one a some-what side note, is there a way to call all 'text formatters' at once, or do I have to add all three (renderer, parser, utils) through dependencies? There is no way to only add a textformatter dependency and then use something like $parser = textformatter->get_parser ?
phpBB Studio / Member of the Studio

Contributing: You can do it too! Including testing Pull Requests (PR).
phpBB Development and Testing made easy.
User avatar
JoshyPHP
Code Contributor
Posts: 1288
Joined: Mon Jul 11, 2011 12:28 am

Re: 3.2 Text Reparser tutorial

Post by JoshyPHP »

posey wrote: Tue Nov 21, 2017 11:43 pm if I want to have that post without BBCodes, I have to edit and submit it again?
Yes, you'd have to do that.
posey wrote: Tue Nov 21, 2017 11:43 pmis there a way to call all 'text formatters' at once
No, they're different services because they have different purposes. You have to call them separately.
I wrote the library that handles markup in phpBB 3.2+.
User avatar
mrgoldy
Former Team Member
Posts: 1394
Joined: Tue Oct 06, 2009 7:34 pm
Location: The Netherlands
Name: Gijs
Contact:

Re: 3.2 Text Reparser tutorial

Post by mrgoldy »

Okay, thanks a lot for the clarification.

Is it then perhaps an idea to run a cron job with the text reparser, whenever a setting concerning BBCodes etc is changed from within the ACP?

By the way, I like the simplicity of working with the enable/disable settings for multiple options. What would be even easier if we could send a bool as paremeter to the function, ie: $parser->set_bbcodes($bool true / false);. Saves a couple of lines, not sure if that's food for thought.

And one last question and then I will stop bugging you (Thanks for your patience thusfar):
When I use the textformatter to store (parse) and output (render) the post, > gets turned into &gt;. Same for some more special characters, if you can call a & sign special. What might I have wrong in my setup?
phpBB Studio / Member of the Studio

Contributing: You can do it too! Including testing Pull Requests (PR).
phpBB Development and Testing made easy.
User avatar
JoshyPHP
Code Contributor
Posts: 1288
Joined: Mon Jul 11, 2011 12:28 am

Re: 3.2 Text Reparser tutorial

Post by JoshyPHP »

There's no interface for triggering the reparser from the ACP. Ideally there would be one, but no one wrote one.

If you change a BBCode's template, the new template is used immediately. If you replace its template with <xsl:apply-templates/> it will just display its content, without its markup.

Special characters such as > or & are systematically encoded. That's expected.
I wrote the library that handles markup in phpBB 3.2+.
User avatar
mrgoldy
Former Team Member
Posts: 1394
Joined: Tue Oct 06, 2009 7:34 pm
Location: The Netherlands
Name: Gijs
Contact:

Re: 3.2 Text Reparser tutorial

Post by mrgoldy »

Once again, I might misunderstand, but with the new text formatter you can no longer use & and > in posts, as they get decoded?
I feel like that's not right, so I guess I am doing something wrong / forgetting something?
phpBB Studio / Member of the Studio

Contributing: You can do it too! Including testing Pull Requests (PR).
phpBB Development and Testing made easy.
User avatar
JoshyPHP
Code Contributor
Posts: 1288
Joined: Mon Jul 11, 2011 12:28 am

Re: 3.2 Text Reparser tutorial

Post by JoshyPHP »

You should describe what you're trying to do in concrete terms.
I wrote the library that handles markup in phpBB 3.2+.
User avatar
mrgoldy
Former Team Member
Posts: 1394
Joined: Tue Oct 06, 2009 7:34 pm
Location: The Netherlands
Name: Gijs
Contact:

Re: 3.2 Text Reparser tutorial

Post by mrgoldy »

Alright:

Input:
post_text wrote:This is a test post with some & signs and other special characters, like "quotes" and > arrows <
Input PHP:

Code: Select all

$post_text = $this->request->variable('post_text, '', true);

$this->config['allow_bbcode'] ?  $this->parser->enable_bbcodes() : $this->parser->disable_bbcodes();
$this->config['allow_smilies'] ? $this->parser->enable_smilies() : $this->parser->disable_smilies();
$this->config['allow_magic_url'] ? this->parser->enable_magic_url() : $this->parser->disable_magic_url();

$post_to_insert = array(
	'post_text'		=> $this->parser->parse($post_text),
);

$sql = 'INSERT INTO ... //etc
Database entry:

Code: Select all

<r>This is a test <B><s>[b]</s>post<e>[/b]</e></B> with some &amp;amp; signs and other special characters, like &amp;quot;quotes&amp;quot; and &amp;gt; arrows &amp;lt;</r>
Output PHP:

Code: Select all

$sql = 'SELECT post_text FROM .. //etc

$this->template->assign_vars(array(
	'POST_TEXT'	=> $this->renderer->render($row['post_text]),
));
Output:
post_text wrote:This is a test post with some &amp; signs and other special characters, like &quot;quotes&quot; and &gt; arrows &lt;
phpBB Studio / Member of the Studio

Contributing: You can do it too! Including testing Pull Requests (PR).
phpBB Development and Testing made easy.
User avatar
JoshyPHP
Code Contributor
Posts: 1288
Joined: Mon Jul 11, 2011 12:28 am

Re: 3.2 Text Reparser tutorial

Post by JoshyPHP »

My guess is that $post_text is already encoded as HTML. Instead you should pass plain text. The parser expects plain text.

What does $this->parser->parse('<&>') return? It should be <t>&lt;&amp;&gt;</t>.
I wrote the library that handles markup in phpBB 3.2+.
Post Reply

Return to “Extension Writers Discussion”