如何在 Jekyll post 中包含一个 Rmarkdown/HTML 文件
How to include an Rmarkdown/HTML file in Jekyll post
我正在使用 Jekyll 制作并托管在 GitHub 上的静态网站。其中一个 post 看起来像 this
此外,我创建了一个 Rmarkdown 文件,我想将生成的 html 文件嵌入到 post 中。我读到 我只需要这样做:
You just have to create a folder with the name _includes/ in the DocumentRoot of your project, then, create an HTML file inside, for example "mycomponent.html" and call it in your post with something like this:
{% include mycomponent.html %}
我在 _includes/ 文件夹中添加了我的 html 文件,并在相应 post 的 markdown 文件末尾添加了这样一段代码。然而,当我这样做时,网站布局完全改变
有什么办法可以避免这种情况?本站所有文件存储here.
编辑:
我发现了另一个建议做的问题 this:
In my opinion the best solution is:
Using jQuery:
a.html:
<html>
<head>
<script src="jquery.js"></script>
<script>
$(function(){
$("#includedContent").load("b.html");
});
</script>
</head>
<body>
<div id="includedContent"></div>
</body>
</html>
b.html:
<p> This is my include file </p>
完全看不懂。以某种方式恢复了网站的布局,但现在一些图像和 html 小部件丢失了。另外,页面的页脚完全乱七八糟。
从存储库中,我假设您已尝试将 _includes/Report.html
包含到另一个文件中。
_includes/Report.html
是一个完整的 HTML 页面(带有 doctype
和 html
标签),而不是 include
设计的部分页面。 Liquid 将用完整的 HTML 替换 include 标签,创建无效标记和布局问题的可能来源:
<!doctype html>
<html>
<head>...</head>
<body>
...
<!doctype html>
<html>
...
</html>
</body>
</html>
要解决此问题,请删除 _includes/Report.html
中的额外标记(保留 script
标记),并使用 Liquid 包含更正的部分:
{% include Report.html %}
我发现没有必要将 html 嵌入另一个。使用 R,可以创建一个带有 post 的 Rmd 文件,然后将其转换为所需的 md 文件。 Jason Fisher 的 website explains it nicely with step-by-step instructions. His GitHub site 也有有用的信息。
另一个有用的site is the one by Juuso Parkkinen. He is the person that told me that he's never embed an html into another and only used R directly to create his Jekyll website by using the following code:
# compiles all .Rmd files in _R directory into .md files in _posts directory,
# if the input file is older than the output file.
# run ./knitpages.R to update all knitr files that need to be updated.
KnitPost <- function(input, outfile, base.url="/") {
# this function is a modified version of an example here:
# http://jfisher-usgs.github.com/r/2012/07/03/knitr-jekyll/
require(knitr);
opts_knit$set(base.url = base.url)
fig.path <- paste0("blog/figs/", sub(".Rmd$", "", basename(input)), "/")
opts_chunk$set(fig.path = fig.path)
opts_chunk$set(fig.cap = "testing")
render_jekyll()
knit(input, outfile, envir = parent.frame())
}
for (infile in list.files("blog/_R", pattern="*.Rmd", full.names=TRUE)) {
outfile = paste0("blog/_posts/", sub(".Rmd$", ".md", basename(infile)))
# knit only if the input file is the last one modified
if (!file.exists(outfile) | file.info(infile)$mtime > file.info(outfile)$mtime) {
KnitPost(infile, outfile)
}
}
他的 GitHub 帐户也是一个有用的参考。
我正在使用 Jekyll 制作并托管在 GitHub 上的静态网站。其中一个 post 看起来像 this
此外,我创建了一个 Rmarkdown 文件,我想将生成的 html 文件嵌入到 post 中。我读到
You just have to create a folder with the name _includes/ in the DocumentRoot of your project, then, create an HTML file inside, for example "mycomponent.html" and call it in your post with something like this:
{% include mycomponent.html %}
我在 _includes/ 文件夹中添加了我的 html 文件,并在相应 post 的 markdown 文件末尾添加了这样一段代码。然而,当我这样做时,网站布局完全改变
有什么办法可以避免这种情况?本站所有文件存储here.
编辑:
我发现了另一个建议做的问题 this:
In my opinion the best solution is:
Using jQuery:
a.html:
<html> <head> <script src="jquery.js"></script> <script> $(function(){ $("#includedContent").load("b.html"); }); </script> </head> <body> <div id="includedContent"></div> </body> </html>
b.html:
<p> This is my include file </p>
完全看不懂。以某种方式恢复了网站的布局,但现在一些图像和 html 小部件丢失了。另外,页面的页脚完全乱七八糟。
从存储库中,我假设您已尝试将 _includes/Report.html
包含到另一个文件中。
_includes/Report.html
是一个完整的 HTML 页面(带有 doctype
和 html
标签),而不是 include
设计的部分页面。 Liquid 将用完整的 HTML 替换 include 标签,创建无效标记和布局问题的可能来源:
<!doctype html>
<html>
<head>...</head>
<body>
...
<!doctype html>
<html>
...
</html>
</body>
</html>
要解决此问题,请删除 _includes/Report.html
中的额外标记(保留 script
标记),并使用 Liquid 包含更正的部分:
{% include Report.html %}
我发现没有必要将 html 嵌入另一个。使用 R,可以创建一个带有 post 的 Rmd 文件,然后将其转换为所需的 md 文件。 Jason Fisher 的 website explains it nicely with step-by-step instructions. His GitHub site 也有有用的信息。
另一个有用的site is the one by Juuso Parkkinen. He is the person that told me that he's never embed an html into another and only used R directly to create his Jekyll website by using the following code:
# compiles all .Rmd files in _R directory into .md files in _posts directory,
# if the input file is older than the output file.
# run ./knitpages.R to update all knitr files that need to be updated.
KnitPost <- function(input, outfile, base.url="/") {
# this function is a modified version of an example here:
# http://jfisher-usgs.github.com/r/2012/07/03/knitr-jekyll/
require(knitr);
opts_knit$set(base.url = base.url)
fig.path <- paste0("blog/figs/", sub(".Rmd$", "", basename(input)), "/")
opts_chunk$set(fig.path = fig.path)
opts_chunk$set(fig.cap = "testing")
render_jekyll()
knit(input, outfile, envir = parent.frame())
}
for (infile in list.files("blog/_R", pattern="*.Rmd", full.names=TRUE)) {
outfile = paste0("blog/_posts/", sub(".Rmd$", ".md", basename(infile)))
# knit only if the input file is the last one modified
if (!file.exists(outfile) | file.info(infile)$mtime > file.info(outfile)$mtime) {
KnitPost(infile, outfile)
}
}
他的 GitHub 帐户也是一个有用的参考。