修改博客框架以支持文章分类

Posted by kalos Aner on November 11, 2024

修改博客框架以支持文章分类

问题:

我使用黄玄的博客框架搭建了一个个人博客网站,想修改一下网站样式可以把两个不同文件夹里的blog(markdown格式)渲染到两个不同的网页。

方法:

基本思路就是修改解析 md 文件的代码,但是这个博客框架解析 md 文件是使用 Jekyll 解析的,所以仅需要修改配置就行。

1、在 _config.yml 中添加以下代码:

1
2
3
collections:
  academic:
    output: true   # 确保生成 HTML 文件

academic 是我定义的变量,这样 Jekyll 就会解析 _academic 文件夹中的文件。

2、把 index.html 文件复制一份然后修改头部信息如下:

1
2
3
4
5
6
---
title: Master
layout: page
description: "「白日依山尽,黄河入海流。 欲穷千里目,更上一层楼。」"
header-img: "img/post-bg-universe.jpg"
---

layout 是重点,必须设置为page,title 可以随便设置,会显示在导航栏。

网页正文代码修改成如下代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
{% assign paginator = site.academic | sort: 'date' | reverse %}

{% for post in paginator %}
<div class="post-preview">
 <a href="{{ post.url | prepend: site.baseurl }}">
     <h2 class="post-title">
         {{ post.title }}
     </h2>
     {% if post.subtitle %}
     <h3 class="post-subtitle">
         {{ post.subtitle }}
     </h3>
     {% endif %}
     <div class="post-content-preview">
         {% if post.lang == 'en' %}
             {{ post.content | strip_html | truncate:300 }}
         {% else %}
             {{ post.content | strip_html | truncate:200 }}
         {% endif %}
     </div>
 </a>
 <p class="post-meta">
     Posted by {% if post.author %}{{ post.author }}{% else %}{{ site.title }}{% endif %} on {{ post.date | date: "%B %-d, %Y" }}
 </p>
</div>
<hr>
{% endfor %}

<!-- Pager -->
{% if paginator.total_pages > 1 %}
<ul class="pager">
 {% if paginator.previous_page %}
 <li class="previous">
     <a href="{{ paginator.previous_page_path | prepend: site.baseurl | replace: '//', '/' }}">&larr; Newer Posts</a>
 </li>
 {% endif %}
 {% if paginator.next_page %}
 <li class="next">
     <a href="{{ paginator.next_page_path | prepend: site.baseurl | replace: '//', '/' }}">Older Posts &rarr;</a>
 </li>
 {% endif %}
</ul>
{% endif %}

其中,site.academic 需要创建对应的文件夹为 _academic

然后在 Rakefile 文件中添加如下代码,这段代码是为了创建完整的文章框架,例如日期、格式等。其中 academic 需要替换成对应的目录。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# Usage: rake post title="A Title" subtitle="A sub title"
desc "Begin a new post in #{CONFIG['academic']}"
task :academic do
  abort("rake aborted: '#{CONFIG['academic']}' directory not found.") unless FileTest.directory?(CONFIG['academic'])
  title = ENV["title"] || "new-post"
  subtitle = ENV["subtitle"] || "This is a subtitle"
  slug = title.downcase.strip.gsub(' ', '-').gsub(/[^\w-]/, '')
  begin
    date = (ENV['date'] ? Time.parse(ENV['date']) : Time.now).strftime('%Y-%m-%d')
  rescue Exception => e
    puts "Error - date format must be YYYY-MM-DD, please check you typed it correctly!"
    exit -1
  end
  filename = File.join(CONFIG['academic'], "#{date}-#{slug}.#{CONFIG['post_ext']}")
  if File.exist?(filename)
    abort("rake aborted!") if ask("#{filename} already exists. Do you want to overwrite?", ['y', 'n']) == 'n'
  end

  puts "Creating new post: #{filename}"
  open(filename, 'w') do |academic|
    academic.puts "---"
    academic.puts "layout: academic"
    academic.puts "title: \"#{title.gsub(/-/,' ')}\""
    academic.puts "subtitle: \"#{subtitle.gsub(/-/,' ')}\""
    academic.puts "date: #{date}"
    academic.puts "author: \"Kalos Aner\""
    academic.puts "header-img: \"img/post-bg-2015.jpg\""
    academic.puts "tags: []"
    academic.puts "---"
  end
end # task :academic

最后为了支持搜索功能,在 search.json 中添加如下代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137

    {
      "title"    : "联邦学习应用场景",
      "subtitle" : "",
      "tags"     : "",
      "url"      : "/academic/2024-11-10-%E8%81%94%E9%82%A6%E5%AD%A6%E4%B9%A0%E5%BA%94%E7%94%A8%E5%9C%BA%E6%99%AF/",
      "date"     : "2024-11-10 00:00:00 +0000"
    } ,
  
    {
      "title"    : "PyTorch、TorchVision、CUDA Toolkit 和 Python 版本的对应关系",
      "subtitle" : "",
      "tags"     : "",
      "url"      : "/academic/2024-11-29-PyTorch%E3%80%81TorchVision%E3%80%81CUDA%20Toolkit%20%E5%92%8C%20Python%20%E7%89%88%E6%9C%AC%E7%9A%84%E5%AF%B9%E5%BA%94%E5%85%B3%E7%B3%BB/",
      "date"     : "2024-11-29 00:00:00 +0000"
    } ,
  
    {
      "title"    : "Conda创建虚拟环境并安装Pytorch",
      "subtitle" : "",
      "tags"     : "",
      "url"      : "/academic/2024-11-30-Conda%E5%88%9B%E5%BB%BA%E8%99%9A%E6%8B%9F%E7%8E%AF%E5%A2%83%E5%B9%B6%E5%AE%89%E8%A3%85Pytorch/",
      "date"     : "2024-11-30 00:00:00 +0000"
    } ,
  
    {
      "title"    : "Python运行requirements.txt 文件安装包",
      "subtitle" : "",
      "tags"     : "",
      "url"      : "/academic/2024-12-01-Python%E8%BF%90%E8%A1%8Crequirements.txt%20%E6%96%87%E4%BB%B6%E5%AE%89%E8%A3%85%E5%8C%85/",
      "date"     : "2024-12-01 00:00:00 +0000"
    } ,
  
    {
      "title"    : "联邦学习之基础用法",
      "subtitle" : "",
      "tags"     : "",
      "url"      : "/academic/2024-12-02-%E8%81%94%E9%82%A6%E5%AD%A6%E4%B9%A0%E4%B9%8B%E5%9F%BA%E7%A1%80%E7%94%A8%E6%B3%95/",
      "date"     : "2024-12-02 00:00:00 +0000"
    } ,
  
    {
      "title"    : "联邦学习之分布训练",
      "subtitle" : "",
      "tags"     : "",
      "url"      : "/academic/2024-12-03-%E8%81%94%E9%82%A6%E5%AD%A6%E4%B9%A0%E4%B9%8B%E5%88%86%E5%B8%83%E8%AE%AD%E7%BB%83/",
      "date"     : "2024-12-03 00:00:00 +0000"
    } ,
  
    {
      "title"    : "联邦学习之自定义化",
      "subtitle" : "",
      "tags"     : "",
      "url"      : "/academic/2024-12-04-%E8%81%94%E9%82%A6%E5%AD%A6%E4%B9%A0%E4%B9%8B%E8%87%AA%E5%AE%9A%E4%B9%89%E5%8C%96/",
      "date"     : "2024-12-04 00:00:00 +0000"
    } ,
  
    {
      "title"    : "联邦学习之隐私增强",
      "subtitle" : "",
      "tags"     : "",
      "url"      : "/academic/2024-12-05-%E8%81%94%E9%82%A6%E5%AD%A6%E4%B9%A0%E4%B9%8B%E9%9A%90%E7%A7%81%E5%A2%9E%E5%BC%BA/",
      "date"     : "2024-12-05 00:00:00 +0000"
    } ,
  
    {
      "title"    : "联邦学习之带宽需求",
      "subtitle" : "",
      "tags"     : "",
      "url"      : "/academic/2024-12-06-%E8%81%94%E9%82%A6%E5%AD%A6%E4%B9%A0%E4%B9%8B%E5%B8%A6%E5%AE%BD%E9%9C%80%E6%B1%82/",
      "date"     : "2024-12-06 00:00:00 +0000"
    } ,
  
    {
      "title"    : "联邦学习中的挑战与解决方案",
      "subtitle" : "",
      "tags"     : "",
      "url"      : "/academic/2024-12-16-%E8%81%94%E9%82%A6%E5%AD%A6%E4%B9%A0%E4%B8%AD%E7%9A%84%E6%8C%91%E6%88%98%E4%B8%8E%E8%A7%A3%E5%86%B3%E6%96%B9%E6%A1%88/",
      "date"     : "2024-12-16 00:00:00 +0000"
    } ,
  
    {
      "title"    : "联邦学习之联邦LLM微调",
      "subtitle" : "",
      "tags"     : "",
      "url"      : "/academic/2024-12-28-%E8%81%94%E9%82%A6%E5%AD%A6%E4%B9%A0%E4%B9%8B%E8%81%94%E9%82%A6LLM%E5%BE%AE%E8%B0%83/",
      "date"     : "2024-12-28 00:00:00 +0000"
    } ,
  
    {
      "title"    : "卷积神经网络",
      "subtitle" : "",
      "tags"     : "",
      "url"      : "/academic/2025-01-04-%E5%8D%B7%E7%A7%AF%E7%A5%9E%E7%BB%8F%E7%BD%91%E7%BB%9C/",
      "date"     : "2025-01-04 00:00:00 +0000"
    } ,
  
    {
      "title"    : "【综述】一文读懂卷积神经网络(CNN)",
      "subtitle" : "",
      "tags"     : "",
      "url"      : "/academic/2025-01-07-%E3%80%90%E7%BB%BC%E8%BF%B0%E3%80%91%E4%B8%80%E6%96%87%E8%AF%BB%E6%87%82%E5%8D%B7%E7%A7%AF%E7%A5%9E%E7%BB%8F%E7%BD%91%E7%BB%9C(CNN)/",
      "date"     : "2025-01-07 00:00:00 +0000"
    } ,
  
    {
      "title"    : "数据异质性",
      "subtitle" : "",
      "tags"     : "",
      "url"      : "/academic/2025-02-21-%E6%95%B0%E6%8D%AE%E5%BC%82%E8%B4%A8%E6%80%A7/",
      "date"     : "2025-02-21 00:00:00 +0000"
    } ,
  
    {
      "title"    : "蒸馏学习流程",
      "subtitle" : "",
      "tags"     : "",
      "url"      : "/academic/2025-02-22-%E8%92%B8%E9%A6%8F%E5%AD%A6%E4%B9%A0%E6%B5%81%E7%A8%8B/",
      "date"     : "2025-02-22 00:00:00 +0000"
    } ,
  
    {
      "title"    : "论文精读和写作逻辑",
      "subtitle" : "",
      "tags"     : "",
      "url"      : "/academic/2025-03-04-%E8%AE%BA%E6%96%87%E7%B2%BE%E8%AF%BB%E5%92%8C%E5%86%99%E4%BD%9C%E9%80%BB%E8%BE%91/",
      "date"     : "2025-03-04 00:00:00 +0000"
    } ,
  
    {
      "title"    : "如何撰写计算机领域的论文",
      "subtitle" : "",
      "tags"     : "",
      "url"      : "/academic/2025-10-21-%E5%A6%82%E4%BD%95%E6%92%B0%E5%86%99%E8%AE%A1%E7%AE%97%E6%9C%BA%E9%A2%86%E5%9F%9F%E7%9A%84%E8%AE%BA%E6%96%87/",
      "date"     : "2025-10-21 00:00:00 +0000"
    } 
  

完结!