From b514ac983b61b1ec86510d0523bb4b4d24beebc8 Mon Sep 17 00:00:00 2001 From: "J. King" Date: Sat, 3 Aug 2019 21:32:13 -0400 Subject: [PATCH] Rely on Zip extension for extracting highlight.js --- RoboFile.php | 79 +++++++++++++++++++++++++++------------------------- 1 file changed, 41 insertions(+), 38 deletions(-) diff --git a/RoboFile.php b/RoboFile.php index fb81a5cd..7651a019 100644 --- a/RoboFile.php +++ b/RoboFile.php @@ -1,7 +1,6 @@ taskExec($postcss)->arg($scss)->option("-o", $css); // download highlight.js - $t->addCode(function() use ($tmp, $themeout) { - $languages = ["php", "bash", "shell", "xml", "nginx", "apache"]; - $post = http_build_query((function($langs) { - $out = []; - foreach($langs as $l) { - $out[$l.".js"] = "on"; + if (extension_loaded("zip")) { + $t->addCode(function() use ($tmp, $themeout) { + $languages = ["php", "bash", "shell", "xml", "nginx", "apache"]; + $post = http_build_query((function($langs) { + $out = []; + foreach($langs as $l) { + $out[$l.".js"] = "on"; + } + return $out; + })($languages)); + $conn = @fopen("https://highlightjs.org/download/", "r"); + if ($conn === false) { + throw new Exception("Unable to download Highlight.js"); } - return $out; - })($languages)); - $conn = fopen("https://highlightjs.org/download/", "r"); - if ($conn === false) { - throw new Exception("Unable to download Highlight.js"); - } - foreach (stream_get_meta_data($conn)['wrapper_data'] as $field) { - if (preg_match("/^Set-Cookie: csrftoken=([^;]+)/i", $field, $cookie)) { - break; + foreach (stream_get_meta_data($conn)['wrapper_data'] as $field) { + if (preg_match("/^Set-Cookie: csrftoken=([^;]+)/i", $field, $cookie)) { + break; + } } - } - $token = stream_get_contents($conn); - preg_match("/ [ - 'method' => "POST", - 'content' => $post, - 'header' => [ - "Referer: https://highlightjs.org/download/", - "Cookie: csrftoken={$cookie[1]}", - "Content-Type: application/x-www-form-urlencoded", - ], - ]])); - if ($hljs === false) { - throw new Exception("Unable to download Highlight.js"); - } else { - file_put_contents($tmp."highlightjs.zip", $hljs); - } - $z = new Zip; - $z->open($tmp."highlightjs.zip"); - $z->extract($themeout, "", "", "highlight.pack.js"); - }, "downloadHighlightjs"); + $token = stream_get_contents($conn); + preg_match("/ [ + 'method' => "POST", + 'content' => $post, + 'header' => [ + "Referer: https://highlightjs.org/download/", + "Cookie: csrftoken={$cookie[1]}", + "Content-Type: application/x-www-form-urlencoded", + ], + ]])); + if ($hljs === false) { + throw new Exception("Unable to download Highlight.js"); + } else { + file_put_contents($tmp."highlightjs.zip", $hljs); + } + $z = new \ZipArchive; + $z->open($tmp."highlightjs.zip"); + $z->extractTo($themeout, "highlight.pack.js"); + }, "downloadHighlightjs"); + } else { + $this->say("Zip extension not installed; not updating hightlight.js"); + } // execute the collection return $t->run(); }