Text to HTML: Difference between revisions

Line 13:
 
The only requirement is to ensure that the result is valid xhtml.
 
=={{header|Phix}}==
The best thing to do here is to keep it utterly trivial.
<lang Phix>constant {hchars,hsubs} = columnize({{"&","&amp;"},
{"<","&lt;"},
{">","&gt;"},
{"\"","&quot;"},
{"\'","&apos;"}})
 
constant fmt = """
<html>
<head><title>%s</title></head>
<body>
<pre>
%s
</pre>
</body>
</html>
"""
 
function text_to_html_page(string title, text)
title = substitute_all(title,hchars,hsubs)
text = substitute_all(text,hchars,hsubs)
return sprintf(fmt,{title,text})
-- return substitute_all(sprintf(fmt,{title,text}),hchars,hsubs)
end function
 
constant text = """
This is
a paragraph
a block of
code
* A one-bullet list
> With quoted text
>
> and code
"""
 
puts(1,text_to_html_page("my title",text))</lang>
{{out}}
The last line of text_to_html() (as commented out) was used to generate the
sanitised version of the output, as needed for inclusion on this page.
<pre>
&lt;html&gt;
&lt;head&gt;&lt;title&gt;my title&lt;/title&gt;&lt;/head&gt;
&lt;body&gt;
&lt;pre&gt;
This is
a paragraph
 
a block of
code
 
* A one-bullet list
&amp;gt; With quoted text
&amp;gt;
&amp;gt; and code
 
&lt;/pre&gt;
&lt;/body&gt;
&lt;/html&gt;
</pre>
 
=={{header|Pike}}==
7,795

edits