2.0: added Targetdir, cleaned up code: webgen.py

This commit is contained in:
Wolfgang Nowak 2021-10-27 19:21:02 +02:00
parent 19466e82f4
commit 7ae948c0e6

View file

@ -3,9 +3,7 @@
import sys, os, uuid, shutil, subprocess, markdown, re, datetime import sys, os, uuid, shutil, subprocess, markdown, re, datetime
sourcedir = os.path.expanduser("~/") targetdir = os.path.expanduser("~/www/i21k.de/posts/")
sourcefile = sourcedir + "up.php"
targetdir = os.path.expanduser("~/www/i21k.de/")
fileending = "" fileending = ""
@ -18,15 +16,20 @@ def edit(headline):
# Parse arguments "--html" and "Some Headline" # Parse arguments "--html" and "Some 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: if len(sys.argv) == 1:
headline = input("Bitte gib eine Headline ein: ") headline = input("Bitte gib eine Headline ein: ")
# there is 1 parameter
elif len(sys.argv) == 2: elif len(sys.argv) == 2:
if sys.argv[1] == "--html": if sys.argv[1] == "--html":
fileending = ".html" fileending = ".html"
headline = input("Bitte gib eine Headline ein: ") headline = input("Bitte gib eine Headline ein: ")
else: else:
headline = str(sys.argv[1]) headline = str(sys.argv[1])
# there are 2 parameters. if --html is given, the fileending will be .html. E
elif len(sys.argv) == 3: elif len(sys.argv) == 3:
if sys.argv[1] == "--html": if sys.argv[1] == "--html":
fileending = ".html" fileending = ".html"
@ -35,32 +38,26 @@ elif len(sys.argv) == 3:
fileending = ".html" fileending = ".html"
headline = str(sys.argv[1]) headline = str(sys.argv[1])
else: else:
sys.exit(str(sys.argv[0]) + ' [--html] "some Headline"\nIf --html is choosen, output will be written to STDOUT.') sys.exit(str(sys.argv[0]) + ' [--html] "some Headline"\n')
headlinemd = headline + ".md" headlinemd = headline + ".md"
headlinehtml = headline + fileending 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")
new_file_name = targetdir + creationtime + " " + headline + fileending
print("You will edit now: ", headlinemd, "\n", "Outputfile= ", new_file_name)
a = input("Press <RETURN> to continue or <CTRL-C> to stop: ")
edit(headlinemd)
if re.match(".*\.md$", 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_tfileending = re.sub(".md$", "", mark_down_file)
else:
html_out_file = sys.stdout
title_of_tfileending = mark_down_file
head1 = ( head1 = (
'<!DOCTYPE HTML><html><head>\n\ '<!DOCTYPE HTML><html><head>\n\
<meta http-equiv="content-type" content="tfileending/html; charset=UTF-8">\n<title>' <meta http-equiv="content-type" content="tfileending/html; charset=UTF-8">\n<title>'
+ title_of_tfileending + headline
+ '</title>\n\ + '</title>\n\
<meta name="syntax" content="markdown">\n\ <meta name="syntax" content="markdown">\n\
<meta name="generator" content="' <meta name="generator" content="'
@ -72,6 +69,8 @@ head1 = (
+ '"> ' + '"> '
) )
# Some fancy css for minimalistic terminal style
styles = " <!--\n\ styles = " <!--\n\
* { font-family: monospace; color: #00f020; background-color: #1c1414; font-size: 1.15em; }\n\ * { font-family: monospace; color: #00f020; background-color: #1c1414; font-size: 1.15em; }\n\
a { font-size: 1.0em;}\n\ a { font-size: 1.0em;}\n\
@ -87,7 +86,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="tfileending/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>"
@ -95,11 +94,14 @@ foot = "</body> </html>"
html_out_file.write(head1 + style_fn + head2 + body1) html_out_file.write(head1 + style_fn + head2 + body1)
with open(mark_down_file, "r", encoding="utf-8") as infile: # Centerpiece - read md-file, convert to html, add head and foot and write result
with open(headlinemd, "r", encoding="utf-8") as infile:
md_data = infile.read() md_data = infile.read()
html_output = markdown.markdown(md_data) html_output = markdown.markdown(md_data)
html_out_file.write(html_output) html_out_file.write(html_output)
html_out_file.write(foot) html_out_file.write(foot)
html_out_file.close()
# return new filename to stdout, so some calling shellscript can use it. # return new filename to stdout, so some calling shellscript can use it.
print(new_file_name) print(new_file_name)