Compare commits

...

2 commits
Main ... main

Author SHA1 Message Date
Wolfgang Nowak
d146866ae1 geändert: md2html.py 2023-10-29 13:37:57 +01:00
Wolfgang Nowak
e0e0c3f00c first commit 2023-10-29 11:23:22 +01:00
2 changed files with 287 additions and 2 deletions

View file

@ -7,12 +7,12 @@ Liest markdownfile ein und spuckt html aus: md2html meinmarkdownfile.md
"""
import sys, markdown2 as markdown
markdown.Markdown.extras = ["break-on-newline", "footnotes"]
markdown.Markdown.extras = ["footnotes", "break-on-newline"]
try:
mdfile = sys.argv[1]
except:
sys.exit("No file, honey.\n"+sys.argv[0]+ ": Liest markdownfile ein und spuckt html aus: md2html meinmarkdownfile.md")
sys.exit("No file, honey.\n" + sys.argv[0] + ": Liest markdownfile ein und spuckt html aus: md2html meinmarkdownfile.md")
headline = mdfile.split(".md")[0]
htmlfilename = headline + ".html"

285
webgena.py Executable file
View file

@ -0,0 +1,285 @@
#!/usr/bin/env python3
import sys, os, uuid, shutil, signal, subprocess, markdown2 as 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. Preset values.
webbasedir = os.path.expanduser("~/www/i21k.de/")
targetdir = webbasedir + "posts/"
sourcedir = os.path.expanduser("~/python/webgen/mds/")
### for module mkdirindex:
filesdir = webbasedir + "files/"
indexfilename = "index.html"
filesdirheadline = "Files"
####
# for spellchecko
# Preferred: aspell -c filename
#
dospellcheck = os.getenv("SPELLCHECK")
spellcheck = "aspell"
spellcheckparam = "-c"
# Configure Markdown, activate "break-on-newline" for letting a line end without havin to put doublespaces there.
#
# More options:
# break-on-newline, code-friendly, cuddled-lists, fenced-code-blocks,
# footnotes, header-ids, highlightjs-lang, html-classes, link-patterns,
# markdown-in-html, numbering, pyshell, smarty-pants, spoiler, strike,
# tag-friendly, tables, toc, use-file-vars, wiki-tables, xml
markdown.Markdown.extras = ["footnotes", "break-on-newline"]
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
# i am not shure bout that spellchecko, is it nessesary?
def edit(headline):
editor = os.getenv("EDITOR")
if not editor:
editor = "vim"
subprocess.call([editor, headline])
if dospellcheck:
print(yellow)
if input("You like to spellcheck it first [y/N]?") == "y":
subprocess.call([spellcheck, spellcheckparam, headline])
print(green)
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 = (
'<!DOCTYPE HTML><html><head>\n <meta http-equiv="content-type" content="text/html; charset=UTF-8">\n<title>'
+ filesdirheadline
+ '</title>\n <meta name="syntax" content="markdown">\n <meta name="generator" content="'
+ "webgen.py"
+ ' markdown2htmlconverter">\n <meta name="settings" content="use_css,pre_wrap,no_foldcolumn,expand_tabs,prevent_copy=">'
)
style_fn = '<link rel="stylesheet" type="text/css" href="/vimstyles.css">'
head2 = "</head><body>"
body1 = '<a class="Statement" href="../">Back</a><br/><br/><hr>'
body1 += '<div class="Statement"> Some files. Maybe useful or not.</div> '
body1 += '<table border="0" class="Neutral">'
foot = '</table><br/><strong>You can\'t avoid chaos.</strong><br/><hr class="myhr" ></body> </html>\n'
linecounter = 0
dirindexfile = open(filesdir + indexfilename, "w")
dirindexfile.write(head1 + style_fn + head2 + body1)
for file in os.listdir(filesdir):
if not re.match("^\.", file) and not file == indexfilename:
size = os.lstat(filesdir + file).st_size
if size > 100000000000:
sizestr = str(int(size / 1073741824)) + " GiB"
elif size > 100000000:
sizestr = str(int(size / 1048576)) + " MiB"
elif size > 10000:
sizestr = str(int(size / 1024)) + " KiB"
else:
sizestr = str(size) + " B"
line = str('<tr><td><a href="' + str(file) + '">' + str(file) + "</a></td><td>&nbsp;" + sizestr + "</td></tr>")
dirindexfile.write(line)
linecounter += 1
dirindexfile.write(foot)
return linecounter
# Parse arguments "--html", "--stdout", --spellcheck and "Some Headline"
# there are 3 parameters. if --html is given, the fileending will be .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", "--spellcheck"]
outfile = ""
args = sys.argv
myname = args.pop(0)
for wish in optwords:
if args.count(wish):
if wish == "--html":
fileending = ".html"
args.pop(args.index(wish))
if wish == "--spellcheck":
dospellcheck = "y"
args.pop(args.index(wish))
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 + "*" + fileending
)
outputfilelength = len(outputfile)
if outputfilelength >= 1:
outputfile = outputfile[outputfilelength - 1]
else:
outputfile = targetdir + datetime.datetime.now().strftime("%Y-%m-%d %H:00 ") + headline + fileending
elif len(args) == 0:
selection = selectfile()
inputfile = selection[0]
outputfile = selection[1] + fileending
headline = selection[2]
elif len(args) > 1:
sys.exit(red + "Too much or wrong parameters:\n" + yellow + myname + ' [--html] [--stdout] ["Some Topic"] ')
# This is informal asking yes/no for editing the text
if outfile == sys.stdout:
outputfile = "STDOUT"
print(yellow + "\nYou will edit now: '" + green + inputfile + yellow + "'\nOutputfile= '" + green + 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(outputfile, "w")
else:
html_out_file = outfile
# The head of the output. Strip path from myname
head1 = (
'<!DOCTYPE HTML><html><head>\n\
<meta http-equiv="content-type" content="text/html; charset=UTF-8">\n<title>'
+ headline
+ '</title>\n\
<meta name="syntax" content="markdown">\n\
<meta name="generator" content="'
+ myname.rpartition("/")[2]
+ ' markdown2htmlconverter">\n\
<meta name="settings" content="use_css,pre_wrap,no_foldcolumn,expand_tabs,prevent_copy=">\n\
<meta name="ctime" content="'
+ creationtimeheader
+ '"> '
)
# Some fancy css for minimalistic terminal style
style_fn = '<link rel="stylesheet" type="text/css" href="/vimstyles.css">'
head2 = "</head><body>\n"
body1 = '<a href="./">Back</a><blockquote><strong>Moin</strong></p>'
foot = '<br /><hr class="myhr" /></body> </html>\n'
# write beginning of html-file
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(inputfile, "r", encoding="utf-8") as infile:
md_data = infile.read()
html_output = markdown.markdown(md_data)
html_out_file.write(html_output)
html_out_file.write(foot)
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(outputfile)
# Dig the files-directory and generate an index.
linecounter = mkdirindex(filesdir, indexfilename, filesdirheadline)
print("Dirindex in ", filesdir, " has ", linecounter, "lines")
# Have a nice time.