Skip to content

Commit

Permalink
better extension example
Browse files Browse the repository at this point in the history
  • Loading branch information
kzykhys committed Nov 15, 2013
1 parent 9bd8b4e commit 5d9f62a
Showing 1 changed file with 39 additions and 22 deletions.
61 changes: 39 additions & 22 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -135,47 +135,64 @@ If your name is the same as one of core extension, it will be replaced by your e

### Extension Example

This sample extension turns `@username ` mentions into links.

``` php
<?php

use Ciconia\Common\Text;
use Ciconia\Extension\ExtensionInterface;
use Ciconia\Markdown;

class YourExtension implements ExtensionInterface
class MentionExtension implements ExtensionInterface
{
// Necessary if your extension calls other events
private $markdown;

//@implement
public function register(Markdown $markdown)
/**
* {@inheritdoc}
*/
public function register(\Ciconia\Markdown $markdown)
{
// Necessary if your extension calls another event
$this->markdown = $markdown;

// Register a callback
$markdown->on('block', array($this, 'processBlock'));
$markdown->on('inline', [$this, 'processMentions']);
}

//@implement
public function getName()
/**
* @param Text $text
*/
public function processMentions(Text $text)
{
return 'yourExtension';
// Turn @username into [@username](http://example.com/user/username)
$text->replace('/(?:^|[^a-zA-Z0-9.])@([A-Za-z]+[A-Za-z0-9]+)/', function (Text $w, Text $username) {
return '[@' . $username . '](http://example.com/user/' . $username . ')';
});
}

public function processBlock(Text $text)
/**
* {@inheritdoc}
*/
public function getName()
{
// Do regexp's
$text->replace('/foo(bar)(baz)/', function (Text $whole, Text $bar, Text $baz) {
// You can call other events registered by other extension
$this->markdown->emit('inline', $bar);

return $baz->upper();
});
return 'mention';
}
}
```

Register your extension.

``` php
<?php

require __DIR__ . '/vendor/autoload.php';

$ciconia = new \Ciconia\Ciconia();
$ciconia->addExtension(new MentionExtension());
echo $ciconia->render('@kzykhys my email address is [email protected]!');
```

Output

``` html
<p><a href="http://example.com/user/kzykhys">@kzykhys</a> my email address is [email protected]!</p>
```

Each extension handles string as a `Text` object. See [API section of kzykhys/Text][textapi].

### Events
Expand Down

0 comments on commit 5d9f62a

Please sign in to comment.