Skip to content

Commit

Permalink
Introduce ruff execution on the guides folder #2876 (#2879)
Browse files Browse the repository at this point in the history
  • Loading branch information
iAndriy committed Dec 25, 2023
1 parent 29d4289 commit ee8e0ed
Show file tree
Hide file tree
Showing 29 changed files with 472 additions and 161 deletions.
2 changes: 1 addition & 1 deletion Makefile
@@ -1,4 +1,4 @@
RUFF_FORMATTED_FOLDERS = sanic examples scripts tests
RUFF_FORMATTED_FOLDERS = sanic examples scripts tests guide docs
.DEFAULT: help

.PHONY: help
Expand Down
2 changes: 1 addition & 1 deletion docs/conf.py
Expand Up @@ -19,7 +19,7 @@
root_directory = os.path.dirname(os.getcwd())
sys.path.insert(0, root_directory)

import sanic
import sanic # noqa


# -- General configuration ------------------------------------------------
Expand Down
476 changes: 366 additions & 110 deletions guide/content/en/emoji.py

Large diffs are not rendered by default.

2 changes: 2 additions & 0 deletions guide/content/en/migrate.py
@@ -1,9 +1,11 @@
import re

from pathlib import Path
from textwrap import indent

from emoji import EMOJI


COLUMN_PATTERN = re.compile(r"---:1\s*(.*?)\s*:--:1\s*(.*?)\s*:---", re.DOTALL)
PYTHON_HIGHLIGHT_PATTERN = re.compile(r"```python\{+.*?\}", re.DOTALL)
BASH_HIGHLIGHT_PATTERN = re.compile(r"```bash\{+.*?\}", re.DOTALL)
Expand Down
1 change: 1 addition & 0 deletions guide/server.py
Expand Up @@ -10,4 +10,5 @@

from webapp.worker.factory import create_app


app = create_app(Path(__file__).parent)
2 changes: 1 addition & 1 deletion guide/webapp/display/base.py
@@ -1,6 +1,6 @@
from __future__ import annotations

from html5tagger import Builder, Document, E # type: ignore
from html5tagger import Builder, Document # type: ignore


class BaseRenderer:
Expand Down
1 change: 1 addition & 0 deletions guide/webapp/display/layouts/base.py
Expand Up @@ -4,6 +4,7 @@
from typing import Generator

from html5tagger import Builder

from sanic import Request


Expand Down
4 changes: 3 additions & 1 deletion guide/webapp/display/layouts/elements/footer.py
@@ -1,6 +1,8 @@
from datetime import datetime
from collections import deque
from datetime import datetime

from html5tagger import Builder, E # type: ignore

from sanic import Request


Expand Down
4 changes: 2 additions & 2 deletions guide/webapp/display/layouts/elements/navbar.py
@@ -1,7 +1,7 @@
from webapp.display.layouts.models import MenuItem

from html5tagger import Builder, E # type: ignore

from sanic import Request
from webapp.display.layouts.models import MenuItem


def do_navbar(builder: Builder, request: Request) -> None:
Expand Down
6 changes: 3 additions & 3 deletions guide/webapp/display/layouts/elements/sidebar.py
@@ -1,8 +1,8 @@
from webapp.display.layouts.models import MenuItem
from webapp.display.text import slugify

from html5tagger import Builder, E # type: ignore

from sanic import Request
from webapp.display.layouts.models import MenuItem
from webapp.display.text import slugify


def do_sidebar(builder: Builder, request: Request) -> None:
Expand Down
2 changes: 2 additions & 0 deletions guide/webapp/display/layouts/home.py
Expand Up @@ -4,8 +4,10 @@
from typing import Generator

from html5tagger import Builder, E

from sanic import Request
from webapp.display.layouts.elements.footer import do_footer

from .base import BaseLayout


Expand Down
3 changes: 1 addition & 2 deletions guide/webapp/display/layouts/main.py
@@ -1,12 +1,11 @@
from contextlib import contextmanager
from typing import Generator

from sanic import Request
from webapp.display.layouts.elements.footer import do_footer
from webapp.display.layouts.elements.navbar import do_navbar
from webapp.display.layouts.elements.sidebar import do_sidebar

from sanic import Request

from .base import BaseLayout


Expand Down
24 changes: 15 additions & 9 deletions guide/webapp/display/markdown.py
@@ -1,23 +1,23 @@
import re

from textwrap import dedent

from html5tagger import HTML, Builder, E # type: ignore
from mistune import HTMLRenderer, create_markdown, escape
from mistune.directives import RSTDirective, TableOfContents
from mistune.util import safe_entity
from pygments import highlight
from pygments.formatters import html
from pygments.lexers import get_lexer_by_name

from html5tagger import HTML, Builder, E # type: ignore

from .code_style import SanicCodeStyle
from .plugins.attrs import Attributes
from .plugins.columns import Column
from .plugins.hook import Hook
from .plugins.inline_directive import inline_directive
from .plugins.mermaid import Mermaid
from .plugins.notification import Notification
from .plugins.span import span
from .plugins.inline_directive import inline_directive
from .plugins.tabs import Tabs
from .text import slugify

Expand All @@ -38,22 +38,26 @@ def block_code(self, code: str, info: str | None = None):
class_="code-block__copy",
onclick="copyCode(this)",
):
builder.div(class_="code-block__rectangle code-block__filled").div(
class_="code-block__rectangle code-block__outlined"
)
builder.div(
class_="code-block__rectangle code-block__filled"
).div(class_="code-block__rectangle code-block__outlined")
else:
builder.pre(E.code(escape(code)))
return str(builder)

def heading(self, text: str, level: int, **attrs) -> str:
ident = slugify(text)
if level > 1:
text += self._make_tag("a", {"href": f"#{ident}", "class": "anchor"}, "#")
text += self._make_tag(
"a", {"href": f"#{ident}", "class": "anchor"}, "#"
)
return self._make_tag(
f"h{level}",
{
"id": ident,
"class": (f"is-size-{level}-desktop " f"is-size-{level+2}-touch"),
"class": (
f"is-size-{level}-desktop " f"is-size-{level+2}-touch"
),
},
text,
)
Expand Down Expand Up @@ -118,7 +122,9 @@ def inline_directive(self, text: str, **attrs) -> str:
def _make_tag(
self, tag: str, attributes: dict[str, str], text: str | None = None
) -> str:
attrs = " ".join(f'{key}="{value}"' for key, value in attributes.items())
attrs = " ".join(
f'{key}="{value}"' for key, value in attributes.items()
)
if text is None:
return f"<{tag} {attrs} />"
return f"<{tag} {attrs}>{text}</{tag}>"
Expand Down
1 change: 1 addition & 0 deletions guide/webapp/display/page/__init__.py
@@ -1,4 +1,5 @@
from .page import Page
from .renderer import PageRenderer


__all__ = ["Page", "PageRenderer"]
56 changes: 43 additions & 13 deletions guide/webapp/display/page/docobject.py
Expand Up @@ -3,14 +3,14 @@
import importlib
import inspect
import pkgutil

from collections import defaultdict
from dataclasses import dataclass, field
from html import escape

from docstring_parser import Docstring, DocstringParam, DocstringRaises
from docstring_parser import parse as parse_docstring
from docstring_parser.common import DocstringExample

from html5tagger import HTML, Builder, E # type: ignore

from ..markdown import render_markdown, slugify
Expand Down Expand Up @@ -141,7 +141,9 @@ def _extract_docobjects(package_name: str) -> dict[str, DocObject]:
docstrings = {}
package = importlib.import_module(package_name)

