|
| 1 | +using System.Text; |
| 2 | +using ChartForgeX.Markup; |
| 3 | +using ChartForgeX.VisualArtifacts; |
| 4 | + |
| 5 | +/// <summary> |
| 6 | +/// Writes examples generated from ChartForgeX v1 markup fences. |
| 7 | +/// </summary> |
| 8 | +internal static class MarkupExamples { |
| 9 | + public static void Write(string output) { |
| 10 | + foreach (var example in Examples) SaveExample(output, example); |
| 11 | + } |
| 12 | + |
| 13 | + private static void SaveExample(string output, MarkupExample example) { |
| 14 | + var parser = new VisualMarkupParser(); |
| 15 | + var result = parser.Parse(example.Source); |
| 16 | + if (result.HasErrors || result.Artifacts.Count != 1) { |
| 17 | + var diagnostics = string.Join(Environment.NewLine, result.Diagnostics.Select(diagnostic => diagnostic.Line.ToString(System.Globalization.CultureInfo.InvariantCulture) + ": " + diagnostic.Severity + ": " + diagnostic.Message)); |
| 18 | + throw new InvalidOperationException("Markup example '" + example.Name + "' did not produce exactly one artifact." + Environment.NewLine + diagnostics); |
| 19 | + } |
| 20 | + |
| 21 | + var artifact = result.Artifacts[0]; |
| 22 | + artifact.SaveSvg(Path.Combine(output, example.Name + ".svg")); |
| 23 | + artifact.SaveHtml(Path.Combine(output, example.Name + ".html")); |
| 24 | + artifact.SavePng(Path.Combine(output, example.Name + ".png")); |
| 25 | + WriteMarkupSource(output, example.Name, example.Source); |
| 26 | + WriteCSharpSource(output, example); |
| 27 | + } |
| 28 | + |
| 29 | + private static void WriteMarkupSource(string output, string name, string source) => |
| 30 | + File.WriteAllText(Path.Combine(output, name + ".cfx.md"), Normalize(source), Encoding.UTF8); |
| 31 | + |
| 32 | + private static void WriteCSharpSource(string output, MarkupExample example) { |
| 33 | + var code = "using System;\n" + |
| 34 | + "using System.Linq;\n" + |
| 35 | + "using ChartForgeX.Markup;\n" + |
| 36 | + "using ChartForgeX.VisualArtifacts;\n\n" + |
| 37 | + "var markdown = @\"" + Normalize(example.Source).Replace("\"", "\"\"", StringComparison.Ordinal) + "\";\n\n" + |
| 38 | + "var result = new VisualMarkupParser().Parse(markdown);\n" + |
| 39 | + "if (result.HasErrors) {\n" + |
| 40 | + " throw new InvalidOperationException(string.Join(Environment.NewLine, result.Diagnostics));\n" + |
| 41 | + "}\n\n" + |
| 42 | + "var artifact = result.Artifacts.Single();\n" + |
| 43 | + "artifact.SaveSvg(\"" + example.Name + ".svg\");\n" + |
| 44 | + "artifact.SavePng(\"" + example.Name + ".png\");\n" + |
| 45 | + "artifact.SaveHtml(\"" + example.Name + ".html\");\n"; |
| 46 | + File.WriteAllText(Path.Combine(output, example.Name + ".csharp.txt"), code.Replace("\r\n", "\n"), Encoding.UTF8); |
| 47 | + } |
| 48 | + |
| 49 | + private static string Normalize(string source) => source.Replace("\r\n", "\n").Trim() + "\n"; |
| 50 | + |
| 51 | + private static readonly MarkupExample[] Examples = { |
| 52 | + new( |
| 53 | + "markup-chart-release-trend", |
| 54 | + """ |
| 55 | +```chartforgex chart v1 |
| 56 | +id release-trend |
| 57 | +title Release Readiness Trend |
| 58 | +subtitle Multi-series chart parsed from native v1 markup |
| 59 | +type smooth-line |
| 60 | +size 920x520 |
| 61 | +labels Intake, Build, Test, Package, Publish |
| 62 | +
|
| 63 | +series Ready type smoothline values 34 48 62 74 88 color #2563EB |
| 64 | +series Blocked type smoothline values 18 14 11 7 4 color #DC2626 |
| 65 | +annotation hline 80 "ship gate" color=#059669 |
| 66 | +``` |
| 67 | +"""), |
| 68 | + new( |
| 69 | + "markup-flow-approval", |
| 70 | + """ |
| 71 | +```chartforgex flow v1 |
| 72 | +id approval-flow |
| 73 | +title Approval Flow |
| 74 | +subtitle FlowArtifact parsed from native v1 markup, statically previewed through topology |
| 75 | +size 1120x620 |
| 76 | +layout layered |
| 77 | +direction left-to-right |
| 78 | +
|
| 79 | +lanes: |
| 80 | +| id | label | status | |
| 81 | +| --- | --- | --- | |
| 82 | +| author | Author | positive | |
| 83 | +| review | Review | warning | |
| 84 | +| release | Release | neutral | |
| 85 | +
|
| 86 | +steps: |
| 87 | +| id | label | kind | lane | status | subtitle | badge | |
| 88 | +| --- | --- | --- | --- | --- | --- | --- | |
| 89 | +| draft | Draft change | start | author | positive | C# or markdown source | 1 | |
| 90 | +| validate | Validate contract | process | review | positive | Parser plus render checks | 2 | |
| 91 | +| approve | Approved? | decision | review | warning | Human gate | ? | |
| 92 | +| publish | Publish package | end | release | positive | Release artifact | 3 | |
| 93 | +
|
| 94 | +connectors: |
| 95 | +| from | to | label | kind | status | |
| 96 | +| --- | --- | --- | --- | --- | |
| 97 | +| draft | validate | submit | flow | positive | |
| 98 | +| validate | approve | evidence | dependency | positive | |
| 99 | +| approve | publish | yes | flow | positive | |
| 100 | +| approve | draft | changes | retry | warning | |
| 101 | +``` |
| 102 | +"""), |
| 103 | + new( |
| 104 | + "markup-sequence-incident", |
| 105 | + """ |
| 106 | +```chartforgex sequence v1 |
| 107 | +id incident-sequence |
| 108 | +title Incident Sequence |
| 109 | +subtitle SequenceArtifact parsed from native v1 markup |
| 110 | +size 960x560 |
| 111 | +
|
| 112 | +participants: |
| 113 | +| id | label | kind | |
| 114 | +| --- | --- | --- | |
| 115 | +| user | User | actor | |
| 116 | +| api | API | participant | |
| 117 | +| db | Database | database | |
| 118 | +
|
| 119 | +messages: |
| 120 | +| from | to | text | style | activate | |
| 121 | +| --- | --- | --- | --- | --- | |
| 122 | +| user | api | Submit request | solid | true | |
| 123 | +| api | db | Store event | dashed | false | |
| 124 | +| db | api | Stored | dashed | false | |
| 125 | +| api | user | Accepted | solid | false | |
| 126 | +
|
| 127 | +notes: |
| 128 | +| placement | participant | text | step | |
| 129 | +| --- | --- | --- | --- | |
| 130 | +| rightOf | api | Processing and validation | 1 | |
| 131 | +
|
| 132 | +blocks: |
| 133 | +| kind | text | start | end | |
| 134 | +| --- | --- | --- | --- | |
| 135 | +| loop | Retry on transient failure | 0 | 2 | |
| 136 | +``` |
| 137 | +"""), |
| 138 | + new( |
| 139 | + "markup-timeline-release-plan", |
| 140 | + """ |
| 141 | +```chartforgex timeline v1 |
| 142 | +id release-plan |
| 143 | +title Release Plan |
| 144 | +subtitle Timeline/Gantt chart parsed from native v1 markup |
| 145 | +type gantt |
| 146 | +size 980x560 |
| 147 | +today 2026-02-12 |
| 148 | +
|
| 149 | +task Design 2026-01-05 2026-01-18 progress=1 color=#2563EB |
| 150 | +task Implement 2026-01-15 2026-02-14 progress=0.72 dependsOn=0 color=#14B8A6 |
| 151 | +task Validate 2026-02-10 2026-02-28 progress=0.35 dependsOn=1 color=#F59E0B |
| 152 | +milestone Release 2026-03-05 dependsOn=2 color=#059669 |
| 153 | +``` |
| 154 | +"""), |
| 155 | + new( |
| 156 | + "markup-table-release-gates", |
| 157 | + """ |
| 158 | +```chartforgex table v1 |
| 159 | +id release-gates |
| 160 | +title Release Gates |
| 161 | +subtitle TableArtifact parsed from native v1 markup with typed columns |
| 162 | +capabilities sort filter export |
| 163 | +
|
| 164 | +columns: |
| 165 | +| id | label | type | alignment | |
| 166 | +| --- | --- | --- | --- | |
| 167 | +| gate | Gate | text | left | |
| 168 | +| owner | Owner | text | left | |
| 169 | +| status | Status | status | center | |
| 170 | +| evidence | Evidence | text | left | |
| 171 | +
|
| 172 | +rows: |
| 173 | +| gate | owner | status | evidence | |
| 174 | +| --- | --- | --- | --- | |
| 175 | +| API grammar | Markup | positive | v1 reference and parser tests | |
| 176 | +| Static output | Core | positive | SVG and PNG parity | |
| 177 | +| Package smoke | Release | warning | Build.ps1 release gate | |
| 178 | +``` |
| 179 | +"""), |
| 180 | + new( |
| 181 | + "markup-topology-service-map", |
| 182 | + """ |
| 183 | +```chartforgex topology v1 |
| 184 | +id service-map |
| 185 | +title Service Map |
| 186 | +subtitle TopologyChart parsed from native v1 markup |
| 187 | +viewport 1120x620 |
| 188 | +layout layered left-to-right |
| 189 | +
|
| 190 | +groups: |
| 191 | +| id | label | status | |
| 192 | +| --- | --- | --- | |
| 193 | +| edge | Edge | healthy | |
| 194 | +| core | Core | warning | |
| 195 | +| data | Data | healthy | |
| 196 | +
|
| 197 | +nodes: |
| 198 | +| id | label | kind | status | group | subtitle | badge | |
| 199 | +| --- | --- | --- | --- | --- | --- | --- | |
| 200 | +| portal | Portal | application | healthy | edge | Public entry | TLS | |
| 201 | +| api | API | service | warning | core | Backpressure watched | P95 | |
| 202 | +| worker | Worker | process | healthy | core | Queue consumer | 4 | |
| 203 | +| db | Database | database | healthy | data | Primary store | HA | |
| 204 | +
|
| 205 | +edges: |
| 206 | +| from | to | label | kind | status | |
| 207 | +| --- | --- | --- | --- | --- | |
| 208 | +| portal | api | HTTPS | data-flow | healthy | |
| 209 | +| api | worker | async | dependency | warning | |
| 210 | +| worker | db | write | data-flow | healthy | |
| 211 | +| api | db | read | data-flow | healthy | |
| 212 | +``` |
| 213 | +""") |
| 214 | + }; |
| 215 | + |
| 216 | + private readonly struct MarkupExample { |
| 217 | + public MarkupExample(string name, string source) { |
| 218 | + Name = name; |
| 219 | + Source = source; |
| 220 | + } |
| 221 | + |
| 222 | + public string Name { get; } |
| 223 | + public string Source { get; } |
| 224 | + } |
| 225 | +} |
0 commit comments