将生成器与 Harp 一起使用

一则或许对你有用的小广告

欢迎加入小哈的星球 ,你将获得:专属的项目实战 / 1v1 提问 / Java 学习路线 / 学习打卡 / 每月赠书 / 社群讨论

  • 新项目:《从零手撸:仿小红书(微服务架构)》 正在持续爆肝中,基于 Spring Cloud Alibaba + Spring Boot 3.x + JDK 17...点击查看项目介绍 ;
  • 《从零手撸:前后端分离博客项目(全栈开发)》 2 期已完结,演示链接: http://116.62.199.48/ ;

截止目前, 星球 内专栏累计输出 63w+ 字,讲解图 2808+ 张,还在持续爆肝中.. 后续还会上新更多项目,目标是将 Java 领域典型的项目都整一波,如秒杀系统, 在线商城, IM 即时通讯,权限管理,Spring Cloud Alibaba 微服务等等,已有 2200+ 小伙伴加入学习 ,欢迎点击围观

我之前在 Harp Jekyll 上写过(并介绍过)博客。总的来说,我认为 Harp 使用起来更简单,但 Jekyll 更强大。 Jekyll 超越 Harp 的方法之一是使用 生成器 。这是您可以编写的脚本,可在生成阶段自动创建文件。几个月前我写了一篇关于这个的博客( 我使用 Jekyll 的经历 ),但如果你不想阅读整篇博文,让我解释一下生成器可以解决什么问题。

大多数静态站点生成器在您站点的模板和最终页面之间具有一对一的映射。例如,我可能有 about.handlebars,它在生成网站时映射到 about.html。您的生成器可能会通过获取 about.handlebars、layout.whatever 和各种包含来创建 about.html,但是您至少有一个文件与一个 URL 相关联。

除少数情况外,这工作正常。类别就是一个很好的例子。如果您想在静态站点中表示类别,则需要为每个类别创建一个文件,例如:bluemix.handlebars、phonegap.handlebars、kittens.handlebars。您可能会在每个项目中包含完全相同的代码(或者更好的是,使用包含),但关键是您需要为每个项目创建一个物理文件。

这并不可怕,老实说,您可能不想随意更改类别(或标签或主题等),但记住必须更新这些文件有点痛苦.正如我在另一篇博文中所写,Jekyll 通过让您使用生成器来解决这个问题。我能够使用 Ruby 查看我的数据并即时创建类别页面。

我最近在考虑关于 Harp 的问题,我突然想到有一种相当简单的方法可以让我获得相同的行为。我倾向于从 Web 应用程序/站点的角度来考虑 Node.js,但您也可以将 Node.js 用于简单的脚本。是的,其他人都知道这一点,我也知道这一点,只是我不经常 去想 它。因此,鉴于我使用的是 Harp 并且我的数据在 JSON 文件中可用,所以我没有理由不能使用简单的 Node.js 脚本来完成 Jekyll 的生成器所做的许多相同的事情。它不会是自动的,换句话说,我必须手动运行它,但我可以将它作为构建脚本(Grunt、Gulp 等)的一部分,这样我就可以自动化整个过程。

这是一个真实的例子。在我的测试中,我正在研究 CFLib 的 Harp 版本。它有我编码成 JSON 的各种类别:


 {
"CMFLLib":{
"desc":"A library that mimics CFML tags. This allows for CFML tags inside of cfscript. This is primarily a CFMX only library."
},
"DataManipulationLib":{
"desc":"DataManipulationLib is a ColdFusion UDF library containing data manipulation functions. This is a general purpose data \"munging\" type library."
},
"DatabaseLib":{
"desc":"A library containing database specific functions (written in CFML), organized by DB platform."
},
"DateLib":{
"desc":"This library is for date/time related function. Specifically, items that work on date or time based objects."
},
"FileSysLib":{
"desc":"FileSysLib is a ColdFusion UDF library containing functions that interact with the file system at various levels. These levels typically include drives, directories, and files."
},
"FinancialLib":{
"desc":"This library contains a set of UDFs that support financial type operations, including things like loan and interest calculations."
},
"MathLib":{
"desc":"A set of mathematical functions. This includes geometry, trig, statistical, and general math functions. These functions typically perform calculations."
},
"NetLib":{
"desc":"A library for Internet related UDFs. This library includes UDFs that retrieve pages from web servers, translate host addresses, and other operations related to Internet operations."
},
"ScienceLib":{
"desc":"A set of UDFs dedicated to scientific equations. This includes weather, astronomy, and other science fields."
},
"SecurityLib":{
"desc":"A set of security related functions."
},
"StrLib":{
"desc":"StrLib is a ColdFusion UDF library containing string (parsing, formatting) functions not included in the core CFML language. These functions typically will translate/verify strings."
},
"UtilityLib":{
"desc":"A set of utility functions for dealing with miscellaneous tasks such as state management, etc."
}
}

