chore(reading): lint-clean lift — local renames, tab escape, DOX pass (#76)
Spike-locals renamed at lift time (identifier_name), literal tab in a string escape fixed (unprintable-ASCII build error), swiftlint force_try disable for the static regex. AGENTS.md reading-mode section rewritten for the apex HTML pipeline; native-only Work Guidance line rescoped to the editing side per ADR-0011.
This commit is contained in:
1 parent
cc04ec236e
commit
f1c915fed1
5 files changed
+111
-83
No files matched your search
@@ -48,18 +48,28 @@ nonisolated enum ReadingPresenter {
|
||||
out.append(Block(sourceLine: line, sourceRange: NSRange(location: loc, length: end - loc)))
|
||||
}
|
||||
// advance line count over the chunk + the blank separator run
|
||||
var i = loc
|
||||
while i < end {
|
||||
if ns.character(at: i) == 0x0A { line += 1 }
|
||||
i += 1
|
||||
var index = loc
|
||||
while index < end {
|
||||
if ns.character(at: index) == 0x0A {
|
||||
line += 1
|
||||
}
|
||||
index += 1
|
||||
}
|
||||
if delim.location == NSNotFound { break }
|
||||
var j = delim.location
|
||||
while j < ns.length {
|
||||
let c = ns.character(at: j)
|
||||
if c == 0x0A { line += 1; j += 1 } else if c == 0x0D { j += 1 } else { break }
|
||||
if delim.location == NSNotFound {
|
||||
break
|
||||
}
|
||||
loc = j
|
||||
var scan = delim.location
|
||||
while scan < ns.length {
|
||||
let char = ns.character(at: scan)
|
||||
if char == 0x0A {
|
||||
line += 1; scan += 1
|
||||
} else if char == 0x0D {
|
||||
scan += 1
|
||||
} else {
|
||||
break
|
||||
}
|
||||
}
|
||||
loc = scan
|
||||
}
|
||||
return out
|
||||
}
|
||||
@@ -67,15 +77,21 @@ nonisolated enum ReadingPresenter {
|
||||
/// Prepend `<!--S:N-->` (N = 1-based source line) before every blank-line
|
||||
/// chunk — apex passes HTML comments through verbatim (#74).
|
||||
static func stampSource(_ source: String) -> String {
|
||||
let isBlank = { (l: Substring) in l.allSatisfy { $0 == " " || $0 == "\t" || $0 == "\r" } }
|
||||
let isBlank = { (line: Substring) in line.allSatisfy { $0 == " " || $0 == "\t" || $0 == "\r" } }
|
||||
let lines = source.split(separator: "\n", omittingEmptySubsequences: false)
|
||||
var out: [String] = []
|
||||
var i = 0
|
||||
while i < lines.count {
|
||||
if isBlank(lines[i]) { i += 1; continue }
|
||||
if !out.isEmpty { out.append("") }
|
||||
out.append("<!--S:\(i + 1)-->")
|
||||
while i < lines.count && !isBlank(lines[i]) { out.append(String(lines[i])); i += 1 }
|
||||
var index = 0
|
||||
while index < lines.count {
|
||||
if isBlank(lines[index]) {
|
||||
index += 1; continue
|
||||
}
|
||||
if !out.isEmpty {
|
||||
out.append("")
|
||||
}
|
||||
out.append("<!--S:\(index + 1)-->")
|
||||
while index < lines.count, !isBlank(lines[index]) {
|
||||
out.append(String(lines[index])); index += 1
|
||||
}
|
||||
}
|
||||
return out.joined(separator: "\n")
|
||||
}
|
||||
@@ -83,10 +99,13 @@ nonisolated enum ReadingPresenter {
|
||||
/// Rewrite `<!--S:N-->` into `data-source-line="N"` on the element that
|
||||
/// follows (spike regex, 0 mis-stamps corpus-wide, #74).
|
||||
static func stampHTML(_ html: String) -> String {
|
||||
let re = try! NSRegularExpression(pattern: #"<!--S:(\d+)-->\s*\n<([a-zA-Z][a-zA-Z0-9]*)"#)
|
||||
let ms = NSMutableString(string: html)
|
||||
re.replaceMatches(in: ms, range: NSMakeRange(0, ms.length), withTemplate: "<$2 data-source-line=\"$1\"")
|
||||
return ms as String
|
||||
// Static literal pattern — compiles by construction; a failure would
|
||||
// crash at first render and every test with it.
|
||||
// swiftlint:disable:next force_try
|
||||
let regex = try! NSRegularExpression(pattern: #"<!--S:(\d+)-->\s*\n<([a-zA-Z][a-zA-Z0-9]*)"#)
|
||||
let mutable = NSMutableString(string: html)
|
||||
regex.replaceMatches(in: mutable, range: NSMakeRange(0, mutable.length), withTemplate: "<$2 data-source-line=\"$1\"")
|
||||
return mutable as String
|
||||
}
|
||||
|
||||
// MARK: Apex runner (bundled binary, ADR-0011)
|
||||
@@ -97,9 +116,11 @@ nonisolated enum ReadingPresenter {
|
||||
if let bundlePath = Bundle.main.path(forResource: "apex", ofType: nil) {
|
||||
return bundlePath
|
||||
}
|
||||
if let env = ProcessInfo.processInfo.environment["APEX_PATH"] { return env }
|
||||
for p in ["/opt/homebrew/bin/apex", "/usr/local/bin/apex"] where FileManager.default.isExecutableFile(atPath: p) {
|
||||
return p
|
||||
if let env = ProcessInfo.processInfo.environment["APEX_PATH"] {
|
||||
return env
|
||||
}
|
||||
for path in ["/opt/homebrew/bin/apex", "/usr/local/bin/apex"] where FileManager.default.isExecutableFile(atPath: path) {
|
||||
return path
|
||||
}
|
||||
return nil
|
||||
}
|
||||
@@ -113,31 +134,33 @@ nonisolated enum ReadingPresenter {
|
||||
guard let bin = apexBinaryPath() else {
|
||||
throw CocoaError(.fileNoSuchFile)
|
||||
}
|
||||
let p = Process()
|
||||
p.executableURL = URL(fileURLWithPath: bin)
|
||||
let process = Process()
|
||||
process.executableURL = URL(fileURLWithPath: bin)
|
||||
var args = ["-t", "html", "--no-mixed-lists", "--embed-images"]
|
||||
if let baseDir { args += ["--base-dir", baseDir.path] }
|
||||
p.arguments = args
|
||||
if let baseDir {
|
||||
args += ["--base-dir", baseDir.path]
|
||||
}
|
||||
process.arguments = args
|
||||
let stdin = Pipe(), stdout = Pipe(), stderr = Pipe()
|
||||
p.standardInput = stdin
|
||||
p.standardOutput = stdout
|
||||
p.standardError = stderr
|
||||
try p.run()
|
||||
process.standardInput = stdin
|
||||
process.standardOutput = stdout
|
||||
process.standardError = stderr
|
||||
try process.run()
|
||||
// Sequential post-exit reads; output is bounded by the document.
|
||||
stdin.fileHandleForWriting.write(Data(markdown.utf8))
|
||||
stdin.fileHandleForWriting.closeFile()
|
||||
let data = stdout.fileHandleForReading.readDataToEndOfFile()
|
||||
guard !data.isEmpty else {
|
||||
p.waitUntilExit()
|
||||
process.waitUntilExit()
|
||||
throw NSError(
|
||||
domain: "apex",
|
||||
code: Int(p.terminationStatus),
|
||||
userInfo: [NSLocalizedDescriptionKey: String(data: stderr.fileHandleForReading.readDataToEndOfFile(), encoding: .utf8) ?? "no stderr"]
|
||||
code: Int(process.terminationStatus),
|
||||
userInfo: [NSLocalizedDescriptionKey: String(data: stderr.fileHandleForReading.readDataToEndOfFile(), encoding: .utf8) ?? "no stderr"],
|
||||
)
|
||||
}
|
||||
// stdout EOF = conversion done (~18 ms). The ~65 ms teardown after is
|
||||
// apex's own — reap async, hand the HTML over immediately (#74).
|
||||
p.terminationHandler = { _ in }
|
||||
process.terminationHandler = { _ in }
|
||||
return String(decoding: data, as: UTF8.self)
|
||||
}
|
||||
|
||||
@@ -165,8 +188,8 @@ nonisolated enum ReadingPresenter {
|
||||
"<!DOCTYPE html><html><head><meta charset=\"utf-8\"><style>\(styleSheet())</style></head><body><pre data-source-line=\"1\">\(escapeHTML(text))</pre></body></html>"
|
||||
}
|
||||
|
||||
static func escapeHTML(_ s: String) -> String {
|
||||
s.replacingOccurrences(of: "&", with: "&")
|
||||
static func escapeHTML(_ text: String) -> String {
|
||||
text.replacingOccurrences(of: "&", with: "&")
|
||||
.replacingOccurrences(of: "<", with: "<")
|
||||
.replacingOccurrences(of: ">", with: ">")
|
||||
}
|
||||
@@ -177,10 +200,12 @@ nonisolated enum ReadingPresenter {
|
||||
/// the spike's fixed rules (proven live, #74). `familyName` (not the
|
||||
/// member PS name) is what CSS font matching wants; weight rides CSS.
|
||||
static func styleSheet() -> String {
|
||||
func family(_ f: NSFont) -> String {
|
||||
"\"\(f.familyName ?? (f.displayName ?? ""))\""
|
||||
func family(_ font: NSFont) -> String {
|
||||
"\"\(font.familyName ?? (font.displayName ?? ""))\""
|
||||
}
|
||||
func px(_ value: Double) -> String {
|
||||
"\(String(format: "%.0f", value))px"
|
||||
}
|
||||
func px(_ v: Double) -> String { "\(String(format: "%.0f", v))px" }
|
||||
let body = ProseTypography.bodyFont
|
||||
let head = ProseTypography.headingFont
|
||||
let code = ProseTypography.codeFont
|
||||
|
||||
Reference in new issue
Block a user