1# Licensed under a 3-clause BSD style license
2"""
3:Author: Simon Gibbons (simongibbons@gmail.com)
4"""
5
6
7from .core import DefaultSplitter
8from .fixedwidth import (FixedWidth,
9                         FixedWidthData,
10                         FixedWidthHeader,
11                         FixedWidthTwoLineDataSplitter)
12
13
14class SimpleRSTHeader(FixedWidthHeader):
15    position_line = 0
16    start_line = 1
17    splitter_class = DefaultSplitter
18    position_char = '='
19
20    def get_fixedwidth_params(self, line):
21        vals, starts, ends = super().get_fixedwidth_params(line)
22        # The right hand column can be unbounded
23        ends[-1] = None
24        return vals, starts, ends
25
26
27class SimpleRSTData(FixedWidthData):
28    start_line = 3
29    end_line = -1
30    splitter_class = FixedWidthTwoLineDataSplitter
31
32
33class RST(FixedWidth):
34    """reStructuredText simple format table.
35
36    See: https://docutils.sourceforge.io/docs/ref/rst/restructuredtext.html#simple-tables
37
38    Example::
39
40        ==== ===== ======
41        Col1  Col2  Col3
42        ==== ===== ======
43          1    2.3  Hello
44          2    4.5  Worlds
45        ==== ===== ======
46
47    Currently there is no support for reading tables which utilize continuation lines,
48    or for ones which define column spans through the use of an additional
49    line of dashes in the header.
50
51    """
52    _format_name = 'rst'
53    _description = 'reStructuredText simple table'
54    data_class = SimpleRSTData
55    header_class = SimpleRSTHeader
56
57    def __init__(self):
58        super().__init__(delimiter_pad=None, bookend=False)
59
60    def write(self, lines):
61        lines = super().write(lines)
62        lines = [lines[1]] + lines + [lines[1]]
63        return lines
64