Skip to content

Commit

Permalink
fix and test tag im/export
Browse files Browse the repository at this point in the history
  • Loading branch information
fr0tt committed Aug 11, 2023
1 parent 081db3b commit 2d7c995
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 9 deletions.
26 changes: 21 additions & 5 deletions app/Helpers/NetscapeBookmarkDecoder.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

use App\Models\Collection;
use App\Services\PostService;
use App\Services\TagService;
use Illuminate\Support\Str;

/*
* This file is heavily inspired by:
Expand Down Expand Up @@ -38,6 +40,7 @@ class NetscapeBookmarkDecoder
{
protected $items;
private PostService $postService;
private TagService $tagService;
private $userId;

const TRUE_PATTERN = 'y|yes|on|checked|ok|1|true|array|\+|okay|yes|t|one';
Expand All @@ -50,6 +53,7 @@ class NetscapeBookmarkDecoder
public function __construct(int $userId)
{
$this->postService = new PostService();
$this->tagService = new TagService();
$this->userId = $userId;
}

Expand Down Expand Up @@ -119,10 +123,11 @@ public function parseString(string $bookmarkString, int $collectionId): array
$tags = array();

if (preg_match('/(tags?|labels?|folders?)="(.*?)"/i', $line, $m7)) {
$tags = array_merge(
$tags,
explode(' ', strtr($m7[2], ',', ' '))
);
if (Str::contains($m7[2], ',')) {
$tags = preg_split('/\s*,\s*/', $m7[2]);
} else {
$tags = preg_split('/\s+/', $m7[2]);
}
}
$this->items[$i]['tags'] = $tags;

Expand Down Expand Up @@ -268,9 +273,20 @@ private function createPost($title, $collectionId, $url, $description, $tags = n
$url,
$collectionId,
$description,
count($tags) > 0 ? $tags : null,
$this->createTags($tags),
$this->userId,
);
}

private function createTags(array $tagNames): array|null
{
if (count($tagNames) === 0) {
return null;
}
return array_map(function ($tagName) {
$tag = $this->tagService->saveTag($tagName, $this->userId);
return $tag->id;
}, $tagNames);
}

}
4 changes: 3 additions & 1 deletion app/Helpers/NetscapeBookmarkEncoder.php
Original file line number Diff line number Diff line change
Expand Up @@ -107,12 +107,14 @@ private function getPosts(int $collectionId = null, int $level): string
$createdAt = strtotime($post->created_at);
$updatedAt = strtotime($post->updated_at);
$description = trim($post->description);
$tags = $post->tags->implode('name', ', ');

$content .= "{$this->tabs($level)}" .
"<DT><A HREF=\"{$post->url}\" " .
"ADD_DATE=\"{$createdAt}\" " .
"LAST_MODIFIED=\"{$updatedAt}\" " .
"ICON=\"{$this->icon($post->base_url)}\" " .
"TAGS=\"{$post->tags->join(', ')}\"" .
"TAGS=\"{$tags}\"" .
">{$post->title}</A>\n" .
"{$this->tabs($level)}" .
"<DD>{$description}\n";
Expand Down
12 changes: 10 additions & 2 deletions tests/Feature/ImAndExportTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -84,10 +84,11 @@ public function testImportBookmarks()
'content' => 'https://www.mozilla.org/en-US/',
'collection_id' => $personal->id
])->exists());
$this->assertTrue(Post::where([
$post = Post::where([
'content' => 'https://www.bbc.com/',
'collection_id' => $collectionA->id
])->exists());
]);
$this->assertTrue($post->exists());
$this->assertTrue(Post::where([
'content' => 'https://www.theguardian.com/international',
'collection_id' => $collectionA1->id
Expand All @@ -105,6 +106,11 @@ public function testImportBookmarks()
'collection_id' => $collectionB->id
])->exists());

$post = $post->with('tags')->first();
$this->assertEquals(2, count($post->tags));
$this->assertEquals('british', $post->tags[0]->name);
$this->assertEquals('tea time', $post->tags[1]->name);

}

public function testExportBookmark()
Expand Down Expand Up @@ -181,6 +187,8 @@ public function testExportBookmarks()
$exported = file_get_contents($response->getFile());
$original = file_get_contents(base_path('tests/bookmarks.html'));

$this->assertStringContainsString('TAGS="british, tea time">BBC', $exported);

$original = strip_tags($original, ['dl', 'p', 'dt', 'h3', 'a', 'dd']);
// $original = preg_replace('/(^.*(?:\n.*))*?<DL>/m', '<DL>', $original, 1);
// remove attributes
Expand Down
2 changes: 1 addition & 1 deletion tests/bookmarks.html
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ <H1>Bookmarks</H1>
<DT><A HREF="https://www.mozilla.org/en-US/" ADD_DATE="1691502756" LAST_MODIFIED="1691502758" ICON_URI="https://www.mozilla.org/media/img/favicons/mozilla/favicon.d25d81d39065.ico" ICON="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAYAAAAf8/9hAAABm0lEQVQ4T92Tv6sxUByHH6GwyXCRDPwFZDH4EYOS7XYpihSLH6PNcOtuWE2kdA1IDCiu4aakjBYLg9lCJJbbfV9H73uH953u9r7fOnVO59PzPefpHAnw+XN8uyT/CcBgMODz+ZDJZGw2Gz4+PnC5XOx2O3q9nvDz+PiIRqNhOp0ym81E5lbCgdvtptvtolar2W63nM9n9Hq9WA+HQy6XC3a7nYeHB/b7PZFIhMlk8gVQKpWk02mKxSLr9ZpYLIZCoaDRaKDT6Xh5eaFWq5HP50kkEpRKJXK53BfgNgsGg7RaLTqdDqFQCJVKxdvbGxaLBa/Xy3w+Jx6PC1C5XCaTyfwd0Gw2CYfD3E41Ho+xWq14PB4WiwXRaJR6vf4nQC6Xk0wmxUa/3yeVSnE8HsX9bTYbT09PDAaD34AbJJvNcjqd7hIdDgftdhutVsv1emW5XIpwpVLB6XSyWq3w+/3ccq+vr0Lq8/MzhULhDjCbzcKsVCpFIpFwOBxEMBAIYDKZRKdqtYrRaBSubrn393dGo9Ed8O2P8Osd/NuAHyMUsgHs4NZPAAAAAElFTkSuQmCC">Internet for people, not profit — Mozilla (US)</A>
<DT><H3 ADD_DATE="1684622800" LAST_MODIFIED="1691623785">CollectionA</H3>
<DL><p>
<DT><A HREF="https://www.bbc.com/" ADD_DATE="1691502598" LAST_MODIFIED="1691623781" ICON_URI="https://www.bbc.com/favicon.ico" ICON="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAYAAAAf8/9hAAAAMklEQVQ4T2P8DwQMFADGUQMYqBMGjIyMKPEAihiixUCxQLRibBYNDgMoSIhUioUR7gIA9Yhj0e5NJ9UAAAAASUVORK5CYII=">BBC - Homepage</A>
<DT><A HREF="https://www.bbc.com/" ADD_DATE="1691502598" LAST_MODIFIED="1691623781" ICON_URI="https://www.bbc.com/favicon.ico" ICON="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAYAAAAf8/9hAAAAMklEQVQ4T2P8DwQMFADGUQMYqBMGjIyMKPEAihiixUCxQLRibBYNDgMoSIhUioUR7gIA9Yhj0e5NJ9UAAAAASUVORK5CYII=" TAGS="british,tea time">BBC - Homepage</A>
<DT><H3 ADD_DATE="1684622812" LAST_MODIFIED="1691623785">SubCollectionA1</H3>
<DL><p>
<DT><A HREF="https://www.theguardian.com/international" ADD_DATE="1691502477" LAST_MODIFIED="1691623785" ICON_URI="https://static.guim.co.uk/images/favicon-32x32.ico" ICON="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAACAAAAAgCAYAAABzenr0AAADK0lEQVRYR7WXTWgTQRSA3yRGPAkq2II/bQ+VRDx50pYi9aKgaAWPtSZbkR5EEATbCoYo2AiKIAqN6EZLvRX8QUEFLVKiOYmC0KCUWvEgQi+ehJis88aMTmbnTTZhHQiBnZn3vnl/84YBAOO/QCMad44CeDsjAHvFBsY2i3/P+4J/VYCn/OObSsm9G0hgTbkVYFU82VEGNiaUSoUBpFc972YFvAko3VmyLUflJEA0npqMMHY8gD5yiedB5lfJzVALzAD81DFgr2wn3tLVDhva1sCm9rUw9aBgZ+QuKoO3y2QNP4BFef+OBJw7cRC6O9qEwhv3XkDx3QLMFucBgV5OnYHnhQ+QzT2Bj4vf6qEIiHoAQjkKz11IQu/2biH0+/IP2D10yadkbGQ/ZE4eEmumH732gxgg6gBi8dRn3eyqUHmkY+MuafbZ6VE7KEKU8p1S1l+AFXEnzRikVbuZlOPpN/adIn0uXbF+3WqxxgQrMqSUH8H5PwBoehZZVKUODfTCrYuOT5Ht9HIxxsoz9zQJgBNlr9qFQSkA9HTTT6FSbNt31h9gBntI61HA0goCIJZweBH7N9zsMAwe6PGJLbz9BP2DWdL8+gTGQ35mzhwvtVjgp3eSEQZ5dfPXuasgfah+x8h2Rm8HBkBL4vClZE1C1YMUmj+nVjvVf7qm9LX7MDH5ODBAo4XoBsZTb0lNPVPkS0FhA+Al5gOg/E+lVKNTWucFQMLx1EVqIdE3B0nBZoFCAcCa0WhQF1YoLsDAPTLQQ6auLRVDAcDTU9ljdRvGQFhp2AqASMOwClFrALwQ8Z6vs6J1P1QtsJXiVgDK8y4vwobLCH1KleOVW4eNAd8sQN1lhJ1vRbuOKStQQdUsQJRfxz/ldYxHMnXAJgjKDc0AqJ1ySy3ZHueyaETVQQH41lItGQoTrjC043qDYmpKdQC01PnrD+tBufIob8/R9BLe15ZTELgBS27qcJ9oOhFi/MoMFN8viPteAhgV42aDcvxsfJjYICQ5KsQR9GGin5y0gOpXU6fc6NLR59UO2LTX+jaUcSEep828Ebm58aUc449T1d8tAaib/sfz/DehMApT4tLRIgAAAABJRU5ErkJggg==">News, sport and opinion from the Guardian&#39;s global edition | The Guardian</A>
Expand Down

0 comments on commit 2d7c995

Please sign in to comment.