Repository Public MIT
1// =============================================================================2// SP BOUCHER | SOFTWARE & RESEARCH3// ARTIFICIAL INTELLIGENCE • FINANCE • ENGINEERING4// =============================================================================5//6// Project : SP GlassKit7// File : Apps/GlassStudio/Kit/Views/MarkdownDocView.swift8// Module : GlassStudio9// Repository : https://git.spboucher.ai/sp-glasskit10// Project Website : https://glass.spboucher.ai11// Created : 2026-12-0212// Last Modified : 2026-12-0213//14// Author : Simon-Pierre Boucher15// Position : Professor of Finance16// Institution : Université du Québec en Outaouais (UQO)17// Organization : SP Boucher18// Website : https://spboucher.ai19// Git : https://git.spboucher.ai20//21// Preferred Contact: contact@spboucher.ai22// Development : dev@spboucher.ai23// Support : support@spboucher.ai24// Security/Admin : admin@spboucher.ai25//26// Copyright (c) 202627// Simon-Pierre Boucher28// All rights reserved where applicable. This file is governed by the29// applicable project license, copyright notices and relevant law.30// Existing third-party rights and notices remain unaffected.31//32// License : MIT33//34// Do not embed credentials, private keys, access tokens,35// certificates or confidential personal information.36//37// =============================================================================38// END OF SP BOUCHER SOURCE HEADER39// =============================================================================4041import SwiftUI4243/// The subset of Markdown used by the component pages: headings, paragraphs, bullet lists, fenced code and44/// pipe tables. Parsing is separate from rendering so that it can be tested.45public enum MarkdownBlock: Equatable, Sendable {46 case heading(level: Int, text: String)47 case paragraph(String)48 case bullets([String])49 case code(String)50 case table(header: [String], rows: [[String]])5152 public static func parse(_ markdown: String) -> [MarkdownBlock] {53 var blocks: [MarkdownBlock] = []54 var paragraph: [String] = [], bullets: [String] = [], code: [String] = [], table: [[String]] = []55 var inCode = false5657 func flush() {58 if !paragraph.isEmpty { blocks.append(.paragraph(paragraph.joined(separator: " "))); paragraph = [] }59 if !bullets.isEmpty { blocks.append(.bullets(bullets)); bullets = [] }60 if !table.isEmpty {61 let rows = table.filter { row in !row.allSatisfy { $0.allSatisfy { "-: ".contains($0) } } }62 if let header = rows.first { blocks.append(.table(header: header, rows: Array(rows.dropFirst()))) }63 table = []64 }65 }6667 for line in markdown.components(separatedBy: "\n") {68 if line.hasPrefix("```") {69 if inCode { blocks.append(.code(code.joined(separator: "\n"))); code = [] } else { flush() }70 inCode.toggle()71 continue72 }73 if inCode { code.append(line); continue }74 let trimmed = line.trimmingCharacters(in: .whitespaces)75 if trimmed.isEmpty { flush(); continue }76 if trimmed.hasPrefix("#") {77 flush()78 let level = trimmed.prefix { $0 == "#" }.count79 blocks.append(.heading(level: min(level, 4), text: trimmed.drop { $0 == "#" }.trimmingCharacters(in: .whitespaces)))80 } else if trimmed.hasPrefix("- ") || trimmed.hasPrefix("* ") {81 if !paragraph.isEmpty || !table.isEmpty { flush() }82 bullets.append(String(trimmed.dropFirst(2)))83 } else if trimmed.hasPrefix("|") {84 if !paragraph.isEmpty || !bullets.isEmpty { flush() }85 var cells = trimmed.split(separator: "|", omittingEmptySubsequences: false).map { $0.trimmingCharacters(in: .whitespaces) }86 if cells.first == "" { cells.removeFirst() }87 if cells.last == "" { cells.removeLast() }88 table.append(cells)89 } else {90 if !bullets.isEmpty || !table.isEmpty { flush() }91 paragraph.append(trimmed)92 }93 }94 if inCode, !code.isEmpty { blocks.append(.code(code.joined(separator: "\n"))) }95 flush()96 return blocks97 }98}99100/// Renders a component documentation page bundled with the Studio.101public struct MarkdownDocView: View {102 private let blocks: [MarkdownBlock]103104 public init(markdown: String) {105 blocks = MarkdownBlock.parse(markdown)106 }107108 public var body: some View {109 VStack(alignment: .leading, spacing: 12) {110 ForEach(Array(blocks.enumerated()), id: \.offset) { _, block in111 view(for: block)112 }113 }114 .frame(maxWidth: .infinity, alignment: .leading)115 }116117 private func inline(_ text: String) -> Text {118 if let attributed = try? AttributedString(markdown: text, options: .init(interpretedSyntax: .inlineOnlyPreservingWhitespace)) {119 return Text(attributed)120 }121 return Text(verbatim: text)122 }123124 @ViewBuilder125 private func view(for block: MarkdownBlock) -> some View {126 switch block {127 case .heading(let level, let text):128 inline(text)129 .font(level <= 1 ? .title2.weight(.semibold) : level == 2 ? .headline : .subheadline.weight(.semibold))130 .padding(.top, level <= 2 ? 6 : 0)131 .accessibilityAddTraits(.isHeader)132 case .paragraph(let text):133 inline(text)134 case .bullets(let items):135 VStack(alignment: .leading, spacing: 6) {136 ForEach(Array(items.enumerated()), id: \.offset) { _, item in137 HStack(alignment: .firstTextBaseline, spacing: 8) {138 Text(verbatim: "•").accessibilityHidden(true)139 inline(item)140 }141 }142 }143 case .code(let code):144 ScrollView(.horizontal) {145 Text(verbatim: code)146 .font(.system(.callout, design: .monospaced))147 .textSelection(.enabled)148 .padding(12)149 }150 .frame(maxWidth: .infinity, alignment: .leading)151 .background(.quaternary.opacity(0.5), in: RoundedRectangle(cornerRadius: 10, style: .continuous))152 case .table(let header, let rows):153 VStack(alignment: .leading, spacing: 8) {154 ForEach(Array(rows.enumerated()), id: \.offset) { _, row in155 VStack(alignment: .leading, spacing: 2) {156 ForEach(Array(row.enumerated()), id: \.offset) { index, cell in157 if !cell.isEmpty {158 if index == 0 {159 inline(cell).font(.callout.weight(.semibold))160 } else {161 HStack(alignment: .firstTextBaseline, spacing: 4) {162 if index < header.count, !header[index].isEmpty {163 Text(verbatim: header[index] + ":").foregroundStyle(.secondary)164 }165 inline(cell)166 }167 .font(.callout)168 }169 }170 }171 }172 .accessibilityElement(children: .combine)173 Divider()174 }175 }176 }177 }178}179180public enum StudioDocs {181 /// The documentation page of a component, bundled with the Studio so that it is available offline.182 public static func markdown(for componentID: String) -> String? {183 guard let url = Bundle.module.url(forResource: "doc-\(componentID)", withExtension: "md") else { return nil }184 return try? String(contentsOf: url, encoding: .utf8)185 }186}187