|
| 1 | +// <copyright file="YamlReader.cs" company="Datadog"> |
| 2 | +// Unless explicitly stated otherwise all files in this repository are licensed under the Apache 2 License. |
| 3 | +// This product includes software developed at Datadog (https://www.datadoghq.com/). Copyright 2017 Datadog, Inc. |
| 4 | +// </copyright> |
| 5 | + |
| 6 | +using System.Collections.Generic; |
| 7 | +using System.Text; |
| 8 | + |
| 9 | +namespace Datadog.Trace.SourceGenerators.Helpers |
| 10 | +{ |
| 11 | + /// <summary> |
| 12 | + /// Simple YAML parser for reading documentation strings from YAML files. |
| 13 | + /// Supports basic YAML features needed for configuration documentation. |
| 14 | + /// </summary> |
| 15 | + internal static class YamlReader |
| 16 | + { |
| 17 | + /// <summary> |
| 18 | + /// Parses a YAML file containing configuration key documentation. |
| 19 | + /// Expects format: KEY: | followed by multi-line documentation |
| 20 | + /// </summary> |
| 21 | + public static Dictionary<string, string> ParseDocumentation(string yamlContent) |
| 22 | + { |
| 23 | + var result = new Dictionary<string, string>(); |
| 24 | + var lines = yamlContent.Replace("\r\n", "\n").Split('\n'); |
| 25 | + |
| 26 | + string? currentKey = null; |
| 27 | + var currentDoc = new StringBuilder(); |
| 28 | + var inMultiLine = false; |
| 29 | + var baseIndent = 0; |
| 30 | + |
| 31 | + for (int i = 0; i < lines.Length; i++) |
| 32 | + { |
| 33 | + var line = lines[i]; |
| 34 | + |
| 35 | + // Skip empty lines and comments when not in multi-line |
| 36 | + if (!inMultiLine && (string.IsNullOrWhiteSpace(line) || line.TrimStart().StartsWith("#"))) |
| 37 | + { |
| 38 | + continue; |
| 39 | + } |
| 40 | + |
| 41 | + // Check for new key (starts at column 0, contains colon) |
| 42 | + if (!inMultiLine && line.Length > 0 && line[0] != ' ' && line.Contains(":")) |
| 43 | + { |
| 44 | + // Save previous key if exists |
| 45 | + if (currentKey != null) |
| 46 | + { |
| 47 | + result[currentKey] = currentDoc.ToString().TrimEnd(); |
| 48 | + currentDoc.Clear(); |
| 49 | + } |
| 50 | + |
| 51 | + var colonIndex = line.IndexOf(':'); |
| 52 | + currentKey = line.Substring(0, colonIndex).Trim(); |
| 53 | + |
| 54 | + // Check if it's a multi-line string (|) |
| 55 | + var afterColon = line.Substring(colonIndex + 1).Trim(); |
| 56 | + if (afterColon == "|" || afterColon == ">") |
| 57 | + { |
| 58 | + inMultiLine = true; |
| 59 | + baseIndent = -1; // Will be set on first content line |
| 60 | + } |
| 61 | + else if (!string.IsNullOrEmpty(afterColon)) |
| 62 | + { |
| 63 | + // Single line value |
| 64 | + currentDoc.Append(afterColon); |
| 65 | + result[currentKey] = currentDoc.ToString(); |
| 66 | + currentDoc.Clear(); |
| 67 | + currentKey = null; |
| 68 | + } |
| 69 | + |
| 70 | + continue; |
| 71 | + } |
| 72 | + |
| 73 | + // Handle multi-line content |
| 74 | + if (inMultiLine && currentKey != null) |
| 75 | + { |
| 76 | + // Check if we've reached the next key (no indentation) |
| 77 | + if (line.Length > 0 && line[0] != ' ' && line.Contains(":")) |
| 78 | + { |
| 79 | + // Save current key and process this line as new key |
| 80 | + result[currentKey] = currentDoc.ToString().TrimEnd(); |
| 81 | + currentDoc.Clear(); |
| 82 | + inMultiLine = false; |
| 83 | + |
| 84 | + var colonIndex = line.IndexOf(':'); |
| 85 | + currentKey = line.Substring(0, colonIndex).Trim(); |
| 86 | + var afterColon = line.Substring(colonIndex + 1).Trim(); |
| 87 | + if (afterColon == "|" || afterColon == ">") |
| 88 | + { |
| 89 | + inMultiLine = true; |
| 90 | + baseIndent = -1; |
| 91 | + } |
| 92 | + |
| 93 | + continue; |
| 94 | + } |
| 95 | + |
| 96 | + // Determine base indentation from first content line |
| 97 | + if (baseIndent == -1 && line.Length > 0 && line[0] == ' ') |
| 98 | + { |
| 99 | + baseIndent = 0; |
| 100 | + while (baseIndent < line.Length && line[baseIndent] == ' ') |
| 101 | + { |
| 102 | + baseIndent++; |
| 103 | + } |
| 104 | + } |
| 105 | + |
| 106 | + // Add content line (remove base indentation) |
| 107 | + if (line.Length > 0) |
| 108 | + { |
| 109 | + var contentStart = 0; |
| 110 | + while (contentStart < line.Length && contentStart < baseIndent && line[contentStart] == ' ') |
| 111 | + { |
| 112 | + contentStart++; |
| 113 | + } |
| 114 | + |
| 115 | + if (currentDoc.Length > 0) |
| 116 | + { |
| 117 | + currentDoc.AppendLine(); |
| 118 | + } |
| 119 | + |
| 120 | + currentDoc.Append(line.Substring(contentStart)); |
| 121 | + } |
| 122 | + else |
| 123 | + { |
| 124 | + // Empty line in multi-line content |
| 125 | + if (currentDoc.Length > 0) |
| 126 | + { |
| 127 | + currentDoc.AppendLine(); |
| 128 | + } |
| 129 | + } |
| 130 | + } |
| 131 | + } |
| 132 | + |
| 133 | + // Save last key |
| 134 | + if (currentKey != null) |
| 135 | + { |
| 136 | + result[currentKey] = currentDoc.ToString().TrimEnd(); |
| 137 | + } |
| 138 | + |
| 139 | + return result; |
| 140 | + } |
| 141 | + } |
| 142 | +} |
0 commit comments