TextFormatter: parse OGP tags

Discussion forum for Extension Writers regarding Extension Development.
Post Reply
User avatar
Ger
Registered User
Posts: 1017
Joined: Wed Jan 02, 2008 7:35 pm
Location: 192.168.1.100
Contact:

TextFormatter: parse OGP tags

Post by Ger » Mon Sep 11, 2017 2:12 pm

I'm looking into auto-parsing magic urls to render a rich formatted block using OGP tags if available.

Now it's pretty trivial to create a PHP class that does:
  1. Fetches a validated URL with CURL
  2. Loads the HTML in e DOMDocument
  3. Fetches all meta name="og:" tags
  4. Builds a HTML block with the information if possible or fallback to the standard phpBB magic url parsing
However: I'm a bit stuck here. I've looked into the TextFormatter docs but I haven't found a good point to add or hook into this process. Even more: I haven't even found the part where this magic parsing takes place. :)

Can somebody give me some pointers about where to start?
Checkout my extensions: Simple CMS, Feed post bot, Modbreak, Magic OGP links and Live topic update

Like my work? Buy me a coffee to keep it coming. :ugeek:

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

Re: TextFormatter: parse OGP tags

Post by JoshyPHP » Mon Sep 11, 2017 3:38 pm

You won't find a hook for running PHP during rendering, but there's a phpBB event for modifying the HTML after rendering.

What I would do is add a tag filter after the normal validation (you know the tag is valid at this point) to scrape the OGP data and add it to the tag in serialized form. Your OGP data will end up stored along with the parsed text. That's for the scraping part. For the custom HTML, what I would do is add the serialized OGP data to the link and post-process the HTML after rendering (using core.text_formatter_s9e_render_after) to replace the link with your custom HTML.

Configure the tag using core.text_formatter_s9e_configure_after:

Code: Select all

$tag = $event['configurator']->tags['URL'];
$tag->filterChain->append('MyOGP::doTag');

$dom = $tag->template->asDOM();
$dom->getElementsByTagName('a')->item(0)->setAttribute('data-ogp', '{@ogp}');
$dom->saveChanges();

class MyOGP
{
	public static function doTag($tag)
	{
		$url = $tag->getAttribute('url');
		$ogp = [];

		$tag->setAttribute('ogp', serialize($ogp));

		return true;
	}
}
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
Ger
Registered User
Posts: 1017
Joined: Wed Jan 02, 2008 7:35 pm
Location: 192.168.1.100
Contact:

Re: TextFormatter: parse OGP tags

Post by Ger » Mon Sep 11, 2017 4:43 pm

Great, I can always count on you đź‘Ť
I will take a look at this shortly
Checkout my extensions: Simple CMS, Feed post bot, Modbreak, Magic OGP links and Live topic update

Like my work? Buy me a coffee to keep it coming. :ugeek:

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

Re: TextFormatter: parse OGP tags

Post by JoshyPHP » Tue Sep 12, 2017 3:44 pm

By the way, in my example I used serialize() but you may want to use json_encode() instead. It's more compact and if at some point in the future you want to read this data client-side in JavaScript, you'll be able to.
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
Ger
Registered User
Posts: 1017
Joined: Wed Jan 02, 2008 7:35 pm
Location: 192.168.1.100
Contact:

Re: TextFormatter: parse OGP tags

Post by Ger » Wed Sep 13, 2017 7:40 am

Thanks. Yeah, I've figured such. serialize() has some other drawbacks as well so I went with json_encode.

I've managed (by proof-of-concept at this point) to add the data-ogp to the URL, that was the easy part. I struggled with the rendering part, it seems that somehow the DOMDocument has too much secrets for me at this point to make it work. However, I figured that I can just as well do this with jQuery since I just prettify an existing link. I have that working, so for now I'm satisfied with that part.

One thing that bothers me though: I only want this to be added to "magic" urls, so the ones that aren't created with [url=...]...[/url] tags. But I don't know how to identify those?
Checkout my extensions: Simple CMS, Feed post bot, Modbreak, Magic OGP links and Live topic update

Like my work? Buy me a coffee to keep it coming. :ugeek:

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

Re: TextFormatter: parse OGP tags

Post by JoshyPHP » Wed Sep 13, 2017 2:56 pm

In MyOGP::doTag() you should be able to get the length of the markup with $tag->getLen(). It should be 0 for magic URLs. In theory you can get the actual text consumed by the tag if you change the callback signature to add the text parameter, but if checking for the length is enough for your use case that's what I recommend.

http://s9e.github.io/TextFormatter/api/ ... r/Tag.html
http://s9etextformatter.readthedocs.io/ ... signature/

Code: Select all

substr($text, $tag->getPos(), $tag->getLen())
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
Ger
Registered User
Posts: 1017
Joined: Wed Jan 02, 2008 7:35 pm
Location: 192.168.1.100
Contact:

Re: TextFormatter: parse OGP tags

Post by Ger » Thu Sep 14, 2017 7:54 am

It works, thank you!

-edit-
I have a first version of the extension here
Checkout my extensions: Simple CMS, Feed post bot, Modbreak, Magic OGP links and Live topic update

Like my work? Buy me a coffee to keep it coming. :ugeek:

Post Reply

Return to “Extension Writers Discussion”

Who is online

Users browsing this forum: No registered users and 7 guests

cron