webgen/webgen.py

106 lines
3.3 KiB
Python
Raw Normal View History

#!/usr/bin/env python3
2021-10-27 16:09:07 +02:00
import sys, os, uuid, shutil, subprocess, markdown, re, datetime
sourcedir = os.path.expanduser("~/")
sourcefile = sourcedir + "up.php"
targetdir = os.path.expanduser("~/www/i21k.de/")
fileending = ""
def edit(headline):
editor = os.getenv("EDITOR")
if not editor:
editor = "vim"
subprocess.call([editor, headline])
return headline
2021-10-27 16:09:07 +02:00
# Parse arguments "--html" and "Some Headline"
if len(sys.argv) == 1:
headline = input("Bitte gib eine Headline ein: ")
elif len(sys.argv) == 2:
if sys.argv[1] == "--html":
fileending = ".html"
headline = input("Bitte gib eine Headline ein: ")
else:
headline = str(sys.argv[1])
elif len(sys.argv) == 3:
if sys.argv[1] == "--html":
fileending = ".html"
headline = str(sys.argv[2])
elif sys.argv[2] == "--html":
fileending = ".html"
headline = str(sys.argv[1])
else:
sys.exit(str(sys.argv[0]) + ' [--html] "some Headline"\nIf --html is choosen, output will be written to STDOUT.')
headlinemd = headline + ".md"
headlinehtml = headline + fileending
print("Headlinefile= ", headlinemd, "\n", "Outputfile= ", headlinehtml)
a = input("Press <RETURN> to continue: ")
edit(headlinemd)
mark_down_file = headlinemd
creationtime = datetime.datetime.now().strftime("%Y-%m-%d %H:00 ")
creationtimeheader = datetime.datetime.now().strftime("%Y-%m-%d %H:%M")
if re.match(".*\.md$", mark_down_file):
new_file_name = creationtime + " " + re.sub(".md$", fileending, mark_down_file)
html_out_file = open(new_file_name, "w")
2021-10-27 16:09:07 +02:00
title_of_tfileending = re.sub(".md$", "", mark_down_file)
else:
html_out_file = sys.stdout
2021-10-27 16:09:07 +02:00
title_of_tfileending = mark_down_file
head1 = (
'<!DOCTYPE HTML><html><head>\n\
2021-10-27 16:09:07 +02:00
<meta http-equiv="content-type" content="tfileending/html; charset=UTF-8">\n<title>'
+ title_of_tfileending
+ '</title>\n\
<meta name="syntax" content="markdown">\n\
2021-10-25 08:11:36 +02:00
<meta name="generator" content="'
+ sys.argv[0]
+ ' markdown2htmlconverter">\n\
<meta name="settings" content="use_css,pre_wrap,no_foldcolumn,expand_tabs,prevent_copy=">\n\
<meta name="ctime" content="'
+ creationtimeheader
+ '"> '
)
styles = " <!--\n\
* { font-family: monospace; color: #00f020; background-color: #1c1414; font-size: 1.15em; }\n\
a { font-size: 1.0em;}\n\
ul { font-size: 1.15em;}\n\
p { font-size: 1.15em;}\n\
li { font-size: 1.15em;}\n\
2021-04-24 14:23:19 +02:00
ol { font-size: 1.0em;}\n\
em { font-size: 1.0em;}\n\
table { background-color: #1c1414; }\n\
blockquote { font-size: 1.15em; }\n\
body { font-family: monospace; color: #00f020; background-color: #1c1414; }\n\
pre { white-space: pre-wrap; font-family: monospace; color: #00f020; font-background-color: #1c1414; }\n\
.Statement { color: #00f020; font-weight: bold; }\n\
.Headandfoot { color: #00f020; font-weight: bold; } "
2021-10-27 16:09:07 +02:00
style_fn = '<link rel="stylesheet" type="tfileending/css" href="/vimstyles.css">'
head2 = "</head><body>\n"
body1 = '<a href="./">Back</a>'
foot = "</body> </html>"
html_out_file.write(head1 + style_fn + head2 + body1)
with open(mark_down_file, "r", encoding="utf-8") as infile:
md_data = infile.read()
html_output = markdown.markdown(md_data)
html_out_file.write(html_output)
html_out_file.write(foot)
# return new filename to stdout, so some calling shellscript can use it.
2021-04-18 20:43:19 +02:00
print(new_file_name)