Skip to content

Instantly share code, notes, and snippets.

@plfstr
Forked from ClementRoy/kirbytext.extended.php
Last active October 21, 2015 10:14
Show Gist options
  • Save plfstr/9d768b655e73e083c74f to your computer and use it in GitHub Desktop.
Save plfstr/9d768b655e73e083c74f to your computer and use it in GitHub Desktop.
Kirby 2 Kirbytag extension for simple CodePen embeds.

Codepen extension for Kirby 2 (fork of Kirbytext Codepen tag by ClementRoy)

This extension for Kirby 2 enables you to easily embed CodePen ‘Pens’ into your sites pages.

(codepen: http://codepen.io/chriscoyier/pen/oqHlh/)

Kirby 2 allows Kirbytext (Kirbys custom implementation of Mark Down) to be extended with custom ‘Kirbytags’. More about Kirbytags.

Sorry, not backwards compatible with Kirby 1. Kirbytext Codepen tag by ClementRoy is compatible.

Installation

Place codepen.php in your /site/tags/ folder, or create a ‘tags’ folder inside your site folder if one is not there.

Usage

Now in your content text files you can now use the new codepen tag:

(codepen: http://codepen.io/chriscoyier/pen/oqHlh/)

This will embed the specified Pen using a default height and styled with the standard CodePen embed theme.

Parameters

You can supply additional parameters in the tag to override the height (500 = 500px height embed) or type (html, css result displayed).

(codepen: http://codepen.io/chriscoyier/pen/oqHlh/ height: 250 type: html)

You can specify an additional class which will apply to the CodePen iframe.

(codepen: http://codepen.io/chriscoyier/pen/oqHlh/ class: picked-pen)

This should be considered for adding custom styles to specific Pens. To apply a theme to all Pens, see next section.

Default Theme

This is a more advanced option and not essential, CodePen embeds will use the default CodePen theme style by default.

If you are a CodePen user, pen themes enable you to create a theme style for your embedded Pens.

Note: Changing the default theme requires editing codepen.php file directly

To change from the default CodePen theme to your personal embed theme, open site/tags/codepen.php and edit the $theme = ''; value, to your theme (data-theme-id) value.

You can find your Theme ID in the standard embed code from the ‘Share’ option in any Pen. Look for the data-theme-id value.

Note: Amending this value will change the theme of ALL your CodePen embeds using this Kirby extension.


Update Updated to include new support for CodePen iframe classes see Parameters section or read about the class feature.

<?php
kirbytext::$tags['codepen'] = array(
'attr' => array(
'height', 'type', 'class'
),
'html' => function($tag) {
$source = $tag->attr('codepen');
if( !preg_match('!codepen.io\/([a-z0-9A-Z_-]+)\/pen\/([a-z0-9_-]+)!i', $source, $array) ) {
return false;
}
// Set default Pen height or use 350 default
$height = $tag->attr('height', '350');
// Accept Pen type overide or use result as default
$result = $tag->attr('type', 'result');
// Set a class on the CodePen iframe
$penclass = $tag->attr('class', '');
// Set default theme-id embed theme (About embed themes: http://blog.codepen.io/2013/07/23/the-new-embed-builder-customize-every-aspect/)
$theme = ""; // Add your theme-id value here. Uses standard CodePen embed theme by default if left blank.
// Builds Pen HTML output
$html = '<figure class="code-container">';
$html .= '<pre class="codepen" data-theme-id="'. $theme .'" data-height="'. $height .'" data-type="'. $result .'" data-slug-hash="'. $array[2] .'" data-user="'. $array[1] .'" data-class="'. $penclass .'"><code></code></pre>';
$html .= '<script async src="http://codepen.io/assets/embed/ei.js"></script></figure>';
return $html;
}
);
The MIT License (MIT)
Copyright (c) 2014 Paul Foster
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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment