【2.1.2】blogdown

一、安装

  1. 安装R
  2. 安装Rstudio
  3. 安装blogdown
  4. 安装hugo

安装blogdown

install.packages('devtools')
devtools::install_github('rstudio/blogdown')

或者:

install.packages('blogdown')

安装Hugo

在Rsutdio中输入:

blogdown::install_hugo()

更新:

blogdown::update_hugo()

或者:
library('blogdown')
install_hugo(force = TRUE)

检查更新版本
blogdown::hugo_version()

报错1:

installation of package ‘roxygen2’ had non-zero exit status
ERROR: dependencies ‘usethis’, ‘roxygen2’, ‘testthat’ are not available for package ‘devtools’
*

install.packages('roxygen2')

package ‘‘roxygen2’’ is not available (for R version 3.4.1)

解决办法:

source("http://bioconductor.org/biocLite.R")
biocLite("roxygen2")
install.packages("roxygen2")


brew install  libcurl4-gnutls-dev
brew install libcurl4-gnutls-dev

二、用法

2.1.创建一个新的文件夹

mkdir blogdown
cd dblogdown

#Rstudio中运行
blogdown::new_site()
静态初始网站就建好啦

2.2. Rsutdio

Rstudio左上角有一个 “Addins” 按钮,

1. “Serve Site”  -- 相当于   blogdown::serve_site()
通过LiveReload技术启动本地话的网页。这个功能会阻塞你的R程序,意味着你一旦开启这个本地网页服务,就不能用R console了。如果你不想阻塞你的R,你可以添加servr.daemon = TRUE 在运行这个的时候,但这个还不稳定。

2. “New Post”  创建一个新的博客,相当于命令blogdown::new_post() 

2.3.R markdown和MARKDOWN的区别

有一些语法上的差别,用的时候再不断积累吧,最大的句别是。R markdown的一些作图代码可以实现出来。 例如:

```{r cool-plot, fig.width='80%', fig.cap='A cool plot.'}
plot(cars, pch = 20)  # not really cool
```

会出来图

2.4. 安装其他的theme

建议是重新建一个文件夹,然后在文件夹中建立这个主题的博客

##cd到新建立的文件夹,然后在R studdio中输入下面的命令

blogdown::new_site(theme = "gcushen/hugo-academic")

或
blogdown::new_site(theme = "yihui/hugo-xmin")

或
blogdown::new_site(theme = "progrhyme/hugo-theme-bootie-docs")

不同主题差异很大,修改的阅读,差异越大

mainroad主题

git clone https://github.com/vimux/mainroad

三、讨论

3.1. wordpress转移到静态上来

方法一:

1.先将wordpress转化为jell格式

1.下载exitwp
git clone https://github.com/thomasf/exitwp.git

2.下载wordpress xml文件 wordpress控制台--tools -export -xml

3. 将下载的xml文件放到exitwp文件夹下wordpress-xml文件夹

4. 用xmllint测试下下来的xml文件有木有什么问题你
	xmllint *.xml

5. 下载所需要的包 

	sudo pip install --upgrade -r pip_requirements.txt

6.转换
python ./exitwp.py	

2.然后jell格式转hugo格式

git clone https://github.com/tiehan/oldblog_xml.git

方法二:

用github上的一个项目yihui/oldblog_xml

https://bookdown.org/yihui/blogdown/from-wordpress.html

去掉post这一行

perl -p -i.bak -e 's/layout: post/#layout: post/g' *.md

3.2 增加评论

  • 多说 2017年6月1日停止服务
  • 网易云跟帖 2017年8月1日停止服务
  • 自建的评论系统挑了 ISSO 和 HashOver
  • Disqus 国外最流行的 ,需要翻墙
  • Gitment 是作者实现的一款基于 GitHub Issues 的评论系统 不是对话式的评论,所以放弃
  • 有言 没有微信等登陆方式,弃坑
  • 来必力 界面更友好,韩国的

仅仅在文章也显示评论

themes/hugo-xmin/layouts/partials/foot_custom.html中添加

{{ if .IsPage }}
  {{ partial "youyan.html" . }}
{{ end }}

youyan.html为注册以后提供的一段代码,放到yoyan.html文件汇总即可

各种评论工具汇总(超赞) https://blog.shuiba.co/comment-systems-recommendation

我用的是来必力 (最后给去掉了,原因在于他引入了 taboola 的广告。。。 )

3.3.增加搜索

自定义搜索

https://cse.google.com/cse/all?hl=zh-CN

点击 “Add” – 输入想搜索的网址"hugo.qinqianshan.com"– “创建”

修改搜索引擎 – 外观(不知道选哪个?) – 保存并获取代码

参考资料:http://devlu.me/2016/01/23/Hexo%E5%8D%9A%E5%AE%A2%E4%BC%98%E5%8C%96%E9%85%8D%E7%BD%AE%E4%B9%8B-%E4%B8%BA%E8%87%AA%E5%B7%B1%E5%8D%9A%E5%AE%A2%E6%B7%BB%E5%8A%A0%E7%AB%99%E5%86%85%E6%90%9C%E7%B4%A2/

swiftype

https://app.swiftype.com 必须用公司邮箱来申请,悲桑中 而且只能免费用14天额

tapirgo

http://tapirgo.com/

显示效果,不好看,暂时用这个吧。后面也不好用了

algolia

Algolia is a powerful service, but it’s a separate dependency to manage and subscribe to, and requires custom code to be written and maintained to regularly crawl and index your site content.

#安装node和npm

brew install node

安装aerobatic

npm install aerobatic-cli -g

hugo-search-index

npm install –save-dev gulp hugo-search-index

在目录下编辑 gulpfile.js 文件

const gulp = require('gulp')
 
// import search index tasks 
require('hugo-search-index/gulp')(gulp)
 
gulp.task('build', ['hugo-search-index'])

运行,建立索引

node_modules/gulp/bin/gulp.js build

并不好用,丢弃

A static website with a dynamic search function? Yes. As alternatives to embeddable scripts from Google or other search engines, you can provide your visitors a custom search by indexing your content files directly.

  • Hugoidx is an experimental application to create a search index. It’s built on top of Bleve.
  • GitHub Gist for Hugo Workflow. This gist contains a simple workflow to create a search index for your static website. It uses a simple Grunt script to index all your content files and lunr.js to serve the search results.
  • hugo-lunr. A simple way to add site search to your static Hugo site using lunr.js. Hugo-lunr will create an index file of any html and markdown documents in your Hugo project.
  • hugo-lunr-zh. A bit like Hugo-lunr, but Hugo-lunr-zh can help you seperate the Chinese keywords.

hugo-lunr-zh

yum install nodejs

在静态网页文件夹下
npm install -g hugo-lunr-zh
会生成一个node_modules文件夹

cd /Users/tanqianshan/Documents/3.blog/blog-mainroad
hugo-lunr-zh  -o public/data.json --matter-delims --- --matter-type yaml -d content/math/significance_test -c /math/significance_test/
-o 未生成的json文件,-d为需要建立json文件的md文件,但这个参数没办法进行递归查找 -contextPath 生成的uri前缀

3.4. 打赏

暂用微信二维码

5. subcategories

通过_index.md 文件来构建分类 hugo中各种变量的介绍:

http://www.gohugo.org/doc/templates/variables/

6.统计阅读量

方法一:

纯HTML的静态网页中添加一段统计网页访问量的JAVA Script代码

http://blog.csdn.net/daiyan_csdn/article/details/51423600

以下代码copy到你的网页中,复制到</body>之前就可以啦!是不是很简单啊!

参考资料:http://www.jianshu.com/p/a87c070dfcf8

方法二:

不算子:

http://ibruce.info/2015/04/04/busuanzi/

7. 本地访问速度优化

作为服务器来被利用

setwd("~/Documents/3.blog/blog-mainroad")
options(blogdown.generator.server = TRUE)
options(blogdown.server.timeout=60);  # 默认的是30秒,但文章多了以后,消耗的时间就会比较久。
blogdown::serve_site()

这种方式仅支持Hugo,不支持Jekyll、Hexo;仅支持Markdown,不支持R Markdown。这种方式简直是不能更快。

或者更高级的设置

options(blogdown.hugo.server = c("-D", "-F", "--navigateToChanged"))

c('-D', '-F'), which means to render draft and future posts in the preview
navigateToChanged in a recent version of Hugo, which asks Hugo to automatically navigate to the changed page

关闭服务:

blogdown::stop_server()
或:
stop_server() # 如果rstudio重启了的话

但内容都在内存中,如果要同步public,需要

blogdown::hugo_build()

或者:

blogdown::build_site()

8.减少内存的消耗

renderToDisk,图片啥的导入到Disk,而不是直接导入到内存中

setwd("~/Documents/3.blog/blog");
options(blogdown.generator.server = TRUE);
options(blogdown.server.timeout=400);  
options(blogdown.hugo.server=c('-D','-F','--navigateToChanged','--renderToDisk'));  
blogdown::serve_site()

更多参数说明:

四、报错

4.1 fatal error: pipe failed

因为max files 的问题,解决办法: https://discourse.gohugo.io/t/on-upgrade-to-v0-76-5-fatal-error-pipe-failed/28912

参考资料

药企,独角兽,苏州。团队长期招人,感兴趣的都可以发邮件聊聊:tiehan@sina.cn
个人公众号,比较懒,很少更新,可以在上面提问题,如果回复不及时,可发邮件给我: tiehan@sina.cn