buildLabelMap static method

Map<String, String> buildLabelMap(
  1. List<LatexNode> children
)

Precomputes the label -> number mapping for the entire document

Implementation

static Map<String, String> buildLabelMap(List<LatexNode> children) {
  final labelToNumber = <String, String>{};
  int counter = 0;

  String nextNumber() {
    counter++;
    return counter.toString();
  }

  void process(List<LatexNode> nodes) {
    for (final node in nodes) {
      if (node is EnvironmentNode) {
        if (_shouldNumberEnvironment(node.name, node.content)) {
          final number = nextNumber();
          for (final labelKey in _findLabelsInNodes(node.content)) {
            labelToNumber[labelKey] = number;
          }
        }
        process(node.content); // Recursive call
      } else if (node is AlignedNode) {
        if (_shouldNumberAligned(node)) {
          final number = nextNumber();
          for (final labelKey in _findLabelsInAllRows(node.rows)) {
            labelToNumber[labelKey] = number;
          }
        }
        for (final row in node.rows) {
          process(row);
        }
      } else if (node is MultlineNode) {
        if (_shouldNumberMultline(node)) {
          final number = nextNumber();
          for (final labelKey in _findLabelsInNodes(node.lines)) {
            labelToNumber[labelKey] = number;
          }
        }
      } else if (node is EqnarrayNode) {
        if (_shouldNumberEqnarray(node)) {
          final number = nextNumber();
          for (final labelKey in _findLabelsInAllRows(node.rows)) {
            labelToNumber[labelKey] = number;
          }
        }
        for (final row in node.rows) {
          process(row);
        }
      } else if (node is SubequationsNode) {
        final parentNumber = nextNumber();
        int subCounterCode = 'a'.codeUnitAt(0);

        for (final labelKey in _findLabelsDirectly(node.content)) {
          labelToNumber[labelKey] = parentNumber;
        }

        for (final child in node.content) {
          if (child is EnvironmentNode) {
            if (_shouldNumberEnvironment(child.name, child.content)) {
              final subNumber =
                  "$parentNumber${String.fromCharCode(subCounterCode)}";
              subCounterCode++;
              for (final labelKey in _findLabelsInNodes(child.content)) {
                labelToNumber[labelKey] = subNumber;
              }
            }
          } else if (child is AlignedNode) {
            if (_shouldNumberAligned(child)) {
              final subNumber =
                  "$parentNumber${String.fromCharCode(subCounterCode)}";
              subCounterCode++;
              for (final labelKey in _findLabelsInAllRows(child.rows)) {
                labelToNumber[labelKey] = subNumber;
              }
            }
          }
        }
      } else if (node is GroupNode) {
        process(node.childrenNodes);
      } else if (node is DocumentNode) {
        process(node.childrenNodes);
      } else if (node is DelimitedNode) {
        process(node.content);
      } else if (node is DisplayMathNode) {
        process(node.childrenNodes);
      } else if (node is InlineMathNode) {
        process(node.childrenNodes);
      } else {
        final children = node.children();
        if (children.isNotEmpty) {
          process(children);
        }
      }
    }
  }

  process(children);
  return labelToNumber;
}