Skip to content

Commit

Permalink
refactor(sudo): use 'pre' HTML tag for code block
Browse files Browse the repository at this point in the history
  • Loading branch information
HitaloM committed Jul 18, 2024
1 parent 4cb89f7 commit db21209
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 23 deletions.
9 changes: 5 additions & 4 deletions src/korone/modules/sudo/handlers/evaluate.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
# SPDX-License-Identifier: BSD-3-Clause
# Copyright (c) 2024 Hitalo M. <https://github.com/HitaloM>

import html
import traceback

from hairydogm.chat_action import ChatActionSender
from hydrogram import Client
from hydrogram.enums import ParseMode
from hydrogram.types import Message
from meval import meval

from korone.decorators import router
from korone.filters import Command, CommandObject, IsSudo
from korone.handlers.abstract import MessageHandler
from korone.modules.sudo.utils import build_text, generate_document
from korone.modules.sudo.utils import generate_document


class Evaluate(MessageHandler):
Expand All @@ -31,7 +31,8 @@ async def handle(client: Client, message: Message) -> None:
except Exception:
traceback_string = traceback.format_exc()
await message.reply(
f"Exception while running the code:\n<pre>{traceback_string}</pre>"
"Exception while running the code:\n"
f"<pre language='bash'>{html.escape(traceback_string)}</pre>"
)
return

Expand All @@ -43,4 +44,4 @@ async def handle(client: Client, message: Message) -> None:
await generate_document(output, message)
return

await message.reply(build_text(str(output)), parse_mode=ParseMode.MARKDOWN)
await message.reply(f"<pre language='bash'>{html.escape(str(output))}</pre>")
9 changes: 5 additions & 4 deletions src/korone/modules/sudo/handlers/execute.py
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
# SPDX-License-Identifier: BSD-3-Clause
# Copyright (c) 2024 Hitalo M. <https://github.com/HitaloM>

import html
import io
import traceback
from contextlib import redirect_stdout

from hairydogm.chat_action import ChatActionSender
from hydrogram import Client
from hydrogram.enums import ParseMode
from hydrogram.types import Message

from korone.decorators import router
from korone.filters import Command, CommandObject, IsSudo
from korone.handlers.abstract import MessageHandler
from korone.modules.sudo.utils import build_text, generate_document
from korone.modules.sudo.utils import generate_document


class Execute(MessageHandler):
Expand Down Expand Up @@ -42,8 +42,9 @@ async def handle(client: Client, message: Message) -> None:
try:
await locals()["__ex"](client, message, reply, user, chat)
except Exception:
traceback_string = traceback.format_exc()
await message.reply(
build_text(traceback.format_exc()), parse_mode=ParseMode.MARKDOWN
f"<pre language='bash'>{html.escape(traceback_string)}</pre>"
)
return

Expand All @@ -56,4 +57,4 @@ async def handle(client: Client, message: Message) -> None:
await generate_document(output, message)
return

await message.reply(build_text(str(output)), parse_mode=ParseMode.MARKDOWN)
await message.reply(f"<pre language='bash'>{html.escape(str(output))}</pre>")
7 changes: 4 additions & 3 deletions src/korone/modules/sudo/handlers/shell.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
# SPDX-License-Identifier: BSD-3-Clause
# Copyright (c) 2024 Hitalo M. <https://github.com/HitaloM>

import html

from hairydogm.chat_action import ChatActionSender
from hydrogram import Client
from hydrogram.enums import ParseMode
from hydrogram.types import Message

from korone.decorators import router
from korone.filters import Command, CommandObject, IsSudo
from korone.handlers.abstract import MessageHandler
from korone.modules.sudo.utils import build_text, generate_document, run_command
from korone.modules.sudo.utils import generate_document, run_command


class Shell(MessageHandler):
Expand All @@ -32,4 +33,4 @@ async def handle(client: Client, message: Message) -> None:
await generate_document(output, message)
return

await message.reply(build_text(output), parse_mode=ParseMode.MARKDOWN)
await message.reply(f"<pre language='bash'>{html.escape(str(output))}</pre>")
7 changes: 2 additions & 5 deletions src/korone/modules/sudo/handlers/updater.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,14 @@

from hairydogm.keyboard import InlineKeyboardBuilder
from hydrogram import Client
from hydrogram.enums import ParseMode
from hydrogram.types import CallbackQuery, Message

from korone import cache
from korone.decorators import router
from korone.filters import Command, IsSudo
from korone.handlers.abstract import CallbackQueryHandler, MessageHandler
from korone.modules.sudo.callback_data import UpdateCallbackData
from korone.modules.sudo.utils import build_text, generate_document, run_command
from korone.modules.sudo.utils import generate_document, run_command


class UpdateCommand(MessageHandler):
Expand Down Expand Up @@ -88,6 +87,4 @@ async def handle(client: Client, callback: CallbackQuery) -> None:
await generate_document(stdout, message)
return

text += build_text(stdout)

await sent.edit(text, parse_mode=ParseMode.MARKDOWN)
await sent.edit(f"<pre language='bash'>{html.escape(str(stdout))}</pre>")
7 changes: 0 additions & 7 deletions src/korone/modules/sudo/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,11 @@

import asyncio
import io
import re
from typing import Any

from hydrogram.types import Message


def build_text(output: str) -> str:
output = re.sub(f'([{re.escape("```")}])', r"\\\1", output)

return f"```bash\n{output}\n```"


async def generate_document(output: Any, message: Message):
with io.BytesIO(str.encode(str(output))) as file:
file.name = "output.txt"
Expand Down

0 comments on commit db21209

Please sign in to comment.