|
85 | 85 | #'@section Navigation Bars: |
86 | 86 | #' |
87 | 87 | #' 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}. |
93 | 112 | #' For additional documentation on creating Bootstrap navigation bars see |
94 | 113 | #' \href{http://getbootstrap.com/components/#navbar}{http://getbootstrap.com/components/#navbar}. |
95 | 114 | #' |
@@ -280,6 +299,14 @@ html_document <- function(toc = FALSE, |
280 | 299 |
|
281 | 300 | # add navbar to includes if necessary |
282 | 301 | 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 | + |
283 | 310 | if (file.exists(navbar)) { |
284 | 311 | if (is.null(includes)) |
285 | 312 | includes <- list() |
@@ -419,6 +446,45 @@ pandoc_body_padding_variable_args <- function(theme) { |
419 | 446 | pandoc_variable_arg("header_padding", headerPadding)) |
420 | 447 | } |
421 | 448 |
|
| 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 | +} |
422 | 488 |
|
423 | 489 |
|
424 | 490 |
|
0 commit comments