Skip to content

Commit

Permalink
Added support for custom inline images with markdown #10, no more twi…
Browse files Browse the repository at this point in the history
…g needed!
  • Loading branch information
rhukster committed Aug 22, 2014
1 parent 447abb9 commit f3aec65
Show file tree
Hide file tree
Showing 8 changed files with 134 additions and 9 deletions.
13 changes: 13 additions & 0 deletions system/src/Grav/Common/Markdown/Markdown.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php
namespace Grav\Common\Markdown;

class Markdown extends \Parsedown
{
use MarkdownGravLinkTrait;

function __construct($page)
{
$this->page = $page;
}

}
13 changes: 13 additions & 0 deletions system/src/Grav/Common/Markdown/MarkdownExtra.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php
namespace Grav\Common\Markdown;

class MarkdownExtra extends \ParsedownExtra
{
use MarkdownGravLinkTrait;

function __construct($page)
{
parent::__construct();
$this->page = $page;
}
}
83 changes: 83 additions & 0 deletions system/src/Grav/Common/Markdown/MarkdownGravLinkTrait.php
Original file line number Diff line number Diff line change
@@ -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;
}
}
9 changes: 9 additions & 0 deletions system/src/Grav/Common/Page/Medium.php
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,15 @@ public function lightbox($width = null, $height = null)
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.
*
Expand Down
10 changes: 7 additions & 3 deletions system/src/Grav/Common/Page/Page.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
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;

Expand Down Expand Up @@ -272,6 +274,9 @@ public function content($var = null)
// If no content, process it
if ($this->content === null) {

// Get media
$this->media();

// Load cached content
/** @var Cache $cache */
$cache = self::$grav['cache'];
Expand Down Expand Up @@ -319,7 +324,6 @@ public function content($var = null)

$this->content = $content;

$this->media();
}

return $this->content;
Expand Down Expand Up @@ -1514,9 +1518,9 @@ protected function parseMarkdownContent($content)
/** @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;
Expand Down
2 changes: 1 addition & 1 deletion vendor/autoload.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@

require_once __DIR__ . '/composer' . '/autoload_real.php';

return ComposerAutoloaderInit7a18ce01239c69086e27f1d973d24372::getLoader();
return ComposerAutoloaderInit647726739e1bb1c8f9d4883b25b63e16::getLoader();
3 changes: 3 additions & 0 deletions vendor/composer/autoload_classmap.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,9 @@
'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',
Expand Down
10 changes: 5 additions & 5 deletions vendor/composer/autoload_real.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

// autoload_real.php @generated by Composer

class ComposerAutoloaderInit7a18ce01239c69086e27f1d973d24372
class ComposerAutoloaderInit647726739e1bb1c8f9d4883b25b63e16
{
private static $loader;

Expand All @@ -19,9 +19,9 @@ public static function getLoader()
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) {
Expand All @@ -42,14 +42,14 @@ public static function getLoader()

$includeFiles = require __DIR__ . '/autoload_files.php';
foreach ($includeFiles as $file) {
composerRequire7a18ce01239c69086e27f1d973d24372($file);
composerRequire647726739e1bb1c8f9d4883b25b63e16($file);
}

return $loader;
}
}

function composerRequire7a18ce01239c69086e27f1d973d24372($file)
function composerRequire647726739e1bb1c8f9d4883b25b63e16($file)
{
require $file;
}

0 comments on commit f3aec65

Please sign in to comment.