Text to HTML: Difference between revisions
Content added Content deleted
m (→{{header|Pike}}: capitalize Pike) |
(→Tcl: Added implementation) |
||
Line 1: | Line 1: | ||
{{Draft task|Text processing}} |
{{Draft task|Text processing}} |
||
When developing a Website it is occasionally necessary to handle text that is received without formatting, and present it in a pleasing manner. to achieve this the text needs to be converted to HTML. |
When developing a Website it is occasionally necessary to handle text that is received without formatting, and present it in a pleasing manner. to achieve this the text needs to be converted to HTML. |
||
Line 151: | Line 150: | ||
return root; |
return root; |
||
}</lang> |
}</lang> |
||
=={{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. |
|||
<lang tcl>package require Tcl 8.5 |
|||
proc splitParagraphs {text} { |
|||
split [regsub -all {\n\s*(\n\s*)+} [string trim $text] \u0000] "\u0000" |
|||
} |
|||
proc determineParagraph {para} { |
|||
set para [regsub -all {\s*\n\s*} $para " "] |
|||
switch -regexp -- $para { |
|||
{^\s*\*+\s} { |
|||
return [list ul [string trimleft $para " \t*"]] |
|||
} |
|||
{^\s*\d+\.\s} { |
|||
set para [string trimleft $para " \t\n0123456789"] |
|||
set para [string range $para 1 end] |
|||
return [list ol [string trimleft $para " \t"]] |
|||
} |
|||
{^#+\s} { |
|||
return [list heading [string trimleft $para " \t#"]] |
|||
} |
|||
} |
|||
return [list normal $para] |
|||
} |
|||
proc markupParagraphContent {para} { |
|||
set para [string map {& & < < > >} $para] |
|||
regsub -all {_([\w&;]+)_} $para {<i>\1</i>} para |
|||
regsub -all {\*([\w&;]+)\*} $para {<b>\1</b>} para |
|||
regsub -all {`([\w&;]+)`} $para {<tt>\1</tt>} para |
|||
return $para |
|||
} |
|||
proc markupText {title text} { |
|||
set title [string map {& & < < > >} $title] |
|||
set result "<html>" |
|||
append result "<head><title>" $title "</title>\n</head>" |
|||
append result "<body>" "<h1>$title</h1>\n" |
|||
set state normal |
|||
foreach para [splitParagraphs $text] { |
|||
lassign [determineParagraph $para] type para |
|||
set para [markupParagraphContent $para] |
|||
switch $state,$type { |
|||
normal,normal {append result "<p>" $para "</p>\n"} |
|||
normal,heading { |
|||
append result "<h2>" $para "</h2>\n" |
|||
set type normal |
|||
} |
|||
normal,ol {append result "<ol>" "<li>" $para "</li>\n"} |
|||
normal,ul {append result "<ul>" "<li>" $para "</li>\n"} |
|||
ul,normal {append result "</ul>" "<p>" $para "</p>\n"} |
|||
ul,heading { |
|||
append result "</ul>" "<h2>" $para "</h2>\n" |
|||
set type normal |
|||
} |
|||
ul,ol {append result "</ul>" "<ol>" "<li>" $para "</li>\n"} |
|||
ul,ul {append result "<li>" $para "</li>\n"} |
|||
ol,normal {append result "</ol>" "<p>" $para "</p>\n"} |
|||
ol,heading { |
|||
append result "</ol>" "<h2>" $para "</h2>\n" |
|||
set type normal |
|||
} |
|||
ol,ol {append result "<li>" $para "</li>\n"} |
|||
ol,ul {append result "</ol>" "<ul>" "<li>" $para "</li>\n"} |
|||
} |
|||
set state $type |
|||
} |
|||
if {$state ne "normal"} { |
|||
append result "</$state>" |
|||
} |
|||
return [append result "</body></html>"] |
|||
}</lang> |
|||
Here's an example of how it would be used. |
|||
<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. |
|||
# Block paragraph types |
|||
* This is a bulleted list |
|||
* And this is the second item in it |
|||
1. Here's a numbered list |
|||
2. Second item |
|||
3. Third item |
|||
# Inline formatting types |
|||
The formatter can render text with _italics_, *bold* and in a `typewriter` |
|||
font. It also does the right thing with <angle brackets> and &ersands, |
|||
but relies on the encoding of the characters to be conveyed separately." |
|||
puts [markupText "Sample" $sample]</lang> |
|||
{{out}} |
|||
<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> |
|||
<h2>Block paragraph types</h2> |
|||
<ul><li>This is a bulleted list</li> |
|||
<li>And this is the second item in it</li> |
|||
</ul><ol><li>Here's a numbered list</li> |
|||
<li>Second item</li> |
|||
<li>Third item</li> |
|||
</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 <angle brackets> and &amp;ersands, but relies on the encoding of the characters to be conveyed separately.</p> |
|||
</body></html></lang> |