Skip to content

Commit

Permalink
Merge branch 'develop' of github.com:Tishka17/html2tg into develop
Browse files Browse the repository at this point in the history
  • Loading branch information
Tishka17 committed Sep 3, 2023
2 parents 1da24ed + 8e34353 commit cb4bbbb
Show file tree
Hide file tree
Showing 11 changed files with 125 additions and 27 deletions.
43 changes: 43 additions & 0 deletions .github/workflows/setup.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
# This workflow will install Python dependencies, run tests and lint with a single version of Python
# For more information see: https://docs.github.com/en/actions/automating-builds-and-tests/building-and-testing-python

name: CI

on:
push:
branches: [ "develop" ]
pull_request:
branches: [ "develop" ]

jobs:
cpython:
runs-on: ${{ matrix.os }}
strategy:
matrix:
os:
- ubuntu-latest
python-version:
- "3.8"
- "3.9"
- "3.10"
- "3.11"

steps:
- uses: actions/checkout@v3
- name: Set up ${{ matrix.python-version }} on ${{ matrix.os }}
uses: actions/setup-python@v3
with:
python-version: ${{ matrix.python-version }}

- name: Install dependencies
run: |
python -m pip install --upgrade pip
pip install . -r requirements_dev.txt
- name: Run ruff
run: |
ruff check src/sulguk
- name: Run tests
run: |
pytest
2 changes: 2 additions & 0 deletions requirements_dev.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
ruff
pytest
22 changes: 22 additions & 0 deletions ruff.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
line-length = 79
src = ["src/sulguk"]

select = [
"E",
"F",
"BLE",
"B",
"COM",
"C4",
"ERA",
"T20",
"I",
"N",
"ASYNC",
"PIE",
"RUF",
]

ignore = [
"RUF001",
]
2 changes: 1 addition & 1 deletion src/sulguk/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
from .wrapper import RenderResult, transform_html

try:
from .aiogram_middleware import AiogramSulgukMiddleware
from .aiogram_middleware import AiogramSulgukMiddleware # noqa: F401

__all__.append("AiogramSulgukMiddleware")
except ImportError:
Expand Down
15 changes: 11 additions & 4 deletions src/sulguk/aiogram_middleware.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,22 @@
NextRequestMiddlewareType,
)
from aiogram.methods import (
Response, TelegramMethod, AnswerInlineQuery, AnswerWebAppQuery,
EditMessageMedia, SendMediaGroup,
AnswerInlineQuery,
AnswerWebAppQuery,
EditMessageMedia,
Response,
SendMediaGroup,
TelegramMethod,
)
from aiogram.methods.base import TelegramType
from aiogram.types import (
InlineQueryResult, InlineQueryResultArticle, UNSET_PARSE_MODE
UNSET_PARSE_MODE,
InlineQueryResult,
InlineQueryResultArticle,
)

from sulguk.data import SULGUK_PARSE_MODE

from .wrapper import transform_html

logger = logging.getLogger(__name__)
Expand All @@ -30,7 +37,7 @@ def __init__(self):
EditMessageMedia: self._process_edit_message_media,
SendMediaGroup: self._process_send_media_group,
AnswerWebAppQuery: self._process_answer_web_app_query,
AnswerInlineQuery: self._process_answer_inline_query
AnswerInlineQuery: self._process_answer_inline_query,
}

