Skip to content
This repository was archived by the owner on Apr 4, 2020. It is now read-only.

Commit 1e90bb2

Browse files
authored
Deprecate extension (#14)
1 parent 9b10753 commit 1e90bb2

File tree

7 files changed

+67
-192
lines changed

7 files changed

+67
-192
lines changed

CHANGELOG.md

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,12 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
77

88
## [Unreleased][unreleased]
99

10+
## [1.1.0] - 2020-04-01
11+
12+
### Deprecated
13+
14+
**This extension has been deprecated**. All of its functionality now exists in league/commonmark 1.3+ under the `League\CommonMark\Extension\Autolink` namespace.
15+
1016
## [1.0.1] - 2020-01-13
1117

1218
### Fixed
@@ -75,7 +81,8 @@ This release brings the email and URL autolink processors into alignment with th
7581

7682
Initial release!
7783

78-
[unreleased]: https://github.com/thephpleague/commonmark-ext-autolink/compare/v1.0.1...HEAD
84+
[unreleased]: https://github.com/thephpleague/commonmark-ext-autolink/compare/v1.1.0...HEAD
85+
[1.1.0]: https://github.com/thephpleague/commonmark-ext-autolink/compare/v1.0.1...v1.1.0
7986
[1.0.1]: https://github.com/thephpleague/commonmark-ext-autolink/compare/v1.0.0...v1.0.1
8087
[1.0.0]: https://github.com/thephpleague/commonmark-ext-autolink/compare/v1.0.0-beta3...v1.0.0
8188
[1.0.0-beta2]: https://github.com/thephpleague/commonmark-ext-autolink/compare/v1.0.0-beta2...v1.0.0-beta3

README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,12 @@
77
[![Quality Score][ico-code-quality]][link-code-quality]
88
[![Total Downloads][ico-downloads]][link-downloads]
99

10+
## DEPRECATED
11+
12+
**This extension has been deprecated**. All of its functionality now exists in [`league/commonmark`][link-league-commonmark] 1.3+ under the `League\CommonMark\Extension\Autolink` namespace, so you should upgrade to that version and use that bundled extension instead of this one.
13+
14+
## Overview
15+
1016
This extension adds [GFM-style autolinking][link-gfm-spec-autolinking] to the [`league/commonmark` Markdown parser for PHP][link-league-commonmark]. It automatically link URLs and email addresses even when the CommonMark `<...>` autolink syntax is not used.
1117

1218
It also provides a parser to autolink `@mentions` to Twitter, Github, or any custom service you wish, though this is disabled by default.

composer.json

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
],
1616
"require": {
1717
"php" : "^7.1",
18-
"league/commonmark": "^1.0"
18+
"league/commonmark": "^1.3"
1919
},
2020
"require-dev": {
2121
"phpunit/phpunit": "^7.5"
@@ -35,7 +35,8 @@
3535
},
3636
"extra": {
3737
"branch-alias": {
38-
"dev-master": "1.1-dev"
38+
"dev-master": "1.2-dev"
3939
}
40-
}
40+
},
41+
"abandoned": "league/commonmark"
4142
}

src/AutolinkExtension.php

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,24 @@
1212
namespace League\CommonMark\Ext\Autolink;
1313

1414
use League\CommonMark\ConfigurableEnvironmentInterface;
15-
use League\CommonMark\Event\DocumentParsedEvent;
15+
use League\CommonMark\Extension\Autolink\AutolinkExtension as CoreExtension;
1616
use League\CommonMark\Extension\ExtensionInterface;
1717

18+
/**
19+
* @deprecated The league/commonmark-ext-autolink extension is now deprecated. All functionality has been moved into league/commonmark 1.3+, so use that instead.
20+
*/
1821
final class AutolinkExtension implements ExtensionInterface
1922
{
23+
private $coreExtension;
24+
25+
public function __construct()
26+
{
27+
$this->coreExtension = new CoreExtension();
28+
@trigger_error(sprintf('league/commonmark-ext-autolink is deprecated; use %s from league/commonmark 1.3+ instead', CoreExtension::class), E_USER_DEPRECATED);
29+
}
30+
2031
public function register(ConfigurableEnvironmentInterface $environment)
2132
{
22-
$environment->addEventListener(DocumentParsedEvent::class, new EmailAutolinkProcessor());
23-
$environment->addEventListener(DocumentParsedEvent::class, new UrlAutolinkProcessor());
33+
$this->coreExtension->register($environment);
2434
}
2535
}

src/EmailAutolinkProcessor.php

Lines changed: 13 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -12,67 +12,30 @@
1212
namespace League\CommonMark\Ext\Autolink;
1313

1414
use League\CommonMark\Event\DocumentParsedEvent;
15-
use League\CommonMark\Inline\Element\Link;
16-
use League\CommonMark\Inline\Element\Text;
15+
use League\CommonMark\Extension\Autolink\EmailAutolinkProcessor as CoreProcessor;
1716

17+
/**
18+
* @deprecated The league/commonmark-ext-autolink extension is now deprecated. All functionality has been moved into league/commonmark 1.3+, so use that instead.
19+
*/
1820
final class EmailAutolinkProcessor
1921
{
2022
const REGEX = '/([A-Za-z0-9.\-_+]+@[A-Za-z0-9\-_]+\.[A-Za-z0-9\-_.]+)/';
2123

24+
private $coreProcessor;
25+
26+
public function __construct()
27+
{
28+
@trigger_error(sprintf('league/commonmark-ext-autolink is deprecated; use %s from league/commonmark 1.3+ instead', CoreProcessor::class), E_USER_DEPRECATED);
29+
$this->coreProcessor = new CoreProcessor();
30+
}
31+
2232
/**
2333
* @param DocumentParsedEvent $e
2434
*
2535
* @return void
2636
*/
2737
public function __invoke(DocumentParsedEvent $e)
2838
{
29-
$walker = $e->getDocument()->walker();
30-
31-
while ($event = $walker->next()) {
32-
$node = $event->getNode();
33-
if ($node instanceof Text && !($node->parent() instanceof Link)) {
34-
self::processAutolinks($node);
35-
}
36-
}
37-
}
38-
39-
private static function processAutolinks(Text $node)
40-
{
41-
$contents = \preg_split(self::REGEX, $node->getContent(), -1, PREG_SPLIT_DELIM_CAPTURE);
42-
43-
if (\count($contents) === 1) {
44-
return;
45-
}
46-
47-
$leftovers = '';
48-
foreach ($contents as $i => $content) {
49-
if ($i % 2 === 0) {
50-
$text = $leftovers . $content;
51-
if ($text !== '') {
52-
$node->insertBefore(new Text($leftovers . $content));
53-
}
54-
55-
$leftovers = '';
56-
continue;
57-
}
58-
59-
// Does the URL end with punctuation that should be stripped?
60-
if (\substr($content, -1) === '.') {
61-
// Add the punctuation later
62-
$content = \substr($content, 0, -1);
63-
$leftovers = '.';
64-
}
65-
66-
// The last character cannot be - or _
67-
if (\in_array(\substr($content, -1), ['-', '_'])) {
68-
$node->insertBefore(new Text($content . $leftovers));
69-
$leftovers = '';
70-
continue;
71-
}
72-
73-
$node->insertBefore(new Link('mailto:' . $content, $content));
74-
}
75-
76-
$node->detach();
39+
$this->coreProcessor->__invoke($e);
7740
}
7841
}

src/InlineMentionParser.php

Lines changed: 15 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -11,80 +11,54 @@
1111

1212
namespace League\CommonMark\Ext\Autolink;
1313

14-
use League\CommonMark\Inline\Element\Link;
14+
use League\CommonMark\Extension\Autolink\InlineMentionParser as CoreParser;
1515
use League\CommonMark\Inline\Parser\InlineParserInterface;
1616
use League\CommonMark\InlineParserContext;
1717

