Search code examples
htmlperlindentation

HTML indenting with Perl


I have a variable (ex. $content) with HTML code (without line breaks - removed before). How to process HTML code with adding TAB indent after each open tag and decrease indent level after each closing tag?

P.S. I don't need external script or programm (like tidy). I need to make this in my own script.

For example: source content:

<!DOCTYPE html><html><head><title>test</title></head>   <body>  <h1>hello!</h1><p>It works!</p></body></html>

needed result:

<!DOCTYPE html>
<html>
    <head>
        <title>test</title>
    </head>
    <body>
        <h1>hello!</h1>
        <p>It works!</p>
    </body>
</html>

Solution

  • use HTML::HTML5::Parser qw();
    use HTML::HTML5::Writer qw();
    use XML::LibXML::PrettyPrint qw();
    
    print HTML::HTML5::Writer->new(
        start_tags => 'force',
        end_tags => 'force',
    )->document(
        XML::LibXML::PrettyPrint->new_for_html(
            indent_string => "\t"
        )->pretty_print(
            HTML::HTML5::Parser->new->parse_string(
                '<!DOCTYPE html><html><head><title>test</title></head>   <body>  <h1>hello!</h1><p>It works!</p></body></html>'
            )
        )
    );
    

    <!DOCTYPE html><html>
        <head>
            <title>test</title>
        </head>
        <body>
            <h1>hello!</h1>
            <p>It works!</p>
        </body>
    </html>