gwern.net 开源项目教程

gwern.net 开源项目教程

gwern.net Site infrastructure for gwern.net (CSS/JS/HS/images/icons). Custom Hakyll website with unique automatic link archiving, recursive tooltip popup UX, dark mode, and typography (sidenotes+dropcaps+admonitions+inflation-adjuster). gwern.net 项目地址: https://gitcode.com/gh_mirrors/gw/gwern.net

1. 项目目录结构及介绍

gwern.net 是一个基于 Hakyll 的静态网站生成器项目。项目目录结构如下:

gwern.net/
├── assets/        # 存储静态资源,如图片、CSS、JavaScript 等
│   ├── css/
│   ├── font/
│   ├── img/
│   └── js/
├── layouts/       # 网站布局和模板文件
├── posts/         # 文章和页面内容
├── static/        # 其他静态文件,如站点图标等
├── templates/     # Hakyll 模板文件
├── .gitignore     # 指定 Git 忽略的文件和目录
├── Hakyll.hs      # Hakyll 配置文件
├── build.sh       # 构建脚本
├── google-cse.html
└── google-search.html
  • assets/:包含所有静态资源,如 CSS 样式表、字体、图片和 JavaScript 文件。
  • layouts/:存放网站的布局和模板文件,用于生成页面的结构。
  • posts/:包含所有文章和页面的 Markdown 文件。
  • static/:存放其他静态文件,如站点图标等。
  • templates/:包含 Hakyll 模板文件,用于定义网站页面结构。
  • .gitignore:指定 Git 忽略的文件和目录,避免将不必要的文件提交到版本控制。
  • Hakyll.hs:Hakyll 配置文件,用于定义网站生成规则。
  • build.sh:构建脚本,用于编译网站。
  • google-cse.htmlgoogle-search.html:Google 搜索引擎优化相关文件。

2. 项目的启动文件介绍

项目的启动主要通过 build.sh 脚本实现。以下是 build.sh 脚本的内容:

#!/bin/bash

# 确保已安装 Hakyll
if ! command -v hakyll &> /dev/null
then
    echo "Hakyll 可执行文件未找到,请先安装 Hakyll。"
    exit
fi

# 构建网站
hakyll -c -o output

运行 build.sh 脚本时,会执行以下步骤:

  1. 检查系统中是否已安装 Hakyll 可执行文件。
  2. 使用 Hakyll 命令行工具,通过 -c 参数指定配置文件 Hakyll.hs,并生成网站到 output 目录。

3. 项目的配置文件介绍

Hakyll.hs 是项目的核心配置文件,用于定义网站生成的规则。以下是 Hakyll.hs 文件的主要部分:

import Hakyll

main :: IO ()
main = hakyllWith config $ do
    match "posts/*" $ do
        route   idRoute
        compile $ pandocCompiler
            >>= loadAndApplyTemplate "templates/default.html" defaultContext
            >>= relativizeUrls

    match "static/**" $ do
        route   idRoute
        compile copyFileCompiler

    match "templates/*" $ do
        route   idRoute
        compile templateBodyCompiler

    create ["about.md"] $ do
        route   idRoute
        compile $ pandocCompiler
            >>= loadAndApplyTemplate "templates/default.html" defaultContext
            >>= relativizeUrls

config :: Config
config = defaultConfig

在这个配置文件中:

  • 使用 hakyllWith 函数指定项目的配置 config
  • 通过 match 函数匹配不同的文件和目录,并定义它们的处理规则。
  • idRoute 用于保持文件的相对路径不变。
  • pandocCompiler 用于将 Markdown 文件编译成 HTML。
  • loadAndApplyTemplate 用于加载 HTML 模板,并将编译后的 Markdown 内容插入模板中。
  • copyFileCompiler 用于复制静态文件。
  • templateBodyCompiler 用于处理模板文件。

以上是 gwern.net 开源项目的目录结构、启动文件和配置文件的详细介绍。希望对您有所帮助!

gwern.net Site infrastructure for gwern.net (CSS/JS/HS/images/icons). Custom Hakyll website with unique automatic link archiving, recursive tooltip popup UX, dark mode, and typography (sidenotes+dropcaps+admonitions+inflation-adjuster). gwern.net 项目地址: https://gitcode.com/gh_mirrors/gw/gwern.net

创作声明:本文部分内容由AI辅助生成(AIGC),仅供参考

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

滑芯桢

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值