Repository Public
1//2// VQuant iOS3// File: Core/Markdown/ReportText.swift4// Description: Report-specific text handling: figure references, VQuant markers (`:gold:`, `:up:`…), CSV export of bars.5//6// Author: Simon-Pierre Boucher7// Contact: contact@spboucher.ai8//910import Foundation1112enum ReportText {13 struct ImageReference: Equatable, Sendable { var alt: String; var url: String }1415 /// A paragraph that is exactly one Markdown image (``).16 static func image(in paragraph: String) -> ImageReference? {17 let t = paragraph.trimmingCharacters(in: .whitespacesAndNewlines)18 guard t.hasPrefix(", !t[close.upperBound...].dropLast().contains(")") else { return nil }19 let alt = String(t[t.index(t.startIndex, offsetBy: 2)..<close.lowerBound])20 let url = String(t[close.upperBound...].dropLast()).trimmingCharacters(in: .whitespaces)21 return url.isEmpty ? nil : ImageReference(alt: alt, url: url)22 }2324 /// Every figure of a report in reading order.25 static func images(in markdown: String) -> [ImageReference] {26 markdown.split(separator: "\n").compactMap { image(in: String($0)) }27 }2829 /// The markers the agent writes (`shared/vqMarkers.ts`), with the SF Symbol drawn in their place and their spoken label.30 static let markers: [String: (symbol: String, label: String)] = [31 "gold": ("1.circle.fill", "1er"), "silver": ("2.circle.fill", "2e"), "bronze": ("3.circle.fill", "3e"),32 "up": ("arrow.up.square.fill", "hausse"), "down": ("arrow.down.square.fill", "baisse"), "flat": ("arrow.right.square.fill", "stable"),33 "bull": ("chart.line.uptrend.xyaxis.circle.fill", "haussier"), "bear": ("chart.line.downtrend.xyaxis.circle.fill", "baissier"),34 "check": ("checkmark.circle.fill", "oui"), "cross": ("xmark.circle.fill", "non"), "warn": ("exclamationmark.triangle.fill", "attention"),35 "star": ("star.fill", "à retenir"), "risk": ("exclamationmark.shield.fill", "risque"), "target": ("target", "objectif"),36 "idea": ("lightbulb.fill", "idée"), "clock": ("clock.fill", "à surveiller"),37 ]3839 enum Piece: Equatable, Sendable { case text(String), marker(String) }4041 /// Splits inline text at `:name:` markers (known names only, not inside a longer word or a URL).42 static func pieces(_ s: String) -> [Piece] {43 guard s.contains(":") else { return [.text(s)] }44 var out: [Piece] = []; var buffer = ""45 let chars = Array(s); var i = 046 func wordish(_ c: Character) -> Bool { c.isLetter || c.isNumber || c == "_" || c == ":" || c == "/" }47 while i < chars.count {48 if chars[i] == ":", (i == 0 || !wordish(chars[i - 1])), let end = chars[(i + 1)...].firstIndex(of: ":"), end - i <= 8 {49 let name = String(chars[(i + 1)..<end])50 let after = end + 1 < chars.count ? chars[end + 1] : " "51 if markers[name] != nil, !(after.isLetter || after.isNumber || after == "_" || after == "/") {52 if !buffer.isEmpty { out.append(.text(buffer)); buffer = "" }53 out.append(.marker(name)); i = end + 1; continue54 }55 }56 buffer.append(chars[i]); i += 157 }58 if !buffer.isEmpty { out.append(.text(buffer)) }59 return out60 }6162 /// Markdown reduced to plain text (titles, previews, VoiceOver).63 static func plain(_ markdown: String, limit: Int = 240) -> String {64 var t = markdown.replacingOccurrences(of: #"!\[[^\]]*\]\([^)]*\)"#, with: "", options: .regularExpression)65 t = t.replacingOccurrences(of: #"[*_`#>|]+"#, with: "", options: .regularExpression)66 t = t.replacingOccurrences(of: #":[a-z]{2,6}:"#, with: "", options: .regularExpression)67 t = t.replacingOccurrences(of: #"\s+"#, with: " ", options: .regularExpression).trimmingCharacters(in: .whitespaces)68 return t.count > limit ? String(t.prefix(limit)) + "…" : t69 }70}7172enum CSVExport {73 /// The bars exactly as received, one row per observation, dates in exchange time.74 static func bars(_ bars: [Bar], intraday: Bool) -> String {75 var out = "date,open,high,low,close,volume\n"76 let cal = MarketDate.calendar77 for b in bars {78 let c = cal.dateComponents([.year, .month, .day, .hour, .minute], from: b.date)79 var d = String(format: "%04d-%02d-%02d", c.year ?? 0, c.month ?? 0, c.day ?? 0)80 if intraday { d += String(format: " %02d:%02d", c.hour ?? 0, c.minute ?? 0) }81 out += "\(d),\(num(b.open)),\(num(b.high)),\(num(b.low)),\(num(b.close)),\(b.volume.map(num) ?? "")\n"82 }83 return out84 }85 private static func num(_ v: Double) -> String {86 v == v.rounded() && abs(v) < 1e15 ? String(Int64(v)) : String(v)87 }88}89