{"action":"created","comment":{"url":"https://api.github.com/repos/shaarli/netscape-bookmark-parser/pulls/comments/63291118","id":63291118,"diff_hunk":"@@ -0,0 +1,222 @@\n+<?php\n+\n+/**\n+ * Generic Netscape bookmark parser\n+ */\n+class NetscapeBookmarkParser\n+{\n+    protected $defaultTag = '';\n+    protected $defaultPub = false;\n+    protected $items = array();\n+\n+    const TRUE_PATTERN = 'y|yes|on|checked|ok|1|true|array|\\+|okay|yes|t|one';\n+    const FALSE_PATTERN = 'n|no|off|empty|null|false|nil|0|-|exit|die|neg|f|zero|void';\n+\n+    /**\n+     * Instantiates a new NetscapeBookmarkParser\n+     *\n+     * @param string $defaultTag Link tag if missing\n+     * @param mixed  $defaultPub Link publication status if missing\n+     *                           - 'true' => public\n+     *                           - 'false' => private)\n+     */\n+    public function __construct($defaultTag=null, $defaultPub=false)\n+    {\n+        if ($defaultTag) {\n+            $this->defaultTag = $defaultTag;\n+        } else {\n+            $this->defaultTag = 'imported-'.date(\"Ymd\");\n+        }\n+        if ($defaultPub) {\n+            $this->defaultPub = $defaultPub;\n+        }\n+    }\n+\n+    /**\n+     * Parses a Netscape bookmark file\n+     *\n+     * @param string $filename Bookmark file to parse\n+     *\n+     * @return array An associative array containing parsed links\n+     */\n+    public function parseFile($filename)\n+    {\n+        return $this->parseString(file_get_contents($filename));\n+    }\n+\n+    /**\n+     * Parses a string containing Netscape-formatted bookmarks\n+     *\n+     * Output format:\n+     *\n+     *     Array\n+     *     (\n+     *         [0] => Array\n+     *             (\n+     *                 [note]  => Some comments about this link\n+     *                 [pub]   => 1\n+     *                 [tags]  => a list of tags\n+     *                 [time]  => 1459371397\n+     *                 [title] => Some page\n+     *                 [uri]   => http://domain.tld:5678/some-page.html\n+     *             )\n+     *         [1] => Array\n+     *             (\n+     *                 ...\n+     *             )\n+     *     )\n+     *\n+     * @param string $bookmarkString String containing Netscape bookmarks\n+     *\n+     * @return array An associative array containing parsed links\n+     */\n+    public function parseString($bookmarkString) {\n+        $i = 0;\n+        $next = false;\n+        $currentTag = $this->defaultTag;\n+\n+        $lines = explode(\"\\n\", $this->sanitizeString($bookmarkString));\n+\n+        foreach ($lines as $line_no => $line) {\n+            if (preg_match('/^<h\\d(.*?)>(.*?)<\\/h\\d>/i', $line, $m1)) {\n+                // a tag is matched: set its value as current\n+                $currentTag = $m1[2];\n+                continue;\n+\n+            } elseif (preg_match('/^<\\/DL>/i', $line)) {\n+                // <DL> matched: stop using tag\n+                $currentTag = $this->defaultTag;\n+            }\n+\n+            if (preg_match('/<a/i', $line, $m2)) {\n+                if ($currentTag) {\n+                    $this->items[$i]['tags'] = $currentTag;\n+                }\n+\n+                if (preg_match('/href=\"(.*?)\"/i', $line, $m3)) {\n+                    $this->items[$i]['uri'] = $m3[1];\n+                } else {\n+                    $this->items[$i]['uri'] = '';\n+                }\n+\n+                if (preg_match('/<a(.*?)>(.*?)<\\/a>/i', $line, $m4)) {\n+                    $this->items[$i]['title'] = $m4[2];\n+                } else {\n+                    $this->items[$i]['title'] = 'untitled';\n+                }\n+\n+                if (preg_match('/note=\"(.*?)\"<\\/a>/i', $line, $m5)) {\n+                     $this->items[$i]['note'] = $m5[1];\n+                } elseif (preg_match('/<dd>(.*?)<\\//i', $line, $m6)) {\n+                     $this->items[$i]['note'] = str_replace('<br>', \"\\n\", $m6[1]);\n+                } else {\n+                    $this->items[$i]['note'] = '';\n+                }\n+\n+                if (preg_match('/(tags?|labels?|folders?)=\"(.*?)\"/i', $line, $m7)) {\n+                     $this->items[$i]['tags'] = strtr($m7[2], ',', ' ');\n+                } else {\n+                    $this->items[$i]['tags'] = $currentTag;\n+                }","path":"NetscapeBookmarkParser.php","position":120,"original_position":120,"commit_id":"b03723898db82c5fc8c11c1b492f360f0552b332","original_commit_id":"b03723898db82c5fc8c11c1b492f360f0552b332","user":{"login":"ArthurHoaro","id":1962678,"avatar_url":"https://avatars.githubusercontent.com/u/1962678?v=3","gravatar_id":"","url":"https://api.github.com/users/ArthurHoaro","html_url":"https://github.com/ArthurHoaro","followers_url":"https://api.github.com/users/ArthurHoaro/followers","following_url":"https://api.github.com/users/ArthurHoaro/following{/other_user}","gists_url":"https://api.github.com/users/ArthurHoaro/gists{/gist_id}","starred_url":"https://api.github.com/users/ArthurHoaro/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/ArthurHoaro/subscriptions","organizations_url":"https://api.github.com/users/ArthurHoaro/orgs","repos_url":"https://api.github.com/users/ArthurHoaro/repos","events_url":"https://api.github.com/users/ArthurHoaro/events{/privacy}","received_events_url":"https://api.github.com/users/ArthurHoaro/received_events","type":"User","site_admin":false},"body":"If I ask for a default tag, it might be a good idea to append it either it finds existing tags or not?","created_at":"2016-05-15T12:48:10Z","updated_at":"2016-05-15T12:48:10Z","html_url":"https://github.com/shaarli/netscape-bookmark-parser/pull/14#discussion_r63291118","pull_request_url":"https://api.github.com/repos/shaarli/netscape-bookmark-parser/pulls/14","_links":{"self":{"href":"https://api.github.com/repos/shaarli/netscape-bookmark-parser/pulls/comments/63291118"},"html":{"href":"https://github.com/shaarli/netscape-bookmark-parser/pull/14#discussion_r63291118"},"pull_request":{"href":"https://api.github.com/repos/shaarli/netscape-bookmark-parser/pulls/14"}}},"pull_request":{"url":"https://api.github.com/repos/shaarli/netscape-bookmark-parser/pulls/14","id":70119482,"html_url":"https://github.com/shaarli/netscape-bookmark-parser/pull/14","diff_url":"https://github.com/shaarli/netscape-bookmark-parser/pull/14.diff","patch_url":"https://github.com/shaarli/netscape-bookmark-parser/pull/14.patch","issue_url":"https://api.github.com/repos/shaarli/netscape-bookmark-parser/issues/14","number":14,"state":"open","locked":false,"title":"Refactor the parser in a single NetscapeBookmarkParser class","user":{"login":"virtualtam","id":1615162,"avatar_url":"https://avatars.githubusercontent.com/u/1615162?v=3","gravatar_id":"","url":"https://api.github.com/users/virtualtam","html_url":"https://github.com/virtualtam","followers_url":"https://api.github.com/users/virtualtam/followers","following_url":"https://api.github.com/users/virtualtam/following{/other_user}","gists_url":"https://api.github.com/users/virtualtam/gists{/gist_id}","starred_url":"https://api.github.com/users/virtualtam/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/virtualtam/subscriptions","organizations_url":"https://api.github.com/users/virtualtam/orgs","repos_url":"https://api.github.com/users/virtualtam/repos","events_url":"https://api.github.com/users/virtualtam/events{/privacy}","received_events_url":"https://api.github.com/users/virtualtam/received_events","type":"User","site_admin":false},"body":"Modifications:\r\n- update Composer autoload\r\n- encapsulate all methods in a single class\r\n- update tests\r\n- update README and example","created_at":"2016-05-15T12:05:23Z","updated_at":"2016-05-15T12:48:10Z","closed_at":null,"merged_at":null,"merge_commit_sha":"31be60be68f90c9dc143e3f7134780dc50fc47a6","assignee":{"login":"virtualtam","id":1615162,"avatar_url":"https://avatars.githubusercontent.com/u/1615162?v=3","gravatar_id":"","url":"https://api.github.com/users/virtualtam","html_url":"https://github.com/virtualtam","followers_url":"https://api.github.com/users/virtualtam/followers","following_url":"https://api.github.com/users/virtualtam/following{/other_user}","gists_url":"https://api.github.com/users/virtualtam/gists{/gist_id}","starred_url":"https://api.github.com/users/virtualtam/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/virtualtam/subscriptions","organizations_url":"https://api.github.com/users/virtualtam/orgs","repos_url":"https://api.github.com/users/virtualtam/repos","events_url":"https://api.github.com/users/virtualtam/events{/privacy}","received_events_url":"https://api.github.com/users/virtualtam/received_events","type":"User","site_admin":false},"milestone":null,"commits_url":"https://api.github.com/repos/shaarli/netscape-bookmark-parser/pulls/14/commits","review_comments_url":"https://api.github.com/repos/shaarli/netscape-bookmark-parser/pulls/14/comments","review_comment_url":"https://api.github.com/repos/shaarli/netscape-bookmark-parser/pulls/comments{/number}","comments_url":"https://api.github.com/repos/shaarli/netscape-bookmark-parser/issues/14/comments","statuses_url":"https://api.github.com/repos/shaarli/netscape-bookmark-parser/statuses/b03723898db82c5fc8c11c1b492f360f0552b332","head":{"label":"shaarli:refactor/class","ref":"refactor/class","sha":"b03723898db82c5fc8c11c1b492f360f0552b332","user":{"login":"shaarli","id":8275533,"avatar_url":"https://avatars.githubusercontent.com/u/8275533?v=3","gravatar_id":"","url":"https://api.github.com/users/shaarli","html_url":"https://github.com/shaarli","followers_url":"https://api.github.com/users/shaarli/followers","following_url":"https://api.github.com/users/shaarli/following{/other_user}","gists_url":"https://api.github.com/users/shaarli/gists{/gist_id}","starred_url":"https://api.github.com/users/shaarli/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/shaarli/subscriptions","organizations_url":"https://api.github.com/users/shaarli/orgs","repos_url":"https://api.github.com/users/shaarli/repos","events_url":"https://api.github.com/users/shaarli/events{/privacy}","received_events_url":"https://api.github.com/users/shaarli/received_events","type":"Organization","site_admin":false},"repo":{"id":52473523,"name":"netscape-bookmark-parser","full_name":"shaarli/netscape-bookmark-parser","owner":{"login":"shaarli","id":8275533,"avatar_url":"https://avatars.githubusercontent.com/u/8275533?v=3","gravatar_id":"","url":"https://api.github.com/users/shaarli","html_url":"https://github.com/shaarli","followers_url":"https://api.github.com/users/shaarli/followers","following_url":"https://api.github.com/users/shaarli/following{/other_user}","gists_url":"https://api.github.com/users/shaarli/gists{/gist_id}","starred_url":"https://api.github.com/users/shaarli/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/shaarli/subscriptions","organizations_url":"https://api.github.com/users/shaarli/orgs","repos_url":"https://api.github.com/users/shaarli/repos","events_url":"https://api.github.com/users/shaarli/events{/privacy}","received_events_url":"https://api.github.com/users/shaarli/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/shaarli/netscape-bookmark-parser","description":"PHP library to parse Netscape format bookmark files","fork":true,"url":"https://api.github.com/repos/shaarli/netscape-bookmark-parser","forks_url":"https://api.github.com/repos/shaarli/netscape-bookmark-parser/forks","keys_url":"https://api.github.com/repos/shaarli/netscape-bookmark-parser/keys{/key_id}","collaborators_url":"https://api.github.com/repos/shaarli/netscape-bookmark-parser/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/shaarli/netscape-bookmark-parser/teams","hooks_url":"https://api.github.com/repos/shaarli/netscape-bookmark-parser/hooks","issue_events_url":"https://api.github.com/repos/shaarli/netscape-bookmark-parser/issues/events{/number}","events_url":"https://api.github.com/repos/shaarli/netscape-bookmark-parser/events","assignees_url":"https://api.github.com/repos/shaarli/netscape-bookmark-parser/assignees{/user}","branches_url":"https://api.github.com/repos/shaarli/netscape-bookmark-parser/branches{/branch}","tags_url":"https://api.github.com/repos/shaarli/netscape-bookmark-parser/tags","blobs_url":"https://api.github.com/repos/shaarli/netscape-bookmark-parser/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/shaarli/netscape-bookmark-parser/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/shaarli/netscape-bookmark-parser/git/refs{/sha}","trees_url":"https://api.github.com/repos/shaarli/netscape-bookmark-parser/git/trees{/sha}","statuses_url":"https://api.github.com/repos/shaarli/netscape-bookmark-parser/statuses/{sha}","languages_url":"https://api.github.com/repos/shaarli/netscape-bookmark-parser/languages","stargazers_url":"https://api.github.com/repos/shaarli/netscape-bookmark-parser/stargazers","contributors_url":"https://api.github.com/repos/shaarli/netscape-bookmark-parser/contributors","subscribers_url":"https://api.github.com/repos/shaarli/netscape-bookmark-parser/subscribers","subscription_url":"https://api.github.com/repos/shaarli/netscape-bookmark-parser/subscription","commits_url":"https://api.github.com/repos/shaarli/netscape-bookmark-parser/commits{/sha}","git_commits_url":"https://api.github.com/repos/shaarli/netscape-bookmark-parser/git/commits{/sha}","comments_url":"https://api.github.com/repos/shaarli/netscape-bookmark-parser/comments{/number}","issue_comment_url":"https://api.github.com/repos/shaarli/netscape-bookmark-parser/issues/comments{/number}","contents_url":"https://api.github.com/repos/shaarli/netscape-bookmark-parser/contents/{+path}","compare_url":"https://api.github.com/repos/shaarli/netscape-bookmark-parser/compare/{base}...{head}","merges_url":"https://api.github.com/repos/shaarli/netscape-bookmark-parser/merges","archive_url":"https://api.github.com/repos/shaarli/netscape-bookmark-parser/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/shaarli/netscape-bookmark-parser/downloads","issues_url":"https://api.github.com/repos/shaarli/netscape-bookmark-parser/issues{/number}","pulls_url":"https://api.github.com/repos/shaarli/netscape-bookmark-parser/pulls{/number}","milestones_url":"https://api.github.com/repos/shaarli/netscape-bookmark-parser/milestones{/number}","notifications_url":"https://api.github.com/repos/shaarli/netscape-bookmark-parser/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/shaarli/netscape-bookmark-parser/labels{/name}","releases_url":"https://api.github.com/repos/shaarli/netscape-bookmark-parser/releases{/id}","deployments_url":"https://api.github.com/repos/shaarli/netscape-bookmark-parser/deployments","created_at":"2016-02-24T20:51:44Z","updated_at":"2016-05-15T09:11:04Z","pushed_at":"2016-05-15T12:07:42Z","git_url":"git://github.com/shaarli/netscape-bookmark-parser.git","ssh_url":"git@github.com:shaarli/netscape-bookmark-parser.git","clone_url":"https://github.com/shaarli/netscape-bookmark-parser.git","svn_url":"https://github.com/shaarli/netscape-bookmark-parser","homepage":"https://packagist.org/packages/kafene/netscape-bookmark-parser","size":39,"stargazers_count":0,"watchers_count":0,"language":"PHP","has_issues":true,"has_downloads":true,"has_wiki":false,"has_pages":false,"forks_count":1,"mirror_url":null,"open_issues_count":1,"forks":1,"open_issues":1,"watchers":0,"default_branch":"shaarli-devel"}},"base":{"label":"shaarli:shaarli-devel","ref":"shaarli-devel","sha":"24a3c6e379708561f2ffc7ff067c334e5d11e12b","user":{"login":"shaarli","id":8275533,"avatar_url":"https://avatars.githubusercontent.com/u/8275533?v=3","gravatar_id":"","url":"https://api.github.com/users/shaarli","html_url":"https://github.com/shaarli","followers_url":"https://api.github.com/users/shaarli/followers","following_url":"https://api.github.com/users/shaarli/following{/other_user}","gists_url":"https://api.github.com/users/shaarli/gists{/gist_id}","starred_url":"https://api.github.com/users/shaarli/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/shaarli/subscriptions","organizations_url":"https://api.github.com/users/shaarli/orgs","repos_url":"https://api.github.com/users/shaarli/repos","events_url":"https://api.github.com/users/shaarli/events{/privacy}","received_events_url":"https://api.github.com/users/shaarli/received_events","type":"Organization","site_admin":false},"repo":{"id":52473523,"name":"netscape-bookmark-parser","full_name":"shaarli/netscape-bookmark-parser","owner":{"login":"shaarli","id":8275533,"avatar_url":"https://avatars.githubusercontent.com/u/8275533?v=3","gravatar_id":"","url":"https://api.github.com/users/shaarli","html_url":"https://github.com/shaarli","followers_url":"https://api.github.com/users/shaarli/followers","following_url":"https://api.github.com/users/shaarli/following{/other_user}","gists_url":"https://api.github.com/users/shaarli/gists{/gist_id}","starred_url":"https://api.github.com/users/shaarli/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/shaarli/subscriptions","organizations_url":"https://api.github.com/users/shaarli/orgs","repos_url":"https://api.github.com/users/shaarli/repos","events_url":"https://api.github.com/users/shaarli/events{/privacy}","received_events_url":"https://api.github.com/users/shaarli/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/shaarli/netscape-bookmark-parser","description":"PHP library to parse Netscape format bookmark files","fork":true,"url":"https://api.github.com/repos/shaarli/netscape-bookmark-parser","forks_url":"https://api.github.com/repos/shaarli/netscape-bookmark-parser/forks","keys_url":"https://api.github.com/repos/shaarli/netscape-bookmark-parser/keys{/key_id}","collaborators_url":"https://api.github.com/repos/shaarli/netscape-bookmark-parser/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/shaarli/netscape-bookmark-parser/teams","hooks_url":"https://api.github.com/repos/shaarli/netscape-bookmark-parser/hooks","issue_events_url":"https://api.github.com/repos/shaarli/netscape-bookmark-parser/issues/events{/number}","events_url":"https://api.github.com/repos/shaarli/netscape-bookmark-parser/events","assignees_url":"https://api.github.com/repos/shaarli/netscape-bookmark-parser/assignees{/user}","branches_url":"https://api.github.com/repos/shaarli/netscape-bookmark-parser/branches{/branch}","tags_url":"https://api.github.com/repos/shaarli/netscape-bookmark-parser/tags","blobs_url":"https://api.github.com/repos/shaarli/netscape-bookmark-parser/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/shaarli/netscape-bookmark-parser/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/shaarli/netscape-bookmark-parser/git/refs{/sha}","trees_url":"https://api.github.com/repos/shaarli/netscape-bookmark-parser/git/trees{/sha}","statuses_url":"https://api.github.com/repos/shaarli/netscape-bookmark-parser/statuses/{sha}","languages_url":"https://api.github.com/repos/shaarli/netscape-bookmark-parser/languages","stargazers_url":"https://api.github.com/repos/shaarli/netscape-bookmark-parser/stargazers","contributors_url":"https://api.github.com/repos/shaarli/netscape-bookmark-parser/contributors","subscribers_url":"https://api.github.com/repos/shaarli/netscape-bookmark-parser/subscribers","subscription_url":"https://api.github.com/repos/shaarli/netscape-bookmark-parser/subscription","commits_url":"https://api.github.com/repos/shaarli/netscape-bookmark-parser/commits{/sha}","git_commits_url":"https://api.github.com/repos/shaarli/netscape-bookmark-parser/git/commits{/sha}","comments_url":"https://api.github.com/repos/shaarli/netscape-bookmark-parser/comments{/number}","issue_comment_url":"https://api.github.com/repos/shaarli/netscape-bookmark-parser/issues/comments{/number}","contents_url":"https://api.github.com/repos/shaarli/netscape-bookmark-parser/contents/{+path}","compare_url":"https://api.github.com/repos/shaarli/netscape-bookmark-parser/compare/{base}...{head}","merges_url":"https://api.github.com/repos/shaarli/netscape-bookmark-parser/merges","archive_url":"https://api.github.com/repos/shaarli/netscape-bookmark-parser/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/shaarli/netscape-bookmark-parser/downloads","issues_url":"https://api.github.com/repos/shaarli/netscape-bookmark-parser/issues{/number}","pulls_url":"https://api.github.com/repos/shaarli/netscape-bookmark-parser/pulls{/number}","milestones_url":"https://api.github.com/repos/shaarli/netscape-bookmark-parser/milestones{/number}","notifications_url":"https://api.github.com/repos/shaarli/netscape-bookmark-parser/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/shaarli/netscape-bookmark-parser/labels{/name}","releases_url":"https://api.github.com/repos/shaarli/netscape-bookmark-parser/releases{/id}","deployments_url":"https://api.github.com/repos/shaarli/netscape-bookmark-parser/deployments","created_at":"2016-02-24T20:51:44Z","updated_at":"2016-05-15T09:11:04Z","pushed_at":"2016-05-15T12:07:42Z","git_url":"git://github.com/shaarli/netscape-bookmark-parser.git","ssh_url":"git@github.com:shaarli/netscape-bookmark-parser.git","clone_url":"https://github.com/shaarli/netscape-bookmark-parser.git","svn_url":"https://github.com/shaarli/netscape-bookmark-parser","homepage":"https://packagist.org/packages/kafene/netscape-bookmark-parser","size":39,"stargazers_count":0,"watchers_count":0,"language":"PHP","has_issues":true,"has_downloads":true,"has_wiki":false,"has_pages":false,"forks_count":1,"mirror_url":null,"open_issues_count":1,"forks":1,"open_issues":1,"watchers":0,"default_branch":"shaarli-devel"}},"_links":{"self":{"href":"https://api.github.com/repos/shaarli/netscape-bookmark-parser/pulls/14"},"html":{"href":"https://github.com/shaarli/netscape-bookmark-parser/pull/14"},"issue":{"href":"https://api.github.com/repos/shaarli/netscape-bookmark-parser/issues/14"},"comments":{"href":"https://api.github.com/repos/shaarli/netscape-bookmark-parser/issues/14/comments"},"review_comments":{"href":"https://api.github.com/repos/shaarli/netscape-bookmark-parser/pulls/14/comments"},"review_comment":{"href":"https://api.github.com/repos/shaarli/netscape-bookmark-parser/pulls/comments{/number}"},"commits":{"href":"https://api.github.com/repos/shaarli/netscape-bookmark-parser/pulls/14/commits"},"statuses":{"href":"https://api.github.com/repos/shaarli/netscape-bookmark-parser/statuses/b03723898db82c5fc8c11c1b492f360f0552b332"}}}}