async def __call__(
Expand Down
14 changes: 12 additions & 2 deletions src/sulguk/entities/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,18 @@

from .base import DecoratedEntity, Entity, Group
from .decoration import (
Blockquote, Bold, Code, Italic, Link, Paragraph, Pre,
Quote, Spoiler, Strikethrough, Underline, Uppercase,
Blockquote,
Bold,
Code,
Italic,
Link,
Paragraph,
Pre,
Quote,
Spoiler,
Strikethrough,
Underline,
Uppercase,
)
from .list import ListGroup, ListItem
from .no_contents import HorizontalLine, NewLine
Expand Down
9 changes: 3 additions & 6 deletions src/sulguk/entities/decoration.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

from sulguk.data import MessageEntity
from sulguk.render import State, TextMode

from .base import DecoratedEntity, Group


Expand All @@ -12,10 +13,7 @@ class Link(DecoratedEntity):

def _get_entity(self, offset: int, length: int) -> MessageEntity:
return MessageEntity(
type="text_link",
url=self.url,
offset=offset,
length=length,
type="text_link", url=self.url, offset=offset, length=length,
)


Expand All @@ -41,7 +39,7 @@ def _get_entity(self, offset: int, length: int) -> MessageEntity:
class Strikethrough(DecoratedEntity):
def _get_entity(self, offset: int, length: int) -> MessageEntity:
return MessageEntity(
type="strikethrough", offset=offset, length=length
type="strikethrough", offset=offset, length=length,
)


Expand All @@ -53,7 +51,6 @@ def _get_entity(self, offset: int, length: int) -> MessageEntity:

@dataclass
class Code(DecoratedEntity):

def _get_entity(self, offset: int, length: int) -> MessageEntity:
return MessageEntity(
type="code", offset=offset, length=length,
Expand Down
8 changes: 3 additions & 5 deletions src/sulguk/entities/progress.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
from dataclasses import dataclass, field
from typing import List, Optional
from dataclasses import dataclass

from sulguk.data import NumberFormat
from sulguk.render import State, int_to_number
from sulguk.render import State

from .base import Entity, Group
from .base import Entity


@dataclass
Expand Down
1 change: 1 addition & 0 deletions src/sulguk/entities/stub.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from dataclasses import dataclass

from sulguk.render import State

from .base import Entity
Expand Down
4 changes: 2 additions & 2 deletions src/sulguk/render/canvas.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ def fix_text_normal(text: str, trim_start: bool) -> str:
text = " ".join(NORMAL_SPACES.split(text))
return text


def fix_text_pre(text: str, trim_start: bool) -> str:
if trim_start and text.startswith("\n"):
text = text[1:]
Expand Down Expand Up @@ -99,7 +100,7 @@ def add_text(self, text):
text = fix_text_normal(
text=text,
trim_start=self.state in (
State.START, State.NEW_LINE, State.EMPTY_LINE, State.SPACE,
State.START, State.NEW_LINE, State.EMPTY_LINE, State.SPACE,
),
)
elif self.text_mode is TextMode.PRE:
Expand All @@ -119,4 +120,3 @@ def add_text(self, text):
self.state = State.NEW_LINE
else:
self.state = State.IN_TEXT

32 changes: 25 additions & 7 deletions src/sulguk/transformer.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,28 @@
from typing import Any, List, Optional, Tuple

from sulguk.render.numbers import NumberFormat

from .entities import (
Blockquote, Bold, Code, Entity, Group, HorizontalLine,
Italic, Link, ListGroup, ListItem, NewLine, Paragraph, Pre, Progress,
Quote, Spoiler, Strikethrough, Stub, Text, Underline,
Blockquote,
Bold,
Code,
Entity,
Group,
HorizontalLine,
Italic,
Link,
ListGroup,
ListItem,
NewLine,
Paragraph,
Pre,
Progress,
Quote,
Spoiler,
Strikethrough,
Stub,
Text,
Underline,
Uppercase,
)

Expand Down Expand Up @@ -121,9 +139,9 @@ def _get_h(self, tag: str, attrs: Attrs):
entity.add(
Bold(
entities=[
Underline(entities=[Uppercase(entities=[inner])])
]
)
Underline(entities=[Uppercase(entities=[inner])]),
],
),
)
elif tag == "h2":
entity.add(Bold(entities=[Underline(entities=[inner])]))
Expand Down Expand Up @@ -195,7 +213,7 @@ def handle_starttag(
nested = entity = Strikethrough()
elif tag in ("code",):
nested = entity = self._get_code(attrs)
elif tag in ("kbd", "samp",):
elif tag in ("kbd", "samp"):
nested = entity = Code()
elif tag in ("div", "footer", "header", "main", "nav", "section"):
nested = entity = Group(block=True)
Expand Down

0 comments on commit cb4bbbb

Please sign in to comment.