Skip to content

Commit b764ae2

Browse files
author
Martin Brecht-Precht
committed
Implemented the latest DispatcherInterface that allows disbaling the dispatching of comment block events.
1 parent f7f528c commit b764ae2

File tree

8 files changed

+89
-22
lines changed

8 files changed

+89
-22
lines changed

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
},
2525
"require": {
2626
"php": ">=5.3",
27-
"markdom/handler-interface": "^1.0.8",
27+
"markdom/handler-interface": "^1.0.9",
2828
"markenwerk/stack-util": "~1.0",
2929
"markenwerk/string-builder": "^1.0.4",
3030
"markenwerk/json-pretty-printer": "~1.0",

example/commonmark-01.php

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

1010
$handler = new CommonmarkHandler();
1111
$dispatcher = new JsonDispatcher($handler);
12-
$dispatcher->processFile(__DIR__ . '/example-data-simple.json');
12+
$dispatcher
13+
->setDispatchCommentBlocks(false)
14+
->processFile(__DIR__ . '/example-data.json');
1315
fwrite(STDOUT, $handler->getResult());

src/Dispatcher/AbstractDispatcher.php

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
<?php
2+
3+
namespace Markdom\Dispatcher;
4+
5+
use Markdom\DispatcherInterface\DispatcherInterface;
6+
7+
/**
8+
* Class AbstractDispatcher
9+
*
10+
* @package Markdom\Dispatcher
11+
*/
12+
abstract class AbstractDispatcher implements DispatcherInterface
13+
{
14+
15+
/**
16+
* @var bool
17+
*/
18+
private $dispatchCommentBlocks = true;
19+
20+
/**
21+
* @return boolean
22+
*/
23+
public function getDispatchCommentBlocks()
24+
{
25+
return $this->dispatchCommentBlocks;
26+
}
27+
28+
/**
29+
* @param boolean $dispatchCommentBlocks
30+
* @return $this
31+
*/
32+
public function setDispatchCommentBlocks($dispatchCommentBlocks)
33+
{
34+
$this->dispatchCommentBlocks = $dispatchCommentBlocks;
35+
return $this;
36+
}
37+
38+
}

src/Dispatcher/CommonmarkDispatcher.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,14 @@
77
use Markdom\Dispatcher\CommonmarkUtil\DocumentProcessor;
88
use Markdom\Dispatcher\Exception\DispatcherException;
99
use Markdom\Dispatcher\HtmlProcessor\HtmlProcessorInterface;
10-
use Markdom\DispatcherInterface\DispatcherInterface;
1110
use Markdom\HandlerInterface\HandlerInterface;
1211

1312
/**
1413
* Class CommonmarkDispatcher
1514
*
1615
* @package Markdom\Dispatcher
1716
*/
18-
class CommonmarkDispatcher implements DispatcherInterface
17+
class CommonmarkDispatcher extends AbstractDispatcher
1918
{
2019

2120
/**
@@ -79,7 +78,9 @@ public function processFile($sourceFile)
7978
public function process($source)
8079
{
8180
$commonMarkEnvironment = Environment::createCommonMarkEnvironment();
82-
$commonMarkEnvironment->addDocumentProcessor(new DocumentProcessor($this->markdomHandler, $this->htmlProcessor));
81+
$commonMarkEnvironment->addDocumentProcessor(
82+
new DocumentProcessor($this->markdomHandler, $this->getDispatchCommentBlocks(), $this->htmlProcessor)
83+
);
8384
$docParser = new DocParser($commonMarkEnvironment);
8485
$docParser->parse($source);
8586
return $this;

src/Dispatcher/CommonmarkUtil/DocumentProcessor.php

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,11 @@ final class DocumentProcessor implements DocumentProcessorInterface
4949
*/
5050
private $htmlProcessor;
5151

52+
/**
53+
* @var bool
54+
*/
55+
private $dispatchCommentBlocks;
56+
5257
/**
5358
* @var Stack
5459
*/
@@ -58,16 +63,21 @@ final class DocumentProcessor implements DocumentProcessorInterface
5863
* DocumentProcessor constructor.
5964
*
6065
* @param HandlerInterface $markdomHandler
66+
* @param bool $dispatchCommentBlocks
6167
* @param HtmlProcessorInterface $htmlProcessor
6268
*/
63-
public function __construct(HandlerInterface $markdomHandler, HtmlProcessorInterface $htmlProcessor = null)
64-
{
69+
public function __construct(
70+
HandlerInterface $markdomHandler,
71+
$dispatchCommentBlocks,
72+
HtmlProcessorInterface $htmlProcessor = null
73+
) {
6574
$this->markdomHandler = $markdomHandler;
66-
$this->imageStack = new Stack();
75+
$this->dispatchCommentBlocks = $dispatchCommentBlocks;
6776
if (is_null($htmlProcessor)) {
6877
$htmlProcessor = new HtmlTextProcessor();
6978
}
7079
$this->htmlProcessor = $htmlProcessor;
80+
$this->imageStack = new Stack();
7181
}
7282

7383
/**
@@ -77,7 +87,11 @@ public function __construct(HandlerInterface $markdomHandler, HtmlProcessorInter
7787
*/
7888
public function processDocument(Document $document)
7989
{
80-
$markdomHandlerEventDispatcher = new MarkdomEventBridge($this->markdomHandler, $this->htmlProcessor);
90+
$markdomHandlerEventDispatcher = new MarkdomEventBridge(
91+
$this->markdomHandler,
92+
$this->dispatchCommentBlocks,
93+
$this->htmlProcessor
94+
);
8195
$walker = $document->walker();
8296
while ($event = $walker->next()) {
8397
$node = $event->getNode();

src/Dispatcher/CommonmarkUtil/MarkdomEventBridge.php

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,11 @@ final class MarkdomEventBridge
4242
*/
4343
private $htmlProcessor;
4444

45+
/**
46+
* @var bool
47+
*/
48+
private $dispatchCommentBlocks;
49+
4550
/**
4651
* @var Node
4752
*/
@@ -51,12 +56,17 @@ final class MarkdomEventBridge
5156
* MarkdomHandlerEventDispatcher constructor.
5257
*
5358
* @param HandlerInterface $commonmarkHandler
59+
* @param bool $dispatchCommentBlocks
5460
* @param HtmlProcessorInterface $htmlProcessor
5561
*/
56-
public function __construct(HandlerInterface $commonmarkHandler, HtmlProcessorInterface $htmlProcessor)
57-
{
62+
public function __construct(
63+
HandlerInterface $commonmarkHandler,
64+
$dispatchCommentBlocks,
65+
HtmlProcessorInterface $htmlProcessor
66+
) {
5867
$this->markdomHandler = $commonmarkHandler;
5968
$this->htmlProcessor = $htmlProcessor;
69+
$this->dispatchCommentBlocks = $dispatchCommentBlocks;
6070
}
6171

6272
/**
@@ -200,7 +210,7 @@ private function transmitContainerBeginEvent(Node $node)
200210
break;
201211
case DocumentProcessor::BLOCK_NODE_HTML_BLOCK:
202212
/** @var HtmlBlock $node */
203-
if ($node->getType() == $node::TYPE_2_COMMENT) {
213+
if ($node->getType() == $node::TYPE_2_COMMENT && $this->dispatchCommentBlocks) {
204214
$this->dispatchBlockBeginEvents(BlockType::TYPE_COMMENT);
205215
$comment = $node->getStringContent();
206216
if (mb_strpos($comment, '<!--') === 0) {
@@ -213,7 +223,7 @@ private function transmitContainerBeginEvent(Node $node)
213223
$this->markdomHandler->onCommentBlock($comment);
214224
} else {
215225
$this->htmlProcessor->handleHtmlBlock($node, $this->markdomHandler);
216-
if(!is_null($node->next())){
226+
if (!is_null($node->next())) {
217227
$this->markdomHandler->onNextBlock();
218228
}
219229
}

src/Dispatcher/PhpObjectDispatcher.php

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44

55
use Markdom\Dispatcher\EventDispatcher\SimpleMarkdomEventDispatcher;
66
use Markdom\Dispatcher\Exception\DispatcherException;
7-
use Markdom\DispatcherInterface\DispatcherInterface;
87
use Markdom\Handler\TypeNameTranslator\KeyNameTranslator;
98
use Markdom\HandlerInterface\HandlerInterface;
109

@@ -13,7 +12,7 @@
1312
*
1413
* @package Markdom\Dispatcher
1514
*/
16-
class PhpObjectDispatcher implements DispatcherInterface
15+
class PhpObjectDispatcher extends AbstractDispatcher
1716
{
1817

1918
/**
@@ -49,13 +48,13 @@ public function process($source)
4948
if (!is_object($source)) {
5049
throw new DispatcherException('Markdom invalid: root node is no object.');
5150
}
52-
if(!isset($source->version) || !is_object($source->version)){
51+
if (!isset($source->version) || !is_object($source->version)) {
5352
throw new DispatcherException('Markdom invalid: no document version specified.');
5453
}
55-
if(!isset($source->version->major) || !isset($source->version->minor)){
54+
if (!isset($source->version->major) || !isset($source->version->minor)) {
5655
throw new DispatcherException('Markdom invalid: no document valid version specified.');
5756
}
58-
if((int)$source->version->major !== 1 || (int)$source->version->minor !== 0){
57+
if ((int)$source->version->major !== 1 || (int)$source->version->minor !== 0) {
5958
throw new DispatcherException('Markdom invalid: version mismatch Expected version 1.0.');
6059
}
6160
$this->eventDispatcher->onDocumentBegin();
@@ -85,7 +84,9 @@ private function processBlocks(array $blocks)
8584
$this->eventDispatcher->onCodeBlock($node->code, $hint);
8685
break;
8786
case KeyNameTranslator::TYPE_COMMENT:
88-
$this->eventDispatcher->onCommentBlock($node->comment);
87+
if ($this->getDispatchCommentBlocks()) {
88+
$this->eventDispatcher->onCommentBlock($node->comment);
89+
}
8990
break;
9091
case KeyNameTranslator::TYPE_DIVISION:
9192
$this->eventDispatcher->onDivisionBlock();

src/Dispatcher/XmlDispatcher.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44

55
use Markdom\Dispatcher\EventDispatcher\SimpleMarkdomEventDispatcher;
66
use Markdom\Dispatcher\Exception\DispatcherException;
7-
use Markdom\DispatcherInterface\DispatcherInterface;
87
use Markdom\Handler\TypeNameTranslator\KeyNameTranslator;
98
use Markdom\HandlerInterface\HandlerInterface;
109

@@ -13,7 +12,7 @@
1312
*
1413
* @package Markdom\Dispatcher
1514
*/
16-
class XmlDispatcher implements DispatcherInterface
15+
class XmlDispatcher extends AbstractDispatcher
1716
{
1817

1918
/**
@@ -85,7 +84,9 @@ private function processBlocks(\DOMNodeList $blocks)
8584
$this->eventDispatcher->onCodeBlock($node->textContent, $hint);
8685
break;
8786
case KeyNameTranslator::TYPE_COMMENT:
88-
$this->eventDispatcher->onCommentBlock($node->textContent);
87+
if ($this->getDispatchCommentBlocks()) {
88+
$this->eventDispatcher->onCommentBlock($node->textContent);
89+
}
8990
break;
9091
case KeyNameTranslator::TYPE_DIVISION:
9192
$this->eventDispatcher->onDivisionBlock();

0 commit comments

Comments
 (0)