Skip to content

Instantly share code, notes, and snippets.

@hastebrot
Created April 30, 2021 14:01
Show Gist options
  • Save hastebrot/3202dee555424c4a95d27ca8955d291c to your computer and use it in GitHub Desktop.
Save hastebrot/3202dee555424c4a95d27ca8955d291c to your computer and use it in GitHub Desktop.
text = """Text attributes _italic_, **bold**, `monospace`. Some implementations may use *single-asterisks* for italic text."""
expected = """<p>Text attributes <em>italic</em>, <strong>bold</strong>, <code>monospace</code>. Some implementations may use <i>single-asterisks</i> for italic text.</p>"""
def lookup(text, start, size):
return text[start:start+size]
token_italic = "_"
token_italic_alt = "*"
token_strong = "**"
token_monospace = "`"
def tokenize(text):
index = 0
result = []
while index < len(text):
if lookup(text, index, 2) == token_strong:
result.append(token_strong)
index += 2
elif lookup(text, index, 1) == token_italic:
result.append(token_italic)
index += 1
elif lookup(text, index, 1) == token_italic_alt:
result.append(token_italic_alt)
index += 1
elif lookup(text, index, 1) == token_monospace:
result.append(token_monospace)
index += 1
else:
result.append(lookup(text, index, 1))
index += 1
return result
tokens = tokenize(text)
flags = {
"italic": False,
"italic_alt": False,
"strong": False,
"monospace": False,
}
paragraph_start = "<p>"
paragraph_end = "</p>"
italic_start = "<em>"
italic_end = "</em>"
italic_alt_start = "<i>"
italic_alt_end = "</i>"
strong_start = "<strong>"
strong_end = "</strong>"
monospace_start = "<code>"
monospace_end = "</code>"
result = ""
for token in tokens:
if token == token_italic:
flags["italic"] = not flags["italic"]
result += italic_start if flags["italic"] else italic_end
elif token == token_italic_alt:
flags["italic_alt"] = not flags["italic_alt"]
result += italic_alt_start if flags["italic_alt"] else italic_alt_end
elif token == token_strong:
flags["strong"] = not flags["strong"]
result += strong_start if flags["strong"] else strong_end
elif token == token_monospace:
flags["monospace"] = not flags["monospace"]
result += monospace_start if flags["monospace"] else monospace_end
else:
result += token
actual = paragraph_start + result + paragraph_end
print(tokens)
print(actual)
print(expected)
assert actual == expected
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment