diff --git a/webgen.py b/webgen.py index fcca2f4..0de0ba2 100755 --- a/webgen.py +++ b/webgen.py @@ -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": - 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') +optwords = ["--html", "--stdout"] +outfile = "" +args = sys.argv +myname = args.pop(0) +for wish in optwords: + if args.count(wish): + if wish == "--html": + fileending = ".html" + 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 to continue or 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 = ( + '\n\ \n\ \n\ \n\ ' head2 = "\n" body1 = 'Back' -foot = " " +foot = " \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) +