Text to HTML: Difference between revisions

m
→‎{{header|Wren}}: Minor tidy including usage now of raw string.
m (→‎{{header|Raku}}: Fix code and comments: Perl 6 --> Raku)
m (→‎{{header|Wren}}: Minor tidy including usage now of raw string.)
(7 intermediate revisions by 6 users not shown)
Line 16:
=={{header|Go}}==
This isn't very sophisticated but does a few things in a simple-minded way.
<langsyntaxhighlight lang="go">package main
 
import (
Line 106:
fmt.Println("</body>")
fmt.Println("</html>")
}</langsyntaxhighlight>
 
{{out}}
<langsyntaxhighlight lang="html"><html>
<head><title>Sample Text</title></head>
<body>
Line 125:
<p>That&#39;s all folks.</p>
</body>
</html></langsyntaxhighlight>
 
=={{header|PhixJulia}}==
{{trans|Go}}
The best thing to do here is to keep it utterly trivial.
<syntaxhighlight lang="julia">using HttpCommon, Printf
<lang Phix>constant {hchars,hsubs} = columnize({{"&","&amp;"},
{"<","&lt;"},
{">","&gt;"},
{"\"","&quot;"},
{"\'","&apos;"}})
 
const exampletxt = """ Sample Text
constant fmt = """
 
This is an example of converting plain text to HTML which demonstrates extracting a title and escaping certain characters within bulleted and numbered lists.
 
* This is a bulleted list with a less than sign (<)
 
* And this is its second line with a greater than sign (>)
 
A 'normal' paragraph between the lists.
 
1. This is a numbered list with an ampersand (&)
 
2. "Second line" in double quotes
 
3. 'Third line' in single quotes
 
That's all folks."""
 
function txt_to_html(t = exampletxt)
p = r"\n\s*(\n\s*)+"
ul = r"^\*"
ol = r"^\d\."
paras = map(p -> escapeHTML(string(p)), split(t, r"[\r\n]+"))
# Assume if first character of first paragraph is white-space
# then it's probably a document title.
firstchar = first(first(paras))
title = "Untitled"
k = 1
if firstchar == ' ' || firstchar == '\t'
title = strip(paras[1])
k = 2
end
println("<html>")
@printf("<head><title>%s</title></head>\n", title)
println("<body>")
 
blist, nlist = false, false
for para in @view paras[k:end]
para2 = strip(para)
 
if occursin(ul, para2)
if !blist
blist = true
println("<ul>")
end
para2 = strip(para2[2:end])
@printf(" <li>%s</li>\n", para2)
continue
elseif blist
blist = false
println("</ul>")
end
 
if occursin(ol, para2)
if !nlist
nlist = true
println("<ol>")
end
para2 = strip(para2[3:end])
@printf(" <li>%s</li>\n", para2)
continue
elseif nlist
nlist = false
println("</ol>")
end
 
if !blist && !nlist
@printf("<p>%s</p>\n", para2)
end
end
if blist
println("</ul>")
end
if nlist
println("</ol>")
end
println("</body>")
println("</html>")
end
 
txt_to_html()
</syntaxhighlight>{{out}}
<pre>
<html>
<head><title>%sSample 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>
<pre>
<ul>
%s
<li>This is a bulleted list with a less than sign (&lt;)</li>
</pre>
<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>
"""
 
=={{header|Nim}}==
function text_to_html_page(string title, text)
{{trans|Go}}
title = substitute_all(title,hchars,hsubs)
<syntaxhighlight lang="nim">import re, strutils, xmltree
text = substitute_all(text,hchars,hsubs)
return sprintf(fmt,{title,text})
-- return substitute_all(sprintf(fmt,{title,text}),hchars,hsubs)
end function
 
const Text = """ Sample Text
constant text = """
This is
a paragraph
a block of
code
* A one-bullet list
> With quoted text
>
> and code
"""
 
This is an example of converting plain text to HTML which demonstrates extracting a title and escaping certain characters within bulleted and numbered lists.
puts(1,text_to_html_page("my title",text))</lang>
 
* This is a bulleted list with a less than sign (<)
 
* And this is its second line with a greater than sign (>)
 
A 'normal' paragraph between the lists.
 
1. This is a numbered list with an ampersand (&)
 
2. "Second line" in double quotes
 
3. 'Third line' in single quotes
 
That's all folks."""
 
 
let p = re"\n\s*(\n\s*)+"
let ul = re"^\*"
let ol = re"^\d\."
let text = xmltree.escape(Text)
let paras = text.split(p)
 
# Assume if first character of first paragraph is white-space
# then it's probably a document title.
let firstChar = paras[0][0]
var titleString = "untitled"
var start = 0
if firstChar.isSpaceAscii:
titleString = paras[0].strip()
start = 1
echo "<html>"
echo "<head><title>", titleString, "</title></body>"
echo "<body>"
 
var blist, nlist = false
for ipara in start..paras.high:
var para = paras[ipara].strip()
 
if para.find(ul) >= 0:
if not blist:
blist = true
echo "<ul>"
echo " <li>", para[1..^1].strip(), "</li>"
continue
elif blist:
blist = false
echo "</ul>"
 
if para.find(ol) >= 0:
if not nlist:
nlist = true
echo "<ol>"
echo " <li>", para[2..^1].strip(), "</li>"
continue
elif nlist:
nlist = false
echo "</ol>"
 
if not (blist or nlist):
echo "<p>", para, "</p>"
 
if blist: echo "</ul>"
if nlist: echo "</ol>"
 
echo "</body>"
echo "</html>"</syntaxhighlight>
 
{{out}}
<pre><html>
<head><title>Sample Text</title></body>
<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 &apos;normal&apos; 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>&apos;Third line&apos; in single quotes</li>
</ol>
<p>That&apos;s all folks.</p>
</body>
</html></pre>
 
=={{header|Perl}}==
{{trans|Raku}}
<syntaxhighlight lang="perl"># 20201023 added Perl programming solution
 
use strict;
use warnings;
 
use Pod::Simple::HTML;
 
# POD example taken from https://juerd.nl/site.plp/perlpodtut
my $pod = <<'POD';
=head1 NAME
 
My::Module - An example module
 
=head1 SYNOPSIS
 
use My::Module;
my $object = My::Module->new();
print $object->as_string;
 
=head1 DESCRIPTION
 
This module does not really exist, it
was made for the sole purpose of
demonstrating how POD works.
 
=head2 Methods
 
=over 12
 
=item C<new>
 
Returns a new My::Module object.
 
=item C<as_string>
 
Returns a stringified representation of
the object. This is mainly for debugging
purposes.
 
=back
 
=head1 LICENSE
 
This is released under the Artistic
License. See L<perlartistic>.
 
=head1 AUTHOR
 
Juerd - L<http://juerd.nl/>
 
=head1 SEE ALSO
 
L<perlpod>, L<perlpodspec>
 
=cut
POD
 
my $parser = Pod::Simple::HTML->new();
$parser->output_fh(*STDOUT);
$parser->parse_string_document($pod)</syntaxhighlight>
 
=={{header|Phix}}==
The best thing to do here is to keep it utterly trivial.
<!--<syntaxhighlight lang="phix">(phixonline)-->
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span>
<span style="color: #008080;">constant</span> <span style="color: #0000FF;">{</span><span style="color: #000000;">hchars</span><span style="color: #0000FF;">,</span><span style="color: #000000;">hsubs</span><span style="color: #0000FF;">}</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">columnize</span><span style="color: #0000FF;">({{</span><span style="color: #008000;">"&"</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"&amp;amp;"</span><span style="color: #0000FF;">},</span>
<span style="color: #0000FF;">{</span><span style="color: #008000;">"&lt;"</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"&amp;lt;"</span><span style="color: #0000FF;">},</span>
<span style="color: #0000FF;">{</span><span style="color: #008000;">"&gt;"</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"&amp;gt;"</span><span style="color: #0000FF;">},</span>
<span style="color: #0000FF;">{</span><span style="color: #008000;">"\""</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"&dquo;"</span><span style="color: #0000FF;">},</span>
<span style="color: #0000FF;">{</span><span style="color: #008000;">"\'"</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"&squo;"</span><span style="color: #0000FF;">}})</span>
<span style="color: #008080;">constant</span> <span style="color: #000000;">fmt</span> <span style="color: #0000FF;">=</span> <span style="color: #008000;">"""
&lt;html&gt;
&lt;head&gt;&lt;title&gt;%s&lt;/title&gt;&lt;/head&gt;
&lt;body&gt;
&lt;pre&gt;
%s
&lt;/pre&gt;
&lt;/body&gt;
&lt;/html&gt;
"""</span>
<span style="color: #008080;">function</span> <span style="color: #000000;">text_to_html_page</span><span style="color: #0000FF;">(</span><span style="color: #004080;">string</span> <span style="color: #000000;">title</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">text</span><span style="color: #0000FF;">)</span>
<span style="color: #000000;">title</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">substitute_all</span><span style="color: #0000FF;">(</span><span style="color: #000000;">title</span><span style="color: #0000FF;">,</span><span style="color: #000000;">hchars</span><span style="color: #0000FF;">,</span><span style="color: #000000;">hsubs</span><span style="color: #0000FF;">)</span>
<span style="color: #000000;">text</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">substitute_all</span><span style="color: #0000FF;">(</span><span style="color: #000000;">text</span><span style="color: #0000FF;">,</span><span style="color: #000000;">hchars</span><span style="color: #0000FF;">,</span><span style="color: #000000;">hsubs</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">return</span> <span style="color: #7060A8;">sprintf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">fmt</span><span style="color: #0000FF;">,{</span><span style="color: #000000;">title</span><span style="color: #0000FF;">,</span><span style="color: #000000;">text</span><span style="color: #0000FF;">})</span>
<span style="color: #000080;font-style:italic;">-- return substitute_all(sprintf(fmt,{title,text}),hchars,hsubs)</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">function</span>
<span style="color: #008080;">constant</span> <span style="color: #000000;">text</span> <span style="color: #0000FF;">=</span> <span style="color: #008000;">"""
This is
a paragraph
a block of
code
* A one-bullet list
&gt; With quoted text
&gt;
&gt; and code
"""</span>
<span style="color: #7060A8;">puts</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #000000;">text_to_html_page</span><span style="color: #0000FF;">(</span><span style="color: #008000;">"my title"</span><span style="color: #0000FF;">,</span><span style="color: #000000;">text</span><span style="color: #0000FF;">))</span>
<!--</syntaxhighlight>-->
{{out}}
The last line of text_to_html() (as commented out) was used to generate the
Line 209 ⟶ 474:
 
this implementation is still incomplete.
<langsyntaxhighlight Pikelang="pike">// function to calculate the average line length (not used yet below)
int linelength(array lines)
{
Line 326 ⟶ 591:
}
return root;
}</langsyntaxhighlight>
 
=={{header|Racket}}==
Line 336 ⟶ 601:
It certainly seems to me as a useful thing compared to some half-baked not-really-markdown-or-anything implementation.)
 
<langsyntaxhighlight lang="racket">
#lang at-exp racket
 
Line 365 ⟶ 630:
> and code
})
</syntaxhighlight>
</lang>
 
{{out}}
Line 393 ⟶ 658:
 
It is '''not''' markup free, but it is actually usable in production.
<syntaxhighlight lang="raku" perl6line>use Pod::To::HTML;
use HTML::Escape;
 
Line 497 ⟶ 762:
 
# normally
#say render($pod6);</langsyntaxhighlight>
 
{{out|Returns something like}}
Line 626 ⟶ 891:
=={{header|Tcl}}==
This renderer doesn't do all that much. Indeed, it deliberately avoids doing all the complexity that is possible; instead it seeks to just provide the minimum that could possibly be useful to someone who is doing very simple text pages.
<langsyntaxhighlight lang="tcl">package require Tcl 8.5
 
proc splitParagraphs {text} {
Line 696 ⟶ 961:
}
return [append result "</body></html>"]
}</langsyntaxhighlight>
Here's an example of how it would be used.
<langsyntaxhighlight lang="tcl">set sample "
This is an example of how a pseudo-markdown-ish formatting scheme could
work. It's really much simpler than markdown, but does support a few things.
Line 720 ⟶ 985:
but relies on the encoding of the characters to be conveyed separately."
 
puts [markupText "Sample" $sample]</langsyntaxhighlight>
{{out}}
<langsyntaxhighlight lang="html"><html><head><title>Sample</title>
</head><body><h1>Sample</h1>
<p>This is an example of how a pseudo-markdown-ish formatting scheme could work. It's really much simpler than markdown, but does support a few things.</p>
Line 733 ⟶ 998:
</ol><h2>Inline formatting types</h2>
<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></langsyntaxhighlight>
 
=={{header|Wren}}==
{{trans|Go}}
{{libheader|Wren-pattern}}
<syntaxhighlight lang="wren">import "./pattern" for Pattern
 
var t = """ Sample Text
 
This is an example of converting plain text to HTML which demonstrates extracting a title and escaping certain characters within bulleted and numbered lists.
 
* This is a bulleted list with a less than sign (<)
 
* And this is its second line with a greater than sign (>)
 
A 'normal' paragraph between the lists.
 
1. This is a numbered list with an ampersand (&)
 
2. "Second line" in double quotes
 
3. 'Third line' in single quotes
 
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>")
</syntaxhighlight>
 
{{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,490

edits