Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Resolve issues about archetypes and new posts with a non-default kind #263

Merged
merged 8 commits into from
Feb 27, 2018
Merged

Resolve issues about archetypes and new posts with a non-default kind #263

merged 8 commits into from
Feb 27, 2018

Conversation

lcolladotor
Copy link
Contributor

Hi,

This pull request resolves two related issues.

It addresses #173 (comment) so that default_kind() can find archetypes such as archetypes/post.md for files like /content/post/2018-02-24-postslug.whatever. It also adds the option to choose the archetype from the RStudio addin as described in #173 (comment).

Next, it solves the situation described in #261 (comment) where new_content() wouldn't work with a provided kind (archetype) if the file did not end in .md.

Screenshots of PR working

Here are two screenshots showing it all working together now. First, I use the modified RStudio addin to select the post archetype (which you can see in the background, contents posted later here also).

screen shot 2018-02-24 at 10 39 15 am

Next is a screenshot of the resulting .Rmd new post file where hugo_toYAML() added the appropriate slug, categories and tags + the rest of the contents from the post.md archetype (aka, kind = 'post').

screen shot 2018-02-24 at 10 39 34 am

archetypes/post.md archetype initial contents

    +++
    title = "{{ replace .TranslationBaseName "-" " " | title }}"
    date = {{ .Date }}
    draft = false

    # Tags and categories
    # For example, use `tags = []` for no tags, or the form `tags = ["A Tag", "Another Tag"]` for one or more tags.
    tags = []
    categories = []

    # Featured image
    # Place your image in the `static/img/` folder and reference its filename below, e.g. `image = "example.jpg"`.
    # Use `caption` to display an image caption.
    #   Markdown linking is allowed, e.g. `caption = "[Image credit](http://example.org)"`.
    # Set `preview` to `false` to disable the thumbnail in listings.
    [header]
    image = ""
    caption = ""
    preview = true
    +++


    ## Add to YAML:

    ```
    output:
      blogdown::html_page:
        toc: no
        fig_width: 5
        fig_height: 5
    ```

    jojojojo

    ```{r bibsetup, echo=FALSE, message=FALSE, warning=FALSE}
    ## Load knitcitations with a clean bibliography
    library('knitcitations')
    cleanbib()
    cite_options(hyperlink = 'to.doc', citation_format = 'text', style = 'html')

    bib <- c('knitcitations' = citation('knitcitations'),
             'blogdown' = citation('blogdown')[2])
    ```

Limitations / stuff for the future

  1. I didn't add a warning/error message if the kind has a non .md file termination in this PR. I realize now that this is a hugo property. In any case, I'm not sure in which of the blogdown functions such a warning would go.
  2. As you can see, I don't know how to edit the archetype file so that hugo and then hugo_toYAML() will keep/add this part to the YAML section:
output:
  blogdown::html_page:
    toc: no
    fig_width: 5
    fig_height: 5

Misc

  • I modified the NEWS.md file following the syntax used previously in the file. Please feel free to edit it. For example, maybe you prefer linking to this PR instead of the issues.
  • In my changes to new_content() I use tools::file_ext() which should be ok, since you have tools under suggests in the DESCRIPTION file.

Best,
Leo

Details: default_kind() now finds the archetype matching the type of content. The new_post addin now lets you choose an archetype.
…arkdown. Resolves #261.

Details: new_content() will create a copy of the file with a .md termination so that hugo will be able to work with it. Once hugo is done, the temporary files are deleted.
Copy link
Member

@yihui yihui left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Next time if you are going to do three orthogonal things, I recommend you to submit three pull requests. In this case, you could have submitted three "bite-sized" PRs, and I could happily accept each in 5 minutes. If all three come in the same PR, I cannot accept the obviously good changes/fixes because of the less ideal ones.

This time you are fine. No need to split this PR, but I'll need more time to review it (looks good overall). Thanks!

@@ -1,7 +1,7 @@
Package: blogdown
Type: Package
Title: Create Blogs and Websites with R Markdown
Version: 0.5.4
Version: 0.5.6
Authors@R: c(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please also add yourself as a ctb here (alphabetical order by first name).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done ^^

@yihui yihui added this to the v0.6 milestone Feb 25, 2018
@lcolladotor
Copy link
Contributor Author

Ok, I'll keep this in mind!

Though from my point of view the 3 changes were related. In any case, I understand where you are coming from.

)
shiny::stopApp()
})
shiny::observeEvent(input$cancel, {
shiny::stopApp()
})
},
stopOnCancel = FALSE, viewer = shiny::dialogViewer('New Post', height = 500)
stopOnCancel = FALSE, viewer = shiny::dialogViewer('New Post', height = 570)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you try to move the Archetype menu to the right of Tags and see how it looks like? I prefer not to increase the height of this addin if possible.

R/hugo.R Outdated
if (!file.exists(file.path('archetypes', atype))) return('default')
gsub('/.*', '', path)
## Assumes path is something like
## /pathToBlogdownDir/content/post/2018-02-24-postslug.Rmd
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

path is supposed to be a relative path without the leading content/; new_content('foo/bar.md') means to create content/foo/bar.md. Does that make sense?

R/hugo.R Outdated
hugo_cmd(c('new', shQuote(path), c('-k', kind)))
file = content_file(path)
hugo_toYAML(file)
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I feel this is too complicated. Will this work (no need if-else)?

# create a .md file and rename it to the expected extension later
path2 = with_ext(path, '.md')
file  = content_file(path)
file2 = content_file(path2)
hugo_cmd(c('new', shQuote(path2), c('-k', kind)))
hugo_toYAML(file2)
file.rename(file2, file)

@yihui yihui mentioned this pull request Feb 26, 2018
1 task
@lcolladotor
Copy link
Contributor Author

I made 3 commits each addressing one of your requested changes.

  • RStudio addin menu location: 14da72a
  • Remove /content/ assumption for default_kind() 1d495b9
  • Remove the larger if() {} else{} from new_content() 74a3a51. I'm not sure how to make it less complicated and still support .Rmd and .Rmarkdown file extensions.

Copy link
Member

@yihui yihui left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you also provide a screenshot of the new addin? I want to make sure the spacing looks correct. Thanks!

R/hugo.R Outdated
## If we do this after fixing the file termination it doesn't work
hugo_toYAML(file_temp)
file = gsub('md$', tools::file_ext(path), file_temp)
file.copy(file_temp, file)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Basically I had written the full code in the comment (meant to replace your lines 193-206 above): #263 (comment) Did you test it? (I was asking if that would work)

# create a .md file and rename it to the expected extension later
path2 = with_ext(path, '.md')
file  = content_file(path)
file2 = content_file(path2)
hugo_cmd(c('new', shQuote(path2), c('-k', kind)))
hugo_toYAML(file2)
file.rename(file2, file)

R/hugo.R Outdated
## (file termination doesn't matter)
atype = gsub('.*/', '', dirname(path))
if (!file.exists(file.path('archetypes', paste0(atype, '.md')))) return('default')
atype
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

With the assumption that path is a relative path like post/2018-02-24-postslug.Rmd, I don't see the need for any changes in this function: gsub('/.*', '.md', path) will return post.md.

… correctly that path is post/date-slug.md or something like that instead of content/post/date-slug.md
@lcolladotor
Copy link
Contributor Author

Hi,

Here's the screenshot you requested (it looks better imo):

screen shot 2018-02-27 at 12 13 09 am

Having understood the path that default_kind() expected, I agree with you that no changes were needed. So I reverted back to the original code in fd675a6. The way I had it, it could have been simplified to:

default_kind = function(path) {
  path = normalizePath(path, '/', mustWork = FALSE)
  if (!grepl('/', path)) return('default')
  atype = dirname(path)
  if (!file.exists(file.path('archetypes', with_ext(atype, '.md')))) return('default')
  atype
}

But it does exactly the same thing as the version you had:

default_kind = function(path) {
  path = normalizePath(path, '/', mustWork = FALSE)
  if (!grepl('/', path)) return('default')
  atype = gsub('/.*', '.md', path)
  if (!file.exists(file.path('archetypes', atype))) return('default')
  gsub('/.*', '', path)
}

I also missed the code in the comments and it does indeed work, so I'm using your code there 7b18d1d. I tested it with both a .Rmd and a .md file extension for the blog post.

Thanks for your patience! I'm new to PRs despite using R and GitHub for years now :P

Will GitHub automatically squash the history? Or do I have to run something to make it cleaner?

Best,
Leo

Copy link
Member

@yihui yihui left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't worry about GIT: I can choose to squash the commits on Github (as I always do). Thanks!

@yihui yihui merged commit c2f14cc into rstudio:master Feb 27, 2018
yihui added a commit that referenced this pull request Feb 27, 2018
@lcolladotor
Copy link
Contributor Author

Cool, thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants