├── LICENSE ├── composer.json └── src ├── TwitterExtension.php ├── TwitterParser.php └── TwitterServiceProvider.php /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2017-2018 Alt Three Services Limited 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in 13 | all copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 21 | THE SOFTWARE. 22 | -------------------------------------------------------------------------------- /composer.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "alt-three/twitter", 3 | "description": "A Twitter handle parser for Laravel 5", 4 | "keywords": ["twitter", "parser", "markdown", "Alt Three"], 5 | "license": "MIT", 6 | "authors": [ 7 | { 8 | "name": "Alt Three", 9 | "email": "support@alt-three.com" 10 | } 11 | ], 12 | "require": { 13 | "php": "^7.1.3", 14 | "illuminate/contracts": "5.5.*|5.6.*|5.7.*", 15 | "illuminate/support": "5.5.*|5.6.*|5.7.*", 16 | "league/commonmark": "^0.18" 17 | }, 18 | "require-dev": { 19 | "graham-campbell/analyzer": "^2.1", 20 | "graham-campbell/markdown": "^10.2", 21 | "graham-campbell/testbench": "^5.1", 22 | "phpunit/phpunit": "^6.5|^7.0" 23 | }, 24 | "autoload": { 25 | "psr-4": { 26 | "AltThree\\Twitter\\": "src/" 27 | } 28 | }, 29 | "autoload-dev": { 30 | "psr-4": { 31 | "AltThree\\Tests\\Twitter\\": "tests/" 32 | } 33 | }, 34 | "config": { 35 | "preferred-install": "dist" 36 | }, 37 | "extra": { 38 | "branch-alias": { 39 | "dev-master": "3.1-dev" 40 | }, 41 | "laravel": { 42 | "providers": [ 43 | "AltThree\\Twitter\\TwitterServiceProvider" 44 | ] 45 | } 46 | }, 47 | "minimum-stability": "dev", 48 | "prefer-stable": true 49 | } 50 | -------------------------------------------------------------------------------- /src/TwitterExtension.php: -------------------------------------------------------------------------------- 1 | 22 | */ 23 | class TwitterExtension extends Extension 24 | { 25 | /** 26 | * The twitter parser. 27 | * 28 | * @var \AltThree\Twitter\TwitterParser 29 | */ 30 | protected $parser; 31 | 32 | /** 33 | * Create a new twitter parser instance. 34 | * 35 | * @param \AltThree\Twitter\TwitterParser $parser 36 | * 37 | * @return void 38 | */ 39 | public function __construct(TwitterParser $parser) 40 | { 41 | $this->parser = $parser; 42 | } 43 | 44 | /** 45 | * Returns a list of inline parsers to add to the existing list. 46 | * 47 | * @return \League\CommonMark\Inline\Parser\InlineParserInterface[] 48 | */ 49 | public function getInlineParsers() 50 | { 51 | return [$this->parser]; 52 | } 53 | } 54 | -------------------------------------------------------------------------------- /src/TwitterParser.php: -------------------------------------------------------------------------------- 1 | 24 | */ 25 | class TwitterParser extends AbstractInlineParser 26 | { 27 | /** 28 | * Get the characters that must be matched. 29 | * 30 | * @return string[] 31 | */ 32 | public function getCharacters() 33 | { 34 | return ['@']; 35 | } 36 | 37 | /** 38 | * Parse a line and determine if it contains a handle. 39 | * 40 | * If it does, then we do the necessary. 41 | * 42 | * @param \League\CommonMark\InlineParserContext $inlineContext 43 | * 44 | * @return bool 45 | */ 46 | public function parse(InlineParserContext $inlineContext) 47 | { 48 | $cursor = $inlineContext->getCursor(); 49 | 50 | $previousChar = $cursor->peek(-1); 51 | if ($previousChar !== null && $previousChar !== ' ') { 52 | return false; 53 | } 54 | 55 | $previousState = $cursor->saveState(); 56 | $cursor->advance(); 57 | 58 | $handle = $cursor->match('/^[A-Za-z0-9_]{1,15}(?!\w)/'); 59 | if (empty($handle)) { 60 | $cursor->restoreState($previousState); 61 | 62 | return false; 63 | } 64 | 65 | $profileUrl = "https://twitter.com/{$handle}"; 66 | $inlineContext->getContainer()->appendChild(new Link($profileUrl, '@'.$handle)); 67 | 68 | return true; 69 | } 70 | } 71 | -------------------------------------------------------------------------------- /src/TwitterServiceProvider.php: -------------------------------------------------------------------------------- 1 | 22 | */ 23 | class TwitterServiceProvider extends ServiceProvider 24 | { 25 | /** 26 | * Register the service provider. 27 | * 28 | * @return void 29 | */ 30 | public function register() 31 | { 32 | $this->registerParser(); 33 | } 34 | 35 | /** 36 | * Register the parser class. 37 | * 38 | * @return void 39 | */ 40 | protected function registerParser() 41 | { 42 | $this->app->singleton(TwitterParser::class, function () { 43 | return new TwitterParser(); 44 | }); 45 | } 46 | 47 | /** 48 | * Get the services provided by the provider. 49 | * 50 | * @return string[] 51 | */ 52 | public function provides() 53 | { 54 | return [ 55 | // 56 | ]; 57 | } 58 | } 59 | --------------------------------------------------------------------------------