使用Perl进行HTML缩进

Ver*_*Lom 2 html perl indentation

我有一个带有HTML代码的变量(例如$ content)(没有换行符 - 之前已删除).如何在每个打开的标记后添加TAB缩进处理HTML代码,并在每个结束标记后减少缩进级别?

PS我不需要外部脚本或程序(如整洁).我需要在我自己的脚本中进行此操作.

例如:源内容:

<!DOCTYPE html><html><head><title>test</title></head>   <body>  <h1>hello!</h1><p>It works!</p></body></html>
Run Code Online (Sandbox Code Playgroud)

需要的结果:

<!DOCTYPE html>
<html>
    <head>
        <title>test</title>
    </head>
    <body>
        <h1>hello!</h1>
        <p>It works!</p>
    </body>
</html>
Run Code Online (Sandbox Code Playgroud)

dax*_*xim 12

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>'
        )
    )
);
Run Code Online (Sandbox Code Playgroud)
<!DOCTYPE html><html>
    <head>
        <title>test</title>
    </head>
    <body>
        <h1>hello!</h1>
        <p>It works!</p>
    </body>
</html>
Run Code Online (Sandbox Code Playgroud)