2.0: webgen.py +commandlineopts +edit

This commit is contained in:
Wolfgang Nowak 2021-10-27 16:09:07 +02:00
parent e4bdcb5991
commit 19466e82f4

View file

@ -1,31 +1,50 @@
#!/usr/bin/env python3 #!/usr/bin/env python3
import sys, os, re, markdown, datetime import sys, os, uuid, shutil, subprocess, markdown, re, datetime
# Getoptions sourcedir = os.path.expanduser("~/")
try: sourcefile = sourcedir + "up.php"
parameters = sys.argv[1] targetdir = os.path.expanduser("~/www/i21k.de/")
except:
sys.exit(
sys.argv[0]
+ ": Bitte eine Text oder Markdowndatei angeben.\ Dieses Tool wandelt eine entsprechende Datei in Html - ohne Endung, mit Datum. mit --html wird .html als Endung angehängt"
)
if parameters == "--html":
fileending = ".html"
try:
parameters = sys.argv[2]
except:
sys.exit(
sys.argv[0]
+ ": Bitte eine Text oder Markdowndatei angeben.\ Dieses Tool wandelt eine entsprechende Datei in Html - ohne Endung, mit Datum. mit --html wird .html als Endung angehängt"
)
else:
fileending = "" fileending = ""
mark_down_file = parameters
def edit(headline):
editor = os.getenv("EDITOR")
if not editor:
editor = "vim"
subprocess.call([editor, headline])
return headline
# 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 ") creationtime = datetime.datetime.now().strftime("%Y-%m-%d %H:00 ")
creationtimeheader = datetime.datetime.now().strftime("%Y-%m-%d %H:%M") creationtimeheader = datetime.datetime.now().strftime("%Y-%m-%d %H:%M")
@ -33,15 +52,15 @@ creationtimeheader = datetime.datetime.now().strftime("%Y-%m-%d %H:%M")
if re.match(".*\.md$", mark_down_file): if re.match(".*\.md$", mark_down_file):
new_file_name = creationtime + " " + re.sub(".md$", fileending, mark_down_file) new_file_name = creationtime + " " + re.sub(".md$", fileending, mark_down_file)
html_out_file = open(new_file_name, "w") html_out_file = open(new_file_name, "w")
title_of_text = re.sub(".md$", "", mark_down_file) title_of_tfileending = re.sub(".md$", "", mark_down_file)
else: else:
html_out_file = sys.stdout html_out_file = sys.stdout
title_of_text = mark_down_file title_of_tfileending = mark_down_file
head1 = ( head1 = (
'<!DOCTYPE HTML><html><head>\n\ '<!DOCTYPE HTML><html><head>\n\
<meta http-equiv="content-type" content="text/html; charset=UTF-8">\n<title>' <meta http-equiv="content-type" content="tfileending/html; charset=UTF-8">\n<title>'
+ title_of_text + title_of_tfileending
+ '</title>\n\ + '</title>\n\
<meta name="syntax" content="markdown">\n\ <meta name="syntax" content="markdown">\n\
<meta name="generator" content="' <meta name="generator" content="'
@ -68,8 +87,7 @@ pre { white-space: pre-wrap; font-family: monospace; color: #00f020; font-backgr
.Statement { color: #00f020; font-weight: bold; }\n\ .Statement { color: #00f020; font-weight: bold; }\n\
.Headandfoot { color: #00f020; font-weight: bold; } " .Headandfoot { color: #00f020; font-weight: bold; } "
style_fn = '<link rel="stylesheet" type="text/css" href="/vimstyles.css">' style_fn = '<link rel="stylesheet" type="tfileending/css" href="/vimstyles.css">'
head2 = "</head><body>\n" head2 = "</head><body>\n"
body1 = '<a href="./">Back</a>' body1 = '<a href="./">Back</a>'
foot = "</body> </html>" foot = "</body> </html>"