Skip to content

Commit 4fac35d

Browse files
author
Martin Brecht-Precht
committed
Added HTML processing to the Commonmark dispatcher.
1 parent aa66f02 commit 4fac35d

File tree

10 files changed

+165
-38
lines changed

10 files changed

+165
-38
lines changed

example/commonmark-01.php

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

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

example/commonmark-02.php

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

1010
$handler = new CommonmarkHandler();
1111
$dispatcher = new CommonmarkDispatcher($handler);
12-
$dispatcher->processFile(__DIR__ . '/result.md');
12+
$dispatcher->processFile(__DIR__ . '/example-data.md');
1313
fwrite(STDOUT, $handler->getResult());

example/example-data-simple.json

Lines changed: 18 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -15,39 +15,31 @@
1515
]
1616
},
1717
{
18-
"type": "Paragraph",
18+
"type": "Heading",
19+
"level": 3,
1920
"contents": [
2021
{
2122
"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-
},
23+
"text": "HTML Block"
24+
}
25+
]
26+
},
27+
{
28+
"type": "Paragraph",
29+
"contents": [
3430
{
3531
"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-
},
32+
"text": "kjsdfkjhasd <p>Test<\/p> ksajdhfgkasjhfgd"
33+
}
34+
]
35+
},
36+
{
37+
"type": "Heading",
38+
"level": 3,
39+
"contents": [
4840
{
4941
"type": "Text",
50-
"text": " with different kinds of "
42+
"text": "blocks in a quote block"
5143
}
5244
]
5345
}

example/example-data.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# some markdown
2+
3+
### HTML Block
4+
5+
kjsdfkjhasd <span style="color:#ff0000;">Test</span> ksajdhfgkasjhfgd
6+
7+
<p style="color:#ff00ff;">Block of HTML</p>
8+
9+
sdklfjhalksdhflakjshdf
10+
11+
klasjhdflkasghdlkfhjg
12+
13+
### blocks in a quote block

src/Dispatcher/CommonmarkDispatcher.php

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
use League\CommonMark\Environment;
77
use Markdom\Dispatcher\CommonmarkUtil\DocumentProcessor;
88
use Markdom\Dispatcher\Exception\DispatcherException;
9+
use Markdom\Dispatcher\HtmlProcessor\HtmlProcessorInterface;
910
use Markdom\DispatcherInterface\DispatcherInterface;
1011
use Markdom\HandlerInterface\HandlerInterface;
1112

@@ -22,6 +23,11 @@ class CommonmarkDispatcher implements DispatcherInterface
2223
*/
2324
private $markdomHandler;
2425

26+
/**
27+
* @var HtmlProcessorInterface
28+
*/
29+
private $htmlProcessor = null;
30+
2531
/**
2632
* Parser constructor.
2733
*
@@ -32,6 +38,24 @@ public function __construct(HandlerInterface $commonmarkHandler)
3238
$this->markdomHandler = $commonmarkHandler;
3339
}
3440

41+
/**
42+
* @return HtmlProcessorInterface
43+
*/
44+
public function getHtmlProcessor()
45+
{
46+
return $this->htmlProcessor;
47+
}
48+
49+
/**
50+
* @param HtmlProcessorInterface $htmlProcessor
51+
* @return $this
52+
*/
53+
public function setHtmlProcessor($htmlProcessor)
54+
{
55+
$this->htmlProcessor = $htmlProcessor;
56+
return $this;
57+
}
58+
3559
/**
3660
* @param string $sourceFile
3761
* @return $this
@@ -55,7 +79,7 @@ public function processFile($sourceFile)
5579
public function process($source)
5680
{
5781
$commonMarkEnvironment = Environment::createCommonMarkEnvironment();
58-
$commonMarkEnvironment->addDocumentProcessor(new DocumentProcessor($this->markdomHandler));
82+
$commonMarkEnvironment->addDocumentProcessor(new DocumentProcessor($this->markdomHandler, $this->htmlProcessor));
5983
$docParser = new DocParser($commonMarkEnvironment);
6084
$docParser->parse($source);
6185
return $this;

src/Dispatcher/CommonmarkUtil/DocumentProcessor.php

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

55
use League\CommonMark\Block\Element\Document;
66
use League\CommonMark\DocumentProcessorInterface;
7+
use Markdom\Dispatcher\HtmlProcessor\HtmlProcessorInterface;
8+
use Markdom\Dispatcher\HtmlProcessor\HtmlTextProcessor;
79
use Markdom\HandlerInterface\HandlerInterface;
810
use Markenwerk\StackUtil\Stack;
911

@@ -42,6 +44,11 @@ final class DocumentProcessor implements DocumentProcessorInterface
4244
*/
4345
private $markdomHandler;
4446

47+
/**
48+
* @var HtmlProcessorInterface
49+
*/
50+
private $htmlProcessor;
51+
4552
/**
4653
* @var Stack
4754
*/
@@ -51,11 +58,16 @@ final class DocumentProcessor implements DocumentProcessorInterface
5158
* DocumentProcessor constructor.
5259
*
5360
* @param HandlerInterface $markdomHandler
61+
* @param HtmlProcessorInterface $htmlProcessor
5462
*/
55-
public function __construct(HandlerInterface $markdomHandler)
63+
public function __construct(HandlerInterface $markdomHandler, HtmlProcessorInterface $htmlProcessor = null)
5664
{
5765
$this->markdomHandler = $markdomHandler;
5866
$this->imageStack = new Stack();
67+
if (is_null($htmlProcessor)) {
68+
$htmlProcessor = new HtmlTextProcessor();
69+
}
70+
$this->htmlProcessor = $htmlProcessor;
5971
}
6072

6173
/**
@@ -65,7 +77,7 @@ public function __construct(HandlerInterface $markdomHandler)
6577
*/
6678
public function processDocument(Document $document)
6779
{
68-
$markdomHandlerEventDispatcher = new MarkdomEventBridge($this->markdomHandler);
80+
$markdomHandlerEventDispatcher = new MarkdomEventBridge($this->markdomHandler, $this->htmlProcessor);
6981
$walker = $document->walker();
7082
while ($event = $walker->next()) {
7183
$node = $event->getNode();

src/Dispatcher/CommonmarkUtil/MarkdomEventBridge.php

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
use League\CommonMark\Block\Element\ListItem;
1111
use League\CommonMark\Inline\Element\Code;
1212
use League\CommonMark\Inline\Element\Emphasis;
13+
use League\CommonMark\Inline\Element\HtmlInline;
1314
use League\CommonMark\Inline\Element\Image;
1415
use League\CommonMark\Inline\Element\Link;
1516
use League\CommonMark\Inline\Element\Newline;
@@ -20,6 +21,7 @@
2021
use Markdom\Common\ContentType;
2122
use Markdom\Common\EmphasisLevel;
2223
use Markdom\Dispatcher\Exception\DispatcherException;
24+
use Markdom\Dispatcher\HtmlProcessor\HtmlProcessorInterface;
2325
use Markdom\HandlerInterface\HandlerInterface;
2426

2527
/**
@@ -35,6 +37,11 @@ final class MarkdomEventBridge
3537
*/
3638
private $markdomHandler;
3739

40+
/**
41+
* @var HtmlProcessorInterface
42+
*/
43+
private $htmlProcessor;
44+
3845
/**
3946
* @var Node
4047
*/
@@ -44,10 +51,12 @@ final class MarkdomEventBridge
4451
* MarkdomHandlerEventDispatcher constructor.
4552
*
4653
* @param HandlerInterface $commonmarkHandler
54+
* @param HtmlProcessorInterface $htmlProcessor
4755
*/
48-
public function __construct(HandlerInterface $commonmarkHandler)
56+
public function __construct(HandlerInterface $commonmarkHandler, HtmlProcessorInterface $htmlProcessor)
4957
{
5058
$this->markdomHandler = $commonmarkHandler;
59+
$this->htmlProcessor = $htmlProcessor;
5160
}
5261

5362
/**
@@ -203,7 +212,7 @@ private function transmitContainerBeginEvent(Node $node)
203212
$comment = trim($comment);
204213
$this->markdomHandler->onCommentBlock($comment);
205214
} else {
206-
// TODO: Handle HTML block
215+
$this->htmlProcessor->handleHtmlBlock($node, $this->markdomHandler);
207216
}
208217
break;
209218
case DocumentProcessor::BLOCK_NODE_IMAGE:
@@ -303,8 +312,6 @@ private function transmitContainerEndEvent(Node $node)
303312
/** @var HtmlBlock $node */
304313
if ($node->getType() == $node::TYPE_2_COMMENT) {
305314
$this->dispatchBlockEndEvents($node, BlockType::TYPE_COMMENT);
306-
} else {
307-
// TODO: Handle HTML block
308315
}
309316
break;
310317
case DocumentProcessor::BLOCK_NODE_IMAGE:
@@ -375,7 +382,8 @@ private function transmitInlineBeginEvent(Node $node)
375382
$this->markdomHandler->onCodeContent($node->getContent());
376383
break;
377384
case DocumentProcessor::INLINE_NODE_HTML_INLINE:
378-
// TODO: Handle inline HTML
385+
/** @var HtmlInline $node */
386+
$this->htmlProcessor->handleInlineHtml($node, $this->markdomHandler);
379387
break;
380388
case DocumentProcessor::INLINE_NODE_NEWLINE:
381389
$this->dispatchContentBeginEvents(ContentType::TYPE_LINE_BREAK);
@@ -408,7 +416,6 @@ private function transmitInlineEndEvent()
408416
$this->dispatchContentEndEvents($this->recentInlineNode, ContentType::TYPE_CODE);
409417
break;
410418
case DocumentProcessor::INLINE_NODE_HTML_INLINE:
411-
// TODO: Handle inline HTML
412419
break;
413420
case DocumentProcessor::INLINE_NODE_NEWLINE:
414421
$this->dispatchContentEndEvents($this->recentInlineNode, ContentType::TYPE_LINE_BREAK);
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?php
2+
3+
namespace Markdom\Dispatcher\HtmlProcessor;
4+
5+
use League\CommonMark\Block\Element\HtmlBlock;
6+
use League\CommonMark\Inline\Element\HtmlInline;
7+
use Markdom\HandlerInterface\HandlerInterface;
8+
9+
/**
10+
* Interface HtmlProcessorInterface
11+
*
12+
* @package Markdom\Dispatcher\HtmlProcessor
13+
*/
14+
interface HtmlProcessorInterface
15+
{
16+
17+
/**
18+
* @param HtmlBlock $htmlBlock
19+
* @param HandlerInterface $markdomHandler
20+
* @return void
21+
*/
22+
public function handleHtmlBlock(HtmlBlock $htmlBlock, HandlerInterface $markdomHandler);
23+
24+
/**
25+
* @param HtmlInline $htmlInline
26+
* @param HandlerInterface $markdomHandler
27+
* @return void
28+
*/
29+
public function handleInlineHtml(HtmlInline $htmlInline, HandlerInterface $markdomHandler);
30+
31+
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
<?php
2+
3+
namespace Markdom\Dispatcher\HtmlProcessor;
4+
5+
use League\CommonMark\Block\Element\HtmlBlock;
6+
use League\CommonMark\Inline\Element\HtmlInline;
7+
use Markdom\Common\BlockType;
8+
use Markdom\Common\ContentType;
9+
use Markdom\HandlerInterface\HandlerInterface;
10+
11+
/**
12+
* Class HtmlTextProcessor
13+
*
14+
* @package Markdom\Dispatcher\HtmlProcessor
15+
*/
16+
class HtmlTextProcessor implements HtmlProcessorInterface
17+
{
18+
19+
/**
20+
* @param HtmlBlock $htmlBlock
21+
* @param HandlerInterface $markdomHandler
22+
* @return void
23+
*/
24+
public function handleHtmlBlock(HtmlBlock $htmlBlock, HandlerInterface $markdomHandler)
25+
{
26+
$plaintext = strip_tags($htmlBlock->getStringContent());
27+
$markdomHandler->onBlockBegin(BlockType::TYPE_PARAGRAPH);
28+
$markdomHandler->onParagraphBlockBegin();
29+
$markdomHandler->onContentsBegin();
30+
$markdomHandler->onContentBegin(ContentType::TYPE_TEXT);
31+
$markdomHandler->onTextContent($plaintext);
32+
$markdomHandler->onContentEnd(ContentType::TYPE_TEXT);
33+
$markdomHandler->onContentsEnd();
34+
$markdomHandler->onParagraphBlockEnd();
35+
$markdomHandler->onBlockEnd(BlockType::TYPE_PARAGRAPH);
36+
// FIXME: Why the hack does the CommonmarkDispatcher not add a blank line after this paragraph?!
37+
}
38+
39+
/**
40+
* @param HtmlInline $htmlInline
41+
* @param HandlerInterface $markdomHandler
42+
* @return void
43+
*/
44+
public function handleInlineHtml(HtmlInline $htmlInline, HandlerInterface $markdomHandler)
45+
{
46+
}
47+
48+
}

src/Handler/CommonmarkUtil/HandlerDelimiter.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
*
88
* @package Markdom\Handler\CommonmarkUtil
99
*/
10-
class HandlerDelimiter
10+
final class HandlerDelimiter
1111
{
1212

1313
/**

0 commit comments

Comments
 (0)