3.2 Text Parser/Renderer tutorial

Discussion forum for Extension Writers regarding Extension Development.
User avatar
posey
Registered User
Posts: 546
Joined: Tue Oct 06, 2009 7:34 pm
Location: The Netherlands
Name: Gijs

3.2 Text Parser/Renderer tutorial

Post by posey » Wed Nov 15, 2017 3:45 pm

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 posey on Mon Nov 27, 2017 10:40 am, edited 1 time in total.
Ultimate Extensions:
Image Ultimate Blog (coming soon)
Image Ultimate Teams

''I'm pretty sure there's a lot more to life than being really, really, ridiculously good looking. And I plan on finding out what that is.''

User avatar
kinerity
Registered User
Posts: 1151
Joined: Mon Sep 01, 2014 1:00 am
Name: Kailey Truscott
Contact:

Re: 3.2 Text Reparser tutorial

Post by kinerity » Wed Nov 15, 2017 7:14 pm

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.
phpBB3 Refugees - Unofficial phpBB 3.0.x support

Image

User avatar
posey
Registered User
Posts: 546
Joined: Tue Oct 06, 2009 7:34 pm
Location: The Netherlands
Name: Gijs

Re: 3.2 Text Reparser tutorial

Post by posey » Mon Nov 20, 2017 9:15 am

Thanks for the pointers, I'll have a closer look at it and fiddle with it today.
Once again, thank you.
Ultimate Extensions:
Image Ultimate Blog (coming soon)
Image Ultimate Teams

''I'm pretty sure there's a lot more to life than being really, really, ridiculously good looking. And I plan on finding out what that is.''

User avatar
posey
Registered User
Posts: 546
Joined: Tue Oct 06, 2009 7:34 pm
Location: The Netherlands
Name: Gijs

Re: 3.2 Text Reparser tutorial

Post by posey » Mon Nov 20, 2017 8:42 pm

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,
Ultimate Extensions:
Image Ultimate Blog (coming soon)
Image Ultimate Teams

''I'm pretty sure there's a lot more to life than being really, really, ridiculously good looking. And I plan on finding out what that is.''

User avatar
kinerity
Registered User
Posts: 1151
Joined: Mon Sep 01, 2014 1:00 am
Name: Kailey Truscott
Contact:

Re: 3.2 Text Reparser tutorial

Post by kinerity » Tue Nov 21, 2017 3:08 pm

JoshyPHP would be best to answer that question.
phpBB3 Refugees - Unofficial phpBB 3.0.x support

Image

User avatar
RMcGirr83
Recognised Extension Developer
Posts: 20444
Joined: Wed Jun 22, 2005 4:33 pm
Location: Your display
Name: Rich McGirr
Contact:

Re: 3.2 Text Reparser tutorial

Post by RMcGirr83 » Tue Nov 21, 2017 3:10 pm

joshyphp wrote: JoshyPHP would be best to answer that question.
In times of change, learners inherit the earth, while the learned find themselves beautifully equipped to deal with a world that no longer exists - Eric Hoffer
Former Modifications/Extensions Team Member | My extensions
Appreciate the extensions/mods/support then buy me a beer
All requests for support via PM will be ignored

User avatar
JoshyPHP
Code Contributor
Posts: 756
Joined: Mon Jul 11, 2011 12:28 am

Re: 3.2 Text Reparser tutorial

Post by JoshyPHP » Tue Nov 21, 2017 3:43 pm

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 thing that does the BBCodes in 3.2. Unless it broke yours, in which case it was somebody else with a similar name.

User avatar
posey
Registered User
Posts: 546
Joined: Tue Oct 06, 2009 7:34 pm
Location: The Netherlands
Name: Gijs

Re: 3.2 Text Reparser tutorial

Post by posey » Tue Nov 21, 2017 11:43 pm

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 ?
Ultimate Extensions:
Image Ultimate Blog (coming soon)
Image Ultimate Teams

''I'm pretty sure there's a lot more to life than being really, really, ridiculously good looking. And I plan on finding out what that is.''

User avatar
JoshyPHP
Code Contributor
Posts: 756
Joined: Mon Jul 11, 2011 12:28 am

Re: 3.2 Text Reparser tutorial

Post by JoshyPHP » Wed Nov 22, 2017 12:11 am

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 pm
is 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 thing that does the BBCodes in 3.2. Unless it broke yours, in which case it was somebody else with a similar name.

User avatar
posey
Registered User
Posts: 546
Joined: Tue Oct 06, 2009 7:34 pm
Location: The Netherlands
Name: Gijs

Re: 3.2 Text Reparser tutorial

Post by posey » Wed Nov 22, 2017 12:19 am

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?
Ultimate Extensions:
Image Ultimate Blog (coming soon)
Image Ultimate Teams

''I'm pretty sure there's a lot more to life than being really, really, ridiculously good looking. And I plan on finding out what that is.''

User avatar
JoshyPHP
Code Contributor
Posts: 756
Joined: Mon Jul 11, 2011 12:28 am

Re: 3.2 Text Reparser tutorial

Post by JoshyPHP » Wed Nov 22, 2017 12:39 am

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 thing that does the BBCodes in 3.2. Unless it broke yours, in which case it was somebody else with a similar name.

User avatar
posey
Registered User
Posts: 546
Joined: Tue Oct 06, 2009 7:34 pm
Location: The Netherlands
Name: Gijs

Re: 3.2 Text Reparser tutorial

Post by posey » Wed Nov 22, 2017 6:08 pm

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?
Ultimate Extensions:
Image Ultimate Blog (coming soon)
Image Ultimate Teams

''I'm pretty sure there's a lot more to life than being really, really, ridiculously good looking. And I plan on finding out what that is.''

User avatar
JoshyPHP
Code Contributor
Posts: 756
Joined: Mon Jul 11, 2011 12:28 am

Re: 3.2 Text Reparser tutorial

Post by JoshyPHP » Wed Nov 22, 2017 6:30 pm

You should describe what you're trying to do in concrete terms.
I wrote the thing that does the BBCodes in 3.2. Unless it broke yours, in which case it was somebody else with a similar name.

User avatar
posey
Registered User
Posts: 546
Joined: Tue Oct 06, 2009 7:34 pm
Location: The Netherlands
Name: Gijs

Re: 3.2 Text Reparser tutorial

Post by posey » Thu Nov 23, 2017 6:53 am

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;
Ultimate Extensions:
Image Ultimate Blog (coming soon)
Image Ultimate Teams

''I'm pretty sure there's a lot more to life than being really, really, ridiculously good looking. And I plan on finding out what that is.''

User avatar
JoshyPHP
Code Contributor
Posts: 756
Joined: Mon Jul 11, 2011 12:28 am

Re: 3.2 Text Reparser tutorial

Post by JoshyPHP » Thu Nov 23, 2017 12:23 pm

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 thing that does the BBCodes in 3.2. Unless it broke yours, in which case it was somebody else with a similar name.

Post Reply

Return to “Extension Writers Discussion”

Who is online

Users browsing this forum: No registered users and 8 guests

cron