Text to HTML: Difference between revisions

Added Wren
m (→‎{{header|Raku}}: Fix code and comments: Perl 6 --> Raku)
(Added Wren)
Line 734:
<p>The formatter can render text with <i>italics</i>, <b>bold</b> and in a <tt>typewriter</tt> font. It also does the right thing with &lt;angle brackets&gt; and &amp;amp;ersands, but relies on the encoding of the characters to be conveyed separately.</p>
</body></html></lang>
 
=={{header|Wren}}==
{{trans|Go}}
{{libheader|Wren-pattern}}
Note that Wren doesn't support any form of raw string so we need to construct the sample text by concatenating strings for each paragraph.
<lang ecmascript>import "/pattern" for Pattern
 
var t =
" Sample Text\n\n" +
"This is an example of converting plain text to HTML which demonstrates extracting a title and escaping certain characters within bulleted and numbered lists.\n\n" +
"* This is a bulleted list with a less than sign (<)\n\n" +
"* And this is its second line with a greater than sign (>)\n\n" +
"A 'normal' paragraph between the lists.\n\n" +
"1. This is a numbered list with an ampersand (&)\n\n" +
"2. \"Second line\" in double quotes\n\n" +
"3. 'Third line' in single quotes\n\n" +
"That's all folks."
 
// prefer the standard &quot; for escaping a double-quote character rather than Go's &#34;
var escapes = [ ["&", "&amp;"], ["<", "&lt;"], [">", "&gt;"], ["\"", "&quot;"], ["'", "&#39;"] ]
for (esc in escapes) t = t.replace(esc[0], esc[1])
var paras = t.split("\n\n")
var ol = Pattern.new("/d.", Pattern.start)
 
// Assume if first character of first paragraph is white-space
// then it's probably a document title.
var firstChar = paras[0][0]
var title = "Untitled"
var k = 0
if (firstChar == " " || firstChar == "\t") {
title = paras[0].trim()
k = 1
}
System.print("<html>")
System.print("<head><title>%(title)</title></head>")
System.print("<body>")
 
var blist = false
var nlist = false
for (para in paras.skip(k)) {
var para2 = para.trim()
var cont = false
if (para2.startsWith("*")) {
if (!blist) {
blist = true
System.print("<ul>")
}
para2 = para2[1..-1].trim()
System.print(" <li>%(para2)</li>")
cont = true
} else if (blist) {
blist = false
System.print("</ul>")
}
 
if (!cont) {
if (ol.isMatch(para2)) {
if (!nlist) {
nlist = true
System.print("<ol>")
}
para2 = para2[2..-1].trim()
System.print(" <li>%(para2)</li>")
cont = true
} else if (nlist) {
nlist = false
System.print("</ol>")
}
if (!cont && !blist && !nlist) System.print("<p>%(para2)</p>")
}
}
 
if (blist) System.print("</ul>")
if (nlist) System.prin("</ol>")
System.print("</body>")
System.print("</html>")
</lang>
 
{{out}}
<pre>
<html>
<head><title>Sample Text</title></head>
<body>
<p>This is an example of converting plain text to HTML which demonstrates extracting a title and escaping certain characters within bulleted and numbered lists.</p>
<ul>
<li>This is a bulleted list with a less than sign (&lt;)</li>
<li>And this is its second line with a greater than sign (&gt;)</li>
</ul>
<p>A &#39;normal&#39; paragraph between the lists.</p>
<ol>
<li>This is a numbered list with an ampersand (&amp;)</li>
<li>&quot;Second line&quot; in double quotes</li>
<li>&#39;Third line&#39; in single quotes</li>
</ol>
<p>That&#39;s all folks.</p>
</body>
</html>
</pre>
9,486

edits