Skip to content

Instantly share code, notes, and snippets.

@fiddlerwoaroof
Last active January 30, 2016 01:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fiddlerwoaroof/b24809107dfb81dc6c8d to your computer and use it in GitHub Desktop.
Save fiddlerwoaroof/b24809107dfb81dc6c8d to your computer and use it in GitHub Desktop.
Markdown Test Case
{
"name": "fiddlerwoaroof/markdown_case",
"require": {
"league/commonmark": "^0.13.0"
},
"authors": [
{
"name": "fiddlerwoaroof",
"email": "fiddlerwoaroof@gmail.com"
}
],
"minimum-stability": "dev"
}
{
"_readme": [
"This file locks the dependencies of your project to a known state",
"Read more about it at https://getcomposer.org/doc/01-basic-usage.md#composer-lock-the-lock-file",
"This file is @generated automatically"
],
"hash": "37a2e934f1fcd85aa7f0f369c5b02bd3",
"content-hash": "ea91e5f62f3e99ff92dd96ba15db5a17",
"packages": [
{
"name": "league/commonmark",
"version": "0.13.0",
"source": {
"type": "git",
"url": "https://github.com/thephpleague/commonmark.git",
"reference": "a4e93bc4fd1a8ff8f534040c4a07371ea5f4b484"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/thephpleague/commonmark/zipball/a4e93bc4fd1a8ff8f534040c4a07371ea5f4b484",
"reference": "a4e93bc4fd1a8ff8f534040c4a07371ea5f4b484",
"shasum": ""
},
"require": {
"ext-mbstring": "*",
"php": ">=5.4.8"
},
"replace": {
"colinodell/commonmark-php": "*"
},
"require-dev": {
"erusev/parsedown": "~1.0",
"jgm/commonmark": "0.24",
"michelf/php-markdown": "~1.4",
"mikehaertl/php-shellcommand": "~1.1.0",
"phpunit/phpunit": "~4.3|~5.0",
"scrutinizer/ocular": "^1.1",
"symfony/finder": "~2.3"
},
"suggest": {
"league/commonmark-extras": "Library of useful extensions including smart punctuation"
},
"bin": [
"bin/commonmark"
],
"type": "library",
"extra": {
"branch-alias": {
"dev-master": "0.14-dev"
}
},
"autoload": {
"psr-4": {
"League\\CommonMark\\": "src/"
}
},
"notification-url": "https://packagist.org/downloads/",
"license": [
"BSD-3-Clause"
],
"authors": [
{
"name": "Colin O'Dell",
"email": "colinodell@gmail.com",
"homepage": "http://www.colinodell.com",
"role": "Lead Developer"
}
],
"description": "Markdown parser for PHP based on the CommonMark spec",
"homepage": "https://github.com/thephpleague/commonmark",
"keywords": [
"commonmark",
"markdown",
"parser"
],
"time": "2016-01-14 04:29:54"
}
],
"packages-dev": [],
"aliases": [],
"minimum-stability": "dev",
"stability-flags": [],
"prefer-stable": false,
"prefer-lowest": false,
"platform": [],
"platform-dev": []
}
% php test.php
Event: League\CommonMark\Node\NodeWalkerEvent Node: League\CommonMark\Block\Element\Document
Event: League\CommonMark\Node\NodeWalkerEvent Node: League\CommonMark\Block\Element\Paragraph
Event: League\CommonMark\Node\NodeWalkerEvent Node: League\CommonMark\Inline\Element\Text
Event: League\CommonMark\Node\NodeWalkerEvent Node: League\CommonMark\Inline\Element\Emphasis
Event: League\CommonMark\Node\NodeWalkerEvent Node: League\CommonMark\Inline\Element\Text
<p>some text <em>new</em> some more text <em>hi there again</em></p>
% php test.php
Event: League\CommonMark\Node\NodeWalkerEvent Node: League\CommonMark\Block\Element\Document
Event: League\CommonMark\Node\NodeWalkerEvent Node: League\CommonMark\Block\Element\Paragraph
Event: League\CommonMark\Node\NodeWalkerEvent Node: League\CommonMark\Inline\Element\Text
Event: League\CommonMark\Node\NodeWalkerEvent Node: League\CommonMark\Inline\Element\Emphasis
Event: League\CommonMark\Node\NodeWalkerEvent Node: League\CommonMark\Inline\Element\Text
Event: League\CommonMark\Node\NodeWalkerEvent Node: League\CommonMark\Inline\Element\Emphasis
Event: League\CommonMark\Node\NodeWalkerEvent Node: League\CommonMark\Inline\Element\Text
Event: League\CommonMark\Node\NodeWalkerEvent Node: League\CommonMark\Inline\Element\Emphasis
Event: League\CommonMark\Node\NodeWalkerEvent Node: League\CommonMark\Inline\Element\Text
Event: League\CommonMark\Node\NodeWalkerEvent Node: League\CommonMark\Inline\Element\Emphasis
Event: League\CommonMark\Node\NodeWalkerEvent Node: League\CommonMark\Block\Element\Paragraph
Event: League\CommonMark\Node\NodeWalkerEvent Node: League\CommonMark\Block\Element\Document
<p>some text <em>hi there</em> some more text <em>hi there again</em></p>
<?php
require_once __DIR__ . '/vendor/autoload.php';
use \League\CommonMark\DocParser;
use \League\CommonMark\Environment;
use \League\CommonMark\HtmlRenderer;
use \League\CommonMark\Inline\Element\Emphasis;
use \League\CommonMark\Inline\Element\Text;
use \League\Uri\Schemes\Http as HTTPUri;
$markdown = "some text *hi there* some more text *hi there again*";
$environment = Environment::createCommonMarkEnvironment();
$parser = new DocParser($environment);
$htmlRenderer = new HtmlRenderer($environment);
$ast = $parser->parse($markdown);
$walker = $ast->walker();
while ($event = $walker->next()) {
$node = $event->getNode();
$eventClass = get_class($event);
$nodeClass = get_class($node);
echo "Event: $eventClass Node: $nodeClass\n";
if ($node instanceof Emphasis) {
/// This line causes an early exit:
$newNode = new Text("new");
$node->firstChild()->replaceWith($newNode);
$walker->resumeAt($newNode, $entering=true);
}
}
echo $htmlRenderer->renderBlock($ast)."\n";
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment