vnu.jarでコマンドラインからNu Markup Checkerを使う

Nu Markup Checkerにはコマンドラインから呼び出して使えるバージョンもあります(validator.github.io)。この記事では一応、vnu.jarと呼ぶことにします。vnu.jarを使うとローカルに置いたHTML5のチェックは次のように関単に行うことができます。

$ java -jar vnu.jar html/index.html
"file:/path/to/html/index.html":8.46-8.46: error: End tag “h5” seen, but there were open elements.
"file:/path/to/html/index.html":8.50-8.50: error: No “p” element in scope but a “p” end tag seen.

vnu.jarを使うと単一のHTMLファイルだけではなく、フォルダ単位で一度にチェックすることも可能です。

$ java -jar vnu.jar html
"file:/path/to/html/index.html":8.46-8.46: error: End tag “h5” seen, but there were open elements.
"file:/path/to/html/index.html":8.50-8.50: error: No “p” element in scope but a “p” end tag seen.
"file:/path/to/html/sub_dir/index.html":8.24-8.24: error: End tag “p” seen, but there were open elements.
"file:/path/to/html/sub_dir/index.html":8.15-8.15: error: Unclosed element “a”.
"file:/path/to/html/sub_dir/index.html":9.7-9.7: error: End tag for  “body” seen, but there were unclosed elements.

validator.github.ioによるとvnu.jarを使ったHTMLの検証を行うgruntのプラグインもあるそうです(grunt-html)。

便利なvnu.jarですが、検証内容には注意が必要です。

この記事を書いている段階で最新の2014年8月25日版vnu.jarW3Cの仕様(HTML 5.1など)ではなくHTML Living Standardに沿っているかをチェックします。そのためhttp://validator.w3.org/とのチェック結果とは一致しない部分がいくつかあります。例えば、vnu.jarはhgroup要素に対してエラーを報告しませんが、http://validator.w3.org/はエラーを報告します。W3Cの仕様に沿うかどうかはNu Markup Checkerのビルド時にRelaxスキーマを書き換えることで行っている(build.py)ため、jarファイルを弄らずに変更することはできません(vnu.jarの中にRelaxスキーマが格納されています)。

また、Nu Markup Checkerは現在も開発が続いている(githubのプロジェクト)ため、自分が使っているvnu.jarと最新のNu Markup Checkerのチェック結果が異なる可能性がある点にも注意が必要です。