Last active
February 4, 2019 22:20
-
-
Save 9999years/1424a2f604a281cfcfa68f127c0fa432 to your computer and use it in GitHub Desktop.
Girl’s Markdown
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
import sys | |
# `pip install Markdown` | |
from markdown import markdown | |
FLOWER = '🌸' | |
START_PINK = '<span style="color: pink">' | |
END_PINK = '</span>' | |
def girlmarkdown(text): | |
md = markdown(text) | |
# we actually grab all the points with flowers in them and then reassemble | |
# the output string to avoid quadratic string-concatenation times | |
splice_points = [] | |
for i, c in enumerate(md): | |
if c == FLOWER: | |
splice_points.append(i) | |
if len(splice_points) % 2 == 1: | |
raise ValueError('Unmatched flowers') | |
# replacing flowers with START_PINK and END_PINK | |
parts = [] | |
before = 0 | |
end = -1 | |
itr = iter(splice_points) | |
for start in itr: | |
end = next(itr) | |
parts.append(md[before:start]) | |
parts.append(START_PINK) | |
parts.append(md[start + 1:end]) | |
parts.append(END_PINK) | |
# make sure to not leave out the chunk of text after the last flower | |
parts.append(md[end + 1:]) | |
return ''.join(parts) | |
def main(): | |
for filename in sys.argv[1:]: | |
with open(filename, encoding='utf-8') as f: | |
print(girlmarkdown(f.read())) | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment