问题
Context
After installed (see previous post) and configured my personal Hugo website for a multilingual setup (by directories), I wanted to start creating content. Ideally, I wanted to use blogdown
in RStudio, via the addins. The website uses the Academic theme, rebranded now as Wowchemy.
The content directory tree is as follows:
content
|
├── en
├── authors
├── files (for static files)
├── home (homepage widgets)
├── post
├── project
└── resources
|
└── hu
├── authors
├── files (for static files)
├── home (homepage widgets)
├── post
├── project
└── resources
Multilingual setup references:
- Hugo Multilingual Part 1: Content translation - general overview, with managing content translations by directories and page bundles
- Start a Multi Language Blog with Hugo on Github Pages - for basic config.toml settings and highlighting the use of partials
- Light/Dark/Automatic translation - for getting to know how to make modification in interface translations (although do not know how to share it with the themes repo)
- How can I add credits by the end of the page? - for translating the footers of the webpages (which is not a content and can not be set in any config file), through partials.
Problem
When I use the new post addin in RStudio, the file is created in place, but does not open automatically for editing. The English and Hungarian language setting both works the same way.
> blogdown:::new_post_addin()
Listening on http://127.0.0.1:6918
C:\Users\HP\Documents\R\website\content\en\post\2020-11-04-how-this-site-was-created\index.en.md created
Warning in file(con, "r") :
cannot open file 'content/post/2020-11-04-how-this-site-was-created/index.en.md': No such file or directory
Warning: Error in file: cannot open the connection
91: file
90: readLines
87: hugo_convert_one
86: new_content
85: blogdown::new_post
84: observeEventHandler
13: shiny::runApp
12: shiny::runGadget
11: eval
10: eval
4: eval
3: eval
2: sys.source
1: blogdown:::new_post_addin
Question
How can this be resolved? As far as I see, something has to be with the file path after creating the file, because the file is created in the right place.
Configuration
> sessionInfo()
R version 4.0.3 (2020-10-10)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 7 x64 (build 7601) Service Pack 1
Matrix products: default
locale:
[1] LC_COLLATE=Hungarian_Hungary.1250 LC_CTYPE=Hungarian_Hungary.1250
[3] LC_MONETARY=Hungarian_Hungary.1250 LC_NUMERIC=C
[5] LC_TIME=Hungarian_Hungary.1250
attached base packages:
[1] stats graphics grDevices utils datasets methods base
other attached packages:
[1] shiny_1.5.0
loaded via a namespace (and not attached):
[1] Rcpp_1.0.5 bookdown_0.21 crayon_1.3.4 digest_0.6.26 later_1.1.0.1
[6] mime_0.9 R6_2.4.1 jsonlite_1.7.1 xtable_1.8-4 magrittr_1.5
[11] evaluate_0.14 blogdown_0.21 rlang_0.4.8 rstudioapi_0.11 miniUI_0.1.1.1
[16] promises_1.1.1 rmarkdown_2.4 tools_4.0.3 tinytex_0.26 fastmap_1.0.1
[21] httpuv_1.5.4 xfun_0.18 yaml_2.2.1 compiler_4.0.3 htmltools_0.5.0
[26] knitr_1.30
回答1:
I am not good at using the debug tools, so just stepped through the code. (Appreciate the suggestion of good tutorials!)
First things first: When in Doubt, Try to Upgrade Your Software Packages This is the blogdown
package creator's advice. Checked.
- Launching the addin,
blogdown:::new_post_addin
is called. - This calls
new_post.R
. The source code can be find at: https://github.com/rstudio/blogdown/blob/master/inst/scripts/new_post.R blogdown::new_post()
is called at the end, withfile
parameter from the updated input text field, which is in the case of the question: "post\2020-11-04-how-this-site-was-created\index.en.md"new_post()
function (in hugo.R) callsnew_content()
with the third argument,open = FALSE
, which means it will not open the file, just overwrite the value of thefile
variable (the path hereafter). At the end ofnew_post()
the file should be opened: this is where the error occurs (by trying to open the file at the wrong place, a wrong path). This means that something bad has to happen innew_content()
.new_content()
modify the path withcontent_file()
. Also this is the step, where the file is actually created.content_file()
modify the path by adding a prefix to it generated byget_config()
.get_config()
tries to extract the value of a field in the configuration, in the case of Hugo: try to findcontentDir
. If this results in NULL (there's no such list item), then see the other possibility and lastly return the default value (which is in the case of Hugo:content
). And that's it! RStudio tries to open a file in the content directory, not in a language subdirectory!- That means that the last option is returned in the row of
%n%
(which is imported from knitr and stands for:if (is.null(x)) y else x
). That means that the previous arguments returned NULL, which means contentDir cannot be found inconfig
variable. The default value ofconfig
is set byconfig = load_config()
. This usesfind_config()
to find the config file to parse. find_config()
usesconfig_files()
to set the value in case of Hugo:c('config.toml', 'config.yaml')
. But my setting is in a subdirectory: in the/config/default_/languages.toml
file! Oh, another Academic theme woe...
To conclude this: blogdown
is currently loads only the config.toml
in the root directory to check the contentDir
value. The Academic, now Wowchemy Hugo theme however keep config files in the /config/default_/
directory also, where the languages.toml
contains the needed value.
To keep or not to keep Academic theme? Stick with blogdown
or not? Maybe the config files should be merged into one config.toml, but couldn't find hint or examples for Academic on the web.
Edit: OK, the root of the problem has been found, but the actual error which raised the error message is in connection with hugo_convert_one()
, as the traceback suggests in the question. This was the first (and last) try to open the file of the wrong path.
来源:https://stackoverflow.com/questions/64688201/blogdown-new-post-addin-creates-but-not-loads-new-file