From f39a05ba594c6806c72a7275d3f7f14bf4bc9706 Mon Sep 17 00:00:00 2001 From: mappu Date: Sat, 7 Nov 2015 18:15:01 +1300 Subject: [PATCH] replace 'written in' with a shields.io SVG --- .hgignore | 2 +- rebuild.php | 22 ++++++++++++++++++++++ 2 files changed, 23 insertions(+), 1 deletion(-) diff --git a/.hgignore b/.hgignore index 1395093..06902db 100644 --- a/.hgignore +++ b/.hgignore @@ -3,4 +3,4 @@ mode:regex ^sites/[^/]+/data/ ^sites/[^/]+/wwwroot/ -/_DRAFTS/ +^shields_cache/ diff --git a/rebuild.php b/rebuild.php index 9f80988..aed5874 100644 --- a/rebuild.php +++ b/rebuild.php @@ -3,6 +3,21 @@ // Code-hosting website // ```````````````````` +function mkshield($left_str, $right_str, $color_str) { + $filename = rawurlencode(str_replace('-', '--', $left_str)).'-'.rawurlencode(str_replace('-', '--', $right_str)).'-'.rawurlencode($color_str).'.svg'; + $cache_path = __DIR__.'/../../shields_cache/'.$filename; + + if (file_exists($cache_path)) { + return file_get_contents($cache_path); + + } else { + $retn = file_get_contents('https://img.shields.io/badge/'.$filename); + file_put_contents($cache_path, $retn); + return $retn; + + } +} + /** * Create a thumbnail of an image. It overscales, centers, and crops to fit the * target dimensions. @@ -178,6 +193,7 @@ class CProject { $this->lastupdate = max($this->lastupdate, filectime($this->dir.$file)); // don't count README updates $this->longdesc = file_get_contents($this->dir.'README.txt'); + $matches = array(); if (preg_match('~Written in ([^\\r\\n]+)~', $this->longdesc, $matches)) { $this->subtag = rtrim($matches[1], ' .'); @@ -190,6 +206,12 @@ class CProject { $parts = explode("\n", $this->longdesc); $this->shortdesc = array_shift($parts); $this->shortdesc[0] = strtolower($this->shortdesc[0]); // cosmetic lowercase + + // Filter longdesc + $this->longdesc = preg_replace_callback('~Written in ([^\\r\\n]+)~', function($matches) { + return ''.mkshield('written in', $matches[1], 'blue').''; + }, $this->longdesc); + continue; }