Τρίτη 12 Νοεμβρίου 2024

Converting plain text to HTML (A rosettacode.org task)

 This module print this (and open default browser and show this as an html page):

<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>


Module CheckIt {
d$={        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.
}

dim par()
nl={
}
d$+=nl
do k=len(d$)
d$=replace$("  ", " ", d$)
until k=len(d$)
do k=len(d$)
d$=replace$(nl+" ", nl, d$)
until k=len(d$)
do k=len(d$)
d$=replace$(nl+nl, nl, d$)
until k=len(d$)
d$=left$(d$, len(d$)-len(nl))
let par()=piece$(@escapeHTML(d$), nl)
endline=len(par())+1
dim par(1 to endline)
string t={<html>
<head>
<title>+++</title>
</head>
<body>
}
par(1)=replace$("+++", par(1), t)
par(endline)={</body>
</html>
}
flush
boolean onelevel_list, onelevel_numeric_list
for i=1 to endline
select case left$(par(i),1)
case "*"
{ CheckFlags(1)
data "<li>"+ltrim$(mid$(par(i), 2))+"</li>"
}
case "<"
{ CheckFlags(0)
while right$(par(i), 2)=nl
par(i)=left$(par(i), len(par(i))-2)
end while
data par(i)
}
case "1" to "9"
{ CheckFlags(2)
m=0
j=val(par(i), 1033, m)
data "<li>"+ltrim$(mid$(par(i), m+1))+"</li>"
}
case else
{ CheckFlags(0)
data "<p>"+par(i)+"</p>"
}
end select
next
Document doc$
if not empty then Doc$=letter$

while not empty
Doc$=nl+letter$
end while
Report Doc$
Clipboard Doc$
const UTF=2, UTF_no_BOM=-2
' insert a BOM
const CRLF=0, LF=10, CR=10
' CRLF is ok
Save.Doc Doc$, "this.html", UTF+CRLF
' Open the default browser
Win file.app$("html"), dir$+"this.html"
End
Sub CheckFlags(c as byte)
if c=1 then
if onelevel_list else
data "<ul>"
onelevel_list=true
end if
else.if c=2 then
if onelevel_numeric_list else
data "<ol>"
onelevel_numeric_list=true
end if
else
if onelevel_list then
data "</ul>"
onelevel_list=false
end if
if onelevel_numeric_list then
data "</ol>"
onelevel_numeric_list=false
end if
end if
End Sub
Function escapeHTML(text As String)
    Local String result = text
    result = replace$( "&", "&", result)
    result = replace$( "<", "<", result)
    result = replace$( ">", ">", result)
    result = replace$( """", """, result)
    result = replace$( "'", "'", result)
    = result
End Function
}
CheckIt


Δεν υπάρχουν σχόλια:

Δημοσίευση σχολίου

You can feel free to write any suggestion, or idea on the subject.