Skip to content

Commit

Permalink
MediaEmbed: added support for custom Mastodon instances
Browse files Browse the repository at this point in the history
  • Loading branch information
JoshyPHP committed Dec 30, 2022
1 parent 1cb7e76 commit 9ad74ec
Show file tree
Hide file tree
Showing 7 changed files with 190 additions and 79 deletions.
26 changes: 26 additions & 0 deletions docs/Plugins/MediaEmbed/Federated_sites.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<h2>Federated sites</h2>

### Mastodon

The default Mastodon media site can be customized with additional hosts. This can be done by modifying the original configuration in `$configurator->MediaEmbed->defaultSites['mastodon']`, or by assigning `mastodon` to the desired hostname in `$configurator->registeredVars['MediaEmbed.hosts']`. In the example below, we declare that the host `infosec.exchange` should be mapped to the `mastodon` site.

```php
$configurator = new s9e\TextFormatter\Configurator;

// Add Mastodon, then create an entry that maps the hostname 'infosec.exchange'
// to the 'mastodon' site
$configurator->MediaEmbed->add('mastodon');
$configurator->registeredVars['MediaEmbed.hosts']['infosec.exchange'] = 'mastodon';

// Get an instance of the parser and the renderer
extract($configurator->finalize());

$text = 'https://infosec.exchange/@SwiftOnSecurity/109579438603578302';
$xml = $parser->parse($text);
$html = $renderer->render($xml);

echo $html;
```
```html
<iframe data-s9e-mediaembed="mastodon" allowfullscreen="" loading="lazy" onload="let c=new MessageChannel;c.port1.onmessage=e=&gt;this.style.height=e.data+'px';this.contentWindow.postMessage('s9e:init','*',[c.port2])" scrolling="no" style="border:0;height:300px;max-width:550px;width:100%" src="https://s9e.github.io/iframe/2/mastodon.min.html#SwiftOnSecurity@infosec.exchange/109579438603578302"></iframe>
```
13 changes: 13 additions & 0 deletions docs/testdox.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2649,6 +2649,8 @@ PHP (s9e\TextFormatter\Tests\Configurator\RendererGenerators\PHP)
[x] Tests from plugins with data set #301
[x] Tests from plugins with data set #302
[x] Tests from plugins with data set #303
[x] Tests from plugins with data set #304
[x] Tests from plugins with data set #305
[x] Tests from plugins (Quick renderer) with data set #0
[x] Tests from plugins (Quick renderer) with data set #1
[x] Tests from plugins (Quick renderer) with data set #2
Expand Down Expand Up @@ -2953,6 +2955,8 @@ PHP (s9e\TextFormatter\Tests\Configurator\RendererGenerators\PHP)
[x] Tests from plugins (Quick renderer) with data set #301
[x] Tests from plugins (Quick renderer) with data set #302
[x] Tests from plugins (Quick renderer) with data set #303
[x] Tests from plugins (Quick renderer) with data set #304
[x] Tests from plugins (Quick renderer) with data set #305
[x] Creates a Quick renderer if $enableQuickRenderer is true
[x] Does not create a Quick renderer if $enableQuickRenderer is false

Expand Down Expand Up @@ -6873,6 +6877,7 @@ Parser (s9e\TextFormatter\Tests\Plugins\MediaEmbed\Parser)
[x] Scraping tests with data set #56
[x] Scraping tests with data set #57
[x] Scraping tests with data set #58
[x] Scraping tests with data set #59
[x] Scraping+rendering tests with data set #0
[x] Scraping+rendering tests with data set #1
[x] Scraping+rendering tests with data set #2
Expand Down Expand Up @@ -7244,6 +7249,9 @@ Parser (s9e\TextFormatter\Tests\Plugins\MediaEmbed\Parser)
[x] Parsing tests with data set #317
[x] Parsing tests with data set #318
[x] Parsing tests with data set #319
[x] Parsing tests with data set #320
[x] Parsing tests with data set #321
[x] Parsing tests with data set #322
[x] Parsing tests (JavaScript) with data set #0
[x] Parsing tests (JavaScript) with data set #1
[x] Parsing tests (JavaScript) with data set #2
Expand Down Expand Up @@ -7564,6 +7572,9 @@ Parser (s9e\TextFormatter\Tests\Plugins\MediaEmbed\Parser)
[x] Parsing tests (JavaScript) with data set #317
[x] Parsing tests (JavaScript) with data set #318
[x] Parsing tests (JavaScript) with data set #319
[x] Parsing tests (JavaScript) with data set #320
[x] Parsing tests (JavaScript) with data set #321
[x] Parsing tests (JavaScript) with data set #322
[x] Parsing+rendering tests with data set #0
[x] Parsing+rendering tests with data set #1
[x] Parsing+rendering tests with data set #2
Expand Down Expand Up @@ -7660,6 +7671,8 @@ Parser (s9e\TextFormatter\Tests\Plugins\MediaEmbed\Parser)
[x] Parsing+rendering tests with data set #93
[x] Parsing+rendering tests with data set #94
[x] Parsing+rendering tests with data set #95
[x] Parsing+rendering tests with data set #96
[x] Parsing+rendering tests with data set #97

Parser Base (s9e\TextFormatter\Tests\Plugins\ParserBase)
[x] Has a default empty setUp() method
Expand Down
144 changes: 72 additions & 72 deletions src/Bundles/MediaPack.php

Large diffs are not rendered by default.

6 changes: 3 additions & 3 deletions src/Bundles/MediaPack/Renderer.php

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ class CachedDefinitionCollection extends SiteDefinitionCollection
'liveleak'=>['attributes'=>[],'example'=>'https://www.liveleak.com/view?t=yIcw_1520190567','extract'=>['!liveleak\\.com/(?:e/|view\\?i=)(?\'id\'\\w+)!'],'homepage'=>'https://www.liveleak.com/','host'=>['liveleak.com'],'iframe'=>['src'=>'//www.liveleak.com/e/{@id}'],'name'=>'Liveleak','scrape'=>[['extract'=>['!liveleak\\.com/e/(?\'id\'\\w+)!'],'match'=>['!liveleak\\.com/view\\?t=!']]],'tags'=>['videos']],
'livestream'=>['attributes'=>[],'example'=>['https://new.livestream.com/jbtvlive/musicmarathon','https://livestream.com/ccscsl/USChessChampionships/videos/83267610','https://livestre.am/58XNV'],'extract'=>['!livestream\\.com/accounts/(?\'account_id\'\\d+)/events/(?\'event_id\'\\d+)!','!/videos/(?\'video_id\'\\d+)!','!original\\.livestream\\.com/(?\'channel\'\\w+)/video\\?clipId=(?\'clip_id\'[-\\w]+)!'],'homepage'=>'https://new.livestream.com/','host'=>['livestre.am','livestream.com'],'iframe'=>['src'=>'//<xsl:choose><xsl:when test="@clip_id">cdn.livestream.com/embed/<xsl:value-of select="@channel"/>?layout=4&amp;autoplay=false&amp;clip=<xsl:value-of select="@clip_id"/></xsl:when><xsl:otherwise>livestream.com/accounts/<xsl:value-of select="@account_id"/>/events/<xsl:value-of select="@event_id"/><xsl:if test="@video_id">/videos/<xsl:value-of select="@video_id"/></xsl:if>/player?autoPlay=false</xsl:otherwise></xsl:choose>'],'name'=>'Livestream','scrape'=>[['extract'=>['!accounts/(?\'account_id\'\\d+)/events/(?\'event_id\'\\d+)!'],'match'=>['@livestream\\.com/(?!accounts/\\d+/events/\\d)@']],['extract'=>['!//original\\.livestream\\.com/(?\'channel\'\\w+)/video/(?\'clip_id\'[-\\w]+)!'],'match'=>['!livestre.am!']]],'tags'=>['livestreaming','videos']],
'mailru'=>['attributes'=>[],'example'=>['https://my.mail.ru/corp/auto/video/testdrive/34.html','https://my.mail.ru/mail/alenka1957/video/1/7.html'],'extract'=>[],'homepage'=>'https://my.mail.ru/','host'=>['my.mail.ru'],'iframe'=>['src'=>'https://my.mail.ru/video/embed/{@id}'],'name'=>'Mail.Ru','scrape'=>[['extract'=>['!"itemId": ?"?(?\'id\'\\d+)!'],'match'=>['!my\\.mail\\.ru/\\w+/\\w+/video/\\w+/\\d!']]],'tags'=>['.ru']],
'mastodon'=>['attributes'=>[],'example'=>'https://mastodon.social/@HackerNewsBot/100181134752056592','extract'=>['#/@(?\'name\'\\w+)/(?\'id\'\\d+)#'],'homepage'=>'https://mastodon.social/','host'=>['mastodon.social'],'iframe'=>['data-s9e-livepreview-ignore-attrs'=>'style','height'=>300,'onload'=>'let c=new MessageChannel;c.port1.onmessage=e=>this.style.height=e.data+\'px\';this.contentWindow.postMessage(\'s9e:init\',\'*\',[c.port2])','src'=>'https://s9e.github.io/iframe/2/mastodon.min.html#{@name}/{@id}','width'=>550],'name'=>'Mastodon','oembed'=>['endpoint'=>'https://mastodon.social/api/oembed','scheme'=>'https://mastodon.social/@{@name}/{@id}'],'scrape'=>[],'tags'=>['social']],
'mastodon'=>['attributes'=>[],'example'=>'https://mastodon.social/@HackerNewsBot/100181134752056592','extract'=>['#//(?\'host\'[-.\\w]+)/@(?\'name\'\\w+)/(?\'id\'\\d+)#'],'homepage'=>'https://mastodon.social/','host'=>['mastodon.social'],'iframe'=>['data-s9e-livepreview-ignore-attrs'=>'style','height'=>300,'onload'=>'let c=new MessageChannel;c.port1.onmessage=e=>this.style.height=e.data+\'px\';this.contentWindow.postMessage(\'s9e:init\',\'*\',[c.port2])','src'=>'https://s9e.github.io/iframe/2/mastodon.min.html#<xsl:value-of select="@name"/><xsl:if test="@host and@host!=\'mastodon.social\'">@<xsl:value-of select="@host"/></xsl:if>/<xsl:value-of select="@id"/>','width'=>550],'name'=>'Mastodon','oembed'=>['endpoint'=>'https://mastodon.social/api/oembed','scheme'=>'https://mastodon.social/@{@name}/{@id}'],'scrape'=>[['extract'=>['#"url":"https://(?\'host\'[-.\\w]+)/@(?\'name\'\\w+)/(?\'id\'\\d+)"#'],'match'=>['#^(?\'origin\'https://[^/]+)/@\\w+@[-.\\w]+/(?\'id\'\\d+)#'],'url'=>'{@origin}/api/v1/statuses/{@id}']],'tags'=>['social']],
'medium'=>['attributes'=>[],'example'=>'https://medium.com/@donnydonny/team-internet-is-about-to-win-net-neutrality-and-they-didnt-need-googles-help-e7e2cf9b8a95','extract'=>['#medium\\.com/(?:s/\\w+/|@?[-\\w]+/)?(?:\\w+-)*(?\'id\'[0-9a-f]+)(?!\\w)#'],'homepage'=>'https://medium.com/','host'=>['medium.com'],'iframe'=>['data-s9e-livepreview-ignore-attrs'=>'style','height'=>316,'max-width'=>900,'onload'=>'let c=new MessageChannel;c.port1.onmessage=e=>this.style.height=e.data+\'px\';this.contentWindow.postMessage(\'s9e:init\',\'*\',[c.port2])','src'=>'https://s9e.github.io/iframe/2/medium.min.html#{@id}','width'=>'100%'],'name'=>'Medium','scrape'=>[],'tags'=>['blogging']],
'megaphone'=>['amp'=>['custom-element'=>'amp-megaphone','src'=>'https://cdn.ampproject.org/v0/amp-megaphone-0.1.js','template'=>'<amp-megaphone layout="fixed-height" height="200" data-episode="{@id}"><xsl:if test="$MEDIAEMBED_THEME=\'light\'"><xsl:attribute name="data-light"/></xsl:if></amp-megaphone>'],'attributes'=>[],'example'=>['https://cms.megaphone.fm/channel/lockedonheat?selected=LKN8165322853','https://player.megaphone.fm/LKN8165322853'],'extract'=>['@megaphone\\.fm/.*?\\?(?:e|selected)=(?\'id\'\\w+)@','@(?:dcs|player|traffic)\\.megaphone\\.fm/(?\'id\'\\w+)@','@megaphone\\.link/(?\'id\'\\w+)@'],'homepage'=>'https://megaphone.fm/','host'=>['megaphone.fm','megaphone.link'],'iframe'=>['height'=>200,'max-width'=>900,'src'=>'https://player.megaphone.fm/<xsl:value-of select="@id"/><xsl:if test="$MEDIAEMBED_THEME=\'light\'">?light=true</xsl:if>','width'=>'100%'],'name'=>'Megaphone','scrape'=>[],'tags'=>['podcasts']],
'metacafe'=>['attributes'=>[],'example'=>'https://www.metacafe.com/watch/10785282/chocolate_treasure_chest_epic_meal_time/','extract'=>['!metacafe\\.com/watch/(?\'id\'\\d+)!'],'homepage'=>'https://www.metacafe.com/','host'=>['metacafe.com'],'iframe'=>['src'=>'//www.metacafe.com/embed/{@id}/'],'name'=>'Metacafe','scrape'=>[],'tags'=>['videos']],
Expand Down
16 changes: 14 additions & 2 deletions src/Plugins/MediaEmbed/Configurator/sites/mastodon.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,18 @@
<tags><tag>social</tag></tags>

