How to make tidy (libtidy5) format html with inline elements treated as block elements

279 Views Asked by At

The new tidy (libtidy5) is great, but i can't figure out to reformat an html file with every tag on a newline so, also inline tags.

This is standard output:

echo "<p><b>Hi</b><i>Tom</i></p>" | tidy -i

<!DOCTYPE html>
<html>
<head>
  <meta name="generator" content=
  "HTML Tidy for HTML5 for Linux version 5.2.0">
  <title></title>
</head>
<body>
  <p><b>Hi</b><i>Tom</i></p>
</body>
</html>

What I want is this ouput:

 <html>
    <head>
      <meta name="generator" content=
      "HTML Tidy for HTML5 for Linux version 5.2.0">
      <title></title>
    </head>
    <body>
      <p>
       <b>Hi</b>
       <i>Tom</i>
     </p>
    </body>
    </html>

Is there a CLI parameter to do that?

(Don't like the workaround to define every html element as a block element, Would be nice if you can easily reformat between normal and all block)

1

There are 1 best solutions below

0
Janghou On

Output as xml comes close:

tidy -i -q -xml

So:

echo "<p><b>Hi</b><i>Tom</i></p>" | tidy -i -q -xml

<p>
  <b>Hi</b>
  <i>Tom</i>
</p>

Or --output-xml 1

echo "<p><b>Hi</b><i>Tom</i></p>" | tidy -iq  --output-xml 1  --show-warnings 0

<html>
  <head>
    <meta name="generator"
    content="HTML Tidy for HTML5 for Linux version 5.2.0" />
    <title></title>
  </head>
  <body>
    <p>
      <b>Hi</b>
      <i>Tom</i>
    </p>
  </body>
</html>