我在首页上使用这些数据来动态列出库。但我也可以在 Node.js 脚本中使用它:


 {
"CMFLLib":{
"desc":"A library that mimics CFML tags. This allows for CFML tags inside of cfscript. This is primarily a CFMX only library."
},
"DataManipulationLib":{
"desc":"DataManipulationLib is a ColdFusion UDF library containing data manipulation functions. This is a general purpose data \"munging\" type library."
},
"DatabaseLib":{
"desc":"A library containing database specific functions (written in CFML), organized by DB platform."
},
"DateLib":{
"desc":"This library is for date/time related function. Specifically, items that work on date or time based objects."
},
"FileSysLib":{
"desc":"FileSysLib is a ColdFusion UDF library containing functions that interact with the file system at various levels. These levels typically include drives, directories, and files."
},
"FinancialLib":{
"desc":"This library contains a set of UDFs that support financial type operations, including things like loan and interest calculations."
},
"MathLib":{
"desc":"A set of mathematical functions. This includes geometry, trig, statistical, and general math functions. These functions typically perform calculations."
},
"NetLib":{
"desc":"A library for Internet related UDFs. This library includes UDFs that retrieve pages from web servers, translate host addresses, and other operations related to Internet operations."
},
"ScienceLib":{
"desc":"A set of UDFs dedicated to scientific equations. This includes weather, astronomy, and other science fields."
},
"SecurityLib":{
"desc":"A set of security related functions."
},
"StrLib":{
"desc":"StrLib is a ColdFusion UDF library containing string (parsing, formatting) functions not included in the core CFML language. These functions typically will translate/verify strings."
},
"UtilityLib":{
"desc":"A set of utility functions for dealing with miscellaneous tasks such as state management, etc."
}
}

所以所有这些都是在我的 JSON 中读取的,读取我构建的用于表示特定图书馆页面的模板,然后将其写在物理文件中,以便 Harp 可以在生成站点时使用它。就我而言,我的模板只是一个包含:


 {
"CMFLLib":{
"desc":"A library that mimics CFML tags. This allows for CFML tags inside of cfscript. This is primarily a CFMX only library."
},
"DataManipulationLib":{
"desc":"DataManipulationLib is a ColdFusion UDF library containing data manipulation functions. This is a general purpose data \"munging\" type library."
},
"DatabaseLib":{
"desc":"A library containing database specific functions (written in CFML), organized by DB platform."
},
"DateLib":{
"desc":"This library is for date/time related function. Specifically, items that work on date or time based objects."
},
"FileSysLib":{
"desc":"FileSysLib is a ColdFusion UDF library containing functions that interact with the file system at various levels. These levels typically include drives, directories, and files."
},
"FinancialLib":{
"desc":"This library contains a set of UDFs that support financial type operations, including things like loan and interest calculations."
},
"MathLib":{
"desc":"A set of mathematical functions. This includes geometry, trig, statistical, and general math functions. These functions typically perform calculations."
},
"NetLib":{
"desc":"A library for Internet related UDFs. This library includes UDFs that retrieve pages from web servers, translate host addresses, and other operations related to Internet operations."
},
"ScienceLib":{
"desc":"A set of UDFs dedicated to scientific equations. This includes weather, astronomy, and other science fields."
},
"SecurityLib":{
"desc":"A set of security related functions."
},
"StrLib":{
"desc":"StrLib is a ColdFusion UDF library containing string (parsing, formatting) functions not included in the core CFML language. These functions typically will translate/verify strings."
},
"UtilityLib":{
"desc":"A set of utility functions for dealing with miscellaneous tasks such as state management, etc."
}
}

我在其中内置了代码来处理生成库显示。是的,所以这很明显,而且我很确定一些 Harp 用户已经在这样做了,但我认为它有点简洁,并且它让我重新评估我何时使用 Harp 与 Jekyll 进行比较。 (我只希望他们支持的不仅仅是 Jade 和 EJS。)

相关文章