Skip to content

Commit

Permalink
New MarkDown parser
Browse files Browse the repository at this point in the history
  • Loading branch information
phpfui committed Jan 21, 2024
1 parent 3e19c39 commit aa0eb2a
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 11 deletions.
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,10 @@
"phpfui/phpfui": "^6.0",
"phpdocumentor/reflection-docblock": "^5.0",
"gitonomy/gitlib": "^1.2",
"cebe/markdown": "^1.2",
"voku/simple_html_dom": "^4.7",
"soundasleep/html2text": "^2.0",
"scrivo/highlight.php": ">=v9.18"
"scrivo/highlight.php": ">=v9.18",
"league/commonmark": "^2.4"
},
"autoload": {
"psr-4": {"PHPFUI\\InstaDoc\\": "src/PHPFUI/InstaDoc/"}
Expand Down
18 changes: 9 additions & 9 deletions src/PHPFUI/InstaDoc/MarkDownParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,11 @@

class MarkDownParser
{
private \cebe\markdown\GithubMarkdown $parser;
private \League\CommonMark\GithubFlavoredMarkdownConverter $parser;

public function __construct()
{
$this->parser = new \cebe\markdown\GithubMarkdown();
$this->parser->html5 = true;
$this->parser->keepListStartNumber = true;
$this->parser->enableNewlines = true;
$this->parser = new \League\CommonMark\GithubFlavoredMarkdownConverter(['html_input' => 'strip', 'allow_unsafe_links' => false, ]);
}

public function fileText(string $filename) : string
Expand All @@ -23,17 +20,20 @@ public function fileText(string $filename) : string

public function html(string $markdown) : string
{
return $this->parser->parseParagraph(\str_replace(['<p>', '</p>'], '', $markdown));
$markdown = \str_replace('<?php', '', $markdown);
$html = $this->parser->convert($markdown);

return \str_replace(['<p>', '</p>'], '', "{$html}");
}

public function text(string $markdown) : string
{
$position = 0;
$markdown = \str_replace('<?php', '', $markdown);
$hl = new \Highlight\Highlighter();

$div = new \PHPFUI\HTML5Element('div');
$div->addClass('markdown-body');
$html = $this->parser->parse($markdown);
$html = "{$this->parser->convert($markdown)}";
$dom = new \voku\helper\HtmlDomParser($html);
$codeBlocks = $dom->find('.language-php');

Expand All @@ -47,6 +47,6 @@ public function text(string $markdown) : string
}
$div->add("{$dom}");

return $div;
return "{$div}";
}
}
41 changes: 41 additions & 0 deletions tests/MDTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php

/**
* This file is part of the PHPFUI package
*
* (c) Bruce Wells
*
* For the full copyright and license information, please view
* the LICENSE.md file that was distributed with this source
* code
*/
class MDTest extends \PHPFUI\HTMLUnitTester\Extensions
{
public function testMarkdownFiles() : void
{
$iterator = new RecursiveIteratorIterator(new RecursiveDirectoryIterator(__DIR__ . '/../vendor'));

$parser = new \PHPFUI\InstaDoc\MarkDownParser();

$ignoreDirectories = ['devbridge', ];

foreach ($iterator as $file)
{
foreach ($ignoreDirectories as $directory)
{
if (\str_contains($file->getPathname(), $directory))
{
continue 2;
}
}
$fileName = \strtolower($file->getFilename());

if ($file->isFile() && \str_ends_with($fileName, '.md'))
{
$html = $parser->fileText($file->getPathname());
$this->assertNotWarningHtml($html, "File {$file->getPathname()} has HTML warnings");
$this->assertValidHtml($html, "File {$file->getPathname()} has HTML errors");
}
}
}
}

0 comments on commit aa0eb2a

Please sign in to comment.