HTML indenting with Perl - html

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>

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>

The manual page says that tidy won't produce output that contains tabs. But it's simple enough to post-process the output to deal with that.
$ tidy -indent foo.html | perl -pe 's|^( +)|"\t" x ((length $1) / 2)|e;'
Using an existing tool has to be a far better solution than inventing it yourself. But if you insist then you should, at least, use a pre-written parser like Perl's HTML::Parser.
I should also point out that your specification of the problem seems to be incorrect. You say you want to add a tab after each opening tag. But your sample output doesn't do that for the <title>, <h1> or &p> tags.

An option I've used is CGI::Pretty.

You could also try Marpa::R2::HTML referring to the source of its companion/demo utility html_fmt to see how to target specific parts of the document for manipulation. I haven't used it and can't try today for want of 5.10 but it looks like it could be a good match.

Related

How do I run or echo HTML snippets in Quarto?

I'm quite new to Quarto and still try to figure basic things out.
Is there a way to run or echo HTML code chunks in Quarto?
This does not work:
{html}
#| echo: false
<html>
<head>
<title>
Test
</title>
</head>
<body>
<h1>Title</h1>
</body>
</html>
So what is the correct way to do it (if it's possible at all)?
EDIT: This works, but only for echo.
``` html
Test
```
Can I run the code instead and embed the result into the PDF file as a static image? Is there an automatic way to do this?
Kind regards
winnewoerp

How can an HTML be contained, inline, within another HTML doc?

Is it possible wrap an HTML document within another, all within the same file? Such as:
<!DOCTYPE html>
<html>
<body>
...
<!DOCTYPE html>
<html>
<body>...</body>
</html>
...
</html>
Why would I want to do that, you ask? (I don't :-) ) The phpinfo() function in PHP generates a full HTML document (to be used as a single call, on its own). I created an HTML document around it. Most browsers display this as I expected, but it is invalid HTML.
<!DOCTYPE html>
<html>
<body>
<h1>PHP Info</H1>
<?php phpinfo(); ?>
</body>
</html>
I can solve this with PHP, but I was for an HTML-only solution; something like:
...
<xframe>
<!DOCTYPE html>
<html>
<body>...</body>
</html>
</xframe>
...
I.e., something like <iframe> without the external reference.
Not as such.
The closest you could come would be to use an iframe with the src expressed as a data: scheme URL (which encodes the entire HTML document inside the URL itself) … but that would require phpinfo() to return a string of HTML instead of just outputting it.

How to display html code in a html page in a formatted manner [duplicate]

This question already has answers here:
How to display raw HTML code on an HTML page
(30 answers)
Closed 2 years ago.
My website is a simple educational one. I want to display HTML code in my web page in a formatted way like they look in a editor. I mean to say the HTML tags should appear in a different color from remaining text etc. This is a code snippet from another website. I want the output of my web page like this:
This is my code :-
<html>
<head>
<title>HTML Tutorial</title>
</head>
<body>
<div class="code">
<xmp>
<!DOCTYPE html>
<html>
<head>
<title>HTML Tutorial</title>
</head>
<body>
This is a simple HTML page
</body>
</html>
</xmp>
</div>
</body>
</html>
How can I achieve desired behavior in my web page. I thank you all for your efforts.
The most vanilla way to do this and have HTML show up as actual content on your webpage is by wrapping you HTML markup you want to display inside of ' <pre> ' tags.
Then you would need to use HTML entities to show the special characters you need, an open bracket is
<
and a closing bracket is
>
You can also use a plug-in to help aid in making your code look nice, like for syntax highlighting and more. A pretty nice javascript plug-in can be found here http://prismjs.com/
Use appropriate HTML markup. Don't use <xmp>, which isn't in HTML.
Use <pre> to indicate that white space is significant
Use entities for characters with special meaning in HTML (e.g. <)
Use <code> to mark up code (e.g. <code class="html tag start-tag"><title></code>).
Apply CSS for the colours you want. The <code> elements give you something to target.
If you're using Prism as suggested by #JustSomeDude, you might want to use the Unescaped Markup plug-in so that you do not need to use the special characters like < to display your HTML tags.

How to make code show up in a HTML paragraph?

I want to be able to make code show up on my website so that it shows up with the code on the webpage instead of using it as code in the HTML file itself.
Example:
<!DOCTYPE html>
<html>
<body>
<p>Hello World</p>
</body>
</html>
Instead of:
Hello World
I know I explained that horribly but I'm sure you can see where I'm coming from. Can you escape in HTML? Or is there a tag that allows for HTML code to be viewed as text on a webpage?
xmp tag
<!DOCTYPE html>
<html>
<body>
<xmp><p>Hello World</p></xmp>
</body>
</html>
keep in mind that xmp tag is considered obsolete, as far as I know it is still supported by most browser but your mileage may vary.
you are safer if you use <pre> and escape html code with < and > like this
<!DOCTYPE html>
<html>
<body>
<pre><p>Hello World</p></pre>
</body>
</html>
There is similar question answered on this link: Display HTML code in HTML
In addition, have a look at the following websites
https://craig.is/making/rainbows
https://highlightjs.org/
You can use the xmp property. Anything inside the xmp that is exempted by the browser while rendering the HTML code.
Example :
<xmp><h1>Heading</h1></xmp>

Writing '<' in HTML

I started learning HTML a little while back and now I have hit a snag regarding displaying '<'. This is the code.
<!DOCTYPE html>
<title>Test html file</title>
<body>
<h1>Sample Heading</h1>
<pre> This is some sample text. Some more text. The pre tag
preserves formatting.
Is a<b?
</pre>
</body>
</html>
The file is locally stored and rendered on my local browser. I expect it should display the text as is, preserving line breaks and whitespace as stated here.
But it doesn't display that, instead it renders everything upto and including 'a'. I understand that the problem is due to '<' being intepreted as the start of a tag, and I am supposed to escape that somehow, but I couldn't find the appropriate syntax for that. Help? Also, what are some other good sources for learning html?
Learn about HTML entities. Code
a<b
Use <
<pre> This is some sample text. Some more text. The pre tag
preserves formatting.
Is a<b?
</pre>
Simply replace < with <.
You can also find other Special characters codes used in Math here.
Here this the code use this same code it will work.
<!DOCTYPE html>
<title>Test html file</title>
<body>
<h1>Sample Heading</h1>
<pre> This is some sample text. Some more text. The pre tag
preserves formatting.
Is a<b?
</pre>
</body>
</html>