Skip to content

Commit

Permalink
Bug fixes and add tests
Browse files Browse the repository at this point in the history
* BUGFIX: table requires two lines prefix
* Add needtable to tests
* Allow table's title (caption)
* Allow misplaced text in the table (added before the table)

Signed-off-by: Liran Funaro <liran.funaro@gmail.com>
  • Loading branch information
liran-funaro committed Aug 12, 2023
1 parent e0ad370 commit 808c5b6
Show file tree
Hide file tree
Showing 9 changed files with 42 additions and 14 deletions.
1 change: 1 addition & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ test:
-D markdown_docinfo=True -D markdown_anchor_sections=True -D markdown_anchor_signatures=True

@cp "$(BUILD_DIR)/overrides/markdown/auto-summery.md" "$(BUILD_DIR)/markdown/overrides-auto-summery.md"
@rm -r $(BUILD_DIR)/markdown/_static $(BUILD_DIR)/markdown/permalink.html
@diff --recursive --color=always --side-by-side --text --suppress-common-lines \
"$(BUILD_DIR)/markdown" "$(EXPECTED_DIR)"

Expand Down
1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ requires-python = ">=3.7"
[project.optional-dependencies]
dev = [
"bumpver", "black", "isort", "flake8", "pylint", "pip-tools", "pytest", "pytest-cov", "coveralls",
"sphinx-needs", "sphinxcontrib-plantuml",
]

[project.urls]
Expand Down
27 changes: 15 additions & 12 deletions sphinx_markdown_builder/contexts.py
Original file line number Diff line number Diff line change
Expand Up @@ -198,8 +198,8 @@ def __init__(self, params=SubContextParams()):
super().__init__(params)
self.body: List[List[List[str]]] = []
self.headers: List[List[List[str]]] = []
self.internal_context = SubContext()

self.is_row = False
self.is_entry = False
self.is_header = False
self.is_body = False
Expand All @@ -213,8 +213,9 @@ def active_output(self) -> List[List[List[str]]]:

@property
def content(self):
assert self.is_entry
return self.active_output[-1][-1]
if self.is_entry:
return self.active_output[-1][-1]
return self.internal_context.content

def enter_head(self):
assert not self.is_header and not self.is_body
Expand All @@ -234,14 +235,11 @@ def exit_body(self):

def enter_row(self):
self.active_output.append([])
self.is_row = True

def exit_row(self):
assert self.is_row
self.is_row = False
pass

def enter_entry(self):
assert self.is_row
self.is_entry = True
self.active_output[-1].append([])
self.ensure_eol_count = 0
Expand All @@ -255,12 +253,17 @@ def make_row(row):
return ["".join(entries).replace("\n", "<br/>") for entries in row]

def make(self):
ctx = SubContext()
prefix = self.internal_context.make()
if prefix:
ctx.add(prefix)

content = [*self.headers, *self.body]
if len(content) == 0:
return ""
headers = self.make_row(content[0])
body = list(map(self.make_row, content[1:]))
return tabulate(body, headers=headers, tablefmt="github")
if len(content) > 0:
headers = self.make_row(content[0])
body = list(map(self.make_row, content[1:]))
ctx.add(tabulate(body, headers=headers, tablefmt="github"), prefix_eol=2)
return ctx.make()


class IndentContext(SubContext):
Expand Down
8 changes: 6 additions & 2 deletions sphinx_markdown_builder/translator.py
Original file line number Diff line number Diff line change
Expand Up @@ -422,7 +422,11 @@ def depart_section(self, _node):

@pushing_context
def visit_title(self, _node):
self._push_context(TitleContext(self.status.section_level))
if isinstance(self.ctx, TableContext):
level = 4
else:
level = self.status.section_level
self._push_context(TitleContext(level))

@pushing_context
def visit_subtitle(self, _node):
Expand Down Expand Up @@ -644,7 +648,7 @@ def table_ctx(self) -> TableContext:

@pushing_context
def visit_table(self, _node):
self._push_context(TableContext(params=SubContextParams(1, 1)))
self._push_context(TableContext(params=SubContextParams(2, 1)))

def visit_thead(self, _node):
self.table_ctx.enter_head() # workaround pylint: disable=no-member
Expand Down
1 change: 1 addition & 0 deletions tests/expected/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,3 +60,4 @@
* [Other text](blocks.md#other-text)
* [Test Image With Target](image-target.md)
* [Empty package](empty.md)
* [Need Table](needtable.md)
10 changes: 10 additions & 0 deletions tests/expected/needtable.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# Need Table

<a id="needtable-needtable-0"></a>

#### Example table

No needs passed the filters

| ID | Title | Status | Type | Outgoing | Tags |
|------|---------|----------|--------|------------|--------|
1 change: 1 addition & 0 deletions tests/source/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
"sphinx.ext.autosummary", # Create neat summary tables
"sphinx.ext.intersphinx", # Link to other project's documentation (see mapping below)
"sphinx_markdown_builder",
"sphinx_needs",
]


Expand Down
1 change: 1 addition & 0 deletions tests/source/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,4 @@ Main Test File
blocks.rst
image-target.rst
empty.rst
needtable.rst
6 changes: 6 additions & 0 deletions tests/source/needtable.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
Need Table
==========

.. needtable:: Example table
:tags: main_example
:style: table

0 comments on commit 808c5b6

Please sign in to comment.