使用 hakyll 构建静态站点_html/css_WEB-ITnose
什么是 hakyll
hakyll 是类似于 jekyll 的一个用来搭建静态站点的工具
hakyll 与 jekyll 的不同
hakyll 与其他大多数同类工具相比 拥有非常大的灵活性,github 默认的静态网页生成器 jekyll 是一个遵循约定大于配置的工具, 使用 jekyll 就必须遵循它的约定.比如博文需要以 yy-mm-dd 开头的文件名形式存放在 ~posts~/ 文件夹下.而 hakyll 就拥有更大的灵活性, hakyll 的哲学在于, 用户书写内容, hakyll 负责把这些 内容转化成不同的展现形式.这样生成的站点形式就不局限于博客, 也可以包含 slides 或者 pdf 之类的内容.
hakyll 的安装与基本使用
hakyll 使用非常高大上的 haskell 语言编写, 所以要使用 hakyll 就需要haskell 环境. 在 mac 下的安装非常简单
brew install ghc cabal-install
安装完成后记得把 \$HOME/.cabal/bin 加入到 \$PATH.然后就可以新建一个站点了
$ hakyll-init my-site $ cd my-site $ ghc --make -threaded site.hs $ ./site build $ ./site watch
打开 http://localhost:8000/ 就能看到默认生成的站点了, 如果你只想像使用 jekyll 一样使用 hakyll 的话, 到这里就够了, 同样在 posts 文件夹下书写你的博文就可以了
hakyll 的高级功能
如果你想领略 hakyll 的强大而灵活的配置, 那么请往下看
hakyll 的工作原理
如果你照着上面的方式安装并初始化了 hakyll 的话,你的站点文件结构就会类似于
. ├── README.md ├── about.rst ├── contact.markdown ├── css │ └── default.scss ├── images │ └── haskell-logo.png ├── index.html ├── posts │ └── use-hakyll-to-build-static-website.org ├── site ├── site.hi ├── site.hs ├── site.o └── templates ├── archive.html ├── default.html ├── post-list.html └── post.html
这样, 其实除了 site 开头的几个文件以外 (site 是编译好的二进制文件,有100mb 左右大小, 记得不要纳入版本控制!), 其他都是静态文件,且结构是可以随便定义的。其中 site.hs 可以认为是 hakyll 的配置文件,打开它就会看到一大堆看(高)不(大)懂(上)的 haskell 代码。
稍微观察一下就会发现, 其中有三个关键词
- match
- route
- compile
match 就是匹配, 后面的文件名是你站点目录下的源文件, 比如 'css/*' 就指代css 文件夹下的所有文件
route 和我们通常程序中所有的路由有点不同, 指的是 match到的文件会被放置在编译好的站点的位置(默认是 ~site~ 文件夹)
compile 就像是一个管道, 一个资源, 从 match (头) 到 route(尾)的过程中需要做的转换工作, 比如 markdown -> html, 或者压缩混淆等
这样一来 hakyll 的结构就很清楚了, 用户提供原始的数据, 然后 hakyll就去编译他们并且放置到正确的位置
下面举几个实际的例子, 来表现一下 hakyll 的强大
sass 支持
-- 使用 compass 来处理 scss 样式 match (fromList ["sass/main.scss", "sass/blog.scss"]) $ do route $ setExtension "css" compile $ getResourceString >>= withItemBody (unixFilter "sass" ["-s", "--scss", "-I", "sass/"]) >>= return . fmap compressCss
创建不存在的页面
create ["archive.html"] $ do route idRoute compile $ do posts <- recentFirst =<< loadAll "posts/*" let archiveCtx = listField "posts" postCtx (return posts) `mappend` constField "title" "Archives" `mappend` defaultContext makeItem "" >>= loadAndApplyTemplate "templates/archive.html" archiveCtx >>= loadAndApplyTemplate "templates/default.html" archiveCtx >>= relativizeUrls
这个例子中 archive.html 这个页面原本是不存在的,里面的内容都是动态获取的, 它读取了 posts/ 下的所有文件 读取他们的 title属性, 然后列出来, 这和 jekyll 的读取一个对象的属性并循环出来有点不同,毕竟 haskell 是函数式语言
幻灯片
match "slides/*" $ do route $ setExtension "html" compile $ getResourceString >>= withItemBody (unixFilter "pandoc" ["-s", "--mathml", "-i", "-t", "dzslides"])
使用强大的 pandoc, 直接就把 markdown 文件变成 slides 了.
真实的例子
这里 列出了很多使用 hakyll的网站, 都有源码在 github 上, 可供参考
总结
hakyll 就是一根管道, 把你书写的内容以合适的方式展现出来便是它的作用了

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



The article discusses the HTML <progress> element, its purpose, styling, and differences from the <meter> element. The main focus is on using <progress> for task completion and <meter> for stati

The article discusses the HTML <datalist> element, which enhances forms by providing autocomplete suggestions, improving user experience and reducing errors.Character count: 159

HTML is suitable for beginners because it is simple and easy to learn and can quickly see results. 1) The learning curve of HTML is smooth and easy to get started. 2) Just master the basic tags to start creating web pages. 3) High flexibility and can be used in combination with CSS and JavaScript. 4) Rich learning resources and modern tools support the learning process.

The article discusses the HTML <meter> element, used for displaying scalar or fractional values within a range, and its common applications in web development. It differentiates <meter> from <progress> and ex

The article discusses the <iframe> tag's purpose in embedding external content into webpages, its common uses, security risks, and alternatives like object tags and APIs.

The article discusses the viewport meta tag, essential for responsive web design on mobile devices. It explains how proper use ensures optimal content scaling and user interaction, while misuse can lead to design and accessibility issues.

HTML defines the web structure, CSS is responsible for style and layout, and JavaScript gives dynamic interaction. The three perform their duties in web development and jointly build a colorful website.

AnexampleofastartingtaginHTMLis,whichbeginsaparagraph.StartingtagsareessentialinHTMLastheyinitiateelements,definetheirtypes,andarecrucialforstructuringwebpagesandconstructingtheDOM.
