Skip to content

Instantly share code, notes, and snippets.

@jakubboucek
Last active September 17, 2020 09:29
Show Gist options
  • Save jakubboucek/92aa81b44d0d5132bff5 to your computer and use it in GitHub Desktop.
Save jakubboucek/92aa81b44d0d5132bff5 to your computer and use it in GitHub Desktop.
Format HTML source in PHP
<?php
$path = isset($argv[1]) ? $argv[1] : __DIR__;
$realpath = realpath($path);
echo "Thit tool is using free online formatter at www.freeformatter.com\n\n";
return HtmlFormatter::processObject($realpath);
/**
* Class HtmlFormatter
*/
class HtmlFormatter
{
/**
* Choose right strategy to process path at input
* @param string $pathName
* @return int return_val
*/
public static function processObject($pathName)
{
if (is_null($pathName)) {
echo "ERROR: Unrecognized file name: {$pathName}\n";
return 1;
}
if (is_dir($pathName)) {
echo "Formatting all .html files in {$pathName}\n";
self::processDirectory($pathName);
} else {
echo "Formatting file {$pathName}\n";
self::processFile($pathName);
}
return 0;
}
/**
* Format all HTML files in directory
* @param string $pathName
*/
public static function processDirectory($pathName)
{
$iterator = new DirectoryIterator($pathName);
foreach ($iterator as $fileinfo) {
// Ignore dot files (system or hidden files)
if (strpos($fileinfo->getFilename(), '.') === 0) {
continue;
}
if ($fileinfo->isDir()) {
self::processDirectory($fileinfo->getPathname());
} else {
self::processFile($fileinfo->getPathname());
}
}
}
/**
* Format one file, but only if is HTML
* @param string $fileName
*/
public static function processFile($fileName)
{
echo $fileName . " ... ";
if (!preg_match('/\.html$/', $fileName)) {
echo "ignoring (not HTML)\n";
return;
}
try {
self::formatFile($fileName);
echo "OK";
} catch (cUrlException $e) {
echo "ERROR (" . $e->getCode() . "): " . $e->getMessage();
} finally {
echo "\n";
}
}
/**
* Format one HTML file
* @param string $fileName
* @throws cUrlException
*/
public static function formatFile($fileName)
{
$sourceHtml = file_get_contents($fileName);
$formattedHtml = self::getFormattedHtml($sourceHtml);
file_put_contents($fileName, $formattedHtml);
}
/**
* Call online formatter and return formatted HTML
* @param string $sourceHtml Source HTML
* @return string Formatted HTML
* @throws cUrlException Thrown when online formatter return error
*/
public static function getFormattedHtml($sourceHtml)
{
$cUrl = curl_init();
curl_setopt($cUrl, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($cUrl, CURLOPT_POST, TRUE);
curl_setopt($cUrl, CURLOPT_FOLLOWLOCATION, 0);
curl_setopt($cUrl, CURLOPT_HEADER, 0);
curl_setopt($cUrl, CURLOPT_USERAGENT, 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/46.0.2490.71 Safari/537.36');
curl_setopt($cUrl, CURLOPT_ENCODING, 'gzip');
curl_setopt($cUrl, CURLOPT_URL, 'http://www.freeformatter.com/html-formatter.html');
curl_setopt($cUrl, CURLOPT_REFERER, 'http://www.freeformatter.com/html-formatter.html');
curl_setopt($cUrl, CURLOPT_HTTPHEADER, [
'Origin: http://www.freeformatter.com',
'Accept-Language: cs,en-US;q=0.8,en;q=0.6',
'Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8',
]);
curl_setopt($cUrl, CURLOPT_POSTFIELDS, array(
'htmlString' => $sourceHtml,
'indentation' => 'TWO_SPACES',
'forceInNewWindow' => 'true',
'encoding' => 'UTF-8',
));
$returnValue = curl_exec($cUrl);
$httpStatus = curl_getinfo($cUrl, CURLINFO_HTTP_CODE);
curl_close($cUrl);
if ($httpStatus != 200) {
throw new cUrlException(strip_tags($returnValue), $httpStatus);
}
return $returnValue;
}
}
/**
* Class cUrlException
*/
class cUrlException extends Exception
{
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment