Skip to content Skip to sidebar Skip to footer

Use Html Tidy To Just Indent Html Code?

Is it possible to use HTML Tidy to just indent HTML code? Sample Code

Solution 2:

I didn't found a possibility "only reindent - without any changes". The next config file will "repair" as low as possible and (mostly) only re-indent the html. Tidy still correcting some errorish conditions, like duplicated (repeated) attributes.

#based on http://tidy.sourceforge.net/docs/quickref.html#HTML, XHTML, XML Options Referenceanchor-as-name:no#?doctype:omitdrop-empty-paras:nofix-backslash:nofix-bad-comments:nofix-uri:nohide-endtags:yes#?#input-xml: yes     #?join-styles:noliteral-attributes:yeslower-literals:nomerge-divs:nomerge-spans:nooutput-html:yespreserve-entities:yesquote-ampersand:noquote-nbsp:noshow-body-only:auto#Diagnostics Options Referenceshow-errors:0show-warnings:0#Pretty Print Options Referencebreak-before-br:yesindent:yesindent-attributes:no#defaultindent-spaces:4tab-size:4wrap:132wrap-asp:nowrap-jste:nowrap-php:nowrap-sections:no#Character Encoding Options Referencechar-encoding:utf8#Miscellaneous Options Referenceforce-output:yesquiet:yestidy-mark:no

For example the next html-fragment

<div>
<div>
<p>
not closed para
<h1>
h1 head
</h1>
<ul>
<li>not closed li
<li>closed li</li>
</ul>
some text
</div>
</div>

will changed to

<div>
    <div>
        <p>
            not closed para
        <h1>
            h1 head
        </h1>
        <ul>
            <li>not closed li
            <li>closed li
            </ul>some text
    </div>
</div>

As you can notice, the hide-endtags: yes hides the closing </li> from the second bullet in the input. Setting the hide-endtags: no - will get the next:

<div>
    <div>
        <p>
            not closed para
        </p>
        <h1>
            h1 head
        </h1>
        <ul>
            <li>not closed li
            </li>
            <li>closed li
            </li>
        </ul>some text
    </div>
</div>

so, tidy adds closing </p> and closing </li> to first bullet.

I didn't found a possibility preserve everything on input and only reindent the file.

Solution 3:

You need the following option:

tidy --show-body-only yes -i4 -w 80 -m file.html

http://tidy.sourceforge.net/docs/quickref.html#show-body-only

-i 4 - indents 4 spaces (EDIT: tidy never uses tabs) or --indent-with-tabs yes - instead (--tab-size may affect wrapping)

-w 80 - wrap at column 80 (default on my system: 68, very narrow)

-m - modify file inplace

(you may want to leave out the last option, and examine the output first)

Showing only body, will naturally leave out the tidy-mark (generator meta).

Another cool options are: --quiet yes - doesn't print W3C advertisements and other unnecessary output (errors still reported)

Solution 4:

To answer the poster's original question, using Tidy to just indent HTML code, here's what I use:

tidy --indent auto --quiet yes --show-body-only auto --show-errors 0 --wrap 0 input.html

input.html

<formaction="?"method="get"accept-charset="utf-8"><ul><li><labelclass="screenReader"for="q">Keywords</label><inputtype="text"name="q"value=""id="q" /></li><li><inputclass="submit"type="submit"value="Search" /></li></ul></form>

Output:

<formaction="?"method="get"accept-charset="utf-8"><ul><li><labelclass="screenReader"for="q">Keywords</label><inputtype="text"name="q"value=""id="q"></li><li><inputclass="submit"type="submit"value="Search"></li></ul></form>

No extra HTML code added. Errors are suppressed. To find out what each option does, it's best to refer to the official reference.

Solution 5:

I am very late to the party :)

But in your tidy config file set

tidy-mark: no

by default this is set to yes.

Once done, tidy will not add meta generator tag to your html.

Post a Comment for "Use Html Tidy To Just Indent Html Code?"