sampling_strategy and methods are the two fields curators most often paste straight out of a source PDF or Word document, and that pasted text carries the source's own line breaks with it – one per visual line, occasionally splitting a word at a hyphen. Left as literal newlines in metadata.yml, those continuation lines are usually indented no more than the field's own key, since that's how the text looked when pasted; YAML then can't tell the difference between a wrapped continuation and a new key, and the file fails to parse (each wrapped line is read as an attempt at a new mapping key). This finds every fields entry in the file – methods occurs once per trait – and rejoins its continuation lines into one, using util_dewrap_join().

metadata_dewrap_fields(
  dataset_id,
  path_data = "data",
  fields = c("sampling_strategy", "methods"),
  dehyphenate = FALSE,
  dry_run = TRUE
)

Arguments

dataset_id

Identifier for a study, or a vector of them

path_data

Path to folder with data

fields

Names of the fields to dewrap

dehyphenate

Remove the hyphen at a line break that splits a lowercase word, rather than keep it? Default FALSE.

dry_run

Print a preview without writing anything. Default TRUE; pass FALSE to apply the rewrite.

Value

A data frame with one row per field that had continuation lines to join, with columns dataset_id, field, line, n_lines_joined, hyphen_joins (the words formed at line breaks ending in a hyphen, separated by "; ", or NA) and value (the joined value as written). Returned invisibly when dry_run = FALSE.

Details

A continuation line is anything following the field's key: line up to a blank line, or the next line at or above the key's own indentation that is itself structurally a new field or a new list item (- ) – not indentation on its own, since pasted text is often left completely unindented, shallower than the key it belongs to. A new field is a key written as a single word (word:), or one of the file's var_in values: a traits entry can carry a field named after a context's var_in, such as stem type: branch, which contains a space. So this relies on the paragraph never containing a line of the form word: or - ... at or above the key's own indentation – true of ordinary prose, and when it is not, the joined field stops early and the file still fails to parse, rather than a key being absorbed into the text.

At a line that ends in a hyphen the hyphen is kept and the next line joined to it without a space; pass dehyphenate = TRUE to remove it instead where it splits a lowercase word (see util_dewrap_join()). The preview lists every such join, since neither rule is right every time.

Only fields are rewritten; everything else in the file – other fields, comments, formatting – is left exactly as it was. custom_R_code in particular is never touched: its line breaks and indentation are intentional, so its whole block is copied through verbatim (a line of code that happens to read like methods: ... is never taken for a field), and asking for it in fields is an error. This does not round-trip the file through write_metadata(), which would reflow the whole file into an unreviewable diff.

A field already spread over several properly indented lines is valid YAML, but is joined all the same. That changes nothing in the value read except at a line ending in a hyphen, which YAML reads as "oven- dried" and this writes as "oven-dried". Run this on the dataset being fixed rather than across a whole compilation: run on austraits.build, it rewrites 400 of 442 files that already parse.

Examples

if (FALSE) { # \dontrun{
metadata_dewrap_fields("Falster_2003")
metadata_dewrap_fields("Falster_2003", dry_run = FALSE)
} # }