1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323
|
# -*- coding: utf-8 -*-
"""
The MIT License (MIT)
Copyright © 2015-2016 Franklin "Snaipe" Mathieu <http://snai.pe/>
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
"""
from __future__ import unicode_literals
from docutils import nodes
from textwrap import wrap
from .unicode import u
class CellDimCalculator(nodes.NodeVisitor):
def __init__(self, document, cols, rows, width):
nodes.NodeVisitor.__init__(self, document)
self.cols = cols
self.rows = rows
self.width = width
self.height = 0
def visit_paragraph(self, node):
first_line = node.astext().split('\n')[0]
# handle weird table sizing from simple rst tables
# disregard cells spanning multiple columns, as
# these don't contribute to the cell width calculation
if len(first_line) >= self.width:
self.width = len(first_line) + 2
sublines = wrap(node.astext(), width = self.width)
self.height = int(len(sublines) / self.rows)
raise nodes.StopTraversal
def visit_table(self, node):
c = TableSizeCalculator(self.document)
node.walkabout(c)
self.height = int(c.height / self.rows)
raise nodes.StopTraversal
def visit_literal_block(self, node):
self.height = int(len(node.astext().split('\n')) / self.rows)
raise nodes.StopTraversal
visit_Text = visit_literal_block
def __getattr__(self, name):
if name.startswith('visit_') or name.startswith('depart_'):
def noop(*args, **kwargs):
pass
return noop
raise AttributeError(name)
class TableSizeCalculator(nodes.NodeVisitor):
def __init__(self, document):
nodes.NodeVisitor.__init__(self, document)
self.level = 0
self.widths = []
self.heights = []
self.rows = 0
def __getattr__(self, name):
if name.startswith('visit_') or name.startswith('depart_'):
def noop(*args, **kwargs):
pass
return noop
raise AttributeError(name)
def visit_table(self, node):
if self.level > 0:
raise nodes.SkipChildren
self.level += 1
def depart_table(self, node):
self.width = sum(self.widths) + (len(self.widths) + 1)
self.height = sum(self.heights) + (len(self.heights) + 1)
self.level -= 1
def visit_tgroup(self, node):
self.cols = node.attributes['cols']
def visit_colspec(self, node):
self.widths.append(node.attributes['colwidth'])
def visit_row(self, node):
self.rows += 1
self.heights.append(1)
self.col = 0
def visit_entry(self, node):
cols = node.attributes.get('morecols', 0) + 1
rows = node.attributes.get('morerows', 0) + 1
width = sum(self.widths[self.col:self.col + cols]) + (cols - 1)
c = CellDimCalculator(self.document, cols, rows, width)
node.walkabout(c)
# Correct invalid column sizing for simple rst tables
if c.width > width and cols == 1:
self.widths[self.col] = c.width
self.heights[-1] = max(self.heights[-1], c.height)
self.col += 1
raise nodes.SkipChildren
class TableDrawer(nodes.NodeVisitor):
def __init__(self, props, document, **options):
nodes.NodeVisitor.__init__(self, document)
self.props = props
self.level = 0
self.lines = ['']
self.line = 0
self.cursor = 0
self.col = 0
self.row = 0
self.nb_rows = 0
self.options = options
def unicode_intersection(char, next):
switch = {
('─', '│'): '┬',
('┐', '│'): '┐',
('┘', '│'): '┤',
('┘', '─'): '┴',
('┴', '│'): '┼',
('│', '─'): '├',
('┤', '─'): '┼',
(' ', '─'): '┘',
('└', '─'): '└',
('═', '│'): '╤',
('╕', '│'): '╕',
('╛', '│'): '╡',
('╛', '═'): '╧',
('╧', '│'): '╪',
('│', '═'): '╞',
('╡', '═'): '╪',
(' ', '═'): '╛',
('╘', '═'): '╘',
}
return switch[(u(char), u(next))]
if options.get('unicode', False):
self.char_single_rule = '─'
self.char_double_rule = '═'
self.char_vertical_rule = '│'
self.get_intersection = unicode_intersection
self.top_left = '┌'
self.top_right = '┐'
self.bottom_left = '╘'
else:
self.char_single_rule = '-'
self.char_double_rule = '='
self.char_vertical_rule = '|'
self.get_intersection = lambda *args: '+'
self.top_left = self.bottom_left = self.top_right = '+'
def __getattr__(self, name):
if name.startswith('visit_') or name.startswith('depart_'):
def noop(*args, **kwargs):
pass
return noop
if name == 'curline':
return self.lines[self.line]
raise AttributeError(name)
def _draw_rule(self):
self.lines[self.line] += self.top_left + self.char_single_rule * (self.props.width - 2) + self.top_right
self.lines.extend([self.char_vertical_rule + ' ' * (self.props.width - 1)] * (self.props.height - 2))
self.lines.extend([self.bottom_left + ' ' * (self.props.width - 1)])
self.line += 1
self.cursor = 0
def visit_table(self, node):
if self.level > 0:
raise nodes.SkipChildren
self.level += 1
self._draw_rule()
def depart_table(self, node):
self.level -= 1
def visit_row(self, node):
self.col = 0
self.cursor = 0
def depart_row(self, node):
self.line += self.props.heights[self.row] + 1
self.row += 1
self.local_row += 1
def visit_thead(self, node):
self.nb_rows = len(node.children)
self.local_row = 0
visit_tbody = visit_thead
def visit_entry(self, node):
cols = node.attributes.get('morecols', 0) + 1
rows = node.attributes.get('morerows', 0) + 1
width = sum(self.props.widths[self.col:self.col + cols]) + (cols - 1)
height = sum(self.props.heights[self.row:self.row + rows]) + (rows - 1)
rule = self.char_double_rule if self.local_row + rows - 1 == self.nb_rows - 1 else self.char_single_rule
sep = self.char_vertical_rule
# Draw the horizontal rule
line = self.lines[self.line + height]
int1 = self.get_intersection(line[self.cursor], rule)
int2 = self.get_intersection(line[self.cursor + width + 1], rule)
line = line[:self.cursor] + int1 + (width * rule) + int2 + line[self.cursor + width + 2:]
self.lines[self.line + height] = line
# Draw the vertical rule
for i in range(height):
line = self.lines[self.line + i]
line = line[:self.cursor + width + 1] + sep + line[self.cursor + width + 2:]
self.lines[self.line + i] = line
line = self.lines[self.line - 1]
int3 = self.get_intersection(line[self.cursor + width + 1], sep)
line = line[:self.cursor + width + 1] + int3 + line[self.cursor + width + 2:]
self.lines[self.line - 1] = line
self.col += cols
self.cursor += width + 1
# Do not recurse
raise nodes.SkipChildren
class TableWriter(nodes.NodeVisitor):
def __init__(self, props, document, **options):
nodes.NodeVisitor.__init__(self, document)
self.props = props
self.level = 0
self.line = 0
self.cursor = 0
self.col = 0
self.row = 0
self.nb_rows = 0
self.options = options
def __getattr__(self, name):
if name.startswith('visit_') or name.startswith('depart_'):
def noop(*args, **kwargs):
pass
return noop
raise AttributeError(name)
def visit_table(self, node):
drawer = TableDrawer(self.props, self.document, **self.options)
node.walkabout(drawer)
self.lines = drawer.lines
def visit_row(self, node):
self.col = 0
self.cursor = 0
def depart_row(self, node):
self.line += self.props.heights[self.row] + 1
self.row += 1
self.local_row += 1
def visit_thead(self, node):
self.nb_rows = len(node.children)
self.local_row = 0
visit_tbody = visit_thead
def visit_entry(self, node):
cols = node.attributes.get('morecols', 0) + 1
rows = node.attributes.get('morerows', 0) + 1
width = sum(self.props.widths[self.col:self.col + cols]) + (cols - 1)
height = sum(self.props.heights[self.row:self.row + rows]) + (rows - 1)
from .ansi import ANSITranslator
if node.children:
v = ANSITranslator(self.document, termsize=(width - 2, height), **self.options)
node.children[0].walkabout(v)
v.strip_empty_lines()
i = 1
for l in v.lines:
for sl in l.split('\n'):
line = self.lines[self.line + i]
line = line[:self.cursor + 2] + sl + line[self.cursor + 2 + len(sl):]
self.lines[self.line + i] = line
i += 1
self.col += cols
self.cursor += width + 1
# Do not recurse
raise nodes.SkipChildren
|