Skip to content

Commit 7ead9cd

Browse files
author
rmp-up
committed
dev: initial state, first version
1 parent b04344b commit 7ead9cd

File tree

11 files changed

+746
-0
lines changed

11 files changed

+746
-0
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
.idea

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
# Yet another doc-parser

composer.json

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
{
2+
"name": "rmp-up/doc-parser",
3+
"description": "Yet another doc-comment parser helper",
4+
"autoload": {
5+
"psr-4": {
6+
"RmpUp\\Doc\\": "lib/"
7+
}
8+
},
9+
"license": "proprietary",
10+
"require": {
11+
"php": ">= 7.0",
12+
"ext-simplexml": "*",
13+
"league/commonmark": "^0.18.1",
14+
"michelf/php-markdown": ">=1.0 <2.0.0",
15+
"zendframework/zend-code": ">=3.0 <4.0.0",
16+
"phpdocumentor/reflection-docblock": "^5.1",
17+
"mikey179/vfsstream": "^1.6",
18+
"gherkins/regexpbuilderphp": "^0.8.0"
19+
}
20+
}

lib/Comment.php

Lines changed: 135 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,135 @@
1+
<?php
2+
/* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
3+
4+
/**
5+
* Comment.php
6+
*
7+
* LICENSE: This source file is created by the company around Pretzlaw
8+
* located in Germany also known as rmp-up. All its contents are proprietary
9+
* and under german copyright law. Consider this file as closed source and/or
10+
* without the permission to reuse or modify its contents.
11+
* This license is available through the world-wide-web at the following URI:
12+
* https://rmp-up.de/license-generic.txt . If you did not receive a copy
13+
* of the license and are unable to obtain it through the web, please send a
14+
* note to [email protected] so we can mail you a copy.
15+
*
16+
* @package phpunit-docgen
17+
* @copyright 2020 Pretzlaw
18+
* @license https://rmp-up.de/license-generic.txt
19+
* @link https://project.rmp-up.de/phpunit-docgen
20+
*/
21+
22+
declare(strict_types=1);
23+
24+
namespace RmpUp\Doc;
25+
26+
use Michelf\MarkdownExtra;
27+
use phpDocumentor\Reflection\DocBlock;
28+
use RuntimeException;
29+
use SimpleXMLElement;
30+
31+
/**
32+
* Comment
33+
*
34+
* @copyright 2020 Pretzlaw (https://rmp-up.de)
35+
*/
36+
class Comment extends HtmlNode
37+
{
38+
/**
39+
* @var DocBlock
40+
*/
41+
private $docBlock;
42+
43+
public function __construct(DocBlock $docBlock)
44+
{
45+
$this->docBlock = $docBlock;
46+
47+
parent::__construct(
48+
simplexml_load_string('<html lang="en">' . MarkdownExtra::defaultTransform($this->markdown()) . '</html>')
49+
);
50+
}
51+
52+
public function docBlock(): DocBlock
53+
{
54+
return $this->docBlock;
55+
}
56+
57+
public function execute($xpathOrIndex)
58+
{
59+
$index = 0;
60+
if (is_int($xpathOrIndex)) {
61+
$index = $xpathOrIndex;
62+
$xpathOrIndex = '//pre/code';
63+
}
64+
65+
$content = $this->xpath($xpathOrIndex, $index);
66+
67+
if (is_array($content) && count($content) === 1) {
68+
// Seems like the only one here.
69+
$content = reset($content);
70+
}
71+
72+
if (!$content || !$content instanceof SimpleXMLElement) {
73+
throw new RuntimeException('Code not found or empty: ' . $xpathOrIndex);
74+
}
75+
76+
$tempFile = tempnam(sys_get_temp_dir(), 'rmpup_dc_');
77+
78+
$isSaved = file_put_contents($tempFile, $content);
79+
80+
if (false === $isSaved) {
81+
throw new RuntimeException('Could not create tempfile for code: ' . $xpathOrIndex);
82+
}
83+
84+
ob_start();
85+
$return = require $tempFile;
86+
$content = ob_get_clean();
87+
unlink($tempFile);
88+
89+
if (1 !== $return) {
90+
return $return;
91+
}
92+
93+
return $content;
94+
}
95+
96+
public function markdown(): string
97+
{
98+
$markdown = $this->docBlock()->getSummary();
99+
100+
if (false === strpos($markdown, "\n")) {
101+
$markdown = '# ' . trim($markdown);
102+
}
103+
104+
$markdown .= PHP_EOL . PHP_EOL;
105+
106+
$description = $this->docBlock()->getDescription();
107+
// Unescape possibly escaped comment blocks
108+
$description = preg_replace('@(\s*)\*\\\/(\s*)$@m', '\1*/', $description);
109+
110+
$markdown .= $description;
111+
112+
return trim($markdown);
113+
}
114+
115+
/**
116+
* @param string $xpath
117+
* @param int|null $index
118+
*
119+
* @return SimpleXMLElement[]|SimpleXMLElement|bool
120+
*/
121+
public function xpath(string $xpath, int $index = null)
122+
{
123+
$elements = $this->xml()->xpath($xpath);
124+
125+
if (null === $index) {
126+
return $elements;
127+
}
128+
129+
if (count($elements) < $index + 1) {
130+
return false;
131+
}
132+
133+
return $elements[$index];
134+
}
135+
}

lib/DocParser.php

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
<?php
2+
/* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
3+
4+
/**
5+
* DocQuery.php
6+
*
7+
* LICENSE: This source file is created by the company around Pretzlaw
8+
* located in Germany also known as rmp-up. All its contents are proprietary
9+
* and under german copyright law. Consider this file as closed source and/or
10+
* without the permission to reuse or modify its contents.
11+
* This license is available through the world-wide-web at the following URI:
12+
* https://rmp-up.de/license-generic.txt . If you did not receive a copy
13+
* of the license and are unable to obtain it through the web, please send a
14+
* note to [email protected] so we can mail you a copy.
15+
*
16+
* @package phpunit-docgen
17+
* @copyright 2020 Pretzlaw
18+
* @license https://rmp-up.de/license-generic.txt
19+
* @link https://project.rmp-up.de/phpunit-docgen
20+
*/
21+
22+
declare(strict_types=1);
23+
24+
namespace RmpUp\Doc;
25+
26+
use phpDocumentor\Reflection\DocBlockFactory;
27+
use ReflectionClass;
28+
29+
/**
30+
* DocQuery
31+
*
32+
* @copyright 2020 Pretzlaw (https://rmp-up.de)
33+
*/
34+
trait DocParser
35+
{
36+
private $docBlockFactory;
37+
38+
public function classComment($className = null): Comment
39+
{
40+
if (null === $className) {
41+
$className = get_class($this);
42+
}
43+
44+
$reflection = new ReflectionClass($className);
45+
46+
$docComment = (string) $reflection->getDocComment();
47+
48+
return new Comment($this->docBlockFactory()->create($docComment));
49+
}
50+
51+
/**
52+
* @return Comment
53+
*
54+
* @deprecated 0.3.0 Use ::classComment() or ::methodComment() instead.
55+
*/
56+
public function comment(): Comment
57+
{
58+
return $this->methodComment(get_class($this), $this->getName());
59+
}
60+
61+
public function methodComment($classOrMethodName = null, $method = null): Comment
62+
{
63+
if (null === $classOrMethodName) {
64+
$classOrMethodName = get_class($this);
65+
}
66+
67+
if (false !== strpos($classOrMethodName, '::')) {
68+
$parts = explode('::', $classOrMethodName);
69+
70+
$classOrMethodName = get_class($this);
71+
$method = array_pop($parts);
72+
73+
if ($parts) {
74+
$classOrMethodName = array_pop($parts);
75+
}
76+
}
77+
78+
$method = (new ReflectionClass($classOrMethodName))->getMethod($method);
79+
80+
return new Comment($this->docBlockFactory()->create((string)$method->getDocComment()));
81+
}
82+
83+
84+
private function docBlockFactory(): DocBlockFactory
85+
{
86+
if (null === $this->docBlockFactory) {
87+
$this->docBlockFactory = DocBlockFactory::createInstance();
88+
}
89+
90+
return $this->docBlockFactory;
91+
}
92+
}

lib/Html.php

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
<?php
2+
/* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
3+
4+
/**
5+
* Html.php
6+
*
7+
* LICENSE: This source file is created by the company around M. Pretzlaw
8+
* located in Germany also known as rmp-up. All its contents are proprietary
9+
* and under german copyright law. Consider this file as closed source and/or
10+
* without the permission to reuse or modify its contents.
11+
* This license is available through the world-wide-web at the following URI:
12+
* https://rmp-up.de/license-generic.txt . If you did not receive a copy
13+
* of the license and are unable to obtain it through the web, please send a
14+
* note to [email protected] so we can mail you a copy.
15+
*
16+
* @package doc
17+
* @copyright 2020 Pretzlaw
18+
* @license https://rmp-up.de/license-generic.txt
19+
*/
20+
21+
declare(strict_types=1);
22+
23+
namespace RmpUp\Doc;
24+
25+
use SimpleXMLElement;
26+
27+
/**
28+
* Html
29+
*
30+
* @copyright 2020 Pretzlaw (https://rmp-up.de)
31+
*/
32+
class Html extends HtmlNode
33+
{
34+
public function __construct(string $html)
35+
{
36+
parent::__construct(simplexml_load_string($html));
37+
}
38+
39+
public function enumeration($index = 0)
40+
{
41+
return $this->listings($index, 'ol');
42+
}
43+
44+
public function itemization($index = 0)
45+
{
46+
return $this->listings($index, 'ul');
47+
}
48+
49+
public function listings($index = 0, $type = null)
50+
{
51+
$query = './/ul[li] | //ol[li]';
52+
if ($type !== null) {
53+
$query = './/' . $type . '[li]';
54+
}
55+
$lists = $this->xml()->xpath($query); // All lists with children
56+
57+
if (count($lists) < $index) {
58+
return null;
59+
}
60+
61+
return $lists[$index];
62+
}
63+
}

0 commit comments

Comments
 (0)