I tried following learn/plugin-tutorial in regards to setting up page-level configuration for a plugin, as well as using the SmartyPants Plugin for reference, but came up short.
The plugin does some preg_replace action to unwrap images from Markdown output (from <p><img></p>
to <img>
), and looks like the following:
unwrapimages.php
:
<?php
namespace Grav\Plugin;
use Grav\Common\Data;
use Grav\Common\Plugin;
use Grav\Common\Grav;
use Grav\Common\Page\Page;
use RocketTheme\Toolbox\Event\Event;
class UnwrapImagesPlugin extends Plugin
{
public static function getSubscribedEvents() {
return [
'onPageContentProcessed' => ['onPageContentProcessed', 0]
];
}
public function onPageContentProcessed(Event $event)
{
$page = $event['page'];
$config = $this->mergeConfig($page);
if ($config->get('process_content')) {
$buffer = $page->content();
$url = $page->url();
$buffer = preg_replace("/<p>\s*?(<a .*<img.*<\/a>|<img.*)?\s*<\/p>/",
"$1",
$buffer);
$buffer = preg_replace("/<img\\s+src\\s*=\\s*(["'][^"']+["']|[^>]+)>/",
'<img src=$1 class="col-md-8 col-md-offset-2" />',
$buffer);
$buffer = preg_replace("/<h1>(.*?)<\/h1>/",
'<h1 class="col-md-8 col-md-offset-2">$1</h1>',
$buffer);
$buffer = preg_replace("/<h2>(.*?)<\/h2>/",
'<h2 class="col-md-8 col-md-offset-2">$1</h2>',
$buffer);
$buffer = preg_replace("/<h3>(.*?)<\/h3>/",
'<h3 class="col-md-8 col-md-offset-2">$1</h3>',
$buffer);
$buffer = preg_replace("/<h4>(.*?)<\/h4>/",
'<h4 class="col-md-8 col-md-offset-2">$1</h4>',
$buffer);
$buffer = preg_replace("/<h5>(.*?)<\/h5>/",
'<h5 class="col-md-8 col-md-offset-2">$1</h5>',
$buffer);
$buffer = preg_replace("/<h6>(.*?)<\/h6>/",
'<h6 class="col-md-8 col-md-offset-2">$1</h6>',
$buffer);
$buffer = preg_replace("/<p>(.*?)<\/p>/",
'<p class="col-md-8 col-md-offset-2">$1</p>',
$buffer);
$page->setRawContent($buffer);
}
}
}
Which I need for a Bootstrap-specific layout (hence the col-md-8
) with varied image sizes. The page frontmatter has:
unwrap_images:
process_content: true
And user/config/plugins/unwrapimages.yaml
: enabled: false
.
In essence, I need to process the plugin on specific pages. I’m not quite sure what I am missing with the above setup, hopefully someone can spot it.