重做文章详情页:拾光阅读版式与目录/代码/灯箱交互

- 新增内容页外壳 base_content.html 与 reading.css / reading.js
- 重写 post_detail.html:分类胶囊、阅读进度、首字下沉、代码块复制、
  侧栏目录、上下篇、图片灯箱、明暗主题
- post_detail 视图补充上下篇、字数与阅读时长
- base.html 补 extra_css / extra_js block,修复脑图详情页样式不加载
- 加入 mindmaps、api、首页静态资源与模板
- 忽略 __pycache__ / *.pyc / *.back,并将已跟踪的 .pyc 移出版本库
This commit is contained in:
2026-11-20 15:37:55 +08:00
parent 7e45fa620e
commit 6ab9cca4f3
70 changed files with 26103 additions and 38 deletions
View File
+29
View File
@@ -0,0 +1,29 @@
from django.contrib import admin
from django import forms
from .models import MindMap
@admin.register(MindMap)
class MindMapAdmin(admin.ModelAdmin):
list_display = ("title", "created_at", "has_mindmap")
search_fields = ("title",)
prepopulated_fields = {"slug": ("title",)}
readonly_fields = ("updated_at",)
def has_mindmap(self, obj):
return bool(obj.mindmap_data and obj.mindmap_data.get("data"))
has_mindmap.boolean = True
has_mindmap.short_description = "已解析"
def save_model(self, request, obj, form, change):
is_new = not obj.pk
super().save_model(request, obj, form, change)
if "xmind_file" in form.changed_data and obj.xmind_file:
try:
obj.parse_and_store(obj.xmind_file.path)
self.message_user(request, f"脑图「{obj.title}」解析成功")
except Exception as e:
self.message_user(
request, f"解析失败: {e}", level="error"
)
+6
View File
@@ -0,0 +1,6 @@
from django.apps import AppConfig
class MindmapsConfig(AppConfig):
default_auto_field = "django.db.models.BigAutoField"
name = "mindmaps"
+70
View File
@@ -0,0 +1,70 @@
# Generated by Django 4.2 on 2026-10-20 09:54
from django.db import migrations, models
class Migration(migrations.Migration):
initial = True
dependencies = []
operations = [
migrations.CreateModel(
name="MindMap",
fields=[
(
"id",
models.BigAutoField(
auto_created=True,
primary_key=True,
serialize=False,
verbose_name="ID",
),
),
("title", models.CharField(max_length=200, verbose_name="标题")),
(
"slug",
models.SlugField(
blank=True, max_length=200, unique=True, verbose_name="别名"
),
),
(
"description",
models.TextField(
blank=True, help_text="列表页显示", verbose_name="简介"
),
),
(
"xmind_file",
models.FileField(
blank=True,
upload_to="mindmaps/xmind/%Y/%m/",
verbose_name="XMind 文件",
),
),
(
"mindmap_data",
models.JSONField(
blank=True,
default=dict,
help_text="由 xmind 解析后生成的前端渲染数据",
verbose_name="脑图数据",
),
),
(
"created_at",
models.DateTimeField(auto_now_add=True, verbose_name="创建时间"),
),
(
"updated_at",
models.DateTimeField(auto_now=True, verbose_name="更新时间"),
),
],
options={
"verbose_name": "脑图",
"verbose_name_plural": "脑图",
"ordering": ["-created_at"],
},
),
]
View File
+68
View File
@@ -0,0 +1,68 @@
import json
from django.db import models
from django.utils.text import slugify
from django.urls import reverse # ← 加这个 import
from xmindparser import xmind_to_dict
def xmind_to_mindmap_format(parsed_data):
"""
把 xmindparser 输出转成 simple-mind-map 标准格式
"""
if not parsed_data:
raise ValueError("XMind 文件内容为空")
sheet = parsed_data[0]
if "topic" not in sheet:
raise ValueError("XMind 第一个画布没有根节点")
def conv(topic):
"""递归转换节点"""
node = {
"data": {
"text": topic.get("title", "(无标题)")
}
}
subs = topic.get("topics") or []
if subs:
node["children"] = [conv(t) for t in subs]
return node
return conv(sheet["topic"])
class MindMap(models.Model):
title = models.CharField("标题", max_length=200)
slug = models.SlugField("别名", max_length=200, unique=True, blank=True)
description = models.TextField("简介", blank=True, help_text="列表页显示")
xmind_file = models.FileField(
"XMind 文件", upload_to="mindmaps/xmind/%Y/%m/", blank=True
)
mindmap_data = models.JSONField(
"脑图数据", default=dict, blank=True,
help_text="由 xmind 解析后生成的前端渲染数据"
)
created_at = models.DateTimeField("创建时间", auto_now_add=True)
updated_at = models.DateTimeField("更新时间", auto_now=True)
class Meta:
ordering = ["-created_at"]
verbose_name = "脑图"
verbose_name_plural = "脑图"
def __str__(self):
return self.title
def get_absolute_url(self):
return reverse("mindmaps:detail", args=[self.slug])
def save(self, *args, **kwargs):
if not self.slug:
self.slug = slugify(self.title) or f"mindmap-{self.pk or 'new'}"
super().save(*args, **kwargs)
def parse_and_store(self, file_path):
"""解析 xmind 文件并存储为前端可用格式"""
parsed = xmind_to_dict(file_path)
self.mindmap_data = xmind_to_mindmap_format(parsed)
self.save()
+3
View File
@@ -0,0 +1,3 @@
from django.test import TestCase
# Create your tests here.
+8
View File
@@ -0,0 +1,8 @@
from django.urls import path
from . import views
app_name = "mindmaps"
urlpatterns = [
path("", views.mindmap_list, name="list"),
path("<slug:slug>/", views.mindmap_detail, name="detail"),
]
+14
View File
@@ -0,0 +1,14 @@
from django.shortcuts import render, get_object_or_404
from .models import MindMap
def mindmap_list(request):
mindmaps = MindMap.objects.filter(mindmap_data__isnull=False).exclude(
mindmap_data={}
)
return render(request, "mindlist.html", {"mindmaps": mindmaps})
def mindmap_detail(request, slug):
mm = get_object_or_404(MindMap, slug=slug)
return render(request, "minddetail.html", {"mindmap": mm})