webgen v4.0.0 +some menues
This commit is contained in:
parent
ad1aa33758
commit
cf64321f6d
1 changed files with 83 additions and 37 deletions
120
webgen.py
120
webgen.py
|
@ -7,7 +7,22 @@ from dialog import Dialog
|
|||
# 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/")
|
||||
sourcedir = os.path.expanduser("~/python/webgen/")
|
||||
fileending = ""
|
||||
locale.setlocale(locale.LC_ALL, "")
|
||||
clear = "\x1b[2J\x1b[H"
|
||||
xwidth = os.get_terminal_size()[0] - 3
|
||||
ywidth = os.get_terminal_size()[1] - 3
|
||||
os.chdir(sourcedir)
|
||||
|
||||
# Prepend an hourly tinestamp to the later name of the file
|
||||
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")
|
||||
|
||||
|
||||
# Initialize a dialog.Dialog instance
|
||||
d = Dialog(dialog="dialog")
|
||||
|
||||
# call favorite editor with filename to write the text
|
||||
def edit(headline):
|
||||
|
@ -18,9 +33,53 @@ def edit(headline):
|
|||
return headline
|
||||
|
||||
|
||||
# Select a topic for the new post or reedit an existing one
|
||||
def selectfile():
|
||||
kannweg = input("Moin: ")
|
||||
return 23
|
||||
# Put files in sourcefolder into list
|
||||
zahl = 0
|
||||
tabelle = []
|
||||
for zeile in glob.glob("*.md"):
|
||||
tabelle.insert(zahl, (str(zahl), zeile))
|
||||
zahl += 1
|
||||
|
||||
# Open list in menu and let user choose one
|
||||
ausgewaehlt = d.menu(
|
||||
"Such dir ne Datei:",
|
||||
width=xwidth,
|
||||
height=ywidth,
|
||||
menu_height=ywidth,
|
||||
title="Ein Thema wählen:",
|
||||
choices=tabelle,
|
||||
cancel="Neuen Namen eingeben",
|
||||
)
|
||||
|
||||
if ausgewaehlt[0] == "cancel":
|
||||
newtopic = d.inputbox("Ok, gib hier ein neues Thema an:", width=xwidth, height=ywidth, title="Ein Thema wählen:", cancel="Exit")
|
||||
if newtopic[0] == "cancel" or newtopic[1] == "":
|
||||
sys.exit("Na, dann eben nicht...")
|
||||
headline = newtopic[1]
|
||||
else:
|
||||
headline = ""
|
||||
|
||||
# Clean filename from leading .md for later construction of a headline
|
||||
if headline == "":
|
||||
headline = re.sub("\.md$", "", tabelle[int(ausgewaehlt[1])][1])
|
||||
|
||||
# if the searched filename doesnt match an existing file we construct a new one
|
||||
if len(glob.glob(sourcedir + headline + ".md")) == 0:
|
||||
sourcefile = sourcedir + headline + ".md"
|
||||
else:
|
||||
sourcefile = str(glob.glob(sourcedir + headline + ".md")[0])
|
||||
|
||||
# if the targetfilename doesnt match an existing file we construct a new one
|
||||
targetfile = glob.glob(targetdir + "[0-9][0-9][0-9][0-9]-[0-9][0-9]-[0-9][0-9] [0-2][0-9]:[0-6][0-9] " + headline + "*")
|
||||
targetfilelength = len(targetfile)
|
||||
if targetfilelength >= 1:
|
||||
targetfile = targetfile[targetfilelength - 1]
|
||||
else:
|
||||
targetfile = targetdir + datetime.datetime.now().strftime("%Y-%m-%d %H:00 ") + headline
|
||||
|
||||
return sourcefile, targetfile, headline
|
||||
|
||||
|
||||
# Parse arguments "--html", "--stdout" and "Some Headline"
|
||||
|
@ -41,37 +100,39 @@ for wish in optwords:
|
|||
if wish == "--stdout":
|
||||
outfile = sys.stdout
|
||||
args.pop(args.index(wish))
|
||||
# Check, if that topic already exists and fetch the appropriate filename
|
||||
if len(args) == 1:
|
||||
headline = args[0]
|
||||
inputfile = headline + ".md"
|
||||
outputfile = glob.glob(targetdir + "[0-9][0-9][0-9][0-9]-[0-9][0-9]-[0-9][0-9] [0-2][0-9]:[0-6][0-9] " + headline + "*")
|
||||
outputfilelength = len(outputfile)
|
||||
if outputfilelength >= 1:
|
||||
outputfile = outputfile[outputfilelength - 1]
|
||||
else:
|
||||
outputfile = targetdir + datetime.datetime.now().strftime("%Y-%m-%d %H:00 ") + headline
|
||||
|
||||
|
||||
elif len(args) == 0:
|
||||
headline = input("Please enter a headline or leave empty for selecting an older file: ")
|
||||
if not headline:
|
||||
headline = selectfile()
|
||||
selection = selectfile()
|
||||
inputfile = selection[0]
|
||||
outputfile = selection[1]
|
||||
headline = selection[2]
|
||||
elif len(args) > 1:
|
||||
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"
|
||||
# Prepend an hourly tinestamp to the later name of the file
|
||||
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")
|
||||
|
||||
# This is informal asking yes/no for editing the text
|
||||
if outfile != sys.stdout:
|
||||
new_file_name = targetdir + creationtime + " " + headline + fileending
|
||||
else:
|
||||
new_file_name = "STDOUT"
|
||||
print("You will edit now: ", headlinemd, "\nOutputfile= ", new_file_name)
|
||||
a = input("Press <RETURN> to continue or <CTRL-C> to stop: ")
|
||||
edit(headlinemd)
|
||||
if outfile == sys.stdout:
|
||||
outputfile = "STDOUT"
|
||||
print("\nYou will edit now: '" + inputfile + "'\nOutputfile= '" + outputfile + "'")
|
||||
a = input("\nPress <RETURN> to continue or <CTRL-C> to stop: ")
|
||||
edit(inputfile)
|
||||
|
||||
|
||||
# Set ouput to stdout, if requested. Caution, the prompts of this script go there, too,
|
||||
# be careful with copy & paste
|
||||
if outfile != sys.stdout:
|
||||
html_out_file = open(new_file_name, "w")
|
||||
html_out_file = open(outputfile, "w")
|
||||
else:
|
||||
html_out_file = outfile
|
||||
|
||||
|
@ -93,21 +154,6 @@ head1 = (
|
|||
|
||||
# Some fancy css for minimalistic terminal style
|
||||
|
||||
styles = " <!--\n\
|
||||
* { font-family: monospace; color: #00f020; background-color: #1c1414; font-size: 1.15em; }\n\
|
||||
a { font-size: 1.0em;}\n\
|
||||
ul { font-size: 1.15em;}\n\
|
||||
p { font-size: 1.15em;}\n\
|
||||
li { font-size: 1.15em;}\n\
|
||||
ol { font-size: 1.0em;}\n\
|
||||
em { font-size: 1.0em;}\n\
|
||||
table { background-color: #1c1414; }\n\
|
||||
blockquote { font-size: 1.15em; }\n\
|
||||
body { font-family: monospace; color: #00f020; background-color: #1c1414; }\n\
|
||||
pre { white-space: pre-wrap; font-family: monospace; color: #00f020; font-background-color: #1c1414; }\n\
|
||||
.Statement { color: #00f020; font-weight: bold; }\n\
|
||||
.Headandfoot { color: #00f020; font-weight: bold; } "
|
||||
|
||||
style_fn = '<link rel="stylesheet" type="text/css" href="/vimstyles.css">'
|
||||
head2 = "</head><body>\n"
|
||||
body1 = '<a href="./">Back</a>'
|
||||
|
@ -117,7 +163,7 @@ foot = "</body> </html>\n"
|
|||
html_out_file.write(head1 + style_fn + head2 + body1)
|
||||
|
||||
# The 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(inputfile, "r", encoding="utf-8") as infile:
|
||||
md_data = infile.read()
|
||||
html_output = markdown.markdown(md_data)
|
||||
html_out_file.write(html_output)
|
||||
|
@ -126,6 +172,6 @@ html_out_file.close()
|
|||
|
||||
# if the new filename is not stdout, return new filename to stdout, so some calling shellscript can use it.
|
||||
if outfile != sys.stdout:
|
||||
print(new_file_name)
|
||||
print(outputfile)
|
||||
|
||||
# Have a nice time.
|
||||
|
|
Loading…
Reference in a new issue