Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 17 additions & 21 deletions lobster/htmldoc/htmldoc.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
# <https://www.gnu.org/licenses/>.

import html
from abc import abstractmethod, ABC
from typing import List, Optional

from lobster.htmldoc import assets

Expand All @@ -37,25 +39,22 @@
"""


class Menu_Item:
class Menu_Item(ABC):
def __init__(self, name):
assert isinstance(name, str)
self.name = name

def generate(self, doc):
assert isinstance(doc, Document)
assert False
@abstractmethod
def generate(self, doc) -> List[str]:
pass


class Menu_Link(Menu_Item):
def __init__(self, name, target):
assert isinstance(target, str)
super().__init__(name)

self.target = target

def generate(self, doc):
assert isinstance(doc, Document)
def generate(self, doc) -> List[str]:
rv = (
f'<a href="{self.target}" '
f'id="menu-item-{html.escape(self.name).replace(" ", "-").lower()}">'
Expand All @@ -75,8 +74,7 @@ def __init__(self, name):
def add_link(self, name, target):
self.items.append(Menu_Link(name, target))

def generate(self, doc):
assert isinstance(doc, Document)
def generate(self, doc) -> List[str]:

doc.style["#navbar .dropdown"] = {
"float" : "left",
Expand Down Expand Up @@ -144,8 +142,8 @@ def generate(self, doc):

class Navigation_Bar:
def __init__(self):
self.left_items = []
self.right_items = []
self.left_items: List[Menu_Item] = []
self.right_items: List[Menu_Item] = []

def add_link(self, name, target, alignment="left"):
assert alignment in ("left", "right")
Expand All @@ -168,8 +166,6 @@ def add_dropdown(self, name, alignment="left"):
return menu

def generate(self, doc):
assert isinstance(doc, Document)

doc.style["#navbar"] = {
"overflow" : "hidden",
"background-color" : doc.primary_color,
Expand Down Expand Up @@ -217,8 +213,6 @@ def generate(self, doc):

class Document:
def __init__(self, title, subtitle):
assert isinstance(title, str)
assert isinstance(subtitle, str)
self.title = title
self.subtitle = subtitle

Expand Down Expand Up @@ -258,16 +252,18 @@ def __init__(self, title, subtitle):
self.css = []

def add_line(self, line):
assert isinstance(line, str)
if len(self.body) == 0:
self.body.append('<div class="content">')
self.body.append(line)

def add_heading(self, level, text, anchor=None, html_identifier=False):
assert isinstance(level, int)
assert isinstance(text, str)
def add_heading(
self,
level: int,
text: str,
anchor: Optional[str] = None,
html_identifier: bool = False,
):
assert 2 <= level <= 7
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of assert here we can add a check and raise an error

Copy link
Member Author

@phiwuu phiwuu Feb 18, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, that's true. But I wanted to keep the refactoring minimal. Raising an exception basically means a new code path, which should be tested by a unit test. There is another assertion of this kind: assert alignment in ("left", "right")
I recommend to do this in a separate pull request.

The main goal of this pull request is to resolve the pylint warning too-few-public-methods. Please do the following:

  1. checkout main branch
  2. remove too-few-public-methods from
    too-few-public-methods,
  3. call make lint, and remember the output
  4. checkout the branch internal/use-abc of this PR, and repeat steps 2 and 3

The errors in file htmldoc.py will disappear. I should have made this clear from the beginning in the PR description. I will update it accordingly.

I removed the assert statements only as a clean-up side-task. Please approve the PR without further changes. Thank you!

assert anchor is None or isinstance(anchor, str)

if level == 2 and self.body:
self.body.append("</div>")
Expand Down
Loading