<host>mastodon.social</host>
<extract>#/@(?'name'\w+)/(?'id'\d+)#</extract>
<iframe width="550" height="300" src="https://s9e.github.io/iframe/2/mastodon.min.html#{@name}/{@id}" onload="let c=new MessageChannel;c.port1.onmessage=e=>this.style.height=e.data+'px';this.contentWindow.postMessage('s9e:init','*',[c.port2])" data-s9e-livepreview-ignore-attrs="style"/>
<extract>#//(?'host'[-.\w]+)/@(?'name'\w+)/(?'id'\d+)#</extract>
<scrape url="{@origin}/api/v1/statuses/{@id}">
<match>#^(?'origin'https://[^/]+)/@\w+@[-.\w]+/(?'id'\d+)#</match>
<extract>#"url":"https://(?'host'[-.\w]+)/@(?'name'\w+)/(?'id'\d+)"#</extract>
</scrape>
<iframe width="550" height="300" onload="let c=new MessageChannel;c.port1.onmessage=e=>this.style.height=e.data+'px';this.contentWindow.postMessage('s9e:init','*',[c.port2])" data-s9e-livepreview-ignore-attrs="style">
<src><![CDATA[
<xsl:text>https://s9e.github.io/iframe/2/mastodon.min.html#</xsl:text>
<xsl:value-of select="@name"/>
<xsl:if test="@host and @host != 'mastodon.social'">@<xsl:value-of select="@host"/></xsl:if>
<xsl:text>/</xsl:text>
<xsl:value-of select="@id"/>
]]></src>
</iframe>
</site>
62 changes: 61 additions & 1 deletion tests/Plugins/MediaEmbed/ParserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1096,6 +1096,16 @@ function ($configurator)
$configurator->MediaEmbed->add('livestream');
}
],
[
'https://mastodon.social/@SwiftOnSecurity@infosec.exchange/109579438826193099',
'<r><MASTODON host="infosec.exchange" id="109579438603578302" name="SwiftOnSecurity">https://mastodon.social/@SwiftOnSecurity@infosec.exchange/109579438826193099</MASTODON></r>',
[],
function ($configurator)
{
$configurator->registeredVars['cacheDir'] = __DIR__ . '/../../.cache';
$configurator->MediaEmbed->add('mastodon');
}
],
[
'http://www.nbcsports.com/video/countdown-rio-olympics-what-makes-perfect-performance',
'<r><NBCSPORTS id="fTQA2MMyx9YO">http://www.nbcsports.com/video/countdown-rio-olympics-what-makes-perfect-performance</NBCSPORTS></r>',
Expand Down Expand Up @@ -2878,11 +2888,41 @@ function ($configurator)
],
[
'https://mastodon.social/@HackerNewsBot/100181134752056592',
'<r><MASTODON id="100181134752056592" name="HackerNewsBot">https://mastodon.social/@HackerNewsBot/100181134752056592</MASTODON></r>',
'<r><MASTODON host="mastodon.social" id="100181134752056592" name="HackerNewsBot">https://mastodon.social/@HackerNewsBot/100181134752056592</MASTODON></r>',
[],
function ($configurator)
{
$configurator->MediaEmbed->add('mastodon');
}
],
[
'https://infosec.exchange/@SwiftOnSecurity/109579438603578302',
'<t>https://infosec.exchange/@SwiftOnSecurity/109579438603578302</t>',
[],
function ($configurator)
{
$configurator->MediaEmbed->add('mastodon');
}
],
[
'https://infosec.exchange/@SwiftOnSecurity/109579438603578302',
'<r><MASTODON host="infosec.exchange" id="109579438603578302" name="SwiftOnSecurity">https://infosec.exchange/@SwiftOnSecurity/109579438603578302</MASTODON></r>',
[],
function ($configurator)
{
$siteConfig = $configurator->MediaEmbed->defaultSites['mastodon'];
$siteConfig['host'][] = 'infosec.exchange';
$configurator->MediaEmbed->add('mastodon', $siteConfig);
}
],
[
'https://infosec.exchange/@SwiftOnSecurity/109579438603578302',
'<r><MASTODON host="infosec.exchange" id="109579438603578302" name="SwiftOnSecurity">https://infosec.exchange/@SwiftOnSecurity/109579438603578302</MASTODON></r>',
[],
function ($configurator)
{
$configurator->MediaEmbed->add('mastodon');
$configurator->registeredVars['MediaEmbed.hosts']['infosec.exchange'] = 'mastodon';
}
],
[
Expand Down Expand Up @@ -4926,6 +4966,26 @@ function ($configurator)
$configurator->MediaEmbed->add('kickstarter');
}
],
[
'https://mastodon.social/@HackerNewsBot/100181134752056592',
'<iframe data-s9e-mediaembed="mastodon" allowfullscreen="" loading="lazy" onload="let c=new MessageChannel;c.port1.onmessage=e=&gt;this.style.height=e.data+\'px\';this.contentWindow.postMessage(\'s9e:init\',\'*\',[c.port2])" scrolling="no" style="border:0;height:300px;max-width:550px;width:100%" src="https://s9e.github.io/iframe/2/mastodon.min.html#HackerNewsBot/100181134752056592"></iframe>',
[],
function ($configurator)
{
$configurator->MediaEmbed->add('mastodon');
}
],
[
'https://infosec.exchange/@SwiftOnSecurity/109579438603578302',
'<iframe data-s9e-mediaembed="mastodon" allowfullscreen="" loading="lazy" onload="let c=new MessageChannel;c.port1.onmessage=e=&gt;this.style.height=e.data+\'px\';this.contentWindow.postMessage(\'s9e:init\',\'*\',[c.port2])" scrolling="no" style="border:0;height:300px;max-width:550px;width:100%" src="https://s9e.github.io/iframe/2/mastodon.min.html#SwiftOnSecurity@infosec.exchange/109579438603578302"></iframe>',
[],
function ($configurator)
{
$siteConfig = $configurator->MediaEmbed->defaultSites['mastodon'];
$siteConfig['host'][] = 'infosec.exchange';
$configurator->MediaEmbed->add('mastodon', $siteConfig);
}
],
[
'https://medium.com/@donnydonny/team-internet-is-about-to-win-net-neutrality-and-they-didnt-need-googles-help-e7e2cf9b8a95',
'<iframe data-s9e-mediaembed="medium" allowfullscreen="" loading="lazy" onload="let c=new MessageChannel;c.port1.onmessage=e=&gt;this.style.height=e.data+\'px\';this.contentWindow.postMessage(\'s9e:init\',\'*\',[c.port2])" scrolling="no" src="https://s9e.github.io/iframe/2/medium.min.html#e7e2cf9b8a95" style="border:0;height:316px;max-width:900px;width:100%"></iframe>',
Expand Down

0 comments on commit 9ad74ec

Please sign in to comment.