user3589620
user3589620

Reputation:

Indent <head> and <body> using HTML Tidy

I want to indent

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Document</title>
</head>
<body>
    <ul>
        <li>List 1</li>
        <li>List 2</li>
        <li>List 3</li>
        <li>List 4</li>
        <li>List 5</li>
    </ul>
</body>
</html>

to

<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>Document</title>
    </head>
    <body>
        <ul>
            <li>List 1</li>
            <li>List 2</li>
            <li>List 3</li>
            <li>List 4</li>
            <li>List 5</li>
        </ul>
    </body>
</html>

with tidy -config config.txt -m index.html.

The config.txt looks like

doctype: html5
indent: auto
indent-spaces: 4
wrap: 0
tab-size: 4
quiet: yes
show-warnings: no
tidy-mark: no

The version is: HTML Tidy for Apple macOS version 5.6.0

I skimmed through HTML Tidy 5.7.0 Options Quick Reference and tried a few things, but without success.

What is missing?

Upvotes: 1

Views: 233

Answers (1)

stackprotector
stackprotector

Reputation: 13412

The "problem" is your option indent: auto. In the man pages, you can read:

If set to auto Tidy will decide whether or not to indent the content of tags such
as <title>, <h1>-<h6>, <li>, <td>, or <p> based on the content including a block-
level element.

When you set indent to yes, your <head> and <body> sections will be indented, but also all list items, which you might not like:

<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>
            Document
        </title>
    </head>
    <body>
        <ul>
            <li>List 1
            </li>
            <li>List 2
            </li>
            <li>List 3
            </li>
            <li>List 4
            </li>
            <li>List 5
            </li>
        </ul>
    </body>
</html>

FYI: Instead of writing all options for tidy into a config file, you can also pass them as options by prefixing each option with -- and adding the value separated by space. The minimal command to produce the above output is:

tidy --indent yes --indent-spaces 4 --quiet yes --tidy-mark no index.html

Might be usefull in some cases.

Upvotes: 0

Related Questions