search
HomeWeb Front-endHTML Tutorial使用 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 就是一根管道, 把你书写的内容以合适的方式展现出来便是它的作用了

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
HTML vs. CSS vs. JavaScript: A Comparative OverviewHTML vs. CSS vs. JavaScript: A Comparative OverviewApr 16, 2025 am 12:04 AM

The roles of HTML, CSS and JavaScript in web development are: HTML is responsible for content structure, CSS is responsible for style, and JavaScript is responsible for dynamic behavior. 1. HTML defines the web page structure and content through tags to ensure semantics. 2. CSS controls the web page style through selectors and attributes to make it beautiful and easy to read. 3. JavaScript controls web page behavior through scripts to achieve dynamic and interactive functions.

HTML: Is It a Programming Language or Something Else?HTML: Is It a Programming Language or Something Else?Apr 15, 2025 am 12:13 AM

HTMLisnotaprogramminglanguage;itisamarkuplanguage.1)HTMLstructuresandformatswebcontentusingtags.2)ItworkswithCSSforstylingandJavaScriptforinteractivity,enhancingwebdevelopment.

HTML: Building the Structure of Web PagesHTML: Building the Structure of Web PagesApr 14, 2025 am 12:14 AM

HTML is the cornerstone of building web page structure. 1. HTML defines the content structure and semantics, and uses, etc. tags. 2. Provide semantic markers, such as, etc., to improve SEO effect. 3. To realize user interaction through tags, pay attention to form verification. 4. Use advanced elements such as, combined with JavaScript to achieve dynamic effects. 5. Common errors include unclosed labels and unquoted attribute values, and verification tools are required. 6. Optimization strategies include reducing HTTP requests, compressing HTML, using semantic tags, etc.

From Text to Websites: The Power of HTMLFrom Text to Websites: The Power of HTMLApr 13, 2025 am 12:07 AM

HTML is a language used to build web pages, defining web page structure and content through tags and attributes. 1) HTML organizes document structure through tags, such as,. 2) The browser parses HTML to build the DOM and renders the web page. 3) New features of HTML5, such as, enhance multimedia functions. 4) Common errors include unclosed labels and unquoted attribute values. 5) Optimization suggestions include using semantic tags and reducing file size.

Understanding HTML, CSS, and JavaScript: A Beginner's GuideUnderstanding HTML, CSS, and JavaScript: A Beginner's GuideApr 12, 2025 am 12:02 AM

WebdevelopmentreliesonHTML,CSS,andJavaScript:1)HTMLstructurescontent,2)CSSstylesit,and3)JavaScriptaddsinteractivity,formingthebasisofmodernwebexperiences.

The Role of HTML: Structuring Web ContentThe Role of HTML: Structuring Web ContentApr 11, 2025 am 12:12 AM

The role of HTML is to define the structure and content of a web page through tags and attributes. 1. HTML organizes content through tags such as , making it easy to read and understand. 2. Use semantic tags such as, etc. to enhance accessibility and SEO. 3. Optimizing HTML code can improve web page loading speed and user experience.

HTML and Code: A Closer Look at the TerminologyHTML and Code: A Closer Look at the TerminologyApr 10, 2025 am 09:28 AM

HTMLisaspecifictypeofcodefocusedonstructuringwebcontent,while"code"broadlyincludeslanguageslikeJavaScriptandPythonforfunctionality.1)HTMLdefineswebpagestructureusingtags.2)"Code"encompassesawiderrangeoflanguagesforlogicandinteract

HTML, CSS, and JavaScript: Essential Tools for Web DevelopersHTML, CSS, and JavaScript: Essential Tools for Web DevelopersApr 09, 2025 am 12:12 AM

HTML, CSS and JavaScript are the three pillars of web development. 1. HTML defines the web page structure and uses tags such as, etc. 2. CSS controls the web page style, using selectors and attributes such as color, font-size, etc. 3. JavaScript realizes dynamic effects and interaction, through event monitoring and DOM operations.

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Chat Commands and How to Use Them
1 months agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor