HTML tidyをCentOSにインストールする

  • HTMLのインデントをよさげに整形してくれるコマンドを探していた。

  • CentOSにインストール済みだったxmllintではうまくインデントを整形してくれなかった

  • そこでHTML Tidyをインストールした。が、yumではなくrpmでインストールしたので、備忘のためにメモを残すことにした。

本論

HTML tidyについて

いわゆるtidyコマンド。

インストール方法

yumでインストールしようとしたが、レポジトリになかった。このため、公式サイトからrpmをダウンロードしてインストールした。

環境
[mk55@localhost tmp]$ cat /etc/redhat-release 
CentOS Linux release 7.4.1708 (Core) 

1.yumで検索してみるもHTML tidyそのものはなかった。

[root@localhost ~]# yum search tidy
Loaded plugins: fastestmirror, langpacks
Loading mirror speeds from cached hostfile
 * base: www.ftp.ne.jp
 * extras: www.ftp.ne.jp
 * updates: www.ftp.ne.jp
============================================================================= N/S matched: tidy ==============================================================================
jtidy-javadoc.noarch : API documentation for jtidy
jtidy.noarch : HTML syntax checker and pretty printer
perltidy.noarch : Tool for indenting and reformatting Perl scripts

  Name and summary matches only, use "search all" for everything.

しかたがないので、 公式サイトのリンクをたどり、GitHubからrpmをダウンロードしてインストールする

ダウンロード

[root@localhost ~]# wget https://github.com/htacg/tidy-html5/releases/download/5.4.0/tidy-5.4.0-64bit.rpm
--2017-11-19 14:41:58--  https://github.com/htacg/tidy-html5/releases/download/5.4.0/tidy-5.4.0-64bit.rpm
Resolving github.com (github.com)... 192.30.255.112, 192.30.255.113
Connecting to github.com (github.com)|192.30.255.112|:443... connected.
HTTP request sent, awaiting response... 302 Found
Location: https://github-production-release-asset-2e65be.s3.amazonaws.com/2793788/064e5cc4-fea3-11e6-856c-26aa9ee8e761?X-Amz-Algorithm=AWS4-HMAC-SHA256&X-Amz-Credential=AKIAIWNJYAX4CSVEH53A%2F20171119%2Fus-east-1%2Fs3%2Faws4_request&X-Amz-Date=20171119T054159Z&X-Amz-Expires=300&X-Amz-Signature=eb31da4e53258e53465edf8916409065a5c16af6462e39af6746b6941a1f1535&X-Amz-SignedHeaders=host&actor_id=0&response-content-disposition=attachment%3B%20filename%3Dtidy-5.4.0-64bit.rpm&response-content-type=application%2Foctet-stream [following]
--2017-11-19 14:41:59--  https://github-production-release-asset-2e65be.s3.amazonaws.com/2793788/064e5cc4-fea3-11e6-856c-26aa9ee8e761?X-Amz-Algorithm=AWS4-HMAC-SHA256&X-Amz-Credential=AKIAIWNJYAX4CSVEH53A%2F20171119%2Fus-east-1%2Fs3%2Faws4_request&X-Amz-Date=20171119T054159Z&X-Amz-Expires=300&X-Amz-Signature=eb31da4e53258e53465edf8916409065a5c16af6462e39af6746b6941a1f1535&X-Amz-SignedHeaders=host&actor_id=0&response-content-disposition=attachment%3B%20filename%3Dtidy-5.4.0-64bit.rpm&response-content-type=application%2Foctet-stream
Resolving github-production-release-asset-2e65be.s3.amazonaws.com (github-production-release-asset-2e65be.s3.amazonaws.com)... 54.231.48.240
Connecting to github-production-release-asset-2e65be.s3.amazonaws.com (github-production-release-asset-2e65be.s3.amazonaws.com)|54.231.48.240|:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: 726845 (710K) [application/octet-stream]
Saving to: ‘tidy-5.4.0-64bit.rpm’

100%[====================================================================================================================================>] 726,845      435KB/s   in 1.6s   

2017-11-19 14:42:01 (435 KB/s) - ‘tidy-5.4.0-64bit.rpm’ saved [726845/726845]

インストール

[root@localhost ~]# rpm -ihv tidy-5.4.0-64bit.rpm 
Preparing...                          ################################# [100%]
Updating / installing...
   1:tidy-5.4.0-1                     ################################# [100%]

使い方

mk-55.hatenablog.com

上のエントリでも書いたが、一応こちらにも記載しておく

整形前
[mk55@localhost tmp]$ markdown test.md 
<h1>h1</h1>

<h3>h3</h3>

<p>test</p>

<p>test2</p>

<table>
<thead>
<tr>
<th> TH </th>
<th> TH </th>
</tr>
</thead>
<tbody>
<tr>
<td> TD </td>
<td> TD </td>
</tr>
<tr>
<td> TD </td>
<td> TD </td>
</tr>
</tbody>
</table>
整形後
[mk55@localhost tmp]$ markdown test.md | tidy -i -utf8  2> /dev/null
<!DOCTYPE html>
<html>
<head>
  <meta name="generator" content=
  "HTML Tidy for HTML5 for Linux version 5.4.0">
  <title></title>
</head>
<body>
  <h1>h1</h1>
  <h3>h3</h3>
  <p>test</p>
  <p>test2</p>
  <table>
    <thead>
      <tr>
        <th>TH</th>
        <th>TH</th>
      </tr>
    </thead>
    <tbody>
      <tr>
        <td>TD</td>
        <td>TD</td>
      </tr>
      <tr>
        <td>TD</td>
        <td>TD</td>
      </tr>
    </tbody>
  </table>
</body>
</html>

インデントだけでなく、漏れてたタグ等も補ってくれる