From 646c75ad9bd2d998542c63283ec67ea5b4a8dd8e Mon Sep 17 00:00:00 2001 From: Christophe de Vienne Date: Tue, 12 May 2009 14:41:46 +0200 Subject: [PATCH 1/2] Add a "longtable" flag option to the "tabularcolumn" directive. If used, the latex writer will use a \longtable instead of \tabular(y) --- sphinx/directives/other.py | 5 ++++- sphinx/writers/latex.py | 40 ++++++++++++++++++++++++++++++++------ 2 files changed, 38 insertions(+), 7 deletions(-) diff --git a/sphinx/directives/other.py b/sphinx/directives/other.py index 80c88f5fd..85f85f35b 100644 --- a/sphinx/directives/other.py +++ b/sphinx/directives/other.py @@ -396,11 +396,14 @@ class TabularColumns(Directive): required_arguments = 1 optional_arguments = 0 final_argument_whitespace = True - option_spec = {} + option_spec = { + 'longtable': directives.flag, + } def run(self): node = addnodes.tabular_col_spec() node['spec'] = self.arguments[0] + node['longtable'] = 'longtable' in self.options return [node] diff --git a/sphinx/writers/latex.py b/sphinx/writers/latex.py index 63d66b558..81368d0e8 100644 --- a/sphinx/writers/latex.py +++ b/sphinx/writers/latex.py @@ -34,6 +34,7 @@ HEADER = r'''%% Generated by Sphinx. %(babel)s %(fontpkg)s %(fncychap)s +%(longtable)s \usepackage{sphinx} %(preamble)s @@ -111,9 +112,11 @@ class Table(object): self.col = 0 self.colcount = 0 self.colspec = None + self.rowcount = 0 self.had_head = False self.has_verbatim = False self.caption = None + self.longtable = False class Desc(object): @@ -140,6 +143,7 @@ class LaTeXTranslator(nodes.NodeVisitor): 'babel': '\\usepackage{babel}', 'fontpkg': '\\usepackage{times}', 'fncychap': '\\usepackage[Bjarne]{fncychap}', + 'longtable': '\\usepackage{longtable}', 'preamble': '', 'title': '', 'date': '', @@ -585,6 +589,7 @@ class LaTeXTranslator(nodes.NodeVisitor): def visit_tabular_col_spec(self, node): self.next_table_colspec = node['spec'] + self.next_table_longtable = node['longtable'] raise nodes.SkipNode def visit_table(self, node): @@ -594,15 +599,18 @@ class LaTeXTranslator(nodes.NodeVisitor): (self.curfilestack[-1], node.line or '')) self.table = Table() self.tablebody = [] + self.table.longtable = self.next_table_longtable # Redirect body output until table is finished. self._body = self.body self.body = self.tablebody def depart_table(self, node): self.body = self._body - if self.table.caption is not None: + if not self.table.longtable and self.table.caption is not None: self.body.append('\n\\begin{threeparttable}\n' '\\caption{%s}\n' % self.table.caption) - if self.table.has_verbatim: + if self.table.longtable: + self.body.append('\n\\begin{longtable}') + elif self.table.has_verbatim: self.body.append('\n\\begin{tabular}') else: self.body.append('\n\\begin{tabulary}{\\textwidth}') @@ -616,12 +624,31 @@ class LaTeXTranslator(nodes.NodeVisitor): self.body.append('{|' + colspec + '}\n') else: self.body.append('{|' + ('L|' * self.table.colcount) + '}\n') + if self.table.longtable and self.table.caption is not None: + self.body.append('\\caption{%s} \\\\\n' % self.table.caption) + + if self.table.longtable: + self.body.append('\\hline\n') + self.body.append('\\endfirsthead\n\n') + self.body.append('\multicolumn{%s}{c}%%\n' % self.table.colcount) + self.body.append('{{\\bfseries \\tablename\\ \\thetable{} -- %s}} \\\\\n' % _('continued from previous page')) + self.body.append('\\hline\n') + self.body.append('\\endhead\n\n') + self.body.append('\\hline \multicolumn{%s}{|r|}{{%s}} \\\\ \\hline\n' % ( + self.table.colcount, _('Continued on next page'))) + self.body.append('\\endfoot\n\n') + self.body.append('\\hline\n') + self.body.append('\\endlastfoot\n\n') + else: + self.body.append('\\hline\n') self.body.extend(self.tablebody) - if self.table.has_verbatim: + if self.table.longtable: + self.body.append('\\end{longtable}\n\n') + elif self.table.has_verbatim: self.body.append('\\end{tabular}\n\n') else: self.body.append('\\end{tabulary}\n\n') - if self.table.caption is not None: + if not self.table.longtable and self.table.caption is not None: self.body.append('\\end{threeparttable}\n\n') self.table = None self.tablebody = None @@ -640,8 +667,8 @@ class LaTeXTranslator(nodes.NodeVisitor): if self.next_table_colspec: self.table.colspec = '{%s}\n' % self.next_table_colspec self.next_table_colspec = None - self.body.append('\\hline\n') - self.table.had_head = True +# self.body.append('\\hline\n') +# self.table.had_head = True def depart_thead(self, node): self.body.append('\\hline\n') @@ -655,6 +682,7 @@ class LaTeXTranslator(nodes.NodeVisitor): self.table.col = 0 def depart_row(self, node): self.body.append('\\\\\n') + self.table.rowcount += 1 def visit_entry(self, node): if node.has_key('morerows') or node.has_key('morecols'): From 80c0ff4b0df01f03eeaa86a5fd987311d335f7a7 Mon Sep 17 00:00:00 2001 From: Christophe de Vienne Date: Tue, 12 May 2009 17:15:34 +0200 Subject: [PATCH 2/2] #19: Remove the 'longtable' flag in favor of .. class: longtable (actually cssclass). If more than 30 lines are found in the table, the 'longtable' mode is automaticaly turned on --- sphinx/directives/other.py | 5 +---- sphinx/writers/latex.py | 7 +++++-- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/sphinx/directives/other.py b/sphinx/directives/other.py index 85f85f35b..80c88f5fd 100644 --- a/sphinx/directives/other.py +++ b/sphinx/directives/other.py @@ -396,14 +396,11 @@ class TabularColumns(Directive): required_arguments = 1 optional_arguments = 0 final_argument_whitespace = True - option_spec = { - 'longtable': directives.flag, - } + option_spec = {} def run(self): node = addnodes.tabular_col_spec() node['spec'] = self.arguments[0] - node['longtable'] = 'longtable' in self.options return [node] diff --git a/sphinx/writers/latex.py b/sphinx/writers/latex.py index 81368d0e8..4cd6070e2 100644 --- a/sphinx/writers/latex.py +++ b/sphinx/writers/latex.py @@ -589,7 +589,6 @@ class LaTeXTranslator(nodes.NodeVisitor): def visit_tabular_col_spec(self, node): self.next_table_colspec = node['spec'] - self.next_table_longtable = node['longtable'] raise nodes.SkipNode def visit_table(self, node): @@ -598,12 +597,14 @@ class LaTeXTranslator(nodes.NodeVisitor): '%s:%s: nested tables are not yet implemented.' % (self.curfilestack[-1], node.line or '')) self.table = Table() + self.table.longtable = 'longtable' in node['classes'] self.tablebody = [] - self.table.longtable = self.next_table_longtable # Redirect body output until table is finished. self._body = self.body self.body = self.tablebody def depart_table(self, node): + if self.table.rowcount > 30: + self.table.longtable = True self.body = self._body if not self.table.longtable and self.table.caption is not None: self.body.append('\n\\begin{threeparttable}\n' @@ -622,6 +623,8 @@ class LaTeXTranslator(nodes.NodeVisitor): colspec = ('p{%.3f\\textwidth}|' % colwidth) * \ self.table.colcount self.body.append('{|' + colspec + '}\n') + elif self.table.longtable: + self.body.append('{|' + ('l|' * self.table.colcount) + '}\n') else: self.body.append('{|' + ('L|' * self.table.colcount) + '}\n') if self.table.longtable and self.table.caption is not None: