Add GemlogParser and skeleton ArticleParser

This commit is contained in:
Annika Backstrom 2022-05-21 17:26:52 +01:00
parent 1a2a5c01a7
commit 2c7901d5a9
7 changed files with 143 additions and 6 deletions

View File

@ -13,6 +13,7 @@
"league/commonmark": "^2.3" "league/commonmark": "^2.3"
}, },
"require-dev": { "require-dev": {
"phpunit/phpunit": "^9.5" "phpunit/phpunit": "^9.5",
"mikey179/vfsstream": "^1.6"
} }
} }

53
composer.lock generated
View File

@ -4,7 +4,7 @@
"Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies",
"This file is @generated automatically" "This file is @generated automatically"
], ],
"content-hash": "cc27d276519ce69d017e43bec647a98a", "content-hash": "848176d1d39c31f3ecc3bbea8204ac22",
"packages": [ "packages": [
{ {
"name": "dflydev/dot-access-data", "name": "dflydev/dot-access-data",
@ -688,6 +688,57 @@
], ],
"time": "2022-03-03T08:28:38+00:00" "time": "2022-03-03T08:28:38+00:00"
}, },
{
"name": "mikey179/vfsstream",
"version": "v1.6.10",
"source": {
"type": "git",
"url": "https://github.com/bovigo/vfsStream.git",
"reference": "250c0825537d501e327df879fb3d4cd751933b85"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/bovigo/vfsStream/zipball/250c0825537d501e327df879fb3d4cd751933b85",
"reference": "250c0825537d501e327df879fb3d4cd751933b85",
"shasum": ""
},
"require": {
"php": ">=5.3.0"
},
"require-dev": {
"phpunit/phpunit": "^4.5|^5.0"
},
"type": "library",
"extra": {
"branch-alias": {
"dev-master": "1.6.x-dev"
}
},
"autoload": {
"psr-0": {
"org\\bovigo\\vfs\\": "src/main/php"
}
},
"notification-url": "https://packagist.org/downloads/",
"license": [
"BSD-3-Clause"
],
"authors": [
{
"name": "Frank Kleine",
"homepage": "http://frankkleine.de/",
"role": "Developer"
}
],
"description": "Virtual file system to mock the real file system in unit tests.",
"homepage": "http://vfs.bovigo.org/",
"support": {
"issues": "https://github.com/bovigo/vfsStream/issues",
"source": "https://github.com/bovigo/vfsStream/tree/master",
"wiki": "https://github.com/bovigo/vfsStream/wiki"
},
"time": "2021-09-25T08:05:01+00:00"
},
{ {
"name": "myclabs/deep-copy", "name": "myclabs/deep-copy",
"version": "1.11.0", "version": "1.11.0",

8
src/ArticleParser.php Normal file
View File

@ -0,0 +1,8 @@
<?php
declare(strict_types=1);
namespace LogseqGem;
class ArticleParser extends Parser {
}

30
src/GemlogParser.php Normal file
View File

@ -0,0 +1,30 @@
<?php
declare(strict_types=1);
namespace LogseqGem;
use League\CommonMark\Node\Block\Document;
use League\CommonMark\Node\Inline\Text;
class GemlogParser extends Parser {
public function getTitles() {
$titles = [];
foreach ($this->document->iterator() as $node) {
if (!$node instanceof Text) {
continue;
}
$text = $node->getLiteral();
if (substr($text, 0, 2) !== '[[' && substr($text, -2) !== ']]') {
continue;
}
$titles[] = substr($text, 2, -2);
}
return $titles;
}
}

View File

@ -4,6 +4,8 @@ declare(strict_types=1);
namespace LogseqGem; namespace LogseqGem;
use League\CommonMark\Node\Block\Document;
use League\CommonMark\Parser\MarkdownParser; use League\CommonMark\Parser\MarkdownParser;
use League\CommonMark\Environment\Environment; use League\CommonMark\Environment\Environment;
use League\CommonMark\Extension\CommonMark\CommonMarkCoreExtension; use League\CommonMark\Extension\CommonMark\CommonMarkCoreExtension;
@ -11,13 +13,17 @@ use League\CommonMark\Extension\CommonMark\CommonMarkCoreExtension;
abstract class Parser { abstract class Parser {
private MarkdownParser $parser; private MarkdownParser $parser;
protected Document $document;
public function __construct() { public function __construct() {
$environment = new Environment(); $environment = new Environment();
$environment->addExtension(new CommonMarkCoreExtension()); $environment->addExtension(new CommonMarkCoreExtension());
$this->parser = new MarkdownParser($environment); $this->parser = new MarkdownParser($environment);
} }
public function parse(string $input) { public function load(string $path) {
return $this->parser->parse($input); $markdown = file_get_contents($path);
$this->document = $this->parser->parse($markdown);
return $this->document;
} }
} }

View File

@ -0,0 +1,29 @@
<?php
declare(strict_types=1);
namespace LogseqGem\Tests;
use PHPUnit\Framework\TestCase;
use org\bovigo\vfs\vfsStream;
use org\bovigo\vfs\vfsStreamDirectory;
use League\CommonMark\Node\Block\Document;
use LogseqGem\GemlogParser;
class GemlogParserTest extends TestCase {
private vfsStreamDirectory $root;
public function setUp(): void {
$this->root = vfsStream::setup();
}
public function testLoad() {
vfsStream::newFile('Gemlog.md')
->withContent("- [[Test link]]\n- [[Link 2]]\n-")
->at($this->root);
$parser = new GemlogParser;
$parser->load(vfsStream::url('root/Gemlog.md'));
$this->assertSame(['Test link', 'Link 2'], $parser->getTitles());
}
}

View File

@ -5,14 +5,26 @@ declare(strict_types=1);
namespace LogseqGem\Tests; namespace LogseqGem\Tests;
use PHPUnit\Framework\TestCase; use PHPUnit\Framework\TestCase;
use LogseqGem\Parser; use org\bovigo\vfs\vfsStream;
use org\bovigo\vfs\vfsStreamDirectory;
use League\CommonMark\Node\Block\Document; use League\CommonMark\Node\Block\Document;
use LogseqGem\Parser;
class ParserTest extends TestCase { class ParserTest extends TestCase {
private vfsStreamDirectory $root;
public function setUp(): void {
$this->root = vfsStream::setup();
}
public function testParse() { public function testParse() {
vfsStream::newFile('test.md')
->withContent("this is a test **markdown** file")
->at($this->root);
$parser = $this->getMockForAbstractClass(Parser::class); $parser = $this->getMockForAbstractClass(Parser::class);
$document = $parser->parse('**foo**'); $document = $parser->load(vfsStream::url('root/test.md'));
$this->assertInstanceOf(Document::class, $document); $this->assertInstanceOf(Document::class, $document);
} }
} }