3.0: new commandlineparser, added optional stdout webgen.py
This commit is contained in:
parent
7ae948c0e6
commit
058ae8ae38
1 changed files with 33 additions and 26 deletions
51
webgen.py
51
webgen.py
|
@ -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):
|
||||||
|
if wish == "--html":
|
||||||
fileending = ".html"
|
fileending = ".html"
|
||||||
headline = input("Bitte gib eine Headline ein: ")
|
args.pop(args.index(wish))
|
||||||
else:
|
if wish == "--stdout":
|
||||||
headline = str(sys.argv[1])
|
outfile = sys.stdout
|
||||||
# there are 2 parameters. if --html is given, the fileending will be .html. E
|
args.pop(args.index(wish))
|
||||||
elif len(sys.argv) == 3:
|
if len(args) == 1:
|
||||||
if sys.argv[1] == "--html":
|
headline = args[0]
|
||||||
fileending = ".html"
|
elif len(args) == 0:
|
||||||
headline = str(sys.argv[2])
|
headline = input("Please enter a headline: ")
|
||||||
elif sys.argv[2] == "--html":
|
elif len(args) > 1:
|
||||||
fileending = ".html"
|
sys.exit("Too much or wrong parameters:\n" + myname + ' [--html] [--stdout] ["Some Topic"] ')
|
||||||
headline = str(sys.argv[1])
|
|
||||||
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")
|
||||||
|
|
||||||
|
if outfile != sys.stdout:
|
||||||
new_file_name = targetdir + creationtime + " " + headline + fileending
|
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)
|
||||||
|
|
||||||
|
if outfile != sys.stdout:
|
||||||
html_out_file = open(new_file_name, "w")
|
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.
|
||||||
|
if outfile!=sys.stdout:
|
||||||
print(new_file_name)
|
print(new_file_name)
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue