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.
# Else there will be no fileending, but it will still contain html
# there is no parameter:
if len(sys.argv) == 1:
headline = input("Bitte gib eine Headline ein: ")
# there is 1 parameter
elif len(sys.argv) == 2:
if sys.argv[1] == "--html":
optwords = ["--html", "--stdout"]
outfile = ""
args = sys.argv
myname = args.pop(0)
for wish in optwords:
if args.count(wish):
if wish == "--html":
fileending = ".html"
headline = input("Bitte gib eine Headline ein: ")
else:
headline = str(sys.argv[1])
# there are 2 parameters. if --html is given, the fileending will be .html. E
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"\n')
args.pop(args.index(wish))
if wish == "--stdout":
outfile = sys.stdout
args.pop(args.index(wish))
if len(args) == 1:
headline = args[0]
elif len(args) == 0:
headline = input("Please enter a headline: ")
elif len(args) > 1:
sys.exit("Too much or wrong parameters:\n" + myname + ' [--html] [--stdout] ["Some Topic"] ')
headlinemd = headline + ".md"
headlinehtml = headline + fileending
creationtime = datetime.datetime.now().strftime("%Y-%m-%d %H:00 ")
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)
a = input("Press <RETURN> to continue or <CTRL-C> to stop: ")
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 = (
@ -61,7 +66,7 @@ head1 = (
+ '</title>\n\
<meta name="syntax" content="markdown">\n\
<meta name="generator" content="'
+ sys.argv[0]
+ myname
+ ' markdown2htmlconverter">\n\
<meta name="settings" content="use_css,pre_wrap,no_foldcolumn,expand_tabs,prevent_copy=">\n\
<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">'
head2 = "</head><body>\n"
body1 = '<a href="./">Back</a>'
foot = "</body> </html>"
foot = "</body> </html>\n"
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.close()
# return new filename to stdout, so some calling shellscript can use it.
print(new_file_name)
# if it exists, return new filename to stdout, so some calling shellscript can use it.
if outfile!=sys.stdout:
print(new_file_name)