webgen/webgen.py

123 lines
4.2 KiB
Python
Raw Normal View History

#!/usr/bin/env python3
2021-10-27 16:09:07 +02:00
import sys, os, uuid, shutil, subprocess, markdown, re, datetime
2021-10-29 01:26:41 +02:00
# 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/")
2021-10-27 16:09:07 +02:00
fileending = ""
2021-10-29 01:26:41 +02:00
# call favorite editor with filename to write the text
2021-10-27 16:09:07 +02:00
def edit(headline):
editor = os.getenv("EDITOR")
if not editor:
editor = "vim"
subprocess.call([editor, headline])
return headline
2021-10-29 01:26:41 +02:00
# Parse arguments "--html", "--stdout" 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
2021-10-27 16:09:07 +02:00
optwords = ["--html", "--stdout"]
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 == "--stdout":
outfile = sys.stdout
args.pop(args.index(wish))
if len(args) == 1:
headline = args[0]
elif len(args) == 0:
headline = input("Please enter a headline: ")
elif len(args) > 1:
sys.exit("Too much or wrong parameters:\n" + myname + ' [--html] [--stdout] ["Some Topic"] ')
2021-10-27 16:09:07 +02:00
2021-10-29 01:26:41 +02:00
# The headline should be the theme of the text. We edit a .md-file
2021-10-27 16:09:07 +02:00
headlinemd = headline + ".md"
2021-10-29 01:26:41 +02:00
# Prepend an hourly tinestamp to the later name of the file
creationtime = datetime.datetime.now().strftime("%Y-%m-%d %H:00 ")
2021-10-29 01:26:41 +02:00
# generate a more precise ctime to be included in the resulting webpage
creationtimeheader = datetime.datetime.now().strftime("%Y-%m-%d %H:%M")
2021-10-29 01:26:41 +02:00
# This is informal asking yes/no for editing the text
if outfile != sys.stdout:
new_file_name = targetdir + creationtime + " " + headline + fileending
else:
2021-10-28 18:56:02 +02:00
new_file_name = "STDOUT"
2021-10-29 01:36:02 +02:00
print("You will edit now: ", headlinemd, "\nOutputfile= ", new_file_name)
a = input("Press <RETURN> to continue or <CTRL-C> to stop: ")
edit(headlinemd)
2021-10-29 01:26:41 +02:00
# 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")
else:
html_out_file = outfile
2021-10-29 01:26:41 +02:00
# The head of the output. Strip path from myname
head1 = (
'<!DOCTYPE HTML><html><head>\n\
2021-11-15 12:43:52 +01:00
<meta http-equiv="content-type" content="text/html; charset=UTF-8">\n<title>'
+ headline
+ '</title>\n\
<meta name="syntax" content="markdown">\n\
2021-10-25 08:11:36 +02:00
<meta name="generator" content="'
2021-10-29 01:26:41 +02:00
+ myname.rpartition("/")[2]
2021-10-25 08:11:36 +02:00
+ ' 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
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\
2021-04-24 14:23:19 +02:00
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>'
foot = "</body> </html>\n"
2021-10-29 01:26:41 +02:00
# write beginning of html-file
2021-10-29 01:36:02 +02:00
html_out_file.write(head1 + style_fn + head2 + body1)
2021-10-29 01:26:41 +02:00
# The 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()
html_output = markdown.markdown(md_data)
html_out_file.write(html_output)
html_out_file.write(foot)
html_out_file.close()
2021-10-29 01:26:41 +02:00
# if the new filename is not stdout, return new filename to stdout, so some calling shellscript can use it.
2021-10-28 18:56:02 +02:00
if outfile != sys.stdout:
print(new_file_name)
2021-10-29 01:26:41 +02:00
# Have a nice time.