ソフトウェア/pukiwiki/gist.inc.php
更新 (昨日 0 / 今日 0 / 総数 3,904)
gist のコードを貼り込めるようにした†
gist のコードを pukiwiki に貼り込むためのプラグインを作った。 作ったのに公開し忘れてた。
https://mushikago.com/i/?p=7801 で紹介されている gist-embed を使ってる。
ソース†
plugins/gist.inc.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);
| public Result execute(Input in) { | |
| Result result = new Result(); | |
| List<Integer> coins2 = new ArrayList<>(); | |
| int sum = 0; | |
| try { | |
| if (in.getSelected() != null && in.getSelected() > 0 && in.getSelected() < 11) { | |
| if (in.getCoins() != null && in.getCoins().size() > 0 && in.getCoins().size() < 101) { | |
| sum = 0; | |
| for (Integer coin : in.getCoins()) { | |
| if (coin == 1) { | |
| sum += 0; | |
| coins2.add(coin); | |
| } else if (coin == 5) { | |
| sum += 0; | |
| coins2.add(coin); | |
| } else if (coin == 10) { | |
| sum += 10; | |
| } else if (coin == 50) { | |
| sum += 50; | |
| } else if (coin == 100) { | |
| sum += 100; | |
| } else if (coin == 500) { | |
| sum += 500; | |
| } | |
| } | |
| Product product = dao.findById(in.getSelected()); | |
| if (product == null) { | |
| result = new Result(); | |
| result.setCoins(in.getCoins()); | |
| result.setProduct(null); | |
| } else if (product.getPrice() < sum) { | |
| result.setProduct(product); | |
| sum -= product.getPrice(); | |
| List<Integer> coins3 = new ArrayList<>(); | |
| coins3.add(500); | |
| coins3.add(100); | |
| coins3.add(50); | |
| coins3.add(10); | |
| for (Integer coin : coins3) { | |
| while (sum > coin) { | |
| coins2.add(coin); | |
| sum -= coin; | |
| } | |
| } | |
| result.setCoins(coins2); | |
| } else { | |
| result = new Result(); | |
| result.setCoins(in.getCoins()); | |
| result.setProduct(null); | |
| } | |
| } else { | |
| throw new IllegalArgumentException(); | |
| } | |
| } else { | |
| throw new IllegalArgumentException(); | |
| } | |
| } catch (Exception e) { | |
| LOG.error(e); | |
| result = new Result(); | |
| result.setCoins(in.getCoins()); | |
| result.setProduct(null); | |
| } | |
| return result; | |
| } |
一部を表示†
#gist(eef52076810a996831c6/df8fbfa8c1b2976feeebb8c2c70fd62fa3f87a2a,10-20);
| if (coin == 1) { | |
| sum += 0; | |
| coins2.add(coin); | |
| } else if (coin == 5) { | |
| sum += 0; | |
| coins2.add(coin); | |
| } else if (coin == 10) { | |
| sum += 10; | |
| } else if (coin == 50) { | |
| sum += 50; | |
| } else if (coin == 100) { |
ハイライト†
#gist(eef52076810a996831c6/df8fbfa8c1b2976feeebb8c2c70fd62fa3f87a2a,12,10-20);
| if (coin == 1) { | |
| sum += 0; | |
| coins2.add(coin); | |
| } else if (coin == 5) { | |
| sum += 0; | |
| coins2.add(coin); | |
| } else if (coin == 10) { | |
| sum += 10; | |
| } else if (coin == 50) { | |
| sum += 50; | |
| } else if (coin == 100) { |
ファイルを指定†
複数あるときに1つのファイルを指定できる。
#gist(eef52076810a996831c6/df8fbfa8c1b2976feeebb8c2c70fd62fa3f87a2a,Hello.java,12,10-20);
| if (coin == 1) { | |
| sum += 0; | |
| coins2.add(coin); | |
| } else if (coin == 5) { | |
| sum += 0; | |
| coins2.add(coin); | |
| } else if (coin == 10) { | |
| sum += 10; | |
| } else if (coin == 50) { | |
| sum += 50; | |
| } else if (coin == 100) { |
jupyter notebook などでは†
レンダリングされた結果が表示される。
#gist(osamutake/e6f656fafca463f7a738aaced256b43a);
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
行番号指定は効かない。
#gist(osamutake/e6f656fafca463f7a738aaced256b43a,2-3);
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
github のコードも貼れるようにすると良いのかも†
https://www.moongift.jp/2013/11/gist-it-githubのファイルをgist風に埋め込み表示/
gist-it というのを使うと良いみたい。