Update blog.py

This commit is contained in:
flat 2024-02-11 15:03:22 +00:00
parent fedd8bc86d
commit cb07ad4e7c
1 changed files with 123 additions and 21 deletions

144
blog.py
View File

@ -3,21 +3,24 @@ import os
import subprocess
import sys
import datetime
from datetime import datetime
import markdown
# Configuration:
author = "John Doe" # blog/post author
author = "flat" # blog/post author
title = author + "'s blog" # blog title
desc = "My little blog. There are many like it, but this one is mine.</p><p><em>you can use custom html here too!</em>" # blog description
autoOpen = True # automatically open new post with preferred editor
authorLink = "https://example.com" # Link to author's website
desc = "My very own, rarely updated, unfinished blog." # blog description
autoOpen = False # automatically open new post with preferred editor
authorLink = "https://tilde.team/~flat/" # Link to author's website
index = "/blog/index.html" # blog index path
style = "body{margin:40px auto;max-width:650px;line-height:1.6;font-size:18px;color:#444;padding:0 10px}h1,h2,h3{line-height:1.2"
footer = '<hr></hr><p><a href="' + index + '">' + title + '</a> - made with <a href="https://tildegit.org/flat/blog.py">pyblog</a></p>'
if sys.argv[1] == "init":
os.mkdir("postmd/")
os.mkdir("posts/")
os.mkdir("posthtml/")
f = open("style.css", "x")
f.write('body{margin:40px auto;max-width:650px;line-height:1.6;font-size:18px;color:#444;padding:0 10px}h1,h2,h3{line-height:1.2}')
f.write(style)
f.close()
f = open("index.html", "x")
f.close()
@ -25,23 +28,23 @@ if sys.argv[1] == "init":
elif sys.argv[1] == "post":
if sys.argv[2] == "new":
f = open("posts/" + sys.argv[3] + ".md", "w")
f.write('<link rel="stylesheet" href="mystyle.css">')
f.write("<p><em>Posted at " + str(datetime.datetime.now()) + " by " + '<a href="' + authorLink + '">' + author + "</a>" "</em></p>")
f = open("postmd/" + sys.argv[3] + ".md", "x")
f.write('<link rel="stylesheet" href="style.css">')
f.write("<p><em>Posted at " + str(datetime.datetime.today()) + " by " + '<a href="' + authorLink + '">' + author + "</a>" "</em></p>")
f.write("\n<!-- Post starts below this line -->")
f.close()
if autoOpen is True:
subprocess.call(("xdg-open", "posts/" + sys.argv[3] + ".md"))
subprocess.call(("xdg-open", "postmd/" + sys.argv[3] + ".md"))
else:
print("New post", sys.argv[3] + ".md created in posts/")
print("New post", sys.argv[3] + ".md created in postmd/")
elif sys.argv[2] == "del":
os.remove("posts/" + sys.argv[3] + ".md")
os.remove("posthtml/" + sys.argv[3] + ".html")
os.remove("postmd/" + sys.argv[3] + ".md")
os.remove("posts/" + sys.argv[3] + ".html")
print("Post " + sys.argv[3] + " deleted")
elif sys.argv[1] == "build":
md = os.listdir("posts")
html = os.listdir("posthtml")
md = os.listdir("postmd")
html = os.listdir("posts")
mdposts = []
htmlposts = []
for x in md:
@ -50,9 +53,9 @@ elif sys.argv[1] == "build":
htmlposts.append(os.path.splitext(x)[0])
for x in mdposts:
if x not in htmlposts:
markdown.markdownFromFile(input="posts/" + x + ".md", output="posthtml/" + x + ".html")
f = open("posthtml/" + x + ".html", "a")
f.write('<hr></hr><p><a href="' + index + '">' + title + '</a> - made with <a href="https://tildegit.org/flat/blog.py">pyblog</a></p>')
markdown.markdownFromFile(input="postmd/" + x + ".md", output="posts/" + x + ".html")
f = open("posts/" + x + ".html", "a")
f.write(footer)
f.close()
print("Added post '" + x + "'!")
f = open("index.html", "w")
@ -60,9 +63,108 @@ elif sys.argv[1] == "build":
f.write("<h1>" + title + "</h1>")
f.write("<p>" + desc + "</p>")
f.write("<h2>All posts:</h2>")
for x in os.listdir("posthtml/"):
f.write('<li><a href="posthtml/' + x + '">' + x + '</a></li>')
f.write('<hr></hr><p><a href="' + index + '">' + title + '</a> - made with <a href="https://tildegit.org/flat/blog.py">pyblog</a></p>')
for x in os.listdir("posts/"):
time = datetime.fromtimestamp(os.path.getctime("posts/" + x))
f.write('<li><a href="posts/' + x + '">' + x + '</a>' + " - " + str(time.month) + "-" + str(time.day) + "-" + str(time.year) + '</li>')
f.write(footer)
else:
print("Invalid argument!")
flat@tilde ~/p/blog> nvim ~/pyblog/blog.py
flat@tilde ~/p/blog> git push ~/pyblog
fatal: not a git repository (or any parent up to mount point /)
Stopping at filesystem boundary (GIT_DISCOVERY_ACROSS_FILESYSTEM not set).
flat@tilde ~/p/blog [128]> cd ..
flat@tilde ~/public_html> cd ..
flat@tilde ~> cd pyblog/
flat@tilde ~/pyblog (main)> git init
Reinitialized existing Git repository in /home/flat/pyblog/.git/
flat@tilde ~/pyblog (main)> git push
Username for 'https://tildegit.org': flat
Password for 'https://flat@tildegit.org':
remote: Verify
fatal: Authentication failed for 'https://tildegit.org/flat/pyblog/'
flat@tilde ~/pyblog (main) [128]> git push
Username for 'https://tildegit.org': flat
Password for 'https://flat@tildegit.org':
remote: Verify
fatal: Authentication failed for 'https://tildegit.org/flat/pyblog/'
flat@tilde ~/pyblog (main) [128]> git push
Username for 'https://tildegit.org': flat@tilde.team
Password for 'https://flat@tilde.team@tildegit.org': flat@tilde ~/pyblog (main) [SIGINT]> git push
Username for 'https://tildegit.org': flat
Password for 'https://flat@tildegit.org':
Everything up-to-date
flat@tilde ~/pyblog (main)> cat blog.py
#!/usr/bin/env python
import os
import subprocess
import sys
import datetime
from datetime import datetime
import markdown
# Configuration:
author = "John Smith" # blog/post author
title = author + "'s blog" # blog title
desc = "There are many like it, but this one is <strong>mine</strong>" # blog description
autoOpen = True # automatically open new post with preferred editor
authorLink = "https://example.com/" # Link to author's website
index = "/blog/index.html" # blog index path
style = "body{margin:40px auto;max-width:650px;line-height:1.6;font-size:18px;color:#444;padding:0 10px}h1,h2,h3{line-height:1.2"
footer = '<hr></hr><p><a href="' + index + '">' + title + '</a> - made with <a href="https://tildegit.org/flat/blog.py">pyblog</a></p>'
if sys.argv[1] == "init":
os.mkdir("postmd/")
os.mkdir("posts/")
f = open("style.css", "x")
f.write(style)
f.close()
f = open("index.html", "x")
f.close()
print("Done! (created directories: posts/, posthtml/, files: index.html, style.css)")
elif sys.argv[1] == "post":
if sys.argv[2] == "new":
f = open("postmd/" + sys.argv[3] + ".md", "x")
f.write('<link rel="stylesheet" href="style.css">')
f.write("<p><em>Posted at " + str(datetime.datetime.today()) + " by " + '<a href="' + authorLink + '">' + author + "</a>" "</em></p>")
f.write("\n<!-- Post starts below this line -->")
f.close()
if autoOpen is True:
subprocess.call(("xdg-open", "postmd/" + sys.argv[3] + ".md"))
else:
print("New post", sys.argv[3] + ".md created in postmd/")
elif sys.argv[2] == "del":
os.remove("postmd/" + sys.argv[3] + ".md")
os.remove("posts/" + sys.argv[3] + ".html")
print("Post " + sys.argv[3] + " deleted")
elif sys.argv[1] == "build":
md = os.listdir("postmd")
html = os.listdir("posts")
mdposts = []
htmlposts = []
for x in md:
mdposts.append(os.path.splitext(x)[0])
for x in html:
htmlposts.append(os.path.splitext(x)[0])
for x in mdposts:
if x not in htmlposts:
markdown.markdownFromFile(input="postmd/" + x + ".md", output="posts/" + x + ".html")
f = open("posts/" + x + ".html", "a")
f.write(footer)
f.close()
print("Added post '" + x + "'!")
f = open("index.html", "w")
f.write('<link rel="stylesheet" href="style.css">')
f.write("<h1>" + title + "</h1>")
f.write("<p>" + desc + "</p>")
f.write("<h2>All posts:</h2>")
for x in os.listdir("posts/"):
time = datetime.fromtimestamp(os.path.getctime("posts/" + x))
f.write('<li><a href="posts/' + x + '">' + x + '</a>' + " - " + str(time.month) + "-" + str(time.day) + "-" + str(time.year) + '</li>')
f.write(footer)
else:
print("Invalid argument!")