Skip to content

Commit 664f052

Browse files
committed
ability to define html_document navigation bar using simple yaml format
1 parent 2733ef0 commit 664f052

8 files changed

Lines changed: 150 additions & 10 deletions

File tree

.Rbuildignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,4 @@ README\.(Rmd|md|html)
55
CONTRIBUTING\.(Rmd|md|html)
66
PANDOC\.(Rmd|md|html)
77
^\.travis\.yml$
8+
tests/testthat/site/.*\.html

R/html_document.R

Lines changed: 71 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -85,11 +85,30 @@
8585
#'@section Navigation Bars:
8686
#'
8787
#' If you have a set of html documents which you'd like to provide a common
88-
#' global navigation bar for, you can include a "_navbar.html" file within the
89-
#' same directory as your html document and it will automatically be included
90-
#' at the top of the document. For a simple example of including a navigation
91-
#' bar see
92-
#' .\href{https://github.com/rstudio/rmarkdown-website/blob/master/_navbar.html}{https://github.com/rstudio/rmarkdown-website/blob/master/_navbar.html}
88+
#' global navigation bar for, you can include a "_navbar.yml" or "_navbar.html"
89+
#' file within the same directory as your html document and it will automatically
90+
#' be included at the top of the document.
91+
#'
92+
#' The "_navbar.yml" file includes \code{title}, \code{type}, \code{left}, and
93+
#' \code{right} fields (to define menu items for the left and right of the navbar
94+
#' resspectively). Menu items include \code{title} and \code{href} fields. For example:
95+
#'
96+
#' \preformatted{ title: "My Website"
97+
#' type: default
98+
#' left:
99+
#' - title: "Home"
100+
#' href: index.Rmd
101+
#' - title: "Other"
102+
#' href: other.Rmd
103+
#' right:
104+
#' - title: GitHub
105+
#' href: https://github.com}
106+
#' The \code{type} field is optional and can take the value "default" or "inverse" (which
107+
#' provides a different color scheme for the navigation bar).
108+
#'
109+
#' Alternatively, you can include a "_navbar.html" file which is a full HTML definition
110+
#' of a bootstrap navigation bar. For a simple example of including a navigation bar see
111+
#' \href{https://github.com/rstudio/rmarkdown-website/blob/master/_navbar.html}{https://github.com/rstudio/rmarkdown-website/blob/master/_navbar.html}.
93112
#' For additional documentation on creating Bootstrap navigation bars see
94113
#' \href{http://getbootstrap.com/components/#navbar}{http://getbootstrap.com/components/#navbar}.
95114
#'
@@ -280,6 +299,14 @@ html_document <- function(toc = FALSE,
280299

281300
# add navbar to includes if necessary
282301
navbar <- file.path(normalize_path(dirname(input_file)), "_navbar.html")
302+
303+
# if there is no _navbar.html look for a _navbar.yml
304+
if (!file.exists(navbar)) {
305+
navbar_yaml <- file.path(dirname(navbar), "_navbar.yml")
306+
if (file.exists(navbar_yaml))
307+
navbar <- navbar_html_from_yaml(navbar_yaml)
308+
}
309+
283310
if (file.exists(navbar)) {
284311
if (is.null(includes))
285312
includes <- list()
@@ -419,6 +446,45 @@ pandoc_body_padding_variable_args <- function(theme) {
419446
pandoc_variable_arg("header_padding", headerPadding))
420447
}
421448

449+
navbar_html_from_yaml <- function(navbar_yaml) {
450+
451+
# parse the yaml
452+
navbar <- yaml_load_file_utf8(navbar_yaml)
453+
454+
# title and type
455+
if (is.null(navbar$title))
456+
navbar$title <- ""
457+
if (is.null(navbar$type))
458+
navbar$type <- "default"
459+
460+
# menu entries
461+
left <- navbar_links_html(navbar$left)
462+
right <- navbar_links_html(navbar$right)
463+
464+
# build the navigation bar and return it as a temp file
465+
template_file <- rmarkdown_system_file("rmd/h/_navbar.html")
466+
template <- paste(readLines(template_file), collapse = "\n")
467+
navbar_html <- sprintf(template,
468+
navbar$type,
469+
navbar$title,
470+
left,
471+
right)
472+
as_tmpfile(navbar_html)
473+
}
474+
475+
navbar_links_html <- function(links) {
476+
if (!is.null(links)) {
477+
tags <- lapply(links, function(x) {
478+
href <- x$href
479+
if (!grepl("^http", href))
480+
href <- gsub("R?md", "html", href)
481+
tags$li(tags$a(href = href, x$title))
482+
})
483+
as.character(tagList(tags))
484+
} else {
485+
""
486+
}
487+
}
422488

423489

424490

inst/NEWS

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ rmarkdown 0.9.6 (unreleased)
44
* Added `render_site` and related functions for rendering collections of
55
documents within a directory as a website.
66

7+
* Ability to define html_document navigation bar using simple yaml format.
8+
79
* Discover `LaTeX` dependencies and add them to the `.tex` preamble (#647)
810

911
* Change `fig_caption` default to TRUE for all formats

inst/rmd/h/_navbar.html

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
2+
<div class="navbar navbar-%s navbar-fixed-top" role="navigation">
3+
<div class="container">
4+
<div class="navbar-header">
5+
<button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#navbar">
6+
<span class="icon-bar"></span>
7+
<span class="icon-bar"></span>
8+
<span class="icon-bar"></span>
9+
</button>
10+
<a class="navbar-brand" href="index.html">%s</a>
11+
</div>
12+
<div id="navbar" class="navbar-collapse collapse">
13+
<ul class="nav navbar-nav">
14+
%s
15+
</ul>
16+
<ul class="nav navbar-nav navbar-right">
17+
%s
18+
</ul>
19+
</div><!--/.nav-collapse -->
20+
</div><!--/.container -->
21+
</div><!--/.navbar -->

man/html_document.Rd

Lines changed: 24 additions & 5 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tests/testthat/site/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
*.html

tests/testthat/site/_navbar.yml

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
title: "Navigation Bar"
2+
type: default
3+
left:
4+
- title: "Home"
5+
href: index.md
6+
- title: "Page A"
7+
href: PageA.html
8+
- title: "Page B"
9+
href: PageB.Rmd
10+
- title: "Page C"
11+
href: PageC.md
12+
right:
13+
- title: "GitHub"
14+
href: https://github.com
15+

tests/testthat/site/site.Rproj

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
Version: 1.0
2+
3+
RestoreWorkspace: Default
4+
SaveWorkspace: Default
5+
AlwaysSaveHistory: Default
6+
7+
EnableCodeIndexing: Yes
8+
UseSpacesForTab: Yes
9+
NumSpacesForTab: 2
10+
Encoding: UTF-8
11+
12+
RnwWeave: Sweave
13+
LaTeX: pdfLaTeX
14+
15+
BuildType: Website

0 commit comments

Comments
 (0)