How to use the command html5validator (with examples)
This article provides examples of how to use the html5validator
command to validate HTML5 files.
Command Description
The html5validator
command is used to validate HTML5 files. It checks the syntax and structure of HTML files and reports any errors or warnings. The command provides several options to control the validation process and customize the output.
Use case 1: Validate a specific file
Code:
html5validator path/to/file
Motivation:
Validating a specific HTML file is useful when you want to check if a particular file follows the HTML5 syntax and structure.
Explanation:
html5validator
: The command to validate HTML files.path/to/file
: The path to the specific file that needs to be validated.
Example output:
No errors or warnings found in /path/to/file.
Use case 2: Validate all HTML files in a specific directory
Code:
html5validator --root path/to/directory
Motivation:
Validating all HTML files in a specific directory is useful when you want to validate multiple files at once, such as all the HTML files in a website directory.
Explanation:
html5validator
: The command to validate HTML files.--root path/to/directory
: The root directory where all the HTML files are located.
Example output:
No errors or warnings found in /path/to/directory/file1.html.
No errors or warnings found in /path/to/directory/file2.html.
...
Use case 3: Show warnings as well as errors
Code:
html5validator --show-warnings path/to/file
Motivation:
By default, the html5validator
command only displays errors. However, in some cases, it is important to also see warnings to identify potential issues in HTML files.
Explanation:
html5validator
: The command to validate HTML files.--show-warnings
: Flag to display warnings in addition to errors.path/to/file
: The path to the specific file that needs to be validated.
Example output:
Error: Element "center" not allowed as child of element "div" in this context.
Warning: The text color "#FFFFF" is not a valid CSS color value.
Use case 4: Match multiple files using a glob pattern
Code:
html5validator --root path/to/directory --match "*.html *.php"
Motivation:
Matching multiple files using a glob pattern allows you to validate only specific HTML files that match a certain pattern, based on their file extensions or names.
Explanation:
html5validator
: The command to validate HTML files.--root path/to/directory
: The root directory where all the HTML files are located.--match "*.html *.php"
: Pattern to match the HTML files. In this example, it matches all files with “.html” or “.php” extensions.
Example output:
No errors or warnings found in /path/to/directory/file1.html.
No errors or warnings found in /path/to/directory/file2.html.
...
No errors or warnings found in /path/to/directory/file3.php.
Use case 5: Ignore specific directory names
Code:
html5validator --root path/to/directory --blacklist "node_modules vendor"
Motivation:
Ignoring specific directory names is useful when you want to exclude certain directories from the validation process, such as “node_modules” or “vendor” directories that contain external dependencies.
Explanation:
html5validator
: The command to validate HTML files.--root path/to/directory
: The root directory where all the HTML files are located.--blacklist "node_modules vendor"
: List of directory names to ignore during validation. In this example, it excludes the “node_modules” and “vendor” directories.
Example output:
No errors or warnings found in /path/to/directory/file1.html.
No errors or warnings found in /path/to/directory/file2.html.
...
Use case 6: Output the results in a specific format
Code:
html5validator --format gnu|xml|json|text path/to/file
Motivation:
Formatting the validation results in a specific format is useful when you need to process the output in a particular way or integrate it into other tools or workflows.
Explanation:
html5validator
: The command to validate HTML files.--format gnu|xml|json|text
: Specify the format of the output. It can be GNU format, XML format, JSON format, or plain text format.path/to/file
: The path to the specific file that needs to be validated.
Example output:
1:2: Error: Start tag seen without seeing a doctype first. Expected “<!DOCTYPE html>”.
2:5: Warning: The “banner” role is unnecessary for element “header”.
Use case 7: Output the log at a specific verbosity level
Code:
html5validator --root path/to/directory --log debug|info|warning
Motivation:
Changing the verbosity level of the log allows you to control the amount of information displayed during the validation process. It can be useful for debugging or when you only want to see specific log levels.
Explanation:
html5validator
: The command to validate HTML files.--root path/to/directory
: The root directory where all the HTML files are located.--log debug|info|warning
: Specify the verbosity level of the log. It can be “debug”, “info”, or “warning”. The default level is “warning”.
Example output:
DEBUG: Skipping file /path/to/directory/file1.html.
INFO: Validating file /path/to/directory/file2.html.
WARNING: No errors or warnings found in /path/to/directory/file2.html.
...
Conclusion:
The html5validator
command is a powerful tool for validating HTML5 files. It provides various options to customize the validation process and control the output format and verbosity level. By understanding and utilizing these use cases, you can effectively validate HTML files and ensure they adhere to the HTML5 standard.