Created
December 12, 2022 17:33
-
-
Save MBlore/b2c1b414e7140b61fec0c1263eecf009 to your computer and use it in GitHub Desktop.
Text Parsing For Conversion to HTML
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html lang="en"> | |
<head> | |
<meta charset="UTF-8"> | |
<meta http-equiv="X-UA-Compatible" content="IE=edge"> | |
<meta name="viewport" content="width=device-width, initial-scale=1.0"> | |
<title>Post Converter</title> | |
</head> | |
<body> | |
<textarea id="src" cols="100" rows="50">[t] | |
This Is a Title | |
[h] | |
This Is a Sub-Heading | |
This is a paragraph of text. | |
This is also a paragraph of text. Here's some code: | |
[c] | |
import sys, pygame | |
print("Hello world") | |
[/c] | |
Here's a list: | |
[li] | |
Item 1 | |
Item 2 | |
Item 3 | |
</textarea> | |
<button id="btn">Convert</button> | |
<textarea id="dest" cols="100" rows="50"></textarea> | |
<script> | |
src = document.getElementById("src") | |
dest = document.getElementById("dest") | |
btn = document.getElementById("btn") | |
btn.addEventListener("click", function() { | |
text = src.value | |
const lines = text.split(/\r?\n/) | |
let out = "" | |
// State for the parsing. | |
grabTitle = false | |
grabHeading = false | |
parsingPara = false | |
parsingCode = false | |
parsingList = false | |
// Walk down each line spitting out the new formatted content. | |
lines.forEach(line => { | |
if (grabTitle) { | |
// This line is the title text. | |
out += "<h1>" + line + "</h1>\r\n" | |
grabTitle = false | |
return | |
} | |
if (line == "[t]") { | |
// The next line is the page title (h1). | |
grabTitle = true | |
return | |
} | |
if (line == "[c]") { | |
// The next lines are code. So wrap in code block. | |
parsingCode = true | |
out += "<pre class=\"line-numbers language-python\"><code>" | |
return | |
} | |
if (line == "[/c]") { | |
// End the code block. | |
parsingCode = false | |
out += "</code></pre>\r\n" | |
return | |
} | |
if (line == "[h]") { | |
// The next line is the sub-heading (h2). | |
grabHeading = true | |
return | |
} | |
if (grabHeading) { | |
out += "<h2>" + line + "</h2>\r\n" | |
grabHeading = false | |
return | |
} | |
if (line == "[li]") { | |
// Next lines are list items, lets start the list. | |
out += "<ul>\r\n" | |
parsingList = true | |
return | |
} | |
if (parsingCode) { | |
// Parsing code, we just write whats being read, including new lines. | |
out += line + "\r\n" | |
return | |
} | |
// Empty line. | |
if (line.trim().length === 0) { | |
if (parsingPara) { | |
// If we were parsing a paragraph of text, close the tag. | |
parsingPara = false | |
out += "</p>\r\n" | |
return | |
} | |
if (parsingList) { | |
// Terminate lists on new empty lines. | |
parsingList = false | |
out += "<ul>\r\n" | |
return | |
} | |
// Not in any mode and we're on a blank line, just skip it. | |
return | |
} | |
if (parsingList) { | |
out += " <li>" + line + "</li>\r\n" | |
return | |
} | |
// Found text, time to wrap in a <p> tag. | |
if (!parsingPara) { | |
parsingPara = true | |
out += "<p>\r\n" | |
out += " " + line + "\r\n" | |
return | |
} else { | |
out += " " + line + "\r\n" | |
} | |
}) | |
dest.value = out | |
}) | |
</script> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment