Hugo安装与部署

hugo号称是The world’s fastest framework for building websites,是由go语言编写的,编译速度和运行速度都是杠杠的。不像hexo依赖于node.js,项目依赖模块多,hexo g生成网页也比较慢。

Hugo 依靠Go语言进行开发,号称世界上最快的构建网站工具,到底有多快?根据相关博主提供的数据,他200篇左右的博文用Hexo 需要10分钟去生成静态网页,而Hugo 只需要10秒。就我个人的体验来说,Hugo确实大大加快了网页生成的速度,平常增加一篇博文然后再生成渲染需要4秒左右,而Hugo基本上是1秒内完成。正是依赖于Hugo 快速生成的特点,调试方便成了Hugo的第二大特点。基本上我在源文件处修改的内容可以实时地显示在网页上,而不用再次敲代码生成再预览,这对于博主来说简直就是一个福音。

Hugo 在传播度上不及Hexo,相应的搭建教程及bug修复上也没有Hexo来的齐全,因此会要求用户有一定的代码能力和debug能力。此外,从Hexo迁移到Hugo会存在一定的时间成本,因为两者的markdown文件中对于Front Matter 的格式定义不同,因此需要修改每篇博文的该部分(当然用脚本去修改是最好的)。最后,是主题问题,Hugo上面还没有像Next一样完善成熟的主题,不过这也让大家不会一窝蜂地选取同一个样式(Hugo的主题样式还是很多的,官网提供了将近300个主题,选择犹豫症慎入)。

Hugo安装

Hugo的安装有很多方式,Install Hugo | Hugo (gohugo.io) ,根据个人喜好可以自行安装。

本人以windows 7 为例,使用Hugo前需要安装Git 和 Go 语言开发环境,点击对应网址下载安装包即可。

在Hugo的官网中选择想要的版本下载zip,将其中的hugo.exe文件解压到想要的地方。比如 C:Hugoin

Releases · gohugoio/hugo (github.com)

 将Hugo添加到Windows的环境变量 PATH中。 计算机——>属性——>高级系统设置——>环境变量——>系统变量——>Path.

 

打开Git Bash,输入 

go

  

 说明go 语言安装成功。

 go version

  查看go的版本信息

 输入

hugo version

  

 显示 hugo 版本信息,说明hugo安装成功

hugo help

hugo 的帮助页面,很多东西都是我们要学习的  

hugo is the main command, used to build your Hugo site.

Hugo is a Fast and Flexible Static Site Generator
built with love by spf13 and friends in Go.

Complete documentation is available at https://gohugo.io/.

Usage:
  hugo [flags]
  hugo [command]

Available Commands:
  check       Contains some verification checks
  config      Print the site configuration
  convert     Convert your content to different formats
  env         Print Hugo version and environment info
  gen         A collection of several useful generators.
  help        Help about any command
  import      Import your site from others.
  list        Listing out various types of content
  new         Create new content for your site
  server      A high performance webserver
  version     Print the version number of Hugo

Flags:
  -b, --baseURL string         hostname (and path) to the root, e.g. https://spf13.com/
  -D, --buildDrafts            include content marked as draft
  -E, --buildExpired           include expired content
  -F, --buildFuture            include content with publishdate in the future
      --cacheDir string        filesystem path to cache directory. Defaults: $TMPDIR/hugo_cache/
      --cleanDestinationDir    remove files from destination not found in static directories
      --config string          config file (default is path/config.yaml|json|toml)
      --configDir string       config dir (default "config")
  -c, --contentDir string      filesystem path to content directory
      --debug                  debug output
  -d, --destination string     filesystem path to write files to
      --disableKinds strings   disable different kind of pages (home, RSS etc.)
      --enableGitInfo          add Git revision, date and author info to the pages
  -e, --environment string     build environment
      --forceSyncStatic        copy all files when static is changed.
      --gc                     enable to run some cleanup tasks (remove unused cache files) after the build
  -h, --help                   help for hugo
      --i18n-warnings          print missing translations
      --ignoreCache            ignores the cache directory
  -l, --layoutDir string       filesystem path to layout directory
      --log                    enable Logging
      --logFile string         log File path (if set, logging enabled automatically)
      --minify                 minify any supported output format (HTML, XML etc.)
      --noChmod                don't sync permission mode of files
      --noTimes                don't sync modification time of files
      --path-warnings          print warnings on duplicate target paths etc.
      --quiet                  build in quiet mode
      --renderToMemory         render to memory (only useful for benchmark testing)
  -s, --source string          filesystem path to read files relative from
      --templateMetrics        display metrics about template executions
      --templateMetricsHints   calculate some improvement hints when combined with --templateMetrics
  -t, --theme strings          themes to use (located in /themes/THEMENAME/)
      --themesDir string       filesystem path to themes directory
      --trace file             write trace to file (not useful in general)
  -v, --verbose                verbose output
      --verboseLog             verbose logging
  -w, --watch                  watch filesystem for changes and recreate as needed

Use "hugo [command] --help" for more information about a command.

  

 新建博客

在自己的目录下建文件夹,如E盘,打开git bash (右键 git bash here),

输入hugo new site "你的文件名字",便可以生成一个用于存放博客的文件夹。

hugo new site myblog

  

目录结构:Directory Structure | Hugo (gohugo.io)

  

安装主题

不同于hexo,hugo没有自带主题,所以建立完文件夹后要导入主题文件。导入主题方式和hexo相似,可以用git clone 的方式,也可以到相应主题的github中下载zip文件然后解压到自己博客的themes文件夹中。

官网主题库:Hugo Themes

Hugo配置文件放置在源文件下,并且支持三种格式:toml,yaml,yml。这个配置文件可以直接从主题文件中的exampleSite 里copy到博客文件夹下,然后进行修改。

  • 注意点1:有些主题没有提供相应的配置文件,得进行自己修改,不建议选用这类主题。
  • 注意点2:配置文件中要确保里面的主题名字和你themes文件夹中相应的主题文件夹名字一样,比如我的主题是pure,那么配置文件里的theme = pure,并且themes 文件夹中也有一个pure的文件夹。这是为了保证工具能依据名字找到相应的主题文件。

生成博文

在 Git Bash 中输入 hugo new posts/xxxx.md,这时候就会在文件夹 content/posts形成你要的markdown文件,打开进行编辑即可。

渲染查看效果

在博客文件夹中打开Git Bash,输入 hugo server,然后打开 http://localhost:1313/ 来查看效果。

注意,markdown文件中的 front matter 部分有一个draft 参数,如果draft设置为true 则可正常渲染,如果设置为false则不予以渲染。相应的如果想查看全部效果则输入hugo server -D 表示将草稿文件也进行渲染。

浅谈我为什么从 HEXO 迁移到 HUGO - 少数派 (sspai.com)

使用Hugo快速搭建个人博客 (qq.com)

原文地址:https://www.cnblogs.com/adam012019/p/14914267.html