include post.template
This commit is contained in:
@@ -1,56 +0,0 @@
|
||||
import datetime
|
||||
FIELDS = ['title', 'subtitle', 'content', 'displayall']
|
||||
def add_entry(file_path, title, subtitle, content, display_all=False):
|
||||
# 1. Import the current list to get the latest ID
|
||||
# We use a namespace to avoid path issues
|
||||
namespace = {}
|
||||
with open(file_path, 'r') as f:
|
||||
exec(f.read(), namespace)
|
||||
|
||||
posts = namespace.get('BLOG_POSTS', [])
|
||||
new_id = max([p['id'] for p in posts], default=0) + 1
|
||||
print("new id", new_id)
|
||||
# 2. Format the new entry string
|
||||
date_str = datetime.datetime.now().strftime("%B %d, %Y")
|
||||
|
||||
new_post_str = f"""
|
||||
{{
|
||||
'id': {new_id},
|
||||
'title': '{title}',
|
||||
'content': \"\"\"
|
||||
{content}
|
||||
\"\"\",
|
||||
'subtitle': '{subtitle}',
|
||||
'date': '{date_str}',
|
||||
"displayall": {display_all}
|
||||
}},
|
||||
"""
|
||||
|
||||
# 3. Append to the file
|
||||
# Note: This appends to the END of the file.
|
||||
# If your file ends with ']', we need to strip that first.
|
||||
with open(file_path, 'r+') as f:
|
||||
content_full = f.read().strip()
|
||||
# Find the last closing bracket of the list
|
||||
if content_full.endswith(']'):
|
||||
content_full = content_full.rsplit(']', 1)[0]
|
||||
|
||||
f.seek(0)
|
||||
f.write(content_full + new_post_str + "\n]")
|
||||
f.truncate()
|
||||
|
||||
print(f"Successfully added Post #{new_id}: {title}")
|
||||
|
||||
# Usage
|
||||
if __name__ == "__main__":
|
||||
#t = input("Title: ")
|
||||
#s = input("Subtitle: ")
|
||||
#c = input("Content (HTML): ")
|
||||
|
||||
# title
|
||||
t = "I have a solution but it only works for spherical chickens in a vacuum!"
|
||||
# subtitle
|
||||
s = ""
|
||||
# content
|
||||
c = "no content"
|
||||
add_entry('content/posts.py', t, s, c)
|
||||
@@ -74,51 +74,81 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 5,
|
||||
"id": "7c80d17a",
|
||||
"execution_count": 8,
|
||||
"id": "e94255ff",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"FIEL_PATH = \"../content/posts.py\"\n",
|
||||
"get_entry(FIEL_PATH, 0)"
|
||||
"import datetime\n",
|
||||
"FIELDS = ['title', 'subtitle', 'content', 'displayall']\n",
|
||||
"# id is incremental\n",
|
||||
"def add_entry(file_path, title, subtitle, content, display_all=False):\n",
|
||||
" # 1. Import the current list to get the latest ID\n",
|
||||
" # We use a namespace to avoid path issues\n",
|
||||
" namespace = {}\n",
|
||||
" with open(file_path, 'r') as f:\n",
|
||||
" exec(f.read(), namespace)\n",
|
||||
" \n",
|
||||
" posts = namespace.get('BLOG_POSTS', [])\n",
|
||||
" new_id = max([p['id'] for p in posts], default=0) + 1\n",
|
||||
" print(\"new id\", new_id)\n",
|
||||
" # 2. Format the new entry string\n",
|
||||
" date_str = datetime.datetime.now().strftime(\"%B %d, %Y\")\n",
|
||||
" \n",
|
||||
" new_post_str = f\"\"\"\n",
|
||||
" {{\n",
|
||||
" 'id': {new_id},\n",
|
||||
" 'title': '{title}',\n",
|
||||
" 'content': \\\"\\\"\\\"\n",
|
||||
" {content}\n",
|
||||
"\\\"\\\"\\\",\n",
|
||||
" 'subtitle': '{subtitle}',\n",
|
||||
" 'date': '{date_str}',\n",
|
||||
" \"displayall\": {display_all}\n",
|
||||
" }},\n",
|
||||
" \"\"\"\n",
|
||||
"\n",
|
||||
" # 3. Append to the file\n",
|
||||
" # Note: This appends to the END of the file. \n",
|
||||
" # If your file ends with ']', we need to strip that first.\n",
|
||||
" with open(file_path, 'r+') as f:\n",
|
||||
" content_full = f.read().strip()\n",
|
||||
" # Find the last closing bracket of the list\n",
|
||||
" if content_full.endswith(']'):\n",
|
||||
" content_full = content_full.rsplit(']', 1)[0]\n",
|
||||
" \n",
|
||||
" f.seek(0)\n",
|
||||
" f.write(content_full + new_post_str + \"\\n]\")\n",
|
||||
" f.truncate()\n",
|
||||
"\n",
|
||||
" print(f\"Successfully added Post #{new_id}: {title}\")\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"id": "e2048547",
|
||||
"execution_count": 9,
|
||||
"id": "7c80d17a",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"new id 10\n",
|
||||
"Successfully added Post #10: test timeline\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"FIEL_PATH = \"../content/posts.py\"\n",
|
||||
"#get_entry(FIEL_PATH, 0)\n",
|
||||
"# update_entry(FIEL_PATH, target_id, new_text)\n",
|
||||
"# add_entry(FIEL_PATH, \"title\", \"subtitle\", new_text)\n",
|
||||
"text=\"\"\"<h1 class=\"text-3xl font-bold mb-8\">Project Timeline</h1>\n",
|
||||
"\n",
|
||||
"# --- Usage Example ---\n",
|
||||
"if __name__ == \"__main__\":\n",
|
||||
" file = \"../content/posts.py\"\n",
|
||||
" target_id = 3\n",
|
||||
"\n",
|
||||
" # Get the old one first to see it\n",
|
||||
" old_post = get_entry(file, target_id)\n",
|
||||
"\n",
|
||||
" if old_post:\n",
|
||||
" pass\n",
|
||||
"\n",
|
||||
" new_text = \"\"\"\n",
|
||||
" <p>sudo pacman -S archlinux-keyring</p>\n",
|
||||
" <p>sudo pacman -Sc</p>\n",
|
||||
" <p>melange:</p>\n",
|
||||
" <ul>\n",
|
||||
" <li><a href=\"/page2/line/lineplot.html\">machine learning with failed hyperparams</a></li>\n",
|
||||
" <li><a href=\"/page2/dblp/bar/index.html\">the dblp dataset in bar plot</a></li>\n",
|
||||
" <li><a href=\"/page2/dblp/edge/index.html\">the dblp dataset with an example of community</a></li>\n",
|
||||
" <li><a href=\"/page2/dblp/full/index.html\">the dblp dataset of 5 years as a graph</a></li>\n",
|
||||
" <li><a href=\"/page2/dblp/time/index.html\">the generated links as a graph</a></li>\n",
|
||||
"\n",
|
||||
" </ul>\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"{% include \"components/timeline.html\" %}\n",
|
||||
"\"\"\"\n",
|
||||
"\n",
|
||||
" update_entry(file, target_id, new_text)"
|
||||
"add_entry(FIEL_PATH, \"test timeline\", \"subtitle\", text)"
|
||||
]
|
||||
}
|
||||
],
|
||||
|
||||
120
edit_script/edit_entry.py
Normal file
120
edit_script/edit_entry.py
Normal file
@@ -0,0 +1,120 @@
|
||||
import datetime
|
||||
|
||||
|
||||
# git entry and print
|
||||
def get_entry(file_path, id):
|
||||
# 1. Import the current list to get the latest ID
|
||||
# use a namespace to avoid path issues
|
||||
namespace = {}
|
||||
|
||||
with open(file_path, "r") as f:
|
||||
exec(f.read(), namespace)
|
||||
|
||||
posts = namespace.get("BLOG_POSTS", [])
|
||||
|
||||
for items in posts:
|
||||
if items.get("id") == id:
|
||||
content = items.get("content")
|
||||
print(content)
|
||||
return items
|
||||
|
||||
|
||||
def update_entry(file_path, post_id, new_content):
|
||||
# 1. Load the existing data into memory
|
||||
namespace = {}
|
||||
with open(file_path, "r") as f:
|
||||
exec(f.read(), namespace)
|
||||
|
||||
posts = namespace.get("BLOG_POSTS", [])
|
||||
|
||||
# 2. Find and update the specific post
|
||||
found = False
|
||||
for post in posts:
|
||||
if post.get("id") == post_id:
|
||||
post["content"] = new_content
|
||||
# Optional: Update the date or add an "updated" field
|
||||
# post['subtitle'] += " (Updated)"
|
||||
found = True
|
||||
break
|
||||
|
||||
if not found:
|
||||
print(f"Error: Post {post_id} not found.")
|
||||
return
|
||||
|
||||
# 3. Write the entire list back to the file
|
||||
# Using repr() or a loop to format it cleanly
|
||||
with open(file_path, "w") as f:
|
||||
f.write("BLOG_POSTS = [\n")
|
||||
for p in posts:
|
||||
# Use .get() to provide a default value if the key is missing
|
||||
display_val = p.get("displayall", False)
|
||||
data_val = p.get("date", "?")
|
||||
subtitle_val = p.get("subtitle", "")
|
||||
|
||||
entry = f""" {{
|
||||
'id': {p["id"]},
|
||||
'title': "{p["title"]}",
|
||||
'subtitle': "{subtitle_val}",
|
||||
'date': "{data_val}",
|
||||
'content': \"\"\"{p["content"]}\"\"\",
|
||||
"displayall": {display_val}
|
||||
}},\n"""
|
||||
f.write(entry)
|
||||
f.write("]\n")
|
||||
|
||||
print(f"Post {post_id} updated successfully.")
|
||||
|
||||
FIELDS = ['title', 'subtitle', 'content', 'displayall']
|
||||
# id is incremental
|
||||
def add_entry(file_path, title, subtitle, content, display_all=False):
|
||||
# 1. Import the current list to get the latest ID
|
||||
# We use a namespace to avoid path issues
|
||||
namespace = {}
|
||||
with open(file_path, 'r') as f:
|
||||
exec(f.read(), namespace)
|
||||
|
||||
posts = namespace.get('BLOG_POSTS', [])
|
||||
new_id = max([p['id'] for p in posts], default=0) + 1
|
||||
print("new id", new_id)
|
||||
# 2. Format the new entry string
|
||||
date_str = datetime.datetime.now().strftime("%B %d, %Y")
|
||||
|
||||
new_post_str = f"""
|
||||
{{
|
||||
'id': {new_id},
|
||||
'title': '{title}',
|
||||
'content': \"\"\"
|
||||
{content}
|
||||
\"\"\",
|
||||
'subtitle': '{subtitle}',
|
||||
'date': '{date_str}',
|
||||
"displayall": {display_all}
|
||||
}},
|
||||
"""
|
||||
|
||||
# 3. Append to the file
|
||||
# Note: This appends to the END of the file.
|
||||
# If your file ends with ']', we need to strip that first.
|
||||
with open(file_path, 'r+') as f:
|
||||
content_full = f.read().strip()
|
||||
# Find the last closing bracket of the list
|
||||
if content_full.endswith(']'):
|
||||
content_full = content_full.rsplit(']', 1)[0]
|
||||
|
||||
f.seek(0)
|
||||
f.write(content_full + new_post_str + "\n]")
|
||||
f.truncate()
|
||||
|
||||
print(f"Successfully added Post #{new_id}: {title}")
|
||||
|
||||
# ------------ exmaple usage --------------------
|
||||
|
||||
FIEL_PATH = "../content/posts.py"
|
||||
#get_entry(FIEL_PATH, 0)
|
||||
# update_entry(FIEL_PATH, target_id, new_text)
|
||||
# add_entry(FIEL_PATH, "title", "subtitle", new_text)
|
||||
text="""<h1 class="text-3xl font-bold mb-8">Project Timeline</h1>
|
||||
|
||||
{% include "components/timeline.html" %}
|
||||
"""
|
||||
add_entry(FIEL_PATH, "test timeline", "subtitle", text)
|
||||
@@ -11,14 +11,16 @@ Template{% endblock %} {% block content %}
|
||||
<hr />
|
||||
{% if post.id == 8 %}
|
||||
<div class="background-svg">
|
||||
<img
|
||||
src="{{ url_for('static', filename='animation.svg') }}"
|
||||
alt=""
|
||||
/>
|
||||
<br>
|
||||
<p>tree source: codepen @uchardon</p></div>
|
||||
<img src="{{ url_for('static', filename='animation.svg') }}" alt="" />
|
||||
<br />
|
||||
<p>tree source: codepen @uchardon</p>
|
||||
</div>
|
||||
{% endif %}
|
||||
|
||||
<div class="blog-content">{{ post.content | safe }}</div>
|
||||
|
||||
{% include post.template %}
|
||||
|
||||
<a href="{{ url_for('home') }}" class="btn btn-default btn-custom"
|
||||
>← Back to Posts</a
|
||||
>
|
||||
|
||||
Reference in New Issue
Block a user