## ----resource-tables, include=FALSE-------------------------------------------
# Locate the resources supplied by this package.
resources_directory <- system.file(package = "form.model")
# Read the package version used in the downloadable archive name.
resources_version <- read.dcf(file.path(resources_directory, "DESCRIPTION"), fields = "Version")[[1L]]
# Describe the files shared by the schema and example download tables.
resources_descriptions <- c(
  "model-1.0.0.yaml" = "Form model schema.",
  "form.yaml" = "Survey form model.",
  "form.xlsx" = "Populated survey form.",
  "form-empty.xlsx" = "Empty survey form.",
  "references-1.0.0.yaml" = "Reference configuration schema.",
  "mapping-1.0.0.yaml" = "Catalogue mapping schema.",
  "README.md" = "Directory layout and usage notes.",
  "references.private.example.yaml" = "Public code lists and a private HTTPS registry template.",
  "references.yaml" = "Target code lists and the survey contact registry.",
  "references-source.yaml" = "Source code lists using technical identifiers.",
  "mapping.yaml" = "Station and observer correspondences between the catalogues.",
  "catalogue.csv" = "Table IDs, paths, versions, source dates and checksums.",
  "catalogue.yaml" = "Catalogue publication metadata and checksum.",
  "catalogue_aliases.csv" = "Aliases attached to individual catalogue lookups.",
  "stations.csv" = "Station reference records.",
  "observers.csv" = "Observer reference records.",
  "contacts.csv" = "Contact registry records.")
# Render website downloads, or file names for the installed package vignette.
resource_table <- function(directory, descriptions = resources_descriptions) {
  # List every file directly inside the selected resource directory.
  files <- list.files(file.path(resources_directory, directory))
  # Leave subdirectories to their own download tables.
  files <- files[!dir.exists(file.path(resources_directory, directory, files))]
  # Display the packaged file names without depending on a network connection.
  labels <- sprintf("`%s`", files)
  # Link each file to its generated website copy when building pkgdown.
  if (identical(Sys.getenv("IN_PKGDOWN"), "true")) {
    # Resolve ordinary downloads relative to this article.
    targets <- paste0("../", file.path(directory, files))
    # Absolute Markdown URLs prevent pkgdown from rewriting raw downloads to HTML pages.
    targets[endsWith(files, ".md")] <- paste0("https://ultreia-io.github.io/form.model/",
      file.path(directory, files[endsWith(files, ".md")]))
    # Render a download link for each original file.
    labels <- sprintf("[`%s`](%s)", files, targets)
  }
  # Select the descriptions for these files.
  purposes <- unname(descriptions[files])
  # Keep new files downloadable until their descriptions are added.
  purposes[is.na(purposes)] <- "Package resource."
  # Print a Markdown table for the current output format.
  cat(knitr::kable(data.frame(File = labels, Purpose = purposes), format = "pipe"), sep = "\n")
}

## ----resource-bundle, echo=FALSE, results='asis'------------------------------
# Offer the complete archive on the website and an offline alternative in the installed guide.
if (identical(Sys.getenv("IN_PKGDOWN"), "true")) {
  # Link the archive containing the original schema and example directories.
  cat(sprintf("[Download all schemas and examples (%s, ZIP)](../form.model-resources-%s.zip).\n",
    resources_version, resources_version))
} else {
  # Explain where the same files are available without downloading anything.
  cat("All schemas and examples are included in the installed package.\n")
}

## ----installed-resources, eval=FALSE------------------------------------------
# # Locate the versioned schemas.
# system.file("schema", package = "form.model")
# # Locate all examples, including the private registry template.
# system.file("examples", package = "form.model")

## ----schema-downloads, echo=FALSE, results='asis'-----------------------------
# List every packaged schema with its download link.
resource_table("schema")

## ----resources-reference-yaml, echo=FALSE, results='asis'---------------------
# Display the bundled reference configuration without copying it into the documentation source.
cat("```yaml\n", paste(readLines(file.path(resources_directory, "examples", "survey", "references.yaml")),
  collapse = "\n"), "\n```\n", sep = "")

## ----resources-mapping-yaml, echo=FALSE, results='asis'-----------------------
# Display the mapping used by the runnable catalogue mapping guide.
cat("```yaml\n", paste(readLines(file.path(resources_directory, "examples", "survey", "mapping.yaml")),
  collapse = "\n"), "\n```\n", sep = "")

## ----resources-correspondence-csv, echo=FALSE, results='asis'-----------------
# Display the station correspondence file supplied with the survey example.
cat("```csv\n", paste(readLines(file.path(resources_directory, "examples", "survey", "correspondences", "stations.csv")),
  collapse = "\n"), "\n```\n", sep = "")

## ----resources-survey, echo=FALSE, results='asis'-----------------------------
# List the survey form files, configurations and usage notes.
resource_table("examples/survey")

## ----resources-correspondences, echo=FALSE, results='asis'--------------------
# Describe the station file as a correspondence rather than a reference table.
resource_table("examples/survey/correspondences",
  c("stations.csv" = "Explicit source station IDs and target station codes."))

## ----resources-source-catalogue, echo=FALSE, results='asis'-------------------
# List the complete source catalogue, including metadata and lookup aliases.
resource_table("examples/survey/catalogue-source/code_lists")

## ----resources-target-catalogue, echo=FALSE, results='asis'-------------------
# List the complete target code-list catalogue.
resource_table("examples/survey/catalogue/code_lists")

## ----resources-registry, echo=FALSE, results='asis'---------------------------
# List the contact registry used by the reference preparation examples.
resource_table("examples/survey/catalogue/registries")

## ----resources-private-template, echo=FALSE, results='asis'-------------------
# List the private HTTPS template and overview of the bundled examples.
resource_table("examples")

## ----resources-online, eval=FALSE---------------------------------------------
# # Load the catalogue retrieval functions.
# library(form.model)
# # Choose a temporary cache for these downloads.
# resources_cache_directory <- tempfile("survey-resources-")
# # Configure the published target code lists.
# reference_source_configuration <- reference_source(
#   "https://ultreia-io.github.io/form.model/examples/survey/catalogue/code_lists/", resources_cache_directory)
# # Retrieve the station and observer tables, verifying their checksums.
# reference_values <- get_reference_tables(c("observers", "stations"), reference_source_configuration)
# # Configure the published contact registry.
# reference_registry_source <- reference_source(
#   "https://ultreia-io.github.io/form.model/examples/survey/catalogue/registries/", resources_cache_directory)
# # Retrieve and verify the contact table.
# reference_registry_tables <- get_reference_tables("contacts", reference_registry_source)
# # Remove the temporary files created by this example.
# unlink(resources_cache_directory, recursive = TRUE)

