Text to HTML: Difference between revisions
Content deleted Content added
SqrtNegInf (talk | contribs) 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 <angle brackets> and &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 " for escaping a double-quote character rather than Go's "
var escapes = [ ["&", "&"], ["<", "<"], [">", ">"], ["\"", """], ["'", "'"] ]
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 (<)</li>
<li>And this is its second line with a greater than sign (>)</li>
</ul>
<p>A 'normal' paragraph between the lists.</p>
<ol>
<li>This is a numbered list with an ampersand (&)</li>
<li>"Second line" in double quotes</li>
<li>'Third line' in single quotes</li>
</ol>
<p>That's all folks.</p>
</body>
</html>
</pre>
|