Skip to content

Commit

Permalink
Enable DND-tables throughout
Browse files Browse the repository at this point in the history
Dungeonsheets normally uses supertabular, but dndtables, which are based on
tabularx, are not compatible with that. As a result, table headings result in
latex errors, and columns end up too wide because text wrapping is lacking.

This patch generates a correct table header for dndtable if it is present,
and adopts the column widths from the normal tables that docutils generates
for supertabular. Uses X column format, to allow text wrapping in dndtables.
  • Loading branch information
PJBrs committed Jan 2, 2024
1 parent e0067b5 commit 2772346
Show file tree
Hide file tree
Showing 8 changed files with 19 additions and 8 deletions.
2 changes: 1 addition & 1 deletion dungeonsheets/forms/features_template.tex
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@
\pdfbookmark[1]{[[ feat.name ]]}{Features - [[ feat.name ]]}
\DndFeatHeader{[[ feat.name ]]}[Source: [[ feat.source ]]]

[[ feat.__doc__|rst_to_latex ]]
[[ feat.__doc__|rst_to_latex(use_dnd_decorations=True) ]]
[% else %]
\pdfbookmark[1]{[[ feat.name ]]}{Features - [[ feat.name ]]}
\subsection*{[[ feat.name ]]}
Expand Down
2 changes: 1 addition & 1 deletion dungeonsheets/forms/infusions_template.tex
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,6 @@ \section*{Infusions}

[% endif %]%

[[ inf.__doc__ | rst_to_latex(top_heading_level=2) ]]
[[ inf.__doc__ | rst_to_latex(top_heading_level=2, use_dnd_decorations=use_dnd_decorations) ]]

[% endfor %]
2 changes: 1 addition & 1 deletion dungeonsheets/forms/magic_items_template.tex
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ \section*{Magic Items}
\textbf{**Not included in stats on Character Sheet} %
[% endif %] %

[[ mitem.__doc__|rst_to_latex ]]
[[ mitem.__doc__|rst_to_latex(use_dnd_decorations=True) ]]
[% endfor %]
[% else %]
[% for mitem in character.magic_items %]
Expand Down
2 changes: 1 addition & 1 deletion dungeonsheets/forms/party_summary_template.tex
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
\pdfbookmark[1]{Summary}{Summary}
\section*{Summary}

[[ summary | rst_to_latex ]]
[[ summary | rst_to_latex(use_dnd_decorations=use_dnd_decorations) ]]

[% endif %]

Expand Down
2 changes: 1 addition & 1 deletion dungeonsheets/forms/spellbook_template.tex
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,6 @@ \subsection*{Spellcasting details}
\end{description}
% \vspace{\zerosep}
[% endif %]
[[ spl.__doc__ | rst_to_latex(top_heading_level=1) ]]
[[ spl.__doc__ | rst_to_latex(top_heading_level=1, use_dnd_decorations=use_dnd_decorations) ]]
[% endfor %]
2 changes: 1 addition & 1 deletion dungeonsheets/forms/subclasses_template.tex
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ \section*{Subclasses}
\pdfbookmark[1]{[[ sc.name ]]}{Subclasses - [[ sc.name ]]}
\DndFeatHeader{[[ sc.name ]]} % Would like to add source here

[[ sc.__doc__ | rst_to_latex(top_heading_level=2) ]]
[[ sc.__doc__ | rst_to_latex(top_heading_level=2, use_dnd_decorations=True) ]]

[% endfor %]
[% else %]
Expand Down
13 changes: 12 additions & 1 deletion dungeonsheets/latex.py
Original file line number Diff line number Diff line change
Expand Up @@ -214,10 +214,21 @@ def rst_to_latex(rst, top_heading_level: int=0, format_dice: bool = True, use_dn
tex = tex_parts["body"]
# Apply fancy D&D decorations
if use_dnd_decorations:
tex = re.sub(r"p{[0-9.]+\\DUtablewidth}", "l ", tex, flags=re.M)
tex = tex.replace(r"\begin{supertabular}[c]", r"\begin{DndTable}")
tex = tex.replace(r"\begin{supertabular}", r"\begin{DndTable}")
tex = tex.replace(r"\end{supertabular}", r"\end{DndTable}")

# Biggest problem: column width. We're looking for the following translation:
# \begin{DndTable}{ p{0.214\DUtablewidth} p{0.086\DUtablewidth} p{0.156\DUtablewidth} p{0.133\DUtablewidth} p{0.121\DUtablewidth}}
# \begin{DndTable}{ >{\hsize=0.214\hsize}X >{\hsize=0.086\hsize}X >{\hsize=0.156\hsize}X >{\hsize=0.133\hsize}X >{\hsize=0.121\hsize}X}
tex = re.sub(r"p{([0-9.]+)\\DUtablewidth}", r">{\\hsize=\1\\DUtablewidth}X", tex, flags=re.M)

# Deal with table header misplacement
# Regex for tabularx (dndtable is based on tabularx, not on supertabular):
#tex = re.sub(r"(begin{DndTable})({[a-z]+})\n\\multicolumn.*{([a-zA-Z\s]+)}}", r"\1[header=\3]\2", tex, flags=re.M)
# Regex for supertabular:
tex = re.sub(r"(begin{DndTable})(.*\n)\\multicolumn.*\n(.*)\n}} \\\\", r"\1[header=\3]\2", tex, flags=re.M)

return tex

def rst_to_boxlatex(rst):
Expand Down
2 changes: 1 addition & 1 deletion tests/test_latex.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ def test_simple_table(self):
self.assertNotIn("endfirsthead", tex)
# Check that fancy decorations uses the DndTable environment
tex = latex.rst_to_latex(table_rst, use_dnd_decorations=True)
self.assertIn(r"\begin{DndTable}{l l l }", tex)
self.assertIn(r"\begin{DndTable}{>{\hsize=0.075\DUtablewidth}X", tex)

def test_rst_all_spells(self):
for spell in spells.all_spells():
Expand Down

0 comments on commit 2772346

Please sign in to comment.