luaObj2Html method

String luaObj2Html(
  1. String title,
  2. LuaObject luaObj, {
  3. LuaObject? parent,
})

If the object's storage is a primitive value, then it tries to visit LuaDoc.keyValueHtml. If it is not excluded via LuaDoc.exclude, then it will render out to html. If the object's storage is that of a function or a table, then fields will also be visited and pushPath update the new anchor tag. After visiting, popPath is called to restore the previous depth information. The end result is a String of decorated html for the autodoc.

Implementation

String luaObj2Html(String title, LuaObject luaObj, {LuaObject? parent}) {
  String content = '';
  String header = '';
  if (luaObj.skipSemanitcs || (luaObj.isTable && luaObj.isNotFunc)) {
    final String anchor = pushPath(title);
    header += '<a id="$anchor"></a>';
    header +=
        '''
        <h3 class="lua-table">
        <a href="#$anchor">$title</a>: <b>table</b>
        </h3>
        ''';

    content += '<ul>';
    for (var MapEntry(:key, :value) in luaObj.fields?.entries ?? {}) {
      final String valueStr = switch (value) {
        final LuaObject obj => luaObj2Html(key, obj.deref(), parent: luaObj),
        _ => '',
      };
      content += valueStr;
    }
    content += '</ul>';
    popPath();
  } else if (luaObj.isFunc) {
    if (luaObj.funcDef != null) {
      final String anchor = pushPath(title);
      final FuncExpr def = luaObj.funcDef!;
      header += '<span>';
      header += '<a id="$anchor"></a>';
      header +=
          '''
          <h3 class="lua-func">
            <a href="#$anchor">${def.id}</a>
            ${def.argsHtml}
          </h3>
          ''';
      header += '</span>';
      popPath();
    } else if (luaObj.hasField('__call')) {
      content += '<i>Callable</i>';
    }
  } else {
    final String anchor = pushPath(title);
    final String dot = switch (parent) {
      null => '',
      _ => '.',
    };

    final Object? value = luaObj.value;
    header +=
        '''
        <span>
          <a id="$anchor"></a>
          <h3 class="lua-field">
            <a href="#$anchor">$dot$title</a>
          </h3>
        </span>
        ''';

    if (value != null) {
      /// Non-null values with a parent must pass the exclusion
      /// filter.
      if (!(parent?.doc?.exclude?.call(luaObj.id) ?? false)) {
        final stub = parent?.doc?.keyValueHtml?.call(luaObj.id) ?? '';
        if (stub.isNotEmpty) {
          content += '<div>$stub</div>';
        }
      }
    }
    popPath();
  }

  if (luaObj.doc?.includeHtml ?? false) {
    // Opinionated styling:
    // Prepend a table's fields and methods
    // with the doc html.
    // Otherwise, append doc html after values
    // and functions.
    content = switch (luaObj.isTable) {
      true => '${luaObj.doc!.html}$content',
      false => '$content${luaObj.doc!.html}',
    };

    content = '<div>$content</div>';
  }

  return '$header$content';
}