37 lines
1.3 KiB
PHP
37 lines
1.3 KiB
PHP
<?php
|
|
|
|
if ($argc !== 2 || !preg_match('/\.html?$/', $argv[1]) || !file_exists($argv[1])) {
|
|
exit('Error: html file not found.'.PHP_EOL);
|
|
}
|
|
$fpath = $argv[1];
|
|
|
|
$timestamp = filemtime($fpath);
|
|
$text = file_get_contents($fpath);
|
|
$encoding = mb_detect_encoding($text);
|
|
if (!in_array($encoding, ['UTF-8', 'ASCII'])) {
|
|
if (in_array($encoding, ['SJIS'])) {
|
|
$encoding = 'CP932';
|
|
}
|
|
$text = mb_convert_encoding($text, 'UTF-8', $encoding);
|
|
$text = preg_replace('/(<meta\s+http-equiv=(?:"|\')content-type(?:"|\')\s+content=(?:"|\')text\/html;\s*charset=)(?:[^"\']+)((?:"|\')\s*>)/is', '\1UTF-8\2', $text);
|
|
}
|
|
$text = \Normalizer::normalize(trim($text), \Normalizer::FORM_C);
|
|
static $config = [
|
|
//'clean' => true,
|
|
'hide-comments' => true,
|
|
'indent' => true,
|
|
'output-xhtml' => true,
|
|
'preserve-entities' => true,
|
|
//'show-body-only' => true,
|
|
'wrap' => 0,
|
|
'input-encoding' => 'utf8',
|
|
'output-encoding' => 'utf8',
|
|
'output-bom' => false,
|
|
];
|
|
$text = tidy_repair_string($text, $config);
|
|
$text = preg_replace('/<table([^>]*)>((?:\s*<caption[^>]*>.*<\/caption>)?\s*)<tr([^>]*)>/Us', '<table\1>\2<tbody><tr\3>', $text);
|
|
$text = preg_replace('/<\/tr>(\s*)<\/table>/s', '</tr></tbody>\1</table>', $text);
|
|
|
|
file_put_contents($fpath, $text);
|
|
touch($fpath, $timestamp);
|