2

Using R Markdown 2 and the YAML include statement I can easily customize the header section, the beginning and the end of the body section using in_header, before_body and after_body respectively, as described in the RMarkdown docu.

---
title: "Habits"
output:
  html_document:
    includes:
      in_header: header.html
      before_body: doc_prefix.html
      after_body: doc_suffix.html
---

How can I do the same for the footer section? (the output should be HTML)

A similar question has been asked here for PDF files and there is another answer for PDFs here. Still, I cannot transfer the solution to HTML footers as my knowledge of pandoc is too limited.

Thanks for your time!

Community
  • 1
  • 1
Mark Heckmann
  • 10,943
  • 4
  • 56
  • 88

1 Answers1

0

This is a workaround to manually inject HTML into the footer using R. I run this across all my documents after they have been generated to get a custom footer. Still a YAML-Pandoc solution as requested would be preferable.

library(stringr)
library(shiny)      # for HTML function

Function inject_code_after_body_tag will read in HTML file and add footer section after final body tag.

inject_code_after_body_tag <- function(file, include) 
{
  file.lines <- readLines(file, warn = FALSE, encoding = "UTF-8")
  include.lines <- readLines(include, warn = FALSE, encoding = "UTF-8")
  inc <- HTML(paste(include.lines, collapse = "\r\n"))
  l <- str_replace(file.lines,
                   perl("(?<=</body>)"),
                   inc)
  HTML(paste(l, collapse = "\r\n"))
}

Now, read in the file and inject code from footer.html

code <- inject_code_after_body_tag(file = "index.html", include = "include/footer.html")  

Finally, write the new code back into the index.html file

writeLines(code, "index.html")
Mark Heckmann
  • 10,943
  • 4
  • 56
  • 88