From f9a9166bb68bbf27101939ac18dad16b21b82b3f Mon Sep 17 00:00:00 2001
From: deadvey <deadvey@deadvey.com>
Date: Sat, 29 Mar 2025 20:41:53 +0000
Subject: [PATCH] works now

---
 .gitignore        |  1 +
 create_pages.py   | 35 ++++++++++++++++++-----------------
 example.config.py |  6 +++++-
 newpost.py        | 10 ++++------
 output.py         |  6 ++++++
 parse_post.py     |  0
 rebuild.py        |  4 ++--
 7 files changed, 36 insertions(+), 26 deletions(-)
 mode change 100755 => 100644 create_pages.py
 mode change 100755 => 100644 newpost.py
 create mode 100644 output.py
 mode change 100755 => 100644 parse_post.py

diff --git a/.gitignore b/.gitignore
index 010dfa4..2e1a812 100644
--- a/.gitignore
+++ b/.gitignore
@@ -1,3 +1,4 @@
 config.py
 __pycache__
 *.swp
+log
diff --git a/create_pages.py b/create_pages.py
old mode 100755
new mode 100644
index 7885c9e..fc3b87a
--- a/create_pages.py
+++ b/create_pages.py
@@ -15,30 +15,31 @@
 
 import config
 import parse_post
+import output
 from datetime import datetime
 
 def create_user_page(userID, username, db):
-	with open(f"{config.webroot}/user/{username}.gmi", "w") as userfile:
-		cursor = db.cursor()
-		cursor.execute(f"SELECT * FROM posts WHERE userID = {userID} ORDER BY postID Desc")
-		userfile.write(f"# {username}:\n")
-		for x in cursor:
-			post = config.user_page_post_format
-			post = parse_post.parse_post_format(post, x, username)
-			userfile.write(post)
+    with open(f"{config.webroot}/user/{username}.gmi", "w") as userfile:
+        output.log("Writing posts to userfile")
+        cursor = db.cursor()
+        cursor.execute(f"SELECT * FROM posts WHERE userID = {userID} ORDER BY postID Desc")
+        userfile.write(f"# {username}:\n")
+        for x in cursor:
+            post = config.user_page_post_format
+            post = parse_post.parse_post_format(post, x, username)
+            userfile.write(post)
 
-def create_post_page(postID, db):
-	with open(f"{config.webroot}/post/{postID}.gmi", "w") as postfile:
-		cursor = db.cursor()
-		cursor.execute(f"SELECT * FROM posts WHERE postID = {postID}")
-		cursor2 = db.cursor()
-		cursor2.execute('SELECT * FROM users')
-		post = config.post_page_post_format
-		post = parse_post.parse_post_format(post, x, cursor2[cursor[0]-1][1])
-		postfile.write(post)
+def create_post_page(post, db):
+    with open(f"{config.webroot}/post/{post[0]}.gmi", "w") as postfile:
+        output.log("Writing post to postfile")
+        output.log(post)
+        postfile_content = config.post_page_post_format
+        postfile_content = parse_post.parse_post_format(postfile_content, post, post[1])
+        postfile.write(postfile_content)
 
 def create_timeline(db):
 	with open(f"{config.webroot}/index.gmi", "w") as timeline_file:
+        output.log("Writing posts to timeline")
 		users = []
 		cursor = db.cursor()
 		cursor.execute(f"SELECT userName FROM users")
diff --git a/example.config.py b/example.config.py
index 7a1481f..ee6dad2 100755
--- a/example.config.py
+++ b/example.config.py
@@ -7,10 +7,14 @@ posts_in_timeline = 100
 
 ### SQL settings ###
 host = "localhost"
-user = "root"
+user = "username"
 password = "password"
 database = "glogger"
 
+### Logging ###
+logfile = "/path/to/logfile"
+verbose = False
+
 ### Format ###
 # The syntax for this is pretty simple
 # %S - post seperator as defined by post_seperator
diff --git a/newpost.py b/newpost.py
old mode 100755
new mode 100644
index 2076a3b..fa6dae8
--- a/newpost.py
+++ b/newpost.py
@@ -25,11 +25,13 @@ import click # Used to write post content, it launches a text editor to type int
 # Other python files #
 import create_pages
 import config
+import rebuild
 
 def newpost(db, userID, username, datetime):
 	title = input("Title: ")
 	content = click.edit()
 	content = content.replace("'", "&#39")
+	content = content.replace("\n", "\\n")
 	print(content)
 	datetime = datetime.now().strftime("%d%m%YZ%H%M%ST")
 
@@ -39,9 +41,5 @@ def newpost(db, userID, username, datetime):
 	for x in cursor:
 		postID = x[0]
 	db.commit()
-	## Write to user page ##
-	create_pages.create_user_page(userID, username, db)
-	create_pages.create_post_page(postID, username, db)
-	create_pages.create_timeline(db)
-
-
+    # Rebuild the config
+	rebuild.rebuild(db)
diff --git a/output.py b/output.py
new file mode 100644
index 0000000..3104c62
--- /dev/null
+++ b/output.py
@@ -0,0 +1,6 @@
+import config
+def log(text):
+    if config.verbose == True:
+        print(text)
+    with open(config.logfile, "w") as logfile:
+        logfile.write(f"{text}\n")
diff --git a/parse_post.py b/parse_post.py
old mode 100755
new mode 100644
diff --git a/rebuild.py b/rebuild.py
index cef252b..11dd9e9 100755
--- a/rebuild.py
+++ b/rebuild.py
@@ -6,6 +6,6 @@ def rebuild(db):
 	for x in cursor:
 		create_pages.create_user_page(x[0], x[1], db)
 		create_pages.create_timeline(db)
-	cursor.execute('SELECT * FROM posts')
+	cursor.execute('SELECT posts.postID, users.username, posts.title, posts.content, posts.pubDate, posts.editDate FROM posts JOIN users ON posts.userID=users.userID')
 	for x in cursor:
-		create_pages.create_post_page(x[0], db)	
+		create_pages.create_post_page(x, db)