getPreprocessedText method

String getPreprocessedText(
  1. String text
)

Get preprocessed text with code blocks highlighted

Implementation

String getPreprocessedText(String text) {
  String preprocessedText = getCleanedText(text, tabulationSpaceCount);
  List<String> lineArray = preprocessedText.split('\n');

  for (int lineIndex = 0; lineIndex < lineArray.length; lineIndex++) {
    String line = lineArray[lineIndex].trim();

    if (line.isEmpty) {
      lineArray[lineIndex] = "";
    } else if (line.startsWith(":::")) {
      // Handle code blocks with syntax highlighting
      if (line.startsWith(":::¨c¨") ||
          line.startsWith(":::¨h¨") ||
          line.startsWith(":::¨cpp¨") ||
          line.startsWith(":::¨hpp¨") ||
          line.startsWith(":::¨cxx¨") ||
          line.startsWith(":::¨hxx¨") ||
          line.startsWith(":::¨cs¨") ||
          line.startsWith(":::¨d¨") ||
          line.startsWith(":::¨java¨") ||
          line.startsWith(":::¨js¨") ||
          line.startsWith(":::¨ts¨")) {
        String fileExtension = line.split('¨')[1];
        String style = line.substring(line.substring(4).indexOf('¨') + 5);

        String codeText = "";
        int postLineIndex;

        for (postLineIndex = lineIndex + 1;
            postLineIndex < lineArray.length;
            postLineIndex++) {
          String codeLine = lineArray[postLineIndex];

          if (codeLine.trim() == ":::") {
            break;
          } else {
            codeText += codeLine;
            codeText += '\n';
          }
        }

        if (codeText.isNotEmpty) {
          codeText = getColorizedText(codeText, "", fileExtension, style);
          List<String> codeLineArray = codeText.split('\n');

          for (int codeLineIndex = 0;
              codeLineIndex < codeLineArray.length &&
                  lineIndex < postLineIndex;
              codeLineIndex++) {
            lineArray[lineIndex] = codeLineArray[codeLineIndex];
            lineIndex++;
          }
        }
      }
    }
  }

  return lineArray.join('\n');
}