webgen v3.1 -Added some comments

This commit is contained in:
Wolfgang Nowak 2021-10-29 01:26:41 +02:00
parent 52a62d7dcf
commit 446c70bcb5

View file

@ -2,11 +2,12 @@
import sys, os, uuid, shutil, subprocess, markdown, re, datetime import sys, os, uuid, shutil, subprocess, markdown, re, datetime
# If not choosen stdout as output, where should the generated file go to and what
# fileending (f.e. .html) shall it have
targetdir = os.path.expanduser("~/www/i21k.de/posts/") targetdir = os.path.expanduser("~/www/i21k.de/posts/")
fileending = "" fileending = ""
# call favorite editor with filename to write the text
def edit(headline): def edit(headline):
editor = os.getenv("EDITOR") editor = os.getenv("EDITOR")
if not editor: if not editor:
@ -15,9 +16,11 @@ def edit(headline):
return headline return headline
# Parse arguments "--html" and "Some Headline" # Parse arguments "--html", "--stdout" and "Some Headline"
# there are 2 parameters. if --html is given, the fileending will be .html. # there are 3 parameters. if --html is given, the fileending will be .html.
# Else there will be no fileending, but it will still contain html # Else, if stdout is choosen there can be no fileending, but it will still contain html
# the name of the program is stored in myname
# dont set a name to outfile, it just is initialised here and will be overwritten
optwords = ["--html", "--stdout"] optwords = ["--html", "--stdout"]
outfile = "" outfile = ""
@ -39,26 +42,31 @@ elif len(args) > 1:
sys.exit("Too much or wrong parameters:\n" + myname + ' [--html] [--stdout] ["Some Topic"] ') sys.exit("Too much or wrong parameters:\n" + myname + ' [--html] [--stdout] ["Some Topic"] ')
# The headline should be the theme of the text. We edit a .md-file
headlinemd = headline + ".md" headlinemd = headline + ".md"
headlinehtml = headline + fileending # Prepend an hourly tinestamp to the later name of the file
creationtime = datetime.datetime.now().strftime("%Y-%m-%d %H:00 ") creationtime = datetime.datetime.now().strftime("%Y-%m-%d %H:00 ")
# generate a more precise ctime to be included in the resulting webpage
creationtimeheader = datetime.datetime.now().strftime("%Y-%m-%d %H:%M") creationtimeheader = datetime.datetime.now().strftime("%Y-%m-%d %H:%M")
# This is informal asking yes/no for editing the text
if outfile != sys.stdout: if outfile != sys.stdout:
new_file_name = targetdir + creationtime + " " + headline + fileending new_file_name = targetdir + creationtime + " " + headline + fileending
else: else:
new_file_name = "STDOUT" 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)
# Set ouput to stdout, if requested. Caution, the prompts of this script go there, too,
# be careful with copy & paste
if outfile != sys.stdout: if outfile != sys.stdout:
html_out_file = open(new_file_name, "w") html_out_file = open(new_file_name, "w")
else: else:
html_out_file = outfile html_out_file = outfile
# The head of the output. Strip path from myname
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>'
@ -66,7 +74,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="'
+ myname + myname.rpartition("/")[2]
+ ' 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="'
@ -93,14 +101,16 @@ 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"
# beginning and end of the text, edit or empty as you like, but dont delete
beginningoftext = "**moin**\n\n"
endoftext = "---\n"
body1 = '<a href="./">Back</a>' body1 = '<a href="./">Back</a>'
foot = "</body> </html>\n" foot = "</body> </html>\n"
# write beginning of html-file
html_out_file.write(head1 + style_fn + head2 + beginningoftext + endoftext + body1)
html_out_file.write(head1 + style_fn + head2 + body1) # The centerpiece - read md-file, convert to html, add head and foot and write result
# Centerpiece - read md-file, convert to html, add head and foot and write result
with open(headlinemd, "r", encoding="utf-8") as infile: 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)
@ -108,6 +118,8 @@ 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()
# if it exists, return new filename to stdout, so some calling shellscript can use it. # if the new filename is not stdout, return new filename to stdout, so some calling shellscript can use it.
if outfile != sys.stdout: if outfile != sys.stdout:
print(new_file_name) print(new_file_name)
# Have a nice time.