Skip to content

Commit aa66f02

Browse files
author
Martin Brecht-Precht
committed
Added first CommonmarkHandler and CommonmarkDispatcher.
Missing the HTML block and inline HTML handling.
1 parent ce363c6 commit aa66f02

20 files changed

+1625
-77
lines changed

composer.json

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,10 @@
2424
},
2525
"require": {
2626
"php": ">=5.3",
27-
"markdom/handler-interface": "^1.0.5",
27+
"markdom/handler-interface": "^1.0.8",
2828
"markenwerk/stack-util": "~1.0",
29-
"markenwerk/string-builder": "~1.0",
30-
"markenwerk/json-pretty-printer": "~1.0"
29+
"markenwerk/string-builder": "^1.0.4",
30+
"markenwerk/json-pretty-printer": "~1.0",
31+
"league/commonmark": "^0.13"
3132
}
3233
}

example/commonmark-01.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?php
2+
3+
namespace Markdom\Test;
4+
5+
use Markdom\Dispatcher\JsonDispatcher;
6+
use Markdom\Handler\CommonmarkHandler;
7+
8+
require_once(__DIR__ . '/../vendor/autoload.php');
9+
10+
$handler = new CommonmarkHandler();
11+
$dispatcher = new JsonDispatcher($handler);
12+
$dispatcher->processFile(__DIR__ . '/example-data.json');
13+
fwrite(STDOUT, $handler->getResult());

example/commonmark-02.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?php
2+
3+
namespace Markdom\Test;
4+
5+
use Markdom\Dispatcher\CommonmarkDispatcher;
6+
use Markdom\Handler\CommonmarkHandler;
7+
8+
require_once(__DIR__ . '/../vendor/autoload.php');
9+
10+
$handler = new CommonmarkHandler();
11+
$dispatcher = new CommonmarkDispatcher($handler);
12+
$dispatcher->processFile(__DIR__ . '/result.md');
13+
fwrite(STDOUT, $handler->getResult());

example/debug.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,5 +9,5 @@
99

1010
$handler = new DebugHandler();
1111
$dispatcher = new JsonDispatcher($handler);
12-
$dispatcher->parseFile(__DIR__ . '/example-data.json');
12+
$dispatcher->processFile(__DIR__ . '/example-data.json');
1313
fwrite(STDOUT, $handler->getResult());

example/example-data-simple.json

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
{
2+
"version": {
3+
"major": 1,
4+
"minor": 0
5+
},
6+
"blocks": [
7+
{
8+
"type": "Heading",
9+
"level": 1,
10+
"contents": [
11+
{
12+
"type": "Text",
13+
"text": "some markdown"
14+
}
15+
]
16+
},
17+
{
18+
"type": "Paragraph",
19+
"contents": [
20+
{
21+
"type": "Text",
22+
"text": "this "
23+
},
24+
{
25+
"type": "Emphasis",
26+
"level": 1,
27+
"contents": [
28+
{
29+
"type": "Text",
30+
"text": "is"
31+
}
32+
]
33+
},
34+
{
35+
"type": "Text",
36+
"text": " a not so simple "
37+
},
38+
{
39+
"type": "Emphasis",
40+
"level": 2,
41+
"contents": [
42+
{
43+
"type": "Text",
44+
"text": "paragraph"
45+
}
46+
]
47+
},
48+
{
49+
"type": "Text",
50+
"text": " with different kinds of "
51+
}
52+
]
53+
}
54+
]
55+
}

example/example-data.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,10 @@
3434
}
3535
]
3636
},
37+
{
38+
"type": "Comment",
39+
"comment": "Etiam porta sem malesuada magna mollis euismod.\nwich is multiline"
40+
},
3741
{
3842
"type": "Comment",
3943
"comment": "Etiam porta sem malesuada magna mollis euismod."

example/html.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,5 +12,5 @@
1212
->setEscapeHtml(false)
1313
->setBreakSoftBreaks(false);
1414
$dispatcher = new JsonDispatcher($handler);
15-
$dispatcher->parseFile(__DIR__ . '/example-data.json');
15+
$dispatcher->processFile(__DIR__ . '/example-data.json');
1616
fwrite(STDOUT, $handler->getResult());

example/json.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,5 +12,5 @@
1212
->setPrettyPrint(true)
1313
->setEscapeUnicode(true);
1414
$dispatcher = new JsonDispatcher($handler);
15-
$dispatcher->parseFile(__DIR__ . '/example-data.json');
15+
$dispatcher->processFile(__DIR__ . '/example-data.json');
1616
fwrite(STDOUT, $handler->getResult());

example/xhtml.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,5 +12,5 @@
1212
->setEscapeHtml(true)
1313
->setBreakSoftBreaks(true);
1414
$dispatcher = new JsonDispatcher($handler);
15-
$dispatcher->parseFile(__DIR__ . '/example-data.json');
15+
$dispatcher->processFile(__DIR__ . '/example-data.json');
1616
fwrite(STDOUT, $handler->getResult());

example/xml.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,5 +11,5 @@
1111
$handler
1212
->setPrettyPrint(true);
1313
$dispatcher = new JsonDispatcher($handler);
14-
$dispatcher->parseFile(__DIR__ . '/example-data.json');
14+
$dispatcher->processFile(__DIR__ . '/example-data.json');
1515
fwrite(STDOUT, $handler->getResult()->saveXML());
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
<?php
2+
3+
namespace Markdom\Dispatcher;
4+
5+
use League\CommonMark\DocParser;
6+
use League\CommonMark\Environment;
7+
use Markdom\Dispatcher\CommonmarkUtil\DocumentProcessor;
8+
use Markdom\Dispatcher\Exception\DispatcherException;
9+
use Markdom\DispatcherInterface\DispatcherInterface;
10+
use Markdom\HandlerInterface\HandlerInterface;
11+
12+
/**
13+
* Class CommonmarkDispatcher
14+
*
15+
* @package Markdom\Dispatcher
16+
*/
17+
class CommonmarkDispatcher implements DispatcherInterface
18+
{
19+
20+
/**
21+
* @var HandlerInterface
22+
*/
23+
private $markdomHandler;
24+
25+
/**
26+
* Parser constructor.
27+
*
28+
* @param HandlerInterface $commonmarkHandler
29+
*/
30+
public function __construct(HandlerInterface $commonmarkHandler)
31+
{
32+
$this->markdomHandler = $commonmarkHandler;
33+
}
34+
35+
/**
36+
* @param string $sourceFile
37+
* @return $this
38+
* @throws DispatcherException
39+
*/
40+
public function processFile($sourceFile)
41+
{
42+
if (!file_exists($sourceFile)) {
43+
throw new DispatcherException('Source file not found');
44+
}
45+
if (!is_readable($sourceFile)) {
46+
throw new DispatcherException('Source file not readable');
47+
}
48+
return $this->process(file_get_contents($sourceFile));
49+
}
50+
51+
/**
52+
* @param string $source
53+
* @return $this
54+
*/
55+
public function process($source)
56+
{
57+
$commonMarkEnvironment = Environment::createCommonMarkEnvironment();
58+
$commonMarkEnvironment->addDocumentProcessor(new DocumentProcessor($this->markdomHandler));
59+
$docParser = new DocParser($commonMarkEnvironment);
60+
$docParser->parse($source);
61+
return $this;
62+
}
63+
64+
}
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
<?php
2+
3+
namespace Markdom\Dispatcher\CommonmarkUtil;
4+
5+
use League\CommonMark\Block\Element\Document;
6+
use League\CommonMark\DocumentProcessorInterface;
7+
use Markdom\HandlerInterface\HandlerInterface;
8+
use Markenwerk\StackUtil\Stack;
9+
10+
/**
11+
* Class CommonmarkDocumentProcessor
12+
*
13+
* @package Markdom\Dispatcher\CommonmarkUtil
14+
*/
15+
final class DocumentProcessor implements DocumentProcessorInterface
16+
{
17+
18+
const BLOCK_NODE_BLOCK_QUOTE = 'League\CommonMark\Block\Element\BlockQuote';
19+
const BLOCK_NODE_DOCUMENT = 'League\CommonMark\Block\Element\Document';
20+
const BLOCK_NODE_EMPHASIS = 'League\CommonMark\Inline\Element\Emphasis';
21+
const BLOCK_NODE_FENCED_CODE = 'League\CommonMark\Block\Element\FencedCode';
22+
const BLOCK_NODE_HEADING = 'League\CommonMark\Block\Element\Heading';
23+
const BLOCK_NODE_HTML_BLOCK = 'League\CommonMark\Block\Element\HtmlBlock';
24+
const BLOCK_NODE_IMAGE = 'League\CommonMark\Inline\Element\Image';
25+
const BLOCK_NODE_INDENTED_CODE = 'League\CommonMark\Block\Element\IndentedCode';
26+
const BLOCK_NODE_INLINE_CONTAINER = 'League\CommonMark\Block\Element\InlineContainer';
27+
const BLOCK_NODE_LINK = 'League\CommonMark\Inline\Element\Link';
28+
const BLOCK_NODE_LIST_BLOCK = 'League\CommonMark\Block\Element\ListBlock';
29+
const BLOCK_NODE_LIST_DATA = 'League\CommonMark\Block\Element\ListData';
30+
const BLOCK_NODE_LIST_ITEM = 'League\CommonMark\Block\Element\ListItem';
31+
const BLOCK_NODE_PARAGRAPH = 'League\CommonMark\Block\Element\Paragraph';
32+
const BLOCK_NODE_STRONG = 'League\CommonMark\Inline\Element\Strong';
33+
const BLOCK_NODE_THEMATIC_BREAK = 'League\CommonMark\Block\Element\ThematicBreak';
34+
35+
const INLINE_NODE_CODE = 'League\CommonMark\Inline\Element\Code';
36+
const INLINE_NODE_HTML_INLINE = 'League\CommonMark\Inline\Element\HtmlInline';
37+
const INLINE_NODE_NEWLINE = 'League\CommonMark\Inline\Element\Newline';
38+
const INLINE_NODE_TEXT = 'League\CommonMark\Inline\Element\Text';
39+
40+
/**
41+
* @var HandlerInterface
42+
*/
43+
private $markdomHandler;
44+
45+
/**
46+
* @var Stack
47+
*/
48+
private $imageStack;
49+
50+
/**
51+
* DocumentProcessor constructor.
52+
*
53+
* @param HandlerInterface $markdomHandler
54+
*/
55+
public function __construct(HandlerInterface $markdomHandler)
56+
{
57+
$this->markdomHandler = $markdomHandler;
58+
$this->imageStack = new Stack();
59+
}
60+
61+
/**
62+
* @param Document $document
63+
*
64+
* @return void
65+
*/
66+
public function processDocument(Document $document)
67+
{
68+
$markdomHandlerEventDispatcher = new MarkdomEventBridge($this->markdomHandler);
69+
$walker = $document->walker();
70+
while ($event = $walker->next()) {
71+
$node = $event->getNode();
72+
if ($event->isEntering()) {
73+
if ($this->imageStack->size() === 0) {
74+
$markdomHandlerEventDispatcher->dispatchMarkdomEvent($event);
75+
}
76+
if (get_class($node) === self::BLOCK_NODE_IMAGE) {
77+
$this->imageStack->push($node);
78+
}
79+
} else {
80+
if (get_class($node) === self::BLOCK_NODE_IMAGE) {
81+
$this->imageStack->pop();
82+
}
83+
if ($this->imageStack->size() === 0) {
84+
$markdomHandlerEventDispatcher->dispatchMarkdomEvent($event);
85+
}
86+
}
87+
}
88+
}
89+
90+
}

0 commit comments

Comments
 (0)