From 5d9f62a17b81dec913420605ef4e669033234822 Mon Sep 17 00:00:00 2001 From: Kazuyuki Hayashi Date: Fri, 15 Nov 2013 16:59:02 +0900 Subject: [PATCH] better extension example --- README.md | 61 +++++++++++++++++++++++++++++++++++-------------------- 1 file changed, 39 insertions(+), 22 deletions(-) diff --git a/README.md b/README.md index ed26828..371e57b 100644 --- a/README.md +++ b/README.md @@ -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 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 +addExtension(new MentionExtension()); +echo $ciconia->render('@kzykhys my email address is example@example.com!'); +``` + +Output + +``` html +

@kzykhys my email address is example@example.com!

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