Skip to content

Instantly share code, notes, and snippets.

@NSG650
Created February 24, 2023 14:44
Show Gist options
  • Save NSG650/3a0647078d53241c1655a3de3329e331 to your computer and use it in GitHub Desktop.
Save NSG650/3a0647078d53241c1655a3de3329e331 to your computer and use it in GitHub Desktop.
Really ugly python script to convert nd [a md like format of mine] to an html blog post for my website
import sys
from datetime import date
def parse_line(line):
if line[0] == "#":
if line[1] == "#":
return f"</div><div><h2>{line[2:]}</h2>"
return f"</div><div><h1>{line[2:]}</h1>"
if line[0] == "!":
text_to_use = line[line.index('[') + 1:line.index(']')]
link_to_use = line[line.index('(') + 1:line.index(')')]
return f"<p style=\"font-size: 70%\"><img src=\"{link_to_use}\" alt=\"{text_to_use}\"><br>{text_to_use}</p>"
if line[0] == "$":
text_to_use = line[line.index('[') + 1:line.index(']')]
link_to_use = line[line.index('(') + 1:line.index(')')]
return f"<a href=\"{link_to_use}\">{text_to_use}</a>"
return f"<p>{line}</p>"
def file_to_html(file):
html = ""
for line in file:
html += parse_line(line)
return html
def main():
if len(sys.argv) < 2:
print("[!] Pass in a file to convert!")
sys.exit(-1)
prologue = open("base/prologue.html", "r")
epilogue = open("base/epilogue.html", "r")
file_name = sys.argv[1]
today = date.today()
today_but_string = today.strftime("%d-%m-%Y")
f = open(file_name, "r")
html = file_to_html(f)
f.close()
output = open(f"{today_but_string}.html", "w")
output.write(prologue.read())
output.write(f"<h1>{file_name}</h1>")
output.write(f"<h4>Posted on {today_but_string}</h4>")
output.write(html)
output.write(epilogue.read())
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment