Added support for custom inline images with markdown #10, no more twig needed!

This commit is contained in:
Andy Miller
2014-08-21 21:55:37 -06:00
parent 447abb928b
commit f3aec65624
8 changed files with 134 additions and 9 deletions

View File

@@ -0,0 +1,13 @@
<?php
namespace Grav\Common\Markdown;
class Markdown extends \Parsedown
{
use MarkdownGravLinkTrait;
function __construct($page)
{
$this->page = $page;
}
}

View File

@@ -0,0 +1,13 @@
<?php
namespace Grav\Common\Markdown;
class MarkdownExtra extends \ParsedownExtra
{
use MarkdownGravLinkTrait;
function __construct($page)
{
parent::__construct();
$this->page = $page;
}
}

View File

@@ -0,0 +1,83 @@
<?php
namespace Grav\Common\Markdown;
use Grav\Common\Debugger;
/**
* A trait to add some custom processing to the identifyLink() method in Parsedown and ParsedownExtra
*/
trait MarkdownGravLinkTrait
{
protected function identifyLink($Excerpt)
{
// Run the parent method to get the actual results
$Excerpt = parent::identifyLink($Excerpt);
$actions = array();
$command = '';
// if this is an image
if (isset($Excerpt['element']['attributes']['src'])) {
$alt = isset($Excerpt['element']['attributes']['alt']) ? $Excerpt['element']['attributes']['alt'] : '';
$title = isset($Excerpt['element']['attributes']['title']) ? $Excerpt['element']['attributes']['title'] : '';
//get the url and parse it
$url = parse_url(htmlspecialchars_decode($Excerpt['element']['attributes']['src']));
// if there is a query, then parse it and build action calls
if (isset($url['query'])) {
parse_str($url['query'], $actions);
foreach ($actions as $action => $params) {
// ignore any url or html actions
if (!in_array($action, ['html','url']))
$command .= '->' . $action . '(' . $params . ')';
}
}
// if there is no host set but there is a path, the file is local
if (!isset($url['host']) && isset($url['path'])) {
// get the media objects for this page
$media = $this->page->media();
// if there is a media file that matches the path referenced..
if (isset($media->images()[$url['path']])) {
// get the medium object
$medium = $media->images()[$url['path']];
// unless one of the actions is lightbox method get the url
if (!isset($actions['lightbox'])) {
$command .= '->url()';
} else {
$command .= '->lightboxRaw()';
}
// evaluate the commands to run against the media object
eval ('$src = $medium'.$command.';');
// set the src element with the new generated url
if (!isset($actions['lightbox']) && !is_array($src)) {
$Excerpt['element']['attributes']['src'] = $src;
} else {
// Create the custom lightbox element
$Element = array(
'name' => 'a',
'attributes' => array('rel' => $src['a_rel'], 'href' => $src['a_url']),
'handler' => 'element',
'text' => array(
'name' => 'img',
'attributes' => array('src' => $src['img_url'], 'alt' => $alt, 'title' => $title)
),
);
// Set the lightbox element on the Excerpt
$Excerpt['element'] = $Element;
}
}
}
}
return $Excerpt;
}
}

View File

@@ -202,6 +202,15 @@ class Medium extends Data
return $this->link($width, $height);
}
public function lightboxRaw($width = null, $height = null)
{
$url = $this->url();
$this->link($width, $height);
$lightbox_url = self::$grav['config']->get('system.base_url_relative') . '/'. $this->linkTarget;
return array('a_url' => $lightbox_url, 'a_rel' => 'lightbox', 'img_url' => $url);
}
/**
* Return link HTML for the medium.
*

View File

@@ -12,6 +12,8 @@ use Grav\Common\Data;
use Grav\Common\Uri;
use Grav\Common\Grav;
use Grav\Common\Taxonomy;
use Grav\Common\Markdown\Markdown;
use Grav\Common\Markdown\MarkdownExtra;
use Grav\Component\EventDispatcher\Event;
use Symfony\Component\Yaml\Yaml;
@@ -272,6 +274,9 @@ class Page
// If no content, process it
if ($this->content === null) {
// Get media
$this->media();
// Load cached content
/** @var Cache $cache */
$cache = self::$grav['cache'];
@@ -319,7 +324,6 @@ class Page
$this->content = $content;
$this->media();
}
return $this->content;
@@ -1514,9 +1518,9 @@ class Page
/** @var Config $config */
$config = self::$grav['config'];
if ($config->get('system.pages.markdown_extra')) {
$parsedown = new \ParsedownExtra();
$parsedown = new MarkdownExtra($this);
} else {
$parsedown = new \Parsedown();
$parsedown = new Markdown($this);
}
$content = $parsedown->parse($content);
return $content;

2
vendor/autoload.php vendored
View File

@@ -4,4 +4,4 @@
require_once __DIR__ . '/composer' . '/autoload_real.php';
return ComposerAutoloaderInit7a18ce01239c69086e27f1d973d24372::getLoader();
return ComposerAutoloaderInit647726739e1bb1c8f9d4883b25b63e16::getLoader();

View File

@@ -48,6 +48,9 @@ return array(
'Grav\\Common\\GravTrait' => $baseDir . '/system/src/Grav/Common/GravTrait.php',
'Grav\\Common\\Inflector' => $baseDir . '/system/src/Grav/Common/Inflector.php',
'Grav\\Common\\Iterator' => $baseDir . '/system/src/Grav/Common/Iterator.php',
'Grav\\Common\\Markdown\\Markdown' => $baseDir . '/system/src/Grav/Common/Markdown/Markdown.php',
'Grav\\Common\\Markdown\\MarkdownExtra' => $baseDir . '/system/src/Grav/Common/Markdown/MarkdownExtra.php',
'Grav\\Common\\Markdown\\MarkdownGravLinkTrait' => $baseDir . '/system/src/Grav/Common/Markdown/MarkdownGravLinkTrait.php',
'Grav\\Common\\Page\\Collection' => $baseDir . '/system/src/Grav/Common/Page/Collection.php',
'Grav\\Common\\Page\\Media' => $baseDir . '/system/src/Grav/Common/Page/Media.php',
'Grav\\Common\\Page\\Medium' => $baseDir . '/system/src/Grav/Common/Page/Medium.php',

View File

@@ -2,7 +2,7 @@
// autoload_real.php @generated by Composer
class ComposerAutoloaderInit7a18ce01239c69086e27f1d973d24372
class ComposerAutoloaderInit647726739e1bb1c8f9d4883b25b63e16
{
private static $loader;
@@ -19,9 +19,9 @@ class ComposerAutoloaderInit7a18ce01239c69086e27f1d973d24372
return self::$loader;
}
spl_autoload_register(array('ComposerAutoloaderInit7a18ce01239c69086e27f1d973d24372', 'loadClassLoader'), true, true);
spl_autoload_register(array('ComposerAutoloaderInit647726739e1bb1c8f9d4883b25b63e16', 'loadClassLoader'), true, true);
self::$loader = $loader = new \Composer\Autoload\ClassLoader();
spl_autoload_unregister(array('ComposerAutoloaderInit7a18ce01239c69086e27f1d973d24372', 'loadClassLoader'));
spl_autoload_unregister(array('ComposerAutoloaderInit647726739e1bb1c8f9d4883b25b63e16', 'loadClassLoader'));
$map = require __DIR__ . '/autoload_namespaces.php';
foreach ($map as $namespace => $path) {
@@ -42,14 +42,14 @@ class ComposerAutoloaderInit7a18ce01239c69086e27f1d973d24372
$includeFiles = require __DIR__ . '/autoload_files.php';
foreach ($includeFiles as $file) {
composerRequire7a18ce01239c69086e27f1d973d24372($file);
composerRequire647726739e1bb1c8f9d4883b25b63e16($file);
}
return $loader;
}
}
function composerRequire7a18ce01239c69086e27f1d973d24372($file)
function composerRequire647726739e1bb1c8f9d4883b25b63e16($file)
{
require $file;
}