Skip to content

Instantly share code, notes, and snippets.

@besstiolle
Forked from abarth500/Gist.php
Last active December 11, 2015 04:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save besstiolle/4545777 to your computer and use it in GitHub Desktop.
Save besstiolle/4545777 to your computer and use it in GitHub Desktop.
Let's start with a simple tag :
<gist>4545777</gist>
Or with a Gist url :
<gist>https://gist.github.com/4545777</gist>
And with its .js extension
<gist>https://gist.github.com/4545777.js</gist>
Even better : for a specific file of your Gist
<gist>https://gist.github.com/4545777.js?file=example.php</gist>
Finally : with the entire <script /> code
<gist><script src="https://gist.github.com/4545777.js"></script></gist>
You will find easily a solution for the integration of your own Gist into mediawiki
<?php
# GitHubGist MediaWiki extension v.1
# Author: Danezis Kevin
# based on the work of Shohei Yokoyama (Shizuoka Univ. Japan)
#
#-Install
# 1. Save this code as $MediaWikiRoot/extensions/Gist/Gist.php
# 2. Append "require_once "$IP/extensions/Gist/Gist.php";" to LocalSetting.php
#
#-Usage
# Let's start with a simple tag :
# <gist>4545777</gist>
#
# Or with a Gist url :
# <gist>https://gist.github.com/4545777</gist>
#
# And with its .js extension
# <gist>https://gist.github.com/4545777.js</gist>
#
# Even better : for a specific file of your Gist
# <gist>https://gist.github.com/4545777.js?file=example.php</gist>
#
# Finally : with the entire <script /> code
# <gist><script src="https://gist.github.com/4545777.js"></script></gist>
#
# You will find easily a solution for the integration of your own Gist into mediawiki
$wgExtensionFunctions[] = 'wfGist';
$wgExtensionCredits['parserhook'][] = array(
'name' => 'Gist',
'description' => 'Integrating your Gist code.',
'author' => 'Danezis Kevin',
'url' => 'http://www.furie.be/'
);
$loadMeOnce = false;
function wfGist() {
global $wgParser;
$wgParser->setHook('gist', 'wfGist_func', null);
}
function wfGist_func($input,$arg, $parser) {
global $loadMeOnce;
$input = trim($input);
$sid = null;
$script = null;
// <script src="https://gist.github.com/xxx.js"></script>
if(preg_match("/^<script/",$input)){
try{
$script = @new SimpleXMLElement($input);
} catch (Exception $e) {
return "<b>ERROR: Gist param ( ".htmlentities($input)." ) is not a valid url.</b><br/>";
}
$input = $script["src"];
}
if (preg_match("/^[0-9a-f]+ ?/",$input)){
$sid = $input;
$script .= '<script src="https://gist.github.com/'.$sid.'.js"></script>';
}elseif (preg_match("/^https\:\/\/gist\.github\.com\/(?P<sid>[0-9a-f]+)$/",$input, $matches)){
$script = '<script src="'.$input.'.js"></script>';
$sid = $matches['sid'];
}elseif (preg_match("/^https\:\/\/gist\.github\.com\/(?P<sid>[0-9a-f]+)\.js(\?file\=.*)?$/",$input, $matches)){
$script = '<script src="'.$input.'"></script>';
$sid = $matches['sid'];
} else {
return "<b>ERROR: Gist param ( ".htmlentities($input)." ) is not a valid id.</b><br/>";
}
// echo "sid = ".$sid." script = ".htmlentities($script)."</br>";
$html = '';
if(!$loadMeOnce) {
$html .= <<<'HTML'
<style>
.gistWrapper{
margin-bottom: 5px;
}
.gist .gist-file .gist-data .line_numbers span {line-height: 1.3em;}
span.info{
background-color: #F3F3F3;
border-bottom: 1px solid #7E7E7E;
border-top: 1px solid #7E7E7E;
display: inline-block;
}
span.first{
border-left: 1px solid #7E7E7E;
border-radius: 12px 0 0 12px;
}
span.last{
border-right: 1px solid #7E7E7E;
border-radius: 0 12px 12px 0;
}
span.info:hover{
text-decoration: underline;
background-color:#79C995;
}
span.info a{
color: #000000;
display: inline-block;
height: 27px;
line-height: 2.1em;
padding: 0 10px;
}
span.gist_logo{
background-image: url(data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAD4AAAAbCAIAAACrwquWAAAACXBIWXMAAA7EAAAOxAGVKw4bAAADVUlEQVR42tWXv2/TQBTHDUXAAgsTDPwTkZCQmBlZkRjsbOxI/AENyQqUUiFVLGlcIiQkQDCgDo2V0EqAYKZxFNNWHcoPhbb2OfY1Nr57F9vxj7PTBESfTlHu3bvc533vnc8RzGNrAn+4UxMLMRNrnf8ZPZH572Sg/zZNNCV0peTjFevbbsy260U/oKRMSu4+FLw2BXRfbYB2sBWfY2EnnMCE6k8JnelNsBOhowkM8SfXPtv2diMZCiiwRolxOxhyKDVQikFoScEOo08PnZLB5oQ9Qgx8C5tIlaVCDpNkFZl4Kwe82duBtb1mbbwGp/15wft0nl72mh9pbbz0I/vbH8DpBYAHgiF+iK4uUdjKmgeOUEcu5kEvyh2ChdcqNJElNUWwR6d9mkiLyJkYkOZnY2pNopJvWrQ7Frqn1CYVXqolsRt6ZK+Zop2VeCWQ7/Nng7kHveyCUcqUXDNhB8RCThNBaVOj7GUlT40mKk3rZz4emYkOtV1pmcEO5DSmtNmqsNo/Ojra/0W+z82MhQ6iL3fHqZbRmukup8g+Bnq4pufPZKMbxJoUXVZpRx0PPTyp3DSiZu588ZYc1K8Gnm/viefZFegCkz+KfnRDx/GE74+EeQadLizcgMnNSn70ShPRSQ1IvmskWOKzhcMU9turd/joQCvJbdqzWnnZvYNt0SltOCwsjySOkwH33Ewc0UjPmY/OZJeWGDsOv2FxwLdsYG3TWyFF9GBhtPuVz3cUdKQNr8R2VSzMNpDtuFx8+prDwI3hRawhPnrmEH5z0/cfvrpBrs8nFzLQDWQPr0TNcVv3PHqD4EdecUlRrNMXXx/bk7zKLmI7ldxwFs7TUjmFvrdT0fd+co5Ev/Mu4g/GLLxO4cVq29Lq6Y92qTqyONklmhG2+JJHmvXpQYKc+oHz+Bx4Bs+vRX4Hr9xmZ1e5S9D1wPoM3rsjtcF66lEtN/wZG8BNwPt6sjmLl8i+L14MO4FA7+3qE5gw0usH51OU4f9RPIWyMoJNyj4V3KfM7T8quq4jexCmnV2lF20EfXU2XPoDG3EW+HfooL3r5ng60r+AHL05iIcvrpNy/3h/2uhD9d2UDNhDhq92jD7e9MksYz7LYNQylY4bfntrBHq/p09swsGxtT/vpgKOH3lP8AAAAABJRU5ErkJggg==);
background-repeat: no-repeat;
padding-left: 62px;
}
.blocGist{
display:none;
margin-top: 10px;
}
.clear{
clear:both;
}
</style>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.0/jquery.min.js"> </script>
<script type="text/javascript">$("document").ready(function() {$(".hide").click(function () { $(this).next("div").toggle("slow"); return false; });});</script>
HTML;
}
$html .= <<<HTML
<div class='gistWrapper'>
<span class='info first gist_logo'>
<a target='_blank' href='https://gist.github.com/$sid/edit'>&Eacute;diter</a>
</span>
<span class='info'>
<a target='_blank' href='https://gist.github.com/$sid#comments'>Commentaires</a>
</span>
<span class='info'>
<a target='_blank' href='https://gist.github.com/$sid/revisions'>R&eacute;visions</a>
</span>
<span class='info hide last'>
<a href='#'>D&eacute;rouler</a>
</span>
<div class='blocGist'>$script</div>
</div>
HTML;
$replace = array(CHR(13),CHR(10),CHR(9));
$html = str_replace($replace,"",$html);
$loadMeOnce = true;
return $html;
}
@besstiolle
Copy link
Author

Correction : gist's url allow Hexa code : 0-9 and a-f

@besstiolle
Copy link
Author

I rewrote the code for my own needs.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment