YAML Libraries

Trevor DeVore lists at mangomultimedia.com
Sun Jan 19 09:45:33 EST 2020

On Fri, Jan 17, 2020 at 2:19 PM Sannyasin Brahmanathaswami via use-livecode
<use-livecode at lists.runrev.com> wrote:

> I found the handler
> ## Monte's YAMLToArray command
> constant kMultiLineModeNone = 0
> constant kMultiLineModeLiteral = 1
> constant kMultiLineModeFolded = 2
> command YAMLToArray pYaml
> [snip] in the levure initialization behavior.
> I think that's it. There is no "ArrayToYaml"  but that is not needed.

BR - I'm glad you were able to locate the handler you need in the source
code. Here is a handler I use for printing out arrays as YAML when I'm
debugging or logging. It most likely could use some improvements but works
well for what I do with it.

Trevor DeVore

Summary: Prints an array using YAML.

Returns: YAML string
function printArray pArray
  local tKeys, tKey, tYAML

  put the keys of pArray into tKeys
  sort tKeys numeric

  repeat for each line tKey in tKeys
    put _outputKeyAsYAML(pArray, tKey, 1) & cr after tYAML
  end repeat
  delete the last char of tYAML

  return tYAML
end printArray

private function _outputKeyAsYAML pArrayA, pKey, pLevel
  local tIndent, tKey, tStr, i

  repeat with i = 1 to pLevel-1
    put space & space after tIndent
  end repeat
  put tIndent after tStr
  put pKey & ": " after tStr

  if pArrayA[pKey] is an array then
    put cr after tStr
    repeat for each key tKey in pArrayA[pKey]
      put _outputKeyAsYAML(pArrayA[pKey], tKey, pLevel+1) & cr after tStr
    end repeat
    if pArrayA[pKey] contains CR then
      put space & space after tIndent
      put "|+" & cr & tIndent after tStr
      replace CR with CR & tIndent in pArrayA[pKey]
    end if

    if pArrayA[pKey] contains "'" then
      replace "'" with "''" in pArrayA[pKey]
      put "'" & pArrayA[pKey] & "'" & cr after tStr
      put pArrayA[pKey] & cr after tStr
    end if
  end if
  delete the last char of tStr

  return tStr
end _outputKeyAsYAML

More information about the use-livecode mailing list