Opengraph and Twitter card now possible to select a default image, fixed regex on img abs

This commit is contained in:
Diego Najar 2017-10-07 02:45:03 +02:00
parent 26c59cea76
commit b8eec33515
4 changed files with 83 additions and 23 deletions

View File

@ -35,7 +35,7 @@ function buildPage($key) {
$contentRaw = $page->contentRaw(); $contentRaw = $page->contentRaw();
$content = Text::pre2htmlentities($contentRaw); // Parse pre code with htmlentities $content = Text::pre2htmlentities($contentRaw); // Parse pre code with htmlentities
$content = $Parsedown->text($content); // Parse Markdown $content = $Parsedown->text($content); // Parse Markdown
$content = Text::imgRel2Abs($content, HTML_PATH_UPLOADS); // Parse img src relative to absolute. $content = Text::imgRel2Abs($content, DOMAIN_UPLOADS); // Parse img src relative to absolute (with domain)
$page->setField('content', $content, true); $page->setField('content', $content, true);
// Pagebrake // Pagebrake

View File

@ -220,8 +220,8 @@ class Text {
public static function imgRel2Abs($string, $base) public static function imgRel2Abs($string, $base)
{ {
$pattern = "/<img([^>]*) src=\"([^http|https|ftp|\/\/][^\"]*)\"/"; $pattern = '/<img([^>]*)(src)=\"(?!https:)(?!http:)(?!\/\/)(.*?)\"(.*?)>/';
$replace = "<img\${1} src=\"".$base. "\${2}\""; $replace = "<img\${1} src=\"".$base."\${3}\" \${4}>";
return preg_replace($pattern, $replace, $string); return preg_replace($pattern, $replace, $string);
} }

View File

@ -2,25 +2,32 @@
class pluginOpenGraph extends Plugin { class pluginOpenGraph extends Plugin {
// Returns the first image from the HTML content public function init()
private function getImage($content)
{ {
$dom = new DOMDocument(); // Fields and default values for the database of this plugin
$dom->loadHTML('<meta http-equiv="content-type" content="text/html; charset=utf-8">'.$content); $this->dbFields = array(
$finder = new DomXPath($dom); 'defaultImage'=>''
);
$images = $finder->query("//img");
if($images->length>0) {
// First image from the list
$image = $images->item(0);
// Get value from attribute src
$imgSrc = $image->getAttribute('src');
// Returns the image src
return $imgSrc;
} }
return false; public function form()
{
global $Language;
$html = '<div>';
$html .= '<label>'.$Language->get('Default image').'</label>';
$html .= '<select name="defaultImage">';
$images = Filesystem::listFiles(PATH_UPLOADS);
foreach ($images as $image) {
$base = basename($image);
$html .= '<option value="'.$base.'" '.(($this->getValue('defaultImage')==$base)?'selected':'').'>'.$base.'</option>';
}
$html .= '</select>';
$html .= '</div>';
return $html;
} }
public function siteHead() public function siteHead()
@ -77,7 +84,9 @@ class pluginOpenGraph extends Plugin {
// Get the image from the content // Get the image from the content
$src = $this->getImage($content); $src = $this->getImage($content);
if ($src!==false) { if ($src!==false) {
$og['image'] = DOMAIN.$src; $og['image'] = $src;
} else {
$og['image'] = DOMAIN_UPLOADS.$this->getValue('defaultImage');
} }
} }
@ -85,4 +94,25 @@ class pluginOpenGraph extends Plugin {
return $html; return $html;
} }
// Returns the first image from the HTML content
private function getImage($content)
{
$dom = new DOMDocument();
$dom->loadHTML('<meta http-equiv="content-type" content="text/html; charset=utf-8">'.$content);
$finder = new DomXPath($dom);
$images = $finder->query("//img");
if($images->length>0) {
// First image from the list
$image = $images->item(0);
// Get value from attribute src
$imgSrc = $image->getAttribute('src');
// Returns the image src
return $imgSrc;
}
return false;
}
} }

View File

@ -2,6 +2,34 @@
class pluginTwitterCards extends Plugin { class pluginTwitterCards extends Plugin {
public function init()
{
// Fields and default values for the database of this plugin
$this->dbFields = array(
'defaultImage'=>''
);
}
public function form()
{
global $Language;
$html = '<div>';
$html .= '<label>'.$Language->get('Default image').'</label>';
$html .= '<select name="defaultImage">';
$images = Filesystem::listFiles(PATH_UPLOADS);
foreach ($images as $image) {
$base = basename($image);
$html .= '<option value="'.$base.'" '.(($this->getValue('defaultImage')==$base)?'selected':'').'>'.$base.'</option>';
}
$html .= '</select>';
$html .= '</div>';
return $html;
}
public function siteHead() public function siteHead()
{ {
global $Url; global $Url;
@ -50,7 +78,9 @@ class pluginTwitterCards extends Plugin {
// Get the image from the content // Get the image from the content
$src = $this->getImage($content); $src = $this->getImage($content);
if ($src!==false) { if ($src!==false) {
$data['image'] = DOMAIN.$src; $og['image'] = $src;
} else {
$og['image'] = DOMAIN_UPLOADS.$this->getValue('defaultImage');
} }
} }