mirror of
https://github.com/larvit/adf-codec.git
synced 2026-11-19 06:35:44 +01:00
Answer architecture round 1: every container joining several walks sums their projections before it joins, the portable reduction refuses a list whose spelling is refused, and heldTextLength moves beside textLength
This commit is contained in:
@@ -1,14 +1,18 @@
|
||||
import type { AdfNode } from '../../adf/document.ts'
|
||||
import type { ConvertErrorPath } from '../../result.ts'
|
||||
import type { WrittenFlavour } from '../portable/conventions.ts'
|
||||
import { holdsOnlyAttributes, nodeContent } from '../../adf/document.ts'
|
||||
import { longestOutput, tooLong } from '../../output-length.ts'
|
||||
import { spellPipeDelimiter, spellPipeRow } from '../pipe-table-syntax.ts'
|
||||
import { success, type ConvertErrorPath, type Result } from '../../result.ts'
|
||||
import { tryPipeCell } from './inline-line.ts'
|
||||
|
||||
export function tryPipeTable(node: AdfNode, path: ConvertErrorPath, flavour: WrittenFlavour): string | undefined {
|
||||
// indent: the characters the containers around the table prefix each of its lines with.
|
||||
export function tryPipeTable(node: AdfNode, path: ConvertErrorPath, { flavour, indent }: { flavour: WrittenFlavour; indent: number }): Result<string> | undefined {
|
||||
const rows = pipeRows(node)
|
||||
if (rows === undefined) return undefined
|
||||
const lines: string[] = []
|
||||
// A row spells ` | ` beside each cell, and a newline and the indent after it.
|
||||
let projected = 0
|
||||
for (const [rowIndex, row] of rows.entries()) {
|
||||
const cells: string[] = []
|
||||
for (const [cellIndex, paragraph] of row.entries()) {
|
||||
@@ -16,11 +20,14 @@ export function tryPipeTable(node: AdfNode, path: ConvertErrorPath, flavour: Wri
|
||||
const line = content.length === 0 ? '' : tryPipeCell(content, [...path, 'content', rowIndex, 'content', cellIndex, 'content', 0], flavour)
|
||||
if (line === undefined) return undefined
|
||||
cells.push(line)
|
||||
projected += line.length + 3
|
||||
}
|
||||
projected += 2 + indent + (rowIndex === 0 ? spellPipeDelimiter(cells.length).length + 1 + indent : 0)
|
||||
if (projected > longestOutput) return tooLong([...path, 'content', rowIndex])
|
||||
lines.push(spellPipeRow(cells))
|
||||
if (rowIndex === 0) lines.push(spellPipeDelimiter(cells.length))
|
||||
}
|
||||
return lines.join('\n')
|
||||
return success(lines.join('\n'))
|
||||
}
|
||||
|
||||
function pipeRows(node: AdfNode): AdfNode[][] | undefined {
|
||||
|
||||
Reference in New Issue
Block a user