Hi folks,
I love sphinx but I absolutely detest creating tables in it because
the rst table creation syntax just sucks. So out of frustration I
created a little python script to generate a basic table (see below),
and I wanted to create a directive so that I could write the
following:
.. simple_table::
Column 1; Column 2
A sentence that is long; another sentence that is long
Please let me know what you think; why is it this way you say sir
which would be converted into rst syntax automagically as follows:
================================= ==============================
Column 1 Column 2
================================= ==============================
A sentence that is long another sentence that is long
Please let me know what you think why is it this way you say sir
================================= ==============================
I've looked at the todo example in the sphinx documentation but to be
honest it is overkill for what I want (I'm not interested in
inheriting from admonition). Hence, I would appreciate any pointers to
make this directive a reality.
My rst table creation code is included below (please note that as a
first iteration, it's not pretty but it works!).
Best,
Alia
<code>
"""
.. simple_table::
Column 1; Column 2
A sentence that is long; another sentence that is long
Please let me know what you think; why is it this way you say sir
"""
eg = """
Column 1; Column 2
A sentence that is long; another sentence that is long
Please let me know what you think; why is it this way you say sir
"""
def make_rst_table(txt):
results = []
lstrip = lambda xs: [i.lstrip() for i in xs]
lines = [i for i in s.splitlines() if i]
header, rows = lines[0], lines[1:]
hcols = lstrip(header.split(';'))
# get longest columns
d = {}
for i, c in enumerate(hcols):
d[i] = len(c)
for row in rows:
rcols = lstrip(row.split(';'))
for i, c in enumerate(rcols):
if len(c) > d[i]:
d[i] = len(c)
else:
continue
lengths = sorted(d.items())
def line(lengths):
results.append(" ".join(length * '=' for i, length in
lengths))
def do_cols(cols):
res = []
for i, c in enumerate(cols):
res.append(c.ljust(d[i]))
results.append(" ".join(res))
def do_rows(rows):
for row in rows:
rcols = lstrip(row.split(';'))
do_cols(rcols)
line(lengths)
do_cols(hcols)
line(lengths)
do_rows(rows)
line(lengths)
return results
for line in make_rst_table(eg):
print line
</code>
Thanks!
AK
--
You received this message because you are subscribed to the Google Groups
"sphinx-dev" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/sphinx-dev?hl=en.