render method

String render(
  1. String input
)

Implementation

String render(String input) {
  var _result = input;

  for (final shortcode in shortcodeTemplates) {
    /*
     Inline shortcodes
    */

    final pattern = RegExp('\\{{2}< ${shortcode.name} ((?!\\/).)* />\\}{2}');
    final inlineMatches = pattern.allMatches(input);

    for (final match in inlineMatches) {
      final variables = _parseInlineShortcode(
        input.substring(match.start, match.end),
      );

      final output = shortcode.render(
        environment: environment,
        values: variables.getValues(),
      );

      _result = _result.replaceFirst(
        input.substring(match.start, match.end),
        output,
      );
    }

    /*
     Block shortcodes
    */

    final startPattern =
        RegExp('\\{{2}< ${shortcode.name}((?!\\/).)* >\\}{2}');
    final endPattern = RegExp('\\{{2}< /${shortcode.name} >\\}{2}');

    final startMatches = startPattern.allMatches(input);
    final endMatches = endPattern.allMatches(input);

    if (startMatches.length != endMatches.length) {
      log.error(
        'Body shortcodes must have both opening closing tag.',
        help: 'Invalid use of ${shortcode.name} shortcode.',
      );

      throw const BuildError(
        'Body shortcodes must have both opening closing tag.',
      );
    }

    for (var x = 0; x < startMatches.length; x++) {
      final startMatch = startMatches.elementAt(x);
      final endMatch = endMatches.elementAt(x);

      final variables = _parseBodyShortcode(
        input.substring(startMatch.start, endMatch.end),
      );
      final output = shortcode.render(
        environment: environment,
        values: variables.getValues(),
      );

      _result = _result.replaceFirst(
        input.substring(startMatch.start, endMatch.end),
        output,
      );
    }
  }
  return _result;
}