Skip to content

Commit 347d301

Browse files
authored
Merge pull request #164 from gRegorLove/issue157
rel=tag hentry/hreview backcompat
2 parents cf53cfd + c3db4cb commit 347d301

File tree

3 files changed

+188
-1
lines changed

3 files changed

+188
-1
lines changed

Mf2/Parser.php

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1344,6 +1344,32 @@ public function parseRelsAndAlternates() {
13441344
return array($rels, $rel_urls, $alternates);
13451345
}
13461346

1347+
/**
1348+
* Find rel=tag elements that don't have class=category and have an href.
1349+
* For each element, get the last non-empty URL segment. Append a <data>
1350+
* element with that value as the category. Uses the mf1 class 'category'
1351+
* which will then be upgraded to p-category during backcompat.
1352+
* @param DOMElement $el
1353+
*/
1354+
public function upgradeRelTagToCategory(DOMElement $el) {
1355+
$rel_tag = $this->xpath->query('.//a[contains(concat(" ",normalize-space(@rel)," ")," tag ") and not(contains(concat(" ", normalize-space(@class), " "), " category ")) and @href]', $el);
1356+
1357+
if ( $rel_tag->length ) {
1358+
foreach ( $rel_tag as $tempEl ) {
1359+
$path = trim(parse_url($tempEl->getAttribute('href'), PHP_URL_PATH), ' /');
1360+
$segments = explode('/', $path);
1361+
$value = array_pop($segments);
1362+
1363+
# build the <data> element
1364+
$dataEl = $tempEl->ownerDocument->createElement('data');
1365+
$dataEl->setAttribute('class', 'category');
1366+
$dataEl->setAttribute('value', $value);
1367+
1368+
# append as child of input element. this should ensure added element does get parsed inside e-*
1369+
$el->appendChild($dataEl);
1370+
}
1371+
}
1372+
}
13471373

13481374
/**
13491375
* Kicks off the parsing routine
@@ -1527,6 +1553,8 @@ public function backcompat(DOMElement $el, $context = '', $isParentMf2 = false)
15271553
switch ( $classname )
15281554
{
15291555
case 'hentry':
1556+
$this->upgradeRelTagToCategory($el);
1557+
15301558
$rel_bookmark = $this->xpath->query('.//a[contains(concat(" ",normalize-space(@rel)," ")," bookmark ") and @href]', $el);
15311559

15321560
if ( $rel_bookmark->length ) {
@@ -1573,6 +1601,8 @@ public function backcompat(DOMElement $el, $context = '', $isParentMf2 = false)
15731601
}
15741602
}
15751603
}
1604+
1605+
$this->upgradeRelTagToCategory($el);
15761606
break;
15771607

15781608
case 'vevent':
@@ -1993,6 +2023,9 @@ public function query($expression, $context = null) {
19932023
'description' => array(
19942024
'replace' => 'e-content'
19952025
),
2026+
'category' => array(
2027+
'replace' => 'p-category'
2028+
),
19962029
),
19972030
'hproduct' => array(
19982031
'fn' => array(

tests/Mf2/ClassicMicroformatsTest.php

Lines changed: 135 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,15 @@ public function testParsesFBerrimanClassicHEntry() {
102102
$result = $parser->parse();
103103
$e = $result['items'][0];
104104
$this->assertContains('h-entry', $e['type']);
105+
$this->assertArrayHasKey('category', $e['properties']);
106+
$this->assertCount(7, $e['properties']['category']);
107+
$this->assertContains('speaking', $e['properties']['category']);
108+
$this->assertContains('web-dev', $e['properties']['category']);
109+
$this->assertContains('conferences', $e['properties']['category']);
110+
$this->assertContains('front-trends', $e['properties']['category']);
111+
$this->assertContains('fronttrends', $e['properties']['category']);
112+
$this->assertContains('speaking', $e['properties']['category']);
113+
$this->assertContains('txjs', $e['properties']['category']);
105114
}
106115

107116
public function testParsesSnarfedOrgArticleCorrectly() {
@@ -566,8 +575,8 @@ public function testMixedMf2andMf1Case1() {
566575

567576
$this->assertCount(1, $result['items'][0]['properties']['author']);
568577
$this->assertCount(1, $result['items'][0]['properties']['author'][0]['type']);
569-
570578
$this->assertEquals('h-card', $result['items'][0]['properties']['author'][0]['type'][0]);
579+
$this->assertArrayNotHasKey('category', $result['items'][0]['properties']);
571580
}
572581

573582

@@ -837,5 +846,130 @@ public function testParsesHfeed() {
837846
$this->assertArrayHasKey('value', $output['items'][0]['children'][0]['properties']['author'][0]);
838847
}
839848

849+
/**
850+
* @see https://github.com/indieweb/php-mf2/issues/157
851+
* @see source: http://www.manton.org/2018/03/indieweb-generation-4-and-hosted-domains.html
852+
*/
853+
public function testHEntryRelTag() {
854+
$input = '<article id="post-6586" class="post-6586 post type-post status-publish format-standard hentry category-technology tag-domains tag-indiewebcamp tag-microblog tag-wordpress">
855+
<header class="entry-header">
856+
857+
<h1 class="entry-title">IndieWeb generation 4 and hosted domains</h1>
858+
</header><!-- .entry-header -->
859+
860+
<div class="entry-content">
861+
<p>Naturally because of the goals of Micro.blog, I see a lot of discussion about “owning your content”. It’s an important part of the mission for Micro.blog to take control back from closed, ad-supported social networks and instead embrace posting on our own blogs again.</p>
862+
<p>But what does it mean to own our content? Do we have to install WordPress or some home-grown blogging system for it to be considered true content ownership, where we have the source code and direct SFTP access to the server? No. If that’s our definition, then content ownership will be permanently reserved for programmers and technical folks who have hours to spend on server configuration.</p>
863+
<p><a href="https://indieweb.org/generations">IndieWebCamp has a generations chart</a> to illustrate the path from early adopters to mainstream users. Eli Mellen highlighted it <a href="https://eli.li/entry.php?id=20180318015703">in a recent post</a> about the need to bridge the gap between the technical aspects of IndieWeb tools and more approachable platforms. With Micro.blog specifically, the goal is “generation 4”, and I think we’re on track to get there.</p>
864+
<p>I want blogging to be as easy as tweeting. Anything short of that isn’t good enough for Micro.blog. You’ll notice when you use Twitter that they never ask you to SFTP into twitter.com to configure your account. They don’t ask you to install anything.</p>
865+
<p>More powerful software that you can endlessly customize will always have its place. It’s good to have a range of options, including open source to tinker with. That’s often where some of the best ideas start. But too often I see people get lost in the weeds of plugins and themes, lured in by the myth that you have to self-host with WordPress to be part of the IndieWeb.</p>
866+
<p>Owning your content isn’t about portable software. It’s about portable URLs and data. It’s about domain names.</p>
867+
<p>When you write and post photos at your own domain name, your content can outlive any one blogging platform. This month marked the 16th anniversary of blogging at manton.org, and in that time I’ve switched blogging platforms and hosting providers a few times. The posts and URLs can all be preserved through those changes because it’s my own domain name.</p>
868+
<p>I was disappointed when Medium announced they were <a href="http://web.archive.org/web/20180301231401/https://help.medium.com/hc/en-us/articles/115005579728-Get-started-with-custom-domains">discontinuing support for custom domain names</a>. I’m linking to the Internet Archive copy because Medium’s help page about this is no longer available. If “no custom domains” is still their policy, it’s a setback for the open web, and dooms Medium to the same dead-end as twitter.com/username URLs.</p>
869+
<p>If you can’t use your own domain name, you can’t own it. Your content will be forever stuck at those silo URLs, beholden to the whims of the algorithmic timeline and shifting priorities of the executive team.</p>
870+
<p>For hosted blogs on Micro.blog, we encourage everyone to map a custom domain to their content, and we throw in free SSL and preserve redirects for old posts on imported WordPress content. There’s more we can do.</p>
871+
<p>I’m working on the next version of the macOS app for Micro.blog now, which features multiple accounts and even multiple blogs under the same account. Here’s a screenshot of the settings screen:</p>
872+
<p><img src="https://manton.org/images/2018/prefs_accounts.png" width="512" height="415" alt="Mac screenshot" scale="0"></p>
873+
<p>The goal with Micro.blog is not to be a stop-gap hosting provider, with truly “serious” users eventually moving on to something else (although we make that easy). We want Micro.blog hosting to be the best platform for owning your content and participating in the Micro.blog and IndieWeb communities.</p>
874+
</div><!-- .entry-content -->
875+
876+
<footer class="entry-meta">
877+
This entry was posted in <a href="http://www.manton.org/category/technology" rel="category tag">Technology</a> and tagged <a href="http://www.manton.org/tag/domains" rel="tag">domains</a>, <a href="http://www.manton.org/tag/indiewebcamp" rel="tag">indiewebcamp</a>, <a href="http://www.manton.org/tag/microblog" rel="tag">microblog</a>, <a href="http://www.manton.org/tag/wordpress" rel="tag">wordpress</a> on <a href="http://www.manton.org/2018/03/indieweb-generation-4-and-hosted-domains.html" title="9:24 am" rel="bookmark"><time class="entry-date" datetime="2018-03-23T09:24:36+00:00">2018/03/23</time></a><span class="by-author"> by <span class="author vcard"><a class="url fn n" href="http://www.manton.org/author/manton" title="View all posts by manton" rel="author">manton</a></span></span>. </footer><!-- .entry-meta -->
878+
</article>';
879+
$parser = new Parser($input);
880+
$output = $parser->parse();
881+
882+
$this->assertArrayHasKey('category', $output['items'][0]['properties']);
883+
$this->assertCount(5, $output['items'][0]['properties']['category']);
884+
$this->assertContains('technology', $output['items'][0]['properties']['category']);
885+
$this->assertContains('domains', $output['items'][0]['properties']['category']);
886+
$this->assertContains('indiewebcamp', $output['items'][0]['properties']['category']);
887+
$this->assertContains('microblog', $output['items'][0]['properties']['category']);
888+
$this->assertContains('wordpress', $output['items'][0]['properties']['category']);
889+
}
890+
891+
public function testHEntryRelTagInContent() {
892+
$input = <<< END
893+
<article class="hentry">
894+
<div class="entry-content">
895+
Entry content should not include the generated <code>data</code> element for rel tag backcompat <a href="/tag/test" rel="tag">test</a>
896+
</div>
897+
</article>
898+
END;
899+
900+
$parser = new Parser($input);
901+
$output = $parser->parse();
902+
$item = $output['items'][0];
903+
904+
$this->assertEquals(['test'], $item['properties']['category']);
905+
$this->assertEquals('Entry content should not include the generated data element for rel tag backcompat test', $item['properties']['content'][0]['value']);
906+
$this->assertEquals('Entry content should not include the generated <code>data</code> element for rel tag backcompat <a href="/tag/test" rel="tag">test</a>', $item['properties']['content'][0]['html']);
907+
}
908+
909+
/**
910+
* @see https://github.com/indieweb/php-mf2/issues/157
911+
* @see source: http://jg.typepad.com/ciel/2006/02/daniel_bouluds_.html
912+
*/
913+
public function testHReviewRelTag() {
914+
$input = '<div class="hreview">
915+
<span class="version" style="display:none">0.2</span>
916+
<h2 class="summary">
917+
Divine Brunch!
918+
</h2>
919+
<abbr class="dtreviewed" title="20060219T1919-0800">
920+
Feb 19, 2006
921+
</abbr>
922+
by <span class="reviewer fn"><a href="http://jg.typepad.com/ciel">Joan Gelfand</a></span>
923+
<span class="type" style="display:none">business</span>
924+
<div class="item vcard">
925+
<a href="http://www.garconsf.com" class="url fn">
926+
Garçon
927+
</a>
928+
<div class="adr">
929+
<div class="street-address">1101 Valencia Street</div>
930+
<span class="locality">San Francisco</span>,
931+
<span class="region">CA</span>
932+
</div>
933+
</div>
934+
<blockquote class="description"><p>
935+
<abbr class="rating" title="3">★★★ </abbr>
936+
Best Benedicts!
937+
Two perfectly poached eggs and a thin slice of tasty, French ham rest on a circle of warm brioche. Drizzled on top is a light, slightly tangy sauce. Seamless! The sophisticated room and great wine list added to the whole experience - Super!</p></blockquote>
938+
<p style="text-align:right;font-size:10px;">Technorati Tags: <a href="http://www.technorati.com/tag/Garcon" rel="tag">Garcon</a>, <a href="http://www.technorati.com/tag/Garçon" rel="tag">Garçon</a></p>
939+
</div>';
940+
$parser = new Parser($input);
941+
$output = $parser->parse();
942+
943+
$this->assertArrayHasKey('category', $output['items'][0]['properties']);
944+
$this->assertCount(2, $output['items'][0]['properties']['category']);
945+
$this->assertContains('Garcon', $output['items'][0]['properties']['category']);
946+
$this->assertContains('Garçon', $output['items'][0]['properties']['category']);
947+
}
948+
949+
/**
950+
* Should return the last non-empty URL segment
951+
* @see https://github.com/indieweb/php-mf2/issues/157
952+
*/
953+
public function testRelTagTrailingSlash() {
954+
$input = '<div class="hentry">
955+
<a rel="tag" href="/tags/testing/">Testing</a>,
956+
<a rel="tag" href="/tags/microformats">Microformats</a>
957+
</div>
958+
959+
<div class="hreview">
960+
<a rel="tag" href="/tags/phpmf2/">php-mf2</a>,
961+
<a rel="tag" href="/tags/mf2py">mf2py</a>
962+
</div>
963+
';
964+
$parser = new Parser($input);
965+
$output = $parser->parse();
966+
967+
$this->assertArrayHasKey('category', $output['items'][0]['properties']);
968+
$this->assertContains('testing', $output['items'][0]['properties']['category']);
969+
$this->assertContains('microformats', $output['items'][0]['properties']['category']);
970+
$this->assertArrayHasKey('category', $output['items'][1]['properties']);
971+
$this->assertContains('phpmf2', $output['items'][1]['properties']['category']);
972+
$this->assertContains('mf2py', $output['items'][1]['properties']['category']);
973+
}
840974
}
841975

tests/Mf2/CombinedMicroformatsTest.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -397,5 +397,25 @@ public function testNestedValueDTProperty() {
397397
$this->assertEquals('1997-12-12', $output['items'][0]['properties']['acme'][0]['value']);
398398
}
399399

400+
/**
401+
* rel=tag should not be upgraded within microformats2
402+
* @see https://github.com/indieweb/php-mf2/issues/157
403+
*/
404+
public function testMf2DoesNotParseRelTag() {
405+
$input = '<div class="h-entry">
406+
<a rel="tag" href="/tags/tests">Tests</a>
407+
</div>
408+
409+
<div class="h-review">
410+
<a rel="tag" href="/tags/reviews">Reviews</a>
411+
</div>
412+
';
413+
$parser = new Parser($input);
414+
$output = $parser->parse();
415+
416+
$this->assertArrayNotHasKey('category', $output['items'][0]['properties']);
417+
$this->assertArrayNotHasKey('category', $output['items'][1]['properties']);
418+
}
419+
400420
}
401421

0 commit comments

Comments
 (0)