SitePipe 使用教程
1. 项目目录结构及介绍
SitePipe 是一个简单的静态网站生成器,适用于创建博客、个人网站等。项目的主要目录结构如下:
site/
├── templates/ # 存放模板文件
│ ├── post.html
│ └── index.html
└── posts/ # 存放 Markdown 格式的文章
└── article.md
templates/
目录:包含了网站的模板文件,index.html
是网站的首页模板,post.html
是文章页面的模板。posts/
目录:包含了所有的文章内容,以 Markdown 格式存储。
2. 项目的启动文件介绍
项目的启动文件是位于项目根目录下的 Main.hs
。这个文件负责初始化和运行 SitePipe。
module Main where
import SitePipe
main :: IO ()
main =
site $
do
-- 加载 posts 目录下的所有 Markdown 文章
posts <- resourceLoader markdownReader ["posts/*.md"]
-- 创建首页的上下文
let indexContext :: Value
indexContext = object ["posts" .= posts, "url" .= "/index.html"]
-- 通过模板输出首页和文章
writeTemplate "templates/index.html" [indexContext]
writeTemplate "templates/post.html" posts
在这个文件中,site
函数是 SitePipe 的入口点,它接受一个 IO 操作,这里是通过 do
表达式组织的几个操作:
- 使用
resourceLoader
加载posts/
目录下的所有 Markdown 文件,并通过markdownReader
将它们转换为 HTML。 - 创建一个包含文章列表和首页 URL 的上下文
indexContext
。 - 使用
writeTemplate
函数将上下文和文章通过模板输出为 HTML。
3. 项目的配置文件介绍
SitePipe 的配置主要通过 sitepipe.cabal
和 stack.yaml
文件来进行。
sitepipe.cabal
文件:这是 Haskell 包的配置文件,它定义了项目的依赖、编译选项和其他元数据。
name: sitepipe
version: 0.3.0.1
synopsis: A simple static site generator
description: SitePipe is a static site generator written in Haskell.
license: BSD3
author: Your Name
maintainer: your.email@example.com
category: Web
build-type: Custom
cabal-version: >=1.10
library
exposed-modules: SitePipe
build-depends: base >= 4.7 && < 5
, pandoc
, aeson
, filepath
, text
, containers
, mustache
, directory
, process
default-language: Haskell2010
other-extensions: OverloadedStrings
ghc-options: -Wall
stack.yaml
文件:这是 Stack 构建工具的配置文件,它指定了项目的依赖和构建参数。
resolver: lts-15.3
maintainer: your.email@example.com
extra-deps:
- pandoc-2.9.2.1
- aeson-1.5.6.0
- filepath-1.4.2.1
- text-1.2.4.0
- containers-0.6.2.1
- mustache-2.2.1
- directory-1.3.6.0
- process-1.6.13.0
这些配置文件在项目初始化时创建,通常不需要用户修改,除非需要定制特定的功能或者修复依赖问题。
创作声明:本文部分内容由AI辅助生成(AIGC),仅供参考