for _, name, _ in pkgutil.walk_packages(package.__path__, package_name + "."):
for _, name, _ in pkgutil.walk_packages(
package.__path__, package_name + "."
):
module = importlib.import_module(name)
for obj_name, obj in inspect.getmembers(module):
if (
Expand Down Expand Up @@ -175,7 +177,9 @@ def _docobject_to_html(
) -> None:
anchor_id = slugify(docobject.full_name.replace(".", "-"))
anchor = E.a("#", class_="anchor", href=f"#{anchor_id}")
class_name, heading = _define_heading_and_class(docobject, anchor, as_method)
class_name, heading = _define_heading_and_class(
docobject, anchor, as_method
)

with builder.div(class_=class_name):
builder(heading)
Expand Down Expand Up @@ -229,7 +233,9 @@ def _docobject_to_html(

if docobject.docstring.params:
with builder.div(class_="box mt-5"):
builder.h5("Parameters", class_="is-size-5 has-text-weight-bold")
builder.h5(
"Parameters", class_="is-size-5 has-text-weight-bold"
)
_render_params(builder, docobject.docstring.params)

if docobject.docstring.returns:
Expand All @@ -254,7 +260,9 @@ def _signature_to_html(
parts = []
parts.append("<span class='function-signature'>")
for decorator in decorators:
parts.append(f"<span class='function-decorator'>@{decorator}</span><br>")
parts.append(
f"<span class='function-decorator'>@{decorator}</span><br>"
)
parts.append(
f"<span class='is-italic'>{object_type}</span> "
f"<span class='has-text-weight-bold'>{name}</span>("
Expand All @@ -268,7 +276,9 @@ def _signature_to_html(
annotation = ""
if param.annotation != inspect.Parameter.empty:
annotation = escape(str(param.annotation))
parts.append(f": <span class='param-annotation'>{annotation}</span>")
parts.append(
f": <span class='param-annotation'>{annotation}</span>"
)
if param.default != inspect.Parameter.empty:
default = escape(str(param.default))
if annotation == "str":
Expand All @@ -279,7 +289,9 @@ def _signature_to_html(
parts.append(")")
if signature.return_annotation != inspect.Signature.empty:
return_annotation = escape(str(signature.return_annotation))
parts.append(f": -> <span class='return-annotation'>{return_annotation}</span>")
parts.append(
f": -> <span class='return-annotation'>{return_annotation}</span>"
)
parts.append("</span>")
return "".join(parts)

Expand Down Expand Up @@ -320,7 +332,8 @@ def _render_params(builder: Builder, params: list[DocstringParam]) -> None:
E.span(
param.type_name,
class_=(
"has-text-weight-normal has-text-purple " "is-size-7 ml-2"
"has-text-weight-normal has-text-purple "
"is-size-7 ml-2"
),
),
]
Expand All @@ -329,7 +342,10 @@ def _render_params(builder: Builder, params: list[DocstringParam]) -> None:
builder.dd(
HTML(
render_markdown(
param.description or param.arg_name or param.type_name or ""
param.description
or param.arg_name
or param.type_name
or ""
)
)
)
Expand All @@ -342,7 +358,11 @@ def _render_raises(builder: Builder, raises: list[DocstringRaises]) -> None:
with builder.dl(class_="mt-2"):
builder.dt(raise_.type_name, class_="is-family-monospace")
builder.dd(
HTML(render_markdown(raise_.description or raise_.type_name or ""))
HTML(
render_markdown(
raise_.description or raise_.type_name or ""
)
)
)


Expand All @@ -358,7 +378,11 @@ def _render_returns(builder: Builder, docobject: DocObject) -> None:
if not return_type or return_type == inspect.Signature.empty:
return_type = "N/A"

term = "Return" if not docobject.docstring.returns.is_generator else "Yields"
term = (
"Return"
if not docobject.docstring.returns.is_generator
else "Yields"
)
builder.h5(term, class_="is-size-5 has-text-weight-bold")
with builder.dl(class_="mt-2"):
builder.dt(return_type, class_="is-family-monospace")
Expand All @@ -373,11 +397,17 @@ def _render_returns(builder: Builder, docobject: DocObject) -> None:
)


def _render_examples(builder: Builder, examples: list[DocstringExample]) -> None:
def _render_examples(
builder: Builder, examples: list[DocstringExample]
) -> None:
with builder.div(class_="box mt-5"):
builder.h5("Examples", class_="is-size-5 has-text-weight-bold")
for example in examples:
with builder.div(class_="mt-2"):
builder(
HTML(render_markdown(example.description or example.snippet or ""))
HTML(
render_markdown(
example.description or example.snippet or ""
)
)
)
1 change: 1 addition & 0 deletions guide/webapp/display/page/page.py
Expand Up @@ -12,6 +12,7 @@
from ..markdown import render_markdown
from .docobject import organize_docobjects


_PAGE_CACHE: dict[
str, dict[str, tuple[Page | None, Page | None, Page | None]]
] = {}
Expand Down
6 changes: 3 additions & 3 deletions guide/webapp/display/page/renderer.py
Expand Up @@ -3,10 +3,10 @@
from contextlib import contextmanager
from typing import Type

from webapp.display.base import BaseRenderer

from html5tagger import HTML, Builder # type: ignore
from sanic import Request, request

from sanic import Request
from webapp.display.base import BaseRenderer

from ..layouts.base import BaseLayout
from .page import Page
Expand Down
3 changes: 1 addition & 2 deletions guide/webapp/display/plugins/attrs.py
Expand Up @@ -2,12 +2,11 @@
from textwrap import dedent
from typing import Any

from html5tagger import HTML, E
from mistune.block_parser import BlockParser
from mistune.core import BlockState
from mistune.directives import DirectivePlugin

from html5tagger import HTML, E


class Attributes(DirectivePlugin):
def __call__(self, directive, md):
Expand Down
1 change: 1 addition & 0 deletions guide/webapp/display/plugins/inline_directive.py
Expand Up @@ -2,6 +2,7 @@

from mistune.markdown import Markdown


DIRECTIVE_PATTERN = r":(?:class|func|meth|attr|exc|mod|data|const|obj|keyword|option|cmdoption|envvar):`(?P<ref>sanic\.[^`]+)`" # noqa: E501


Expand Down
3 changes: 1 addition & 2 deletions guide/webapp/display/plugins/mermaid.py
Expand Up @@ -3,14 +3,13 @@
from textwrap import dedent
from typing import Any

from html5tagger import HTML, E
from mistune import HTMLRenderer
from mistune.block_parser import BlockParser
from mistune.core import BlockState
from mistune.directives import DirectivePlugin, RSTDirective
from mistune.markdown import Markdown

from html5tagger import HTML, E


class Mermaid(DirectivePlugin):
def parse(
Expand Down
3 changes: 1 addition & 2 deletions guide/webapp/display/plugins/notification.py
@@ -1,6 +1,5 @@
from mistune.directives import Admonition

from html5tagger import HTML, E
from mistune.directives import Admonition


class Notification(Admonition):
Expand Down

0 comments on commit ee8e0ed

Please sign in to comment.