diff --git a/webgen-ni.py b/webgen-ni.py new file mode 100755 index 0000000..eb3149f --- /dev/null +++ b/webgen-ni.py @@ -0,0 +1,256 @@ +#!/usr/bin/env python3 + +import sys, os, uuid, shutil, signal, subprocess, markdown, re, datetime, locale, glob +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 +webbasedir = os.path.expanduser("~/www/i21k.de/") +targetdir = webbasedir + "posts/" +sourcedir = os.path.expanduser("~/python/webgen/") +### for module mkdirindex: +filesdir = webbasedir + "files/" +indexfilename = "index.html" +filesdirheadline = "Files" +#### + +fileending = "" +locale.setlocale(locale.LC_ALL, "") +clear = "\x1b[2J\x1b[H" +green = "\x1b[38;5;46m" +red = "\x1b[38;5;9m" +yellow = "\x1b[38;5;226m" +greenonblack = "\x1b[38;5;46m\x1b[48;5;16m" +redonblack = "\x1b[38;5;9m\x1b[48;5;16m" + +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") + +# Let's stop everything at CTRL-c + + +def sigint_handler(signum, frame): + sys.exit(yellow + "\nPfff....I'll tell your mom!\n") + + +signal.signal(signal.SIGINT, sigint_handler) + + +# call favorite editor with filename to write the text +def edit(headline): + editor = os.getenv("EDITOR") + if not editor: + editor = "vim" + subprocess.call([editor, headline]) + return headline + + +# Select a topic for the new post or reedit an existing one +def selectfile(): + # Put all .md files in sourcefolder into list with a format, which dialog can eat. + + filelist = glob.glob("*.md") + filelist.sort() + tabelle = [] + zahl = 0 + for zeile in filelist: + 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", + ) + + # Nothing has been choosen, so lets ask for a topic to generate a filename + 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(yellow + "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]) + print(headline) +# a = input(yellow + "What do we dooo here?") + + # 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 + +def mkdirindex(filesdir, indexfilename, filesdirheadline): + """Dig the files-directory and generate an index.""" + + head1 = ( + '
\n \nMoin' +foot = "\n" +foot = "