18+
/**
19+
* @deprecated The league/commonmark-ext-autolink extension is now deprecated. All functionality has been moved into league/commonmark 1.3+, so use that instead.
20+
*/
1821
final class InlineMentionParser implements InlineParserInterface
1922
{
20-
/** @var string */
21-
private $linkPattern;
22-
23-
/** @var string */
24-
private $handleRegex;
23+
private $coreParser;
2524

2625
/**
2726
* @param string $linkPattern
2827
* @param string $handleRegex
2928
*/
3029
public function __construct($linkPattern, $handleRegex = '/^[A-Za-z0-9_]+(?!\w)/')
3130
{
32-
$this->linkPattern = $linkPattern;
33-
$this->handleRegex = $handleRegex;
31+
@trigger_error(sprintf('league/commonmark-ext-autolink is deprecated; use %s from league/commonmark 1.3+ instead', CoreParser::class), E_USER_DEPRECATED);
32+
$this->coreParser = new CoreParser($linkPattern, $handleRegex);
3433
}
3534

3635
/**
3736
* {@inheritdoc}
3837
*/
3938
public function getCharacters(): array
4039
{
41-
return ['@'];
40+
return $this->coreParser->getCharacters();
4241
}
4342

4443
/**
4544
* {@inheritdoc}
4645
*/
4746
public function parse(InlineParserContext $inlineContext): bool
4847
{
49-
$cursor = $inlineContext->getCursor();
50-
51-
// The @ symbol must not have any other characters immediately prior
52-
$previousChar = $cursor->peek(-1);
53-
if ($previousChar !== null && $previousChar !== ' ') {
54-
// peek() doesn't modify the cursor, so no need to restore state first
55-
return false;
56-
}
57-
58-
// Save the cursor state in case we need to rewind and bail
59-
$previousState = $cursor->saveState();
60-
61-
// Advance past the @ symbol to keep parsing simpler
62-
$cursor->advance();
63-
64-
// Parse the handle
65-
$handle = $cursor->match($this->handleRegex);
66-
if (empty($handle)) {
67-
// Regex failed to match; this isn't a valid Twitter handle
68-
$cursor->restoreState($previousState);
69-
70-
return false;
71-
}
72-
73-
$url = \sprintf($this->linkPattern, $handle);
74-
75-
$inlineContext->getContainer()->appendChild(new Link($url, '@' . $handle));
76-
77-
return true;
48+
return $this->coreParser->parse($inlineContext);
7849
}
7950

8051
public static function createTwitterHandleParser()
8152
{
82-
return new self('https://twitter.com/%s', '/^[A-Za-z0-9_]{1,15}(?!\w)/');
53+
@trigger_error(sprintf('league/commonmark-ext-autolink is deprecated; use %s from league/commonmark 1.3+ instead', CoreParser::class), E_USER_DEPRECATED);
54+
55+
return CoreParser::createTwitterHandleParser();
8356
}
8457

8558
public static function createGithubHandleParser()
8659
{
87-
// RegEx adapted from https://github.com/shinnn/github-username-regex/blob/master/index.js
88-
return new self('https://www.github.com/%s', '/^[a-z\d](?:[a-z\d]|-(?=[a-z\d])){0,38}(?!\w)/');
60+
@trigger_error(sprintf('league/commonmark-ext-autolink is deprecated; use %s from league/commonmark 1.3+ instead', CoreParser::class), E_USER_DEPRECATED);
61+
62+
return CoreParser::createGithubHandleParser();
8963
}
9064
}

src/UrlAutolinkProcessor.php

Lines changed: 8 additions & 94 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,11 @@
1212
namespace League\CommonMark\Ext\Autolink;
1313

1414
use League\CommonMark\Event\DocumentParsedEvent;
15-
use League\CommonMark\Inline\Element\Link;
16-
use League\CommonMark\Inline\Element\Text;
15+
use League\CommonMark\Extension\Autolink\UrlAutolinkProcessor as CoreProcessor;
1716

17+
/**
18+
* @deprecated The league/commonmark-ext-autolink extension is now deprecated. All functionality has been moved into league/commonmark 1.3+, so use that instead.
19+
*/
1820
final class UrlAutolinkProcessor
1921
{
2022
// RegEx adapted from https://github.com/symfony/symfony/blob/4.2/src/Symfony/Component/Validator/Constraints/UrlValidator.php
@@ -41,11 +43,12 @@ final class UrlAutolinkProcessor
4143
(?:\# (?:[\pL\pN\-._\~!$&\'()*+,;=:@/?]|%%[0-9A-Fa-f]{2})* )? # a fragment (optional)
4244
)~ixu';
4345

44-
private $finalRegex;
46+
private $coreProcessor;
4547

4648
public function __construct(array $allowedProtocols = ['http', 'https', 'ftp'])
4749
{
48-
$this->finalRegex = \sprintf(self::REGEX, \implode('|', $allowedProtocols));
50+
@trigger_error(sprintf('league/commonmark-ext-autolink is deprecated; use %s from league/commonmark 1.3+ instead', CoreProcessor::class), E_USER_DEPRECATED);
51+
$this->coreProcessor = new CoreProcessor($allowedProtocols);
4952
}
5053

5154
/**
@@ -55,95 +58,6 @@ public function __construct(array $allowedProtocols = ['http', 'https', 'ftp'])
5558
*/
5659
public function __invoke(DocumentParsedEvent $e)
5760
{
58-
$walker = $e->getDocument()->walker();
59-
60-
while ($event = $walker->next()) {
61-
$node = $event->getNode();
62-
if ($node instanceof Text && !($node->parent() instanceof Link)) {
63-
self::processAutolinks($node, $this->finalRegex);
64-
}
65-
}
66-
}
67-
68-
private static function processAutolinks(Text $node, $regex)
69-
{
70-
$contents = \preg_split($regex, $node->getContent(), -1, PREG_SPLIT_DELIM_CAPTURE);
71-
72-
if (\count($contents) === 1) {
73-
return;
74-
}
75-
76-
$leftovers = '';
77-
foreach ($contents as $i => $content) {
78-
// Even-indexed elements are things before/after the URLs
79-
if ($i % 2 === 0) {
80-
// Insert any left-over characters here as well
81-
$text = $leftovers . $content;
82-
if ($text !== '') {
83-
$node->insertBefore(new Text($leftovers . $content));
84-
}
85-
86-
$leftovers = '';
87-
continue;
88-
}
89-
90-
$leftovers = '';
91-
92-
// Does the URL end with punctuation that should be stripped?
93-
if (\preg_match('/(.+)([?!.,:*_~]+)$/', $content, $matches)) {
94-
// Add the punctuation later
95-
$content = $matches[1];
96-
$leftovers = $matches[2];
97-
}
98-
99-
// Does the URL end with something that looks like an entity reference?
100-
if (\preg_match('/(.+)(&[A-Za-z0-9]+;)$/', $content, $matches)) {
101-
$content = $matches[1];
102-
$leftovers = $matches[2] . $leftovers;
103-
}
104-
105-
// Does the URL need its closing paren chopped off?
106-
if (\substr($content, -1) === ')' && self::hasMoreCloserParensThanOpeners($content)) {
107-
$content = \substr($content, 0, -1);
108-
$leftovers = ')' . $leftovers;
109-
}
110-
111-
self::addLink($node, $content);
112-
}
113-
114-
$node->detach();
115-
}
116-
117-
private static function addLink(Text $node, $url)
118-
{
119-
// Auto-prefix 'http://' onto 'www' URLs
120-
if (\substr($url, 0, 4) === 'www.') {
121-
$node->insertBefore(new Link('http://' . $url, $url));
122-
123-
return;
124-
}
125-
126-
$node->insertBefore(new Link($url, $url));
127-
}
128-
129-
/**
130-
* @param string $content
131-
*
132-
* @return bool
133-
*/
134-
private static function hasMoreCloserParensThanOpeners($content)
135-
{
136-
// Scan the entire autolink for the total number of parentheses.
137-
// If there is a greater number of closing parentheses than opening ones,
138-
// we don’t consider the last character part of the autolink, in order to
139-
// facilitate including an autolink inside a parenthesis.
140-
\preg_match_all('/[()]/', $content, $matches);
141-
142-
$charCount = ['(' => 0, ')' => 0];
143-
foreach ($matches[0] as $char) {
144-
$charCount[$char]++;
145-
}
146-
147-
return $charCount[')'] > $charCount['('];
61+
$this->coreProcessor->__invoke($e);
14862
}
14963
}

0 commit comments

Comments
 (0)