ソフトウェア/pukiwiki/gist.inc.php

(2185d) 更新


公開メモ

gist のコードを貼り込めるようにした

gist のコードを pukiwiki に貼り込むためのプラグインを作った。 作ったのに公開し忘れてた。

https://mushikago.com/i/?p=7801 で紹介されている gist-embed を使ってる。

ソース

plugins/gist.inc.php

LANG:php
<?php

function plugin_gist_init()
{
        global $head_tags;
        $head_tags[] = <<<EOF
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/gist-embed/2.7.1/gist-embed.min.js"></script>
EOF;
}

function plugin_gist_convert_sub($s)
{
	if (!$s) {
		return '';
	}
	if (preg_match('/^[0-9,]+$/', $s)) {
		return ' data-gist-highlight-line="' + $s + '"';
	}
	if (preg_match('/^[,0-9\-]+$/', $s)) {
		return ' data-gist-line="' + $s + '"';
	}
	return ' data-gist-file="' + $s + '"';
}

function plugin_gist_convert()
{
	global $script, $vars, $asin, $asin_all;
	$args = func_get_args();
	$id = $args[0];
	$options = plugin_gist_convert_sub($args[1])
	         + plugin_gist_convert_sub($args[2])
	         + plugin_gist_convert_sub($args[3]);
	return "<code data-gist-id=\"$id\"$options></code>";
}

?>

使用例

普通に表示

#gist(eef52076810a996831c6/df8fbfa8c1b2976feeebb8c2c70fd62fa3f87a2a);

一部を表示

#gist(eef52076810a996831c6/df8fbfa8c1b2976feeebb8c2c70fd62fa3f87a2a,10-20);

ハイライト

#gist(eef52076810a996831c6/df8fbfa8c1b2976feeebb8c2c70fd62fa3f87a2a,12,10-20);

ファイルを指定

複数あるときに1つのファイルを指定できる。

#gist(eef52076810a996831c6/df8fbfa8c1b2976feeebb8c2c70fd62fa3f87a2a,Hello.java,12,10-20);

jupyter notebook などでは

レンダリングされた結果が表示される。

#gist(osamutake/e6f656fafca463f7a738aaced256b43a);

行番号指定は効かない。

#gist(osamutake/e6f656fafca463f7a738aaced256b43a,2-3);

github のコードも貼れるようにすると良いのかも

https://www.moongift.jp/2013/11/gist-it-githubのファイルをgist風に埋め込み表示/

gist-it というのを使うと良いみたい。

コメント・質問





Counter: 2891 (from 2010/06/03), today: 2, yesterday: 1