Skip to content

Commit

Permalink
Synced to 307ac0e
Browse files Browse the repository at this point in the history
  • Loading branch information
JoshyPHP committed Jan 4, 2018
1 parent 88fb9bf commit 4245b70
Show file tree
Hide file tree
Showing 14 changed files with 400 additions and 480 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "s9e/text-formatter",
"version": "0.14.0-dev",
"version": "1.0.0",
"type": "library",
"description": "Multi-purpose text formatting and markup library. Plugins offer support for BBCodes, Markdown, emoticons, HTML, embedding media (YouTube, etc...), enhanced typography and more.",
"homepage": "https://github.com/s9e/TextFormatter/",
Expand Down
2 changes: 1 addition & 1 deletion src/Bundles/Fatdown.php

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion src/Bundles/Forum.php

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion src/Bundles/MediaPack.php

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion src/Configurator/Bundles/Forum.php
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ public function configure(Configurator $configurator)
foreach ($sites as $siteId)
{
$configurator->MediaEmbed->add($siteId);
$configurator->BBCodes->add($siteId, ['contentAttributes' => ['url']]);
$configurator->BBCodes->add($siteId, ['contentAttributes' => ['id', 'url']]);
}
$configurator->Autoemail;
$configurator->Autolink;
Expand Down
213 changes: 96 additions & 117 deletions src/Plugins/MediaEmbed/Configurator.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,56 +8,33 @@
namespace s9e\TextFormatter\Plugins\MediaEmbed;
use InvalidArgumentException;
use RuntimeException;
use s9e\TextFormatter\Configurator\Helpers\RegexpBuilder;
use s9e\TextFormatter\Configurator\Items\Attribute;
use s9e\TextFormatter\Configurator\Items\AttributePreprocessor;
use s9e\TextFormatter\Configurator\Items\Regexp;
use s9e\TextFormatter\Configurator\Items\Tag;
use s9e\TextFormatter\Configurator\JavaScript\Dictionary;
use s9e\TextFormatter\Plugins\ConfiguratorBase;
use s9e\TextFormatter\Plugins\MediaEmbed\Configurator\Collections\CachedDefinitionCollection;
use s9e\TextFormatter\Plugins\MediaEmbed\Configurator\Collections\SiteCollection;
use s9e\TextFormatter\Plugins\MediaEmbed\Configurator\TemplateBuilder;
class Configurator extends ConfiguratorBase
{
public $allowedFilters = [
'hexdec',
'stripslashes',
'urldecode'
];
protected $collection;
public $allowedFilters = ['stripslashes', 'urldecode'];
protected $createMediaBBCode = \true;
public $defaultSites;
protected $quickMatch = '://';
protected $regexp = '/\\bhttps?:\\/\\/[^["\'\\s]+/Si';
protected $sites = [];
protected $tagName = 'MEDIA';
protected $templateBuilder;
protected function setUp()
{
$this->collection = new SiteCollection;
$this->configurator->registeredVars['mediasites'] = $this->collection;
$tag = $this->configurator->tags->add($this->tagName);
$tag->rules->autoClose();
$tag->rules->denyChild($this->tagName);
$tag->filterChain->clear();
$tag->filterChain
->append(__NAMESPACE__ . '\\Parser::filterTag')
->addParameterByName('parser')
->addParameterByName('mediasites')
->setJS(\file_get_contents(__DIR__ . '/Parser/tagFilter.js'));
if ($this->createMediaBBCode)
$this->configurator->BBCodes->set(
$this->tagName,
[
'contentAttributes' => ['url'],
'defaultAttribute' => 'site'
]
);
if (!isset($this->defaultSites))
$this->defaultSites = new CachedDefinitionCollection;
$this->defaultSites = new CachedDefinitionCollection;
$this->templateBuilder = new TemplateBuilder;
$this->createMediaTag();
if ($this->createMediaBBCode)
$this->configurator->BBCodes->set($this->tagName, ['contentAttributes' => ['url']]);
}
public function asConfig()
{
if (!\count($this->collection))
if (empty($this->sites))
return;
return [
'quickMatch' => $this->quickMatch,
Expand All @@ -68,105 +45,107 @@ public function asConfig()
public function add($siteId, array $siteConfig = \null)
{
$siteId = $this->normalizeId($siteId);
$siteConfig = (isset($siteConfig)) ? $this->defaultSites->normalizeValue($siteConfig) : $this->defaultSites->get($siteId);
$this->collection[$siteId] = $siteConfig;
$tag = new Tag;
$tag->rules->allowChild('URL');
$tag->rules->autoClose();
$tag->rules->denyChild($siteId);
$tag->rules->denyChild($this->tagName);
$attributes = [
'url' => ['type' => 'url']
];
$attributes += $this->addScrapes($tag, $siteConfig['scrape']);
foreach ($siteConfig['extract'] as $regexp)
{
$attrRegexps = $tag->attributePreprocessors->add('url', $regexp)->getAttributes();
foreach ($attrRegexps as $attrName => $attrRegexp)
$attributes[$attrName]['regexp'] = $attrRegexp;
}
if (isset($siteConfig['attributes']))
foreach ($siteConfig['attributes'] as $attrName => $attrConfig)
foreach ($attrConfig as $configName => $configValue)
$attributes[$attrName][$configName] = $configValue;
$hasRequiredAttribute = \false;
foreach ($attributes as $attrName => $attrConfig)
{
$attribute = $this->addAttribute($tag, $attrName, $attrConfig);
$hasRequiredAttribute |= $attribute->required;
}
if (isset($attributes['id']['regexp']))
{
$attrRegexp = \preg_replace('(\\^(.*)\\$)s', "^(?'id'$1)$", $attributes['id']['regexp']);
$tag->attributePreprocessors->add('url', $attrRegexp);
}
if (!$hasRequiredAttribute)
$tag->filterChain
->append([__NAMESPACE__ . '\\Parser', 'hasNonDefaultAttribute'])
->setJS(\file_get_contents(__DIR__ . '/Parser/hasNonDefaultAttribute.js'));
$tag->template = $this->templateBuilder->build($siteId, $siteConfig);
if (isset($siteConfig))
$siteConfig = $this->defaultSites->normalizeValue($siteConfig);
else
$siteConfig = $this->defaultSites->get($siteId);
$siteConfig['extract'] = $this->convertRegexps($siteConfig['extract']);
$siteConfig['scrape'] = $this->convertScrapes($siteConfig['scrape']);
$this->checkAttributeFilters($siteConfig['attributes']);
$tag = new Tag([
'attributes' => $this->getAttributesConfig($siteConfig),
'rules' => [
'allowChild' => 'URL',
'autoClose' => \true,
'denyChild' => [$siteId, $this->tagName]
],
'template' => $this->templateBuilder->build($siteId, $siteConfig)
]);
$this->configurator->templateNormalizer->normalizeTag($tag);
$this->configurator->templateChecker->checkTag($tag);
$this->configurator->tags->add($siteId, $tag);
$this->sites[$siteId] = $siteConfig;
return $tag;
}
protected function addAttribute(Tag $tag, $attrName, array $attrConfig)
public function finalize()
{
$attribute = $tag->attributes->add($attrName);
if (isset($attrConfig['preFilter']))
$this->appendFilter($attribute, $attrConfig['preFilter']);
if (isset($attrConfig['type']))
$hosts = [];
$sites = [];
foreach ($this->sites as $siteId => $siteConfig)
{
$filter = $this->configurator->attributeFilters['#' . $attrConfig['type']];
$attribute->filterChain->append($filter);
foreach ($siteConfig['host'] as $host)
$hosts[$host] = $siteId;
$sites[$siteId] = [$siteConfig['extract'], $siteConfig['scrape']];
}
elseif (isset($attrConfig['regexp']))
$attribute->filterChain->append('#regexp')->setRegexp($attrConfig['regexp']);
if (isset($attrConfig['required']))
$attribute->required = $attrConfig['required'];
else
$attribute->required = ($attrName === 'id');
if (isset($attrConfig['postFilter']))
$this->appendFilter($attribute, $attrConfig['postFilter']);
if (isset($attrConfig['defaultValue']))
$attribute->defaultValue = $attrConfig['defaultValue'];
return $attribute;
$this->configurator->registeredVars['MediaEmbed.hosts'] = new Dictionary($hosts);
$this->configurator->registeredVars['MediaEmbed.sites'] = new Dictionary($sites);
}
protected function addScrapes(Tag $tag, array $scrapes)
protected function checkAttributeFilters(array $attributes)
{
$attributes = [];
$scrapeConfig = [];
foreach ($scrapes as $scrape)
foreach ($attributes as $attrConfig)
{
$attrNames = [];
foreach ($scrape['extract'] as $extractRegexp)
{
$attributePreprocessor = new AttributePreprocessor($extractRegexp);
foreach ($attributePreprocessor->getAttributes() as $attrName => $attrRegexp)
{
$attrNames[] = $attrName;
$attributes[$attrName]['regexp'] = $attrRegexp;
}
}
$attrNames = \array_unique($attrNames);
\sort($attrNames);
$entry = [$scrape['match'], $scrape['extract'], $attrNames];
if (isset($scrape['url']))
$entry[] = $scrape['url'];
$scrapeConfig[] = $entry;
if (empty($attrConfig['filterChain']))
continue;
foreach ($attrConfig['filterChain'] as $filter)
if (\substr($filter, 0, 1) !== '#' && !\in_array($filter, $this->allowedFilters, \true))
throw new RuntimeException("Filter '$filter' is not allowed in media sites");
}
$tag->filterChain->insert(1, __NAMESPACE__ . '\\Parser::scrape')
->addParameterByName('scrapeConfig')
->addParameterByName('cacheDir')
->setVar('scrapeConfig', $scrapeConfig)
->setJS('returnTrue');
return $attributes;
}
protected function appendFilter(Attribute $attribute, $filter)
protected function convertRegexp($regexp)
{
if (!\in_array($filter, $this->allowedFilters, \true))
throw new RuntimeException("Filter '" . $filter . "' is not allowed");
$attribute->filterChain->append($this->configurator->attributeFilters[$filter]);
$regexp = new Regexp($regexp);
return [$regexp, $regexp->getCaptureNames()];
}
protected function convertRegexps(array $regexps)
{
return \array_map([$this, 'convertRegexp'], $regexps);
}
protected function convertScrapeConfig(array $config)
{
$config['extract'] = $this->convertRegexps($config['extract']);
$config['match'] = $this->convertRegexps($config['match']);
return $config;
}
protected function convertScrapes(array $scrapes)
{
return \array_map([$this, 'convertScrapeConfig'], $scrapes);
}
protected function createMediaTag()
{
$tag = $this->configurator->tags->add($this->tagName);
$tag->rules->autoClose();
$tag->rules->denyChild($this->tagName);
$tag->filterChain->clear();
$tag->filterChain
->append(__NAMESPACE__ . '\\Parser::filterTag')
->resetParameters()
->addParameterByName('tag')
->addParameterByName('parser')
->addParameterByName('MediaEmbed.hosts')
->addParameterByName('MediaEmbed.sites')
->addParameterByName('cacheDir')
->setJS(\file_get_contents(__DIR__ . '/Parser/tagFilter.js'));
}
protected function getAttributeNamesFromRegexps(array $regexps)
{
$attrNames = [];
foreach ($regexps as $_53d26d37)
{
list($regexp, $map) = $_53d26d37;
$attrNames += \array_flip(\array_filter($map));
}
return $attrNames;
}
protected function getAttributesConfig(array $siteConfig)
{
$attrNames = $this->getAttributeNamesFromRegexps($siteConfig['extract']);
foreach ($siteConfig['scrape'] as $scrapeConfig)
$attrNames += $this->getAttributeNamesFromRegexps($scrapeConfig['extract']);
$attributes = $siteConfig['attributes'] + \array_fill_keys(\array_keys($attrNames), []);
foreach ($attributes as &$attrConfig)
$attrConfig += ['required' => \false];
unset($attrConfig);
return $attributes;
}
protected function normalizeId($siteId)
{
Expand Down
Loading

0 comments on commit 4245b70

Please sign in to comment.