3.0: new commandlineparser, added optional stdout webgen.py

This commit is contained in:
Wolfgang Nowak 2021-10-28 18:54:10 +02:00
parent 7ae948c0e6
commit 058ae8ae38

View file

@ -19,39 +19,44 @@ def edit(headline):
# there are 2 parameters. if --html is given, the fileending will be .html. # there are 2 parameters. if --html is given, the fileending will be .html.
# Else there will be no fileending, but it will still contain html # Else there will be no fileending, but it will still contain html
# there is no parameter: optwords = ["--html", "--stdout"]
if len(sys.argv) == 1: outfile = ""
headline = input("Bitte gib eine Headline ein: ") args = sys.argv
# there is 1 parameter myname = args.pop(0)
elif len(sys.argv) == 2: for wish in optwords:
if sys.argv[1] == "--html": if args.count(wish):
fileending = ".html" if wish == "--html":
headline = input("Bitte gib eine Headline ein: ") fileending = ".html"
else: args.pop(args.index(wish))
headline = str(sys.argv[1]) if wish == "--stdout":
# there are 2 parameters. if --html is given, the fileending will be .html. E outfile = sys.stdout
elif len(sys.argv) == 3: args.pop(args.index(wish))
if sys.argv[1] == "--html": if len(args) == 1:
fileending = ".html" headline = args[0]
headline = str(sys.argv[2]) elif len(args) == 0:
elif sys.argv[2] == "--html": headline = input("Please enter a headline: ")
fileending = ".html" elif len(args) > 1:
headline = str(sys.argv[1]) sys.exit("Too much or wrong parameters:\n" + myname + ' [--html] [--stdout] ["Some Topic"] ')
else:
sys.exit(str(sys.argv[0]) + ' [--html] "some Headline"\n')
headlinemd = headline + ".md" headlinemd = headline + ".md"
headlinehtml = headline + fileending headlinehtml = headline + fileending
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")
new_file_name = targetdir + creationtime + " " + headline + fileending
if outfile != sys.stdout:
new_file_name = targetdir + creationtime + " " + headline + fileending
else:
new_file_name="STDOUT"
print("You will edit now: ", headlinemd, "\n", "Outputfile= ", new_file_name) print("You will edit now: ", headlinemd, "\n", "Outputfile= ", new_file_name)
a = input("Press <RETURN> to continue or <CTRL-C> to stop: ") a = input("Press <RETURN> to continue or <CTRL-C> to stop: ")
edit(headlinemd) edit(headlinemd)
html_out_file = open(new_file_name, "w") if outfile != sys.stdout:
html_out_file = open(new_file_name, "w")
else:
html_out_file = outfile
head1 = ( head1 = (
@ -61,7 +66,7 @@ head1 = (
+ '</title>\n\ + '</title>\n\
<meta name="syntax" content="markdown">\n\ <meta name="syntax" content="markdown">\n\
<meta name="generator" content="' <meta name="generator" content="'
+ sys.argv[0] + myname
+ ' markdown2htmlconverter">\n\ + ' markdown2htmlconverter">\n\
<meta name="settings" content="use_css,pre_wrap,no_foldcolumn,expand_tabs,prevent_copy=">\n\ <meta name="settings" content="use_css,pre_wrap,no_foldcolumn,expand_tabs,prevent_copy=">\n\
<meta name="ctime" content="' <meta name="ctime" content="'
@ -89,7 +94,7 @@ pre { white-space: pre-wrap; font-family: monospace; color: #00f020; font-backgr
style_fn = '<link rel="stylesheet" type="text/css" href="/vimstyles.css">' style_fn = '<link rel="stylesheet" type="text/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>\n"
html_out_file.write(head1 + style_fn + head2 + body1) html_out_file.write(head1 + style_fn + head2 + body1)
@ -103,5 +108,7 @@ with open(headlinemd, "r", encoding="utf-8") as infile:
html_out_file.write(foot) html_out_file.write(foot)
html_out_file.close() html_out_file.close()
# return new filename to stdout, so some calling shellscript can use it. # if it exists, return new filename to stdout, so some calling shellscript can use it.
print(new_file_name) if outfile!=sys.stdout:
print(new_file_name)