include post.template
This commit is contained in:
@@ -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",
|
||||
" \"\"\"\n",
|
||||
"\n",
|
||||
" update_entry(file, target_id, new_text)"
|
||||
"{% include \"components/timeline.html\" %}\n",
|
||||
"\"\"\"\n",
|
||||
"add_entry(FIEL_PATH, \"test timeline\", \"subtitle\", text)"
|
||||
]
|
||||
}
|
||||
],
|
||||
|
||||
Reference in New Issue
Block a user