Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pretty HTML snippet output

I've a snippet of HTML <div><p>text1</p></div><div><p>text1</p></div>

I want to make it pretty like this

<div>
  <p>text1</p>
</div>
<div>
  <p>text1</p>
</div>

What would be most simple way to do it? (I've looked on transform and jsoup) but not sure what would be really smart to use. Thanks!

like image 960
Dmytro Pastovenskyi Avatar asked Mar 22 '15 16:03

Dmytro Pastovenskyi


3 Answers

You can use Jsoup like

String html = "<div><p>text1</p></div><div><p>text1</p></div>";
Document doc = Jsoup.parseBodyFragment(html);

But this will wrap your text into

<html>
  <head></head>
  <body>
    ..
  </body>
</html>

To get rid of this part you can get part from <body> like

System.out.println(doc.body().html());

which prints

<div>
 <p>text1</p>
</div>
<div>
 <p>text1</p>
</div>

If you want to increase indentation you can set it earlier with

doc.outputSettings().indentAmount(4); 

now result will look like

<div>
    <p>text1</p>
</div>
<div>
    <p>text1</p>
</div>
like image 118
Pshemo Avatar answered Nov 19 '22 21:11

Pshemo


jTidy could fit for this task - http://jtidy.sourceforge.net/howto.html

public String prettyPrintHTML(String rawHTML)
{    
    Tidy tidy = new Tidy();
    tidy.setXHTML(true);
    tidy.setIndentContent(true);
    tidy.setPrintBodyOnly(true);
    tidy.setTidyMark(false);

    // HTML to DOM
    Document htmlDOM = tidy.parseDOM(new ByteArrayInputStream(rawHTML.getBytes()), null);

    // Pretty Print
    OutputStream out = new ByteArrayOutputStream();
    tidy.pprint(htmlDOM, out);

    return out.toString();
}
like image 25
Jens A. Koch Avatar answered Nov 19 '22 20:11

Jens A. Koch


I would use HTML Tidy here is an online version.

Many of the text editors have plugins or built in functionality for this.

Sublime Text

BBEdit

Coda

like image 2
whoacowboy Avatar answered Nov 19 '22 21:11

whoacowboy