Commit 85e0a0e8 authored by Administrator's avatar Administrator 💬

Add page injection goodies. This is awesome!

parent 5638ada0
# v1.3.0
## 12/08/2017
1. [](#new)
* Added multi-lang support to Page Inject plugin [#10](https://github.com/getgrav/grav-plugin-page-inject/issues/10)
# v1.2.0
## 10/11/2016
1. [](#improved)
* Support Grav-style link route resolution (e.g. `../your-route`) [#5](https://github.com/getgrav/grav-plugin-page-inject/issues/5)
1. [](#bugfix)
* Fixed issue with `page-inject` processing Twig twice [#7](https://github.com/getgrav/grav-plugin-page-inject/issues/7)
# v1.1.1
## 10/21/2015
1. [](#new)
* Added `active` config option to enable/disable site-wide
1. [](#bugfix)
* Fixed issue with plugin not processing reliably with cache-enabled
# v1.1.0
## 08/25/2015
1. [](#improved)
* Added blueprints for Grav Admin plugin
# v1.0.0
## 06/18/2015
1. [](#new)
* ChangeLog started...
The MIT License (MIT)
Copyright (c) 2015 Grav
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
# Grav Page Inject Plugin
`Page Inject` is a powerful [Grav][grav] Plugin that lets you inject entire pages or page content into other pages using simple markdown syntax
# Installation
Installing the Page Inject plugin can be done in one of two ways. Our GPM (Grav Package Manager) installation method enables you to quickly and easily install the plugin with a simple terminal command, while the manual method enables you to do so via a zip file.
## GPM Installation (Preferred)
The simplest way to install this plugin is via the [Grav Package Manager (GPM)](http://learn.getgrav.org/advanced/grav-gpm) through your system's Terminal (also called the command line). From the root of your Grav install type:
bin/gpm install page-inject
This will install the Page Inject plugin into your `/user/plugins` directory within Grav. Its files can be found under `/your/site/grav/user/plugins/page-inject`.
## Manual Installation
To install this plugin, just download the zip version of this repository and unzip it under `/your/site/grav/user/plugins`. Then, rename the folder to `page-inject`. You can find these files either on [GitHub](https://github.com/getgrav/grav-plugin-page-inject) or via [GetGrav.org](http://getgrav.org/downloads/plugins#extras).
You should now have all the plugin files under
/your/site/grav/user/plugins/page-inject
# Config Defaults
```
enabled: true
active: true
processed_content: true
```
If you need to change any value, then the best process is to copy the [page-inject.yaml](page-inject.yaml) file into your `users/config/plugins/` folder (create it if it doesn't exist), and then modify there. This will override the default settings.
The `active` option allows you to enable/disable the plugin site-wide, and then enable it on page via Page Config overrides. This is useful to optimize performance.
the `processed_content` option means the page is pre-rendered before being injected. This is the default behavior and means that relative image links and other path-sensitive content works correctly. You can however set this to `false` and then the raw markdown is inject and processed along with the rest of the current page. This is releavant for `content-inject` links **only**.
### Page Config
You can override the plugin options by adding overrides in the page header frontmatter:
```
page-inject:
active: true
processed_content: true
```
# Usage
There are two ways to use this plugin in your markdown content:
1. **Page Injection**
```
[plugin:page-inject](/route/to/page)
```
This approach includes an entire page rendered with the associated template. This works best for modular page content or content that uses a specific template that provides appropriate styling that is intended to be part of other pages. you can also pass an optional template name and use that template to render teh page (as long as you also provide the template in your theme):
```
[plugin:page-inject](/route/to/page?template=custom-template)
```
2. **Content Injection**
```
[plugin:content-inject](/route/to/page)
```
Sometimes you just want the content of another page injected directly into your current page. Use `content-inject` for this purpose. The content is not rendered with the assoicated twig template, merely injected into the current page.
[grav]: http://github.com/getgrav/grav
name: Page Inject
version: 1.3.0
description: "**Page Inject** is a powerful plugin that lets you inject entire pages or page content into other pages using simple markdown syntax"
icon: trello
author:
name: Team Grav
email: devs@getgrav.org
url: http://getgrav.org
homepage: https://github.com/getgrav/grav-plugin-page-inject
keywords: inject, embed, markdown
bugs: https://github.com/getgrav/grav-plugin-page-inject/issues
license: MIT
form:
validation: strict
fields:
enabled:
type: toggle
label: Plugin status
highlight: 1
default: 0
options:
1: Enabled
0: Disabled
validate:
type: bool
active:
type: toggle
label: Activate Site-Wide
highlight: 1
default: 1
options:
1: Enabled
0: Disabled
validate:
type: bool
help: This will ensure the plugin is activated site wide. You can override with page header options.
processed_content:
type: toggle
label: Processed Content
highlight: 1
default: 0
options:
1: Enabled
0: Disabled
validate:
type: bool
help: If enabled the page is pre-rendered before being injected, so relative paths work correctly
<?php
/**
* PageInject
*
* This plugin embeds other Grav pages from markdown URLs
*
* Licensed under MIT, see LICENSE.
*/
namespace Grav\Plugin;
use Grav\Common\Grav;
use Grav\Common\Plugin;
use Grav\Common\Page\Page;
use Grav\Common\Uri;
use RocketTheme\Toolbox\Event\Event;
class PageInjectPlugin extends Plugin
{
/**
* Return a list of subscribed events.
*
* @return array The list of events of the plugin of the form
* 'name' => ['method_name', priority].
*/
public static function getSubscribedEvents()
{
return [
'onPluginsInitialized' => ['onPluginsInitialized', 0],
];
}
/**
* Initialize configuration.
*/
public function onPluginsInitialized()
{
if ($this->isAdmin()) {
$this->active = false;
return;
}
$this->enable([
'onPageContentRaw' => ['onPageContentRaw', 0],
]);
}
/**
* Add content after page content was read into the system.
*
* @param Event $event An event object, when `onPageContentRaw` is fired.
*/
public function onPageContentRaw(Event $event)
{
/** @var Page $page */
$page = $event['page'];
$config = $this->mergeConfig($page);
$twig = $this->grav['twig'];
if ($config->get('enabled') && $config->get('active')) {
// Get raw content and substitute all formulas by a unique token
$raw = $page->getRawContent();
// build an anonymous function to pass to `parseLinks()`
$function = function ($matches) use (&$page, &$twig, &$config) {
$search = $matches[0];
$type = $matches[1];
$page_path = $matches[3] ?: $matches[2];
$template = $matches[4];
// Process Link a little manually
$grav = Grav::instance();
$language = $grav['language'];
$language_append = '';
if ($type == 'link' && $language->enabled()) {
$language_append = $language->getLanguageURLPrefix();
}
$base = $grav['base_url_relative'];
$base_url = rtrim($base . $grav['pages']->base(), '/') . $language_append;
// $page_path = str_replace($base_url, '', Uri::convertUrl($page, $page_path));
$page_path = Uri::convertUrl($page, $page_path, 'link', false, true);
$inject = $page->find($page_path);
if ($inject) {
if ($type == 'page-inject') {
if ($template) {
$inject->template($template);
}
$inject->modularTwig(true);
$replace = $inject->content();
} else {
if ($config->get('processed_content')) {
$replace = $inject->content();
} else {
$replace = $inject->rawMarkdown();
}
}
} else {
// replace with what you started with
$replace = $matches[0];
}
// do the replacement
return str_replace($search, $replace, $search);
};
// set the parsed content back into as raw content
$page->setRawContent($this->parseInjectLinks($raw, $function));
}
}
protected function parseInjectLinks($content, $function)
{
$regex = '/\[plugin:(content-inject|page-inject)\]\(((.*)\?template=(.*)|(.*))\)/i';
return preg_replace_callback($regex, $function, $content);
}
}
enabled: true
active: true
processed_content: true
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment