babelquarto: 渲染多语言四开书

huangapple go评论55阅读模式
英文:

babelquarto: Rendering a Multilingual Quarto Book

问题

我目前正在尝试使用[babelquarto]标签来创建一个多语言的[Quarto]书籍,我已经使用了[babelquarto]网站提供的代码来实现这个目的。

library(babelquarto)
library(fs)

parent_dir <- getwd()

quarto_multilingual_book(
    parent_dir = parent_dir,
    book_dir = "blop"
)

Creating project at D:23-06-23_babelquarto\blop:
  - Created _quarto.yml
  - Created index.qmd
  - Created intro.qmd
  - Created summary.qmd
  - Created references.qmd
  - Created cover.png
  - Created references.bib

readLines(file.path(parent_dir, "blop", "_quarto.yml"))

[1] "project:"
 [2] "  type: book"
 [3] ""
 [4] "book:"
 [5] "  title: \"blop\""
 [6] "  author: \"MYaseen\""
 [7] "  date: \"6/16/2023\""
 [8] "  chapters:"
 [9] "    - index.qmd"
[10] "    - intro.qmd"
[11] "    - summary.qmd"
[12] "    - references.qmd"
[13] ""
[14] "bibliography: references.bib"
[15] ""
[16] "format:"
[17] "  html:"
[18] "    theme: cosmo"
[19] ""
[20] "babelquarto:"
[21] "  mainlanguage: 'en'"
[22] "  languages: ['es', 'fr']"
[23] "title-es: title in es"
[24] "title-fr: title in fr"
[25] "description-es: description in es"
[26] "description-fr: description in fr"
[27] "author-es: author in es"
[28] "author-fr: author in fr"
[29] "lang: en"

dir_tree(file.path(parent_dir, "blop"))
D:/2023-06-23_babelquarto/blop
├── cover.png
├── index.es.qmd
├── index.fr.qmd
├── index.qmd
├── intro.es.qmd
├── intro.fr.qmd
├── intro.qmd
├── references.bib
├── references.es.qmd
├── references.fr.qmd
├── references.qmd
├── summary.es.qmd
├── summary.fr.qmd
├── summary.qmd
└── _quarto.yml

library(quarto)
quarto_render("./blop")

这将渲染四个.qmd文档,如下所示:
babelquarto: 渲染多语言四开书

我相信提供的[R]标签应该包括所有三本书。然而,我担心可能忽视了一些简单的东西。

预期输出
预期输出在这里

英文:

I am currently experimenting with [tag:babelquarto] in order to create a Multilingual [tag:Quarto] Book, and I have utilized the provided code from the [tag:babelquarto] website for this purpose.

library(babelquarto)
library(fs)

parent_dir &lt;- getwd()

quarto_multilingual_book(
    parent_dir = parent_dir
  , book_dir   = &quot;blop&quot;
  )

Creating project at D:23-06-23_babelquarto\blop:
  - Created _quarto.yml
  - Created index.qmd
  - Created intro.qmd
  - Created summary.qmd
  - Created references.qmd
  - Created cover.png
  - Created references.bib

readLines(file.path(parent_dir, &quot;blop&quot;, &quot;_quarto.yml&quot;))

[1] &quot;project:&quot;                         
 [2] &quot;  type: book&quot;                     
 [3] &quot;&quot;                                 
 [4] &quot;book:&quot;                            
 [5] &quot;  title: \&quot;blop\&quot;&quot;                
 [6] &quot;  author: \&quot;MYaseen\&quot;&quot;            
 [7] &quot;  date: \&quot;6/16/2023\&quot;&quot;            
 [8] &quot;  chapters:&quot;                      
 [9] &quot;    - index.qmd&quot;                  
[10] &quot;    - intro.qmd&quot;                  
[11] &quot;    - summary.qmd&quot;                
[12] &quot;    - references.qmd&quot;             
[13] &quot;&quot;                                 
[14] &quot;bibliography: references.bib&quot;     
[15] &quot;&quot;                                 
[16] &quot;format:&quot;                          
[17] &quot;  html:&quot;                          
[18] &quot;    theme: cosmo&quot;                 
[19] &quot;&quot;                                 
[20] &quot;babelquarto:&quot;                     
[21] &quot;  mainlanguage: &#39;en&#39;&quot;             
[22] &quot;  languages: [&#39;es&#39;, &#39;fr&#39;]&quot;        
[23] &quot;title-es: title in es&quot;            
[24] &quot;title-fr: title in fr&quot;            
[25] &quot;description-es: description in es&quot;
[26] &quot;description-fr: description in fr&quot;
[27] &quot;author-es: author in es&quot;          
[28] &quot;author-fr: author in fr&quot;          
[29] &quot;lang: en&quot;       



dir_tree(file.path(parent_dir, &quot;blop&quot;))
D:/2023-06-23_babelquarto/blop
├── cover.png
├── index.es.qmd
├── index.fr.qmd
├── index.qmd
├── intro.es.qmd
├── intro.fr.qmd
├── intro.qmd
├── references.bib
├── references.es.qmd
├── references.fr.qmd
├── references.qmd
├── summary.es.qmd
├── summary.fr.qmd
├── summary.qmd
└── _quarto.yml

library(quarto)
quarto_render(&quot;./blop&quot;)

which renders only four .qmd document as shown below:
babelquarto: 渲染多语言四开书

I believe that the provided [tag:R] should include all three books. However, I'm concerned that I may be overlooking something simple.

Expected Output
The expected output is here.

答案1

得分: 1

你需要使用babelquarto::render_book,而不是使用quarto::quarto_render

babelquarto::render_book("./blop")

它会为其他语言创建相应的文件,

> fs::dir_tree("./blop/_book", recurse=FALSE)
./blop/_book
├── es
├── fr
├── index.html
├── intro.html
├── references.html
├── search.json
├── site_libs
└── summary.html

babelquarto: 渲染多语言四开书


使用Github-Pages托管多语言Quarto书籍的问题

babelquarto::render_book 会在HTML文件中生成多语言版本的链接(在 index.htmlintro.html 等文件中),如下所示(摘自 index.html):

<li id="language-link-li-es">
<i class="bi bi-globe2"></i><span> </span><a class="toc-action" href="/es/index.es.html" id="language-link-es">Version in ES</a>
</li>
<li id="language-link-li-fr">
<i class="bi bi-globe2"></i><span> </span><a class="toc-action" href="/fr/index.fr.html" id="language-link-fr">Version in FR</a>
</li>

其中链接为:

/es/index.es.html
/fr/index.fr.html

同样,在 index.es.html 文件中也是如此:

/index.html
/fr/index.fr.html

根据我的理解,由于这些相对链接前面有斜杠,导航到多语言版本之间在Github页面上无法正常工作。

解决方案

以下是解决此问题的我的简单方法。

我编写了以下函数,它接收 _book 目录并遍历该目录中的每个HTML文件,并在这些语言链接前面添加基本URL,其中基本URL 将是 Github 页面的URL,如 https://<user-name>.github.io/<repo-name>

library(rvest)
library(fs)

add_base_url <- function(file_path, base_link) {
  html_content <- rvest::read_html(file_path)
  lang_links <- html_content %>%
    rvest::html_elements('a[id*="language-link"]') %>%
    rvest::html_attr("href")
  lang_link_with_baseurl <- paste0(base_link, lang_links)
  raw_html <- paste(readLines(file_path), collapse="\n")
  for (i in seq_along(lang_links)) {
    raw_html <- sub(lang_links[i], lang_link_with_baseurl[i], raw_html)
  }
  return(raw_html)
}

create_abs_lang_link <- function(path, base_link) {
  html_files_path <- fs::dir_ls(path, recurse = TRUE, glob="*.html")
  for (file_path in html_files_path) {
    edited_raw_html <- add_base_url(file_path, base_link)
    writeLines(edited_raw_html, file_path)
  }
}

步骤:

  1. 渲染并生成多语言Quarto书籍,

    babelquarto::render_book("./blop")
    

    这将生成一个包含渲染后HTML文件的目录 _book

  2. 然后将 _book 目录传递给函数 create_abs_lang_link

    create_abs_lang_link("_book", base_link = "https://<user-name>.github.io/<repo-name>")
    
  3. 然后在Github页面上托管此 _book 目录。链接应该现在正常工作。

这是一个示例Github存储库和链接到Quarto Multilinuage Book的链接。

英文:

You need to use babelquarto::render_book, instead of quarto::quarto_render,

babelquarto::render_book(&quot;./blop&quot;)

It would create the corresponding files for other languages,

&gt; fs::dir_tree(&quot;./blop/_book&quot;, recurse=FALSE)
./blop/_book
├── es
├── fr
├── index.html
├── intro.html
├── references.html
├── search.json
├── site_libs
└── summary.html

<hr>

babelquarto: 渲染多语言四开书

<hr>

Issue with Hosting the Multilingual Quarto book with Github-Pages

babelquarto::render_book generates the links for multilingual versions in html files (in index.html, intro.html etc) as the following (excerpt from index.html),

&lt;li id=&quot;language-link-li-es&quot;&gt;
&lt;i class=&quot;bi bi-globe2&quot;&gt;&lt;/i&gt;&lt;span&gt; &lt;/span&gt;&lt;a class=&quot;toc-action&quot; href=&quot;/es/index.es.html&quot; id=&quot;language-link-es&quot;&gt;Version in ES&lt;/a&gt;
&lt;/li&gt;
&lt;li id=&quot;language-link-li-fr&quot;&gt;
&lt;i class=&quot;bi bi-globe2&quot;&gt;&lt;/i&gt;&lt;span&gt; &lt;/span&gt;&lt;a class=&quot;toc-action&quot; href=&quot;/fr/index.fr.html&quot; id=&quot;language-link-fr&quot;&gt;Version in FR&lt;/a&gt;
&lt;/li&gt;

where the links are,

/es/index.es.html
/fr/index.fr.html

Similarly from the index.es.html,

/index.html
/fr/index.fr.html

And as per my understanding, due to the forward slash in front of these relative links, navigation between multilingual versions does not work well with Github pages.

Solution

Here's my naive approach to solving this problem.

I have written the following function that receives the _book directory and goes through each HTML file in that directory and add a base URL in front of these language links where the base-url would be the url of github page like https://&lt;user-name&gt;.github.io/&lt;repo-name&gt;.

library(rvest)
library(fs)

add_base_url &lt;- function(file_path, base_link) {
  html_content &lt;- rvest::read_html(file_path)
  lang_links &lt;- html_content %&gt;% 
    rvest::html_elements(&#39;a[id*=&quot;language-link&quot;]&#39;) %&gt;% 
    rvest::html_attr(&quot;href&quot;)
  lang_link_with_baseurl &lt;- paste0(base_link, lang_links)
  raw_html &lt;- paste(readLines(file_path), collapse=&quot;\n&quot;)
  for (i in seq_along(lang_links)) {
    raw_html &lt;- sub(lang_links[i], lang_link_with_baseurl[i], raw_html)
  }
  return(raw_html)
}

create_abs_lang_link &lt;- function(path, base_link) {
  html_files_path &lt;- fs::dir_ls(path, recurse = TRUE, glob=&quot;*.html&quot;)
  for (file_path in html_files_path) {
    edited_raw_html &lt;- add_base_url(file_path, base_link)
    writeLines(edited_raw_html, file_path)
  }
}

Steps:

  1. Render and generate the multilingual quarto book,

    babelquarto::render_book(&quot;./blop&quot;)
    

    This will generate a directory _book with the rendered html files

  2. Then pass the _book directory to the function create_abs_lang_link.

    create_abs_lang_link(&quot;_book&quot;, base_link = &quot;https://&lt;user-name&gt;.github.io/&lt;repo-name&gt;&quot;)
    

3 . Then host this _book directory in github pages. The links should work now.

Here's an example github repository and link to Quarto Multilinuage Book.

huangapple
  • 本文由 发表于 2023年6月16日 15:37:51
  • 转载请务必保留本文链接:https://go.coder-hub.com/76487947.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定