pyblog/blog.py

171 lines
6.8 KiB
Python

#!/usr/bin/env python
import os
import subprocess
import sys
import datetime
from datetime import datetime
import markdown
# Configuration:
author = "flat" # blog/post author
title = author + "'s blog" # blog title
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/")
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!")
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!")