Skip to content

Commit 38b2f2b

Browse files
committed
Add failing test and fix for #157 (hentry)
Update existing tests to verify rel-tag upgrade
1 parent e044330 commit 38b2f2b

File tree

2 files changed

+80
-1
lines changed

2 files changed

+80
-1
lines changed

Mf2/Parser.php

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1319,6 +1319,32 @@ public function parseRelsAndAlternates() {
13191319
return array($rels, $rel_urls, $alternates);
13201320
}
13211321

1322+
/**
1323+
* Find rel=tag elements that don't have class=category and have an href.
1324+
* For each element, get the last non-empty URL segment. Append a <data>
1325+
* element with that value as the category. Uses the mf1 class 'category'
1326+
* which will then be upgraded to p-category during backcompat.
1327+
* @param DOMElement $el
1328+
*/
1329+
public function upgradeRelTagToCategory(DOMElement $el) {
1330+
$rel_tag = $this->xpath->query('.//a[contains(concat(" ",normalize-space(@rel)," ")," tag ") and not(contains(concat(" ", normalize-space(@class), " "), " category ")) and @href]', $el);
1331+
1332+
if ( $rel_tag->length ) {
1333+
foreach ( $rel_tag as $tempEl ) {
1334+
$path = trim(parse_url($tempEl->getAttribute('href'), PHP_URL_PATH), ' /');
1335+
$segments = explode('/', $path);
1336+
$value = array_pop($segments);
1337+
1338+
# build the <data> element
1339+
$dataEl = $tempEl->ownerDocument->createElement('data');
1340+
$dataEl->setAttribute('class', 'category');
1341+
$dataEl->setAttribute('value', $value);
1342+
1343+
# append before the current element
1344+
$tempEl->parentNode->insertBefore($dataEl, $tempEl);
1345+
}
1346+
}
1347+
}
13221348

13231349
/**
13241350
* Kicks off the parsing routine
@@ -1502,6 +1528,8 @@ public function backcompat(DOMElement $el, $context = '', $isParentMf2 = false)
15021528
switch ( $classname )
15031529
{
15041530
case 'hentry':
1531+
$this->upgradeRelTagToCategory($el);
1532+
15051533
$rel_bookmark = $this->xpath->query('.//a[contains(concat(" ",normalize-space(@rel)," ")," bookmark ") and @href]', $el);
15061534

15071535
if ( $rel_bookmark->length ) {

tests/Mf2/ClassicMicroformatsTest.php

Lines changed: 52 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,47 @@ 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+
840891
}
841892

0 commit comments

Comments
 (0)