Tables in the plain-text target, with captions

A @table now renders to txt.  What it needed settled how plain text sets
a block off from the prose around it, which is by INDENTING it: that is
the only structural device the medium has, where html and LaTeX have
boxes, rules and space.  An indented block is one that has already been
laid out, and the target leaves its lines alone.

A caption is placed above a table or a listing and below a figure, in
every target rather than only in plain text.  A figure is taken in at a
glance, and the caption then names what the reader has already seen; a
table is read, and a reader who meets the rows before the subject has to
read them twice.

Also fixed, all three of them silent: an indented paragraph was dropped
from plain-text output rather than left as written; an @indent block was
re-flowed, so the indentation of its later lines appeared as gaps in the
middle of a line; and a table captioned with ":number false" lost the
rule under its caption, while an unnumbered table wrongly consumed a
number that a reader counting captions would then find missing.

(from dev 8173c371bba6)
This commit is contained in:
ack committed 2026-08-26 23:00:58 +02:00
1 parent af29bbf7ac
commit b80e823ab3
7 files changed
+197 -25

No files matched your search

+36 -3
View File
@@ -11,9 +11,11 @@ import klammer_base
import html_util
from html_util import E
import latex_util
import txt_util
from indexed_range import Indexed_ranges, hline_names, vline_names
import table_cell
import font
import phases
# ---- :format functions ---------------------------------------------------
# A :format function takes (value, target) and returns the formatted cell
@@ -864,7 +866,38 @@ class Table(klammer_base.Klammer_base):
# there, so the rule stays unconditional.)
return latex_util.block(result)
# This should signal that it has not be implemented by not being defined.
# def txt(self):
# return "Table in .txt format not implemented"
# txt target
def txt_column_widths(self):
def ws(lst, ext=0):
result = [len(e) for e in lst]
if ext > 0:
result += ["~"] * (ext - (len(lst)))
return result
row_count = max(ws(self.rows))
widths = ws(self.rows[0], row_count)
for row in self.rows[1:]:
widths = list(max(widths, ws(row, row_count)))
return widths
def txt(self):
def hpos(p):
return {'l':'<', 'c':'^', 'r':'>'}[p]
def if_empty(char):
return "~" if len(char) == 0 else char
widths = self.txt_column_widths()
pos = [hpos(p) for p in self.cell_hpos]
result = ""
indent = "~" * int(self.Target_txt_indent)
colsep = " "
for row in self.rows:
result += indent + colsep.join(
[f"{if_empty(elt):{pos}{width}}" for elt, width, pos in
list(zip(row, widths, pos))])
result += "\n"
result = txt_util.add_caption(
result, "Table", self.number, self.caption, self.caption_side, \
int(self.Target_txt_indent))
return result