mirror of
https://github.com/sphinx-doc/sphinx.git
synced 2025-02-25 18:55:22 -06:00
refactor: Update type annotations in sphinx.writers.*
This commit is contained in:
@@ -1468,7 +1468,7 @@ class LaTeXTranslator(SphinxTranslator):
|
|||||||
self.body.append('\n\\end{flushright}\n')
|
self.body.append('\n\\end{flushright}\n')
|
||||||
|
|
||||||
def visit_index(self, node: Element) -> None:
|
def visit_index(self, node: Element) -> None:
|
||||||
def escape(value):
|
def escape(value: str) -> str:
|
||||||
value = self.encode(value)
|
value = self.encode(value)
|
||||||
value = value.replace(r'\{', r'\sphinxleftcurlybrace{}')
|
value = value.replace(r'\{', r'\sphinxleftcurlybrace{}')
|
||||||
value = value.replace(r'\}', r'\sphinxrightcurlybrace{}')
|
value = value.replace(r'\}', r'\sphinxrightcurlybrace{}')
|
||||||
@@ -1478,7 +1478,7 @@ class LaTeXTranslator(SphinxTranslator):
|
|||||||
value = value.replace('|', r'\textbar{}')
|
value = value.replace('|', r'\textbar{}')
|
||||||
return value
|
return value
|
||||||
|
|
||||||
def style(string):
|
def style(string: str) -> str:
|
||||||
match = EXTRA_RE.match(string)
|
match = EXTRA_RE.match(string)
|
||||||
if match:
|
if match:
|
||||||
return match.expand(r'\\spxentry{\1}\\spxextra{\2}')
|
return match.expand(r'\\spxentry{\1}\\spxextra{\2}')
|
||||||
|
|||||||
@@ -12,7 +12,7 @@ import os
|
|||||||
import re
|
import re
|
||||||
import textwrap
|
import textwrap
|
||||||
from itertools import groupby, chain
|
from itertools import groupby, chain
|
||||||
from typing import Any, Dict, List, Iterable, Optional, Set, Tuple, Union
|
from typing import Any, Dict, Generator, List, Iterable, Optional, Set, Tuple, Union
|
||||||
from typing import cast
|
from typing import cast
|
||||||
|
|
||||||
from docutils import nodes, writers
|
from docutils import nodes, writers
|
||||||
@@ -32,23 +32,23 @@ class Cell:
|
|||||||
"""Represents a cell in a table.
|
"""Represents a cell in a table.
|
||||||
It can span on multiple columns or on multiple lines.
|
It can span on multiple columns or on multiple lines.
|
||||||
"""
|
"""
|
||||||
def __init__(self, text="", rowspan=1, colspan=1):
|
def __init__(self, text: str = "", rowspan: int = 1, colspan: int = 1) -> None:
|
||||||
self.text = text
|
self.text = text
|
||||||
self.wrapped = [] # type: List[str]
|
self.wrapped = [] # type: List[str]
|
||||||
self.rowspan = rowspan
|
self.rowspan = rowspan
|
||||||
self.colspan = colspan
|
self.colspan = colspan
|
||||||
self.col = None
|
self.col = None # type: Optional[int]
|
||||||
self.row = None
|
self.row = None # type: Optional[int]
|
||||||
|
|
||||||
def __repr__(self):
|
def __repr__(self) -> str:
|
||||||
return "<Cell {!r} {}v{}/{}>{}>".format(
|
return "<Cell {!r} {}v{}/{}>{}>".format(
|
||||||
self.text, self.row, self.rowspan, self.col, self.colspan
|
self.text, self.row, self.rowspan, self.col, self.colspan
|
||||||
)
|
)
|
||||||
|
|
||||||
def __hash__(self):
|
def __hash__(self) -> int:
|
||||||
return hash((self.col, self.row))
|
return hash((self.col, self.row))
|
||||||
|
|
||||||
def wrap(self, width):
|
def wrap(self, width: int) -> None:
|
||||||
self.wrapped = my_wrap(self.text, width)
|
self.wrapped = my_wrap(self.text, width)
|
||||||
|
|
||||||
|
|
||||||
@@ -98,7 +98,7 @@ class Table:
|
|||||||
+--------+--------+
|
+--------+--------+
|
||||||
|
|
||||||
"""
|
"""
|
||||||
def __init__(self, colwidth=None):
|
def __init__(self, colwidth: List[int] = None) -> None:
|
||||||
self.lines = [] # type: List[List[Cell]]
|
self.lines = [] # type: List[List[Cell]]
|
||||||
self.separator = 0
|
self.separator = 0
|
||||||
self.colwidth = (colwidth if colwidth is not None
|
self.colwidth = (colwidth if colwidth is not None
|
||||||
@@ -106,19 +106,19 @@ class Table:
|
|||||||
self.current_line = 0
|
self.current_line = 0
|
||||||
self.current_col = 0
|
self.current_col = 0
|
||||||
|
|
||||||
def add_row(self):
|
def add_row(self) -> None:
|
||||||
"""Add a row to the table, to use with ``add_cell()``. It is not needed
|
"""Add a row to the table, to use with ``add_cell()``. It is not needed
|
||||||
to call ``add_row()`` before the first ``add_cell()``.
|
to call ``add_row()`` before the first ``add_cell()``.
|
||||||
"""
|
"""
|
||||||
self.current_line += 1
|
self.current_line += 1
|
||||||
self.current_col = 0
|
self.current_col = 0
|
||||||
|
|
||||||
def set_separator(self):
|
def set_separator(self) -> None:
|
||||||
"""Sets the separator below the current line.
|
"""Sets the separator below the current line.
|
||||||
"""
|
"""
|
||||||
self.separator = len(self.lines)
|
self.separator = len(self.lines)
|
||||||
|
|
||||||
def add_cell(self, cell):
|
def add_cell(self, cell: Cell) -> None:
|
||||||
"""Add a cell to the current line, to use with ``add_row()``. To add
|
"""Add a cell to the current line, to use with ``add_row()``. To add
|
||||||
a cell spanning on multiple lines or rows, simply set the
|
a cell spanning on multiple lines or rows, simply set the
|
||||||
``cell.colspan`` or ``cell.rowspan`` BEFORE inserting it to
|
``cell.colspan`` or ``cell.rowspan`` BEFORE inserting it to
|
||||||
@@ -129,13 +129,13 @@ class Table:
|
|||||||
self[self.current_line, self.current_col] = cell
|
self[self.current_line, self.current_col] = cell
|
||||||
self.current_col += cell.colspan
|
self.current_col += cell.colspan
|
||||||
|
|
||||||
def __getitem__(self, pos):
|
def __getitem__(self, pos: Tuple[int, int]) -> Cell:
|
||||||
line, col = pos
|
line, col = pos
|
||||||
self._ensure_has_line(line + 1)
|
self._ensure_has_line(line + 1)
|
||||||
self._ensure_has_column(col + 1)
|
self._ensure_has_column(col + 1)
|
||||||
return self.lines[line][col]
|
return self.lines[line][col]
|
||||||
|
|
||||||
def __setitem__(self, pos, cell):
|
def __setitem__(self, pos: Tuple[int, int], cell: Cell) -> None:
|
||||||
line, col = pos
|
line, col = pos
|
||||||
self._ensure_has_line(line + cell.rowspan)
|
self._ensure_has_line(line + cell.rowspan)
|
||||||
self._ensure_has_column(col + cell.colspan)
|
self._ensure_has_column(col + cell.colspan)
|
||||||
@@ -145,19 +145,19 @@ class Table:
|
|||||||
cell.row = line
|
cell.row = line
|
||||||
cell.col = col
|
cell.col = col
|
||||||
|
|
||||||
def _ensure_has_line(self, line):
|
def _ensure_has_line(self, line: int) -> None:
|
||||||
while len(self.lines) < line:
|
while len(self.lines) < line:
|
||||||
self.lines.append([])
|
self.lines.append([])
|
||||||
|
|
||||||
def _ensure_has_column(self, col):
|
def _ensure_has_column(self, col: int) -> None:
|
||||||
for line in self.lines:
|
for line in self.lines:
|
||||||
while len(line) < col:
|
while len(line) < col:
|
||||||
line.append(None)
|
line.append(None)
|
||||||
|
|
||||||
def __repr__(self):
|
def __repr__(self) -> str:
|
||||||
return "\n".join(repr(line) for line in self.lines)
|
return "\n".join(repr(line) for line in self.lines)
|
||||||
|
|
||||||
def cell_width(self, cell, source):
|
def cell_width(self, cell: Cell, source: List[int]) -> int:
|
||||||
"""Give the cell width, according to the given source (either
|
"""Give the cell width, according to the given source (either
|
||||||
``self.colwidth`` or ``self.measured_widths``).
|
``self.colwidth`` or ``self.measured_widths``).
|
||||||
This take into account cells spanning on multiple columns.
|
This take into account cells spanning on multiple columns.
|
||||||
@@ -168,7 +168,7 @@ class Table:
|
|||||||
return width + (cell.colspan - 1) * 3
|
return width + (cell.colspan - 1) * 3
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def cells(self):
|
def cells(self) -> Generator[Cell, None, None]:
|
||||||
seen = set() # type: Set[Cell]
|
seen = set() # type: Set[Cell]
|
||||||
for lineno, line in enumerate(self.lines):
|
for lineno, line in enumerate(self.lines):
|
||||||
for colno, cell in enumerate(line):
|
for colno, cell in enumerate(line):
|
||||||
@@ -176,7 +176,7 @@ class Table:
|
|||||||
yield cell
|
yield cell
|
||||||
seen.add(cell)
|
seen.add(cell)
|
||||||
|
|
||||||
def rewrap(self):
|
def rewrap(self) -> None:
|
||||||
"""Call ``cell.wrap()`` on all cells, and measure each column width
|
"""Call ``cell.wrap()`` on all cells, and measure each column width
|
||||||
after wrapping (result written in ``self.measured_widths``).
|
after wrapping (result written in ``self.measured_widths``).
|
||||||
"""
|
"""
|
||||||
@@ -189,7 +189,7 @@ class Table:
|
|||||||
for col in range(cell.col, cell.col + cell.colspan):
|
for col in range(cell.col, cell.col + cell.colspan):
|
||||||
self.measured_widths[col] = max(self.measured_widths[col], width)
|
self.measured_widths[col] = max(self.measured_widths[col], width)
|
||||||
|
|
||||||
def physical_lines_for_line(self, line):
|
def physical_lines_for_line(self, line: List[Cell]) -> int:
|
||||||
"""From a given line, compute the number of physical lines it spans
|
"""From a given line, compute the number of physical lines it spans
|
||||||
due to text wrapping.
|
due to text wrapping.
|
||||||
"""
|
"""
|
||||||
@@ -198,7 +198,7 @@ class Table:
|
|||||||
physical_lines = max(physical_lines, len(cell.wrapped))
|
physical_lines = max(physical_lines, len(cell.wrapped))
|
||||||
return physical_lines
|
return physical_lines
|
||||||
|
|
||||||
def __str__(self):
|
def __str__(self) -> str:
|
||||||
out = []
|
out = []
|
||||||
self.rewrap()
|
self.rewrap()
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user