博文和页面的区别 articles: 博文,是指具体写的文章。
pages: 页面,一般指一些固定不变的页面,比如关于等
元数据 元数据可以进行一些搜索引擎的优化,包含博文的一些基础信息。
如果用restructuredtext来编写内容的话,可以像下面这样写元数据头
my super title##############:date: 2010-10-03 10:20:modified: 2010-10-04 18:40:tags: thats, awesome:category: yeah:slug: my-super-post:authors: alexis metaireau, conan doyle:summary: short version for index and feeds
多个标签(tags)和多个作者(authors)可以用逗号分割。
:tags: pelican, publishing tool; pelican, bird:authors: metaireau, alexis; doyle, conan
pelican 还支持在restructuredtext中写abbr标签,使用方法如下
this will be turned into :abbr:`html (hypertext markup language)`.
同样可以使用markdown语法(以.md,.markdown,.mkd,.mdown结尾的文件),markdown生成html文件需要markdown的模块支持,通过命令pip install markdown安装。
pelican 还支持markdown的扩展语法,markdown的扩展没有包含在markdown的模块下面,需要通过md_extensions进行配置。
用markdown写文章的基本格式如下
title: my super titledate: 2010-12-03 10:20modified: 2010-12-05 19:30category: pythontags: pelican, publishingslug: my-super-postauthors: alexis metaireau, conan doylesummary: short version for index and feedsthis is the content of my super blog post.
还可以用插件支持更多的格式,这个以后研究了。
pelican也能直接解析html和htm文件,解析html文件就比较直接了,跟复制没啥区别。
my super title this is the content of my super blog post.
温馨提示:除了标题之外,其他元数据不是必填项,如果没有指定时间且default_date设置为'fs',pelican就会根据文件的时间戳来确定时间,文章的分类可以根据子目录的名称来确定,例如:python/foobar/myfoobar.rst的分类就行foobar,如果不希望子目录的名称不作为分类名称,可以通过设置use_folder_as_category为false。
modified是值最后编辑文章的时间,默认和date相同。另外,可以通过modified进行rss推送,可能在刚写文章的时候rss已经推送了一遍,后来进行了修改,就可以通过modified进行再次推送。
authors用来表示多个作者,如果只有一个作者,可以使用author。
如果希望文章的摘要有一个确定的长度,可以通过设置summary_max_length,pelican会从文章的开头进行截取制定的长度作为摘要。
也可以通过设置filename_metadata文件名称来提取元数据,默认情况下,filename_metadata只是提出文件名上面的的日期信息,如果希望提出文件名中的除日期信息之外的内容为slug,可以将filename_metadata设置为:'(?p\d{4}-\d{2}-\d{2})_(?p.*)'
需要注意的是,在文件头编写的元数据信息会覆盖通过文件名提取出来的信息。
页面 如果在content下面创建了目录pages,这个目录下所有的文件都将被生成静态页面,比如about和contact页面。
可以通过设置display_pages_on_menu来控制页面是否在导航栏显示,默认情况是true
如果希望页面不在导航条上显示,可以在页面的元数据里面添加:status: hidden属性,对于404页面很有效果,因为404静态页面我们是不希望在导航条上面显示的。
引用内部的链接 从pelican3.1开始,可以通过content的目录结构在不同的文章之间进行链接,这样可以很方便引用操作了。
引用content里面的的内容的语法是:{filename}path/to/file,{fielname}是必填内容,可以同时支持linux和windows
假设content的目录结构是这样的
website/├── content│ ├── category/│ │ └── article1.rst│ ├── article2.md│ └── pages│ └── about.md└── pelican.conf.py
在article1.rst中链接代码
the first article#################:date: 2012-12-01 10:02see below intra-site link examples in restructuredtext format.`a link relative to the current file `_`a link relative to the content root `_
在article2.md
title: the second articledate: 2012-12-01 10:02see below intra-site link examples in markdown format.[a link relative to the current file]({filename}category/article1.rst)[a link relative to the content root]({filename}/category/article1.rst)
链接静态文件 有时候我们需要引用一些除了博客和页面以为的资源,比如图片,pdf文件等,这就需要在pelicanconf.py中配置static_paths属性,pelican默认设置content下的images为图片资源,其他的资源需要进行配置。
假设content的目录结构如下
content├── images│ └── han.jpg├── pdfs│ └── menu.pdf└── pages └── test.md
在test.md中引用
[our menu]({filename}/pdfs/menu.pdf)
pelicanconf.py新增配置
static_paths = ['images', 'pdfs']
生产页面时,pelican会将han.jpg复制到output/images/han.jpg,menu.pdf复制到output/pdfs/menu.pdf,同时将链接适配到test.md
附件 从pelican 3.5开始,通过{attach}path/to/file可以给文章或者页面添加附件,语法和{filename}类似,但是附件会被复制到文章或者页面的同一个目录下面。这种方式只有在static_paths设置了该目录才能使用这个标签。
假设content的目录结构如下
content├── blog│ ├── icons│ │ └── icon.png│ ├── photo.jpg│ └── testpost.md└── downloads └── archive.zip
pelicanconf.py的配置如下
path = 'content'static_paths = ['blog', 'downloads']article_paths = ['blog']article_save_as = '{date:%y}/{slug}.html'article_url = '{date:%y}/{slug}.html'
testpost.md的内容如下
title: test postcategory: testdate: 2014-10-31[downloadable file]({attach}/downloads/archive.zip)
生成之后的output的目录结构如下
output└── 2014 ├── archive.zip ├── icons │ └── icon.png ├── photo.jpg └── test-post.html
如果使用{attach}引用文章所在目录之外的资源,在生成文件的时候会将这个资源复制到文章所在的目录下。
如果资源被多篇文章通过{attach}引用,在第一个被引用的地方pelican会以attach的方式来处理这个资源,在之后的引用的地方会按照{filename}的方式来链接这个资源,也就是说,在同一个目录下同一个资源被多次引用,pelican只复制一次这个资源到该目录下面。
请谨慎使用多篇文章引用同一个资源,因为pelican对{attach}资源处理的顺序是不确定的,就可能导致资源复制到不确定的目录下面,只有当所有的引用同一个资源的文章在同一个目录下面的时候才是安全的。
链接标签和分类 链接标签和分类的语法为{tag}tagname和{category}foobar
语法高亮 pelican为代码块提供了语法高亮功能,对于restructuredtext用户,可以使用code-block标记,假设我们要高亮python的代码。
.. code-block:: python print(pelican is a static site generator.)
对于markdown用户,按照以下语法
there are two ways to specify the identifier: :::python print(the triple-colon syntax will *not* show line numbers.)to display line numbers, use a path-less shebang instead of colons: #!python print(the path-less shebang syntax *will* show line numbers.)
如果是普通的代码,分不出具体语言的,可以按照如下语法格式,用一对```包起代码
发布草稿 有时候我们希望把我们的草稿先发布出来给部分朋友看看,不让文章在首页和分类列表里面显示出来,我们可以在文章中添加status: draft属性。
如果将所有刚写好的文章都作为草搞件发布的话,可以在配置文件中配置default_metadata属性
default_metadata = { 'status': 'draft',}
这样文章在发布的时候默认就是草稿,通过修改文章的配置为status: published将文章正式发布出来。
文章同步发布在我的个人站点: http://www.xinxingzhao.com/blog/2016/03/25/pelican-writing-content.html