Docs
This commit is contained in:
parent
4ec65920af
commit
f8b24cba49
4 changed files with 198 additions and 189 deletions
|
|
@ -0,0 +1,12 @@
|
||||||
|
"""
|
||||||
|
Godot Websocket - ChrisLR 2022
|
||||||
|
|
||||||
|
This provides parsing the ansi text to bbcode used by Godot for their RichTextLabel
|
||||||
|
and also provides the proper portal service to dedicate a port for Godot's Websockets.
|
||||||
|
|
||||||
|
This allows you to connect both the regular webclient and a godot specific webclient.
|
||||||
|
You can simply connect the resulting text to Godot's RichTextLabel and have the proper display.
|
||||||
|
You could also pass extra data to this client for advanced functionality.
|
||||||
|
|
||||||
|
See the docs for more information.
|
||||||
|
"""
|
||||||
|
|
@ -1,9 +1,10 @@
|
||||||
"""Tests for text2html """
|
"""Tests for text2bbcode """
|
||||||
|
|
||||||
from django.test import TestCase
|
|
||||||
from evennia.utils import ansi
|
|
||||||
from evennia.contrib.base_systems.godotwebsocket import text2bbcode
|
|
||||||
import mock
|
import mock
|
||||||
|
from django.test import TestCase
|
||||||
|
|
||||||
|
from evennia.contrib.base_systems.godotwebsocket import text2bbcode
|
||||||
|
from evennia.utils import ansi
|
||||||
|
|
||||||
|
|
||||||
class TestText2Bbcode(TestCase):
|
class TestText2Bbcode(TestCase):
|
||||||
|
|
|
||||||
|
|
@ -1,23 +1,18 @@
|
||||||
"""
|
"""
|
||||||
ANSI -> html converter
|
Godot Websocket - ChrisLR 2022
|
||||||
|
|
||||||
Credit for original idea and implementation
|
This file contains the necessary code and data to convert text with color tags to bbcode (For godot)
|
||||||
goes to Muhammad Alkarouri and his
|
|
||||||
snippet #577349 on http://code.activestate.com.
|
|
||||||
|
|
||||||
(extensively modified by Griatch 2010)
|
|
||||||
"""
|
"""
|
||||||
|
|
||||||
from evennia.utils.text2html import TextToHTMLparser
|
|
||||||
|
|
||||||
from evennia.utils.ansi import *
|
from evennia.utils.ansi import *
|
||||||
|
from evennia.utils.text2html import TextToHTMLparser
|
||||||
|
|
||||||
# All xterm256 RGB equivalents
|
# All xterm256 RGB equivalents
|
||||||
|
|
||||||
XTERM256_FG = "\033[38;5;{}m"
|
XTERM256_FG = "\033[38;5;{}m"
|
||||||
XTERM256_BG = "\033[48;5;{}m"
|
XTERM256_BG = "\033[48;5;{}m"
|
||||||
|
|
||||||
COLOR_INDICE_TO_HEX = {'color-000': '#000000', 'color-001': '#800000', 'color-002': '#008000', 'color-003': '#808000',
|
COLOR_INDICE_TO_HEX = {
|
||||||
|
'color-000': '#000000', 'color-001': '#800000', 'color-002': '#008000', 'color-003': '#808000',
|
||||||
'color-004': '#000080', 'color-005': '#800080', 'color-006': '#008080', 'color-007': '#c0c0c0',
|
'color-004': '#000080', 'color-005': '#800080', 'color-006': '#008080', 'color-007': '#c0c0c0',
|
||||||
'color-008': '#808080', 'color-009': '#ff0000', 'color-010': '#00ff00', 'color-011': '#ffff00',
|
'color-008': '#808080', 'color-009': '#ff0000', 'color-010': '#00ff00', 'color-011': '#ffff00',
|
||||||
'color-012': '#0000ff', 'color-013': '#ff00ff', 'color-014': '#00ffff', 'color-015': '#ffffff',
|
'color-012': '#0000ff', 'color-013': '#ff00ff', 'color-014': '#00ffff', 'color-015': '#ffffff',
|
||||||
|
|
@ -166,7 +161,14 @@ COLOR_INDICE_TO_HEX = {'color-000': '#000000', 'color-001': '#800000', 'color-00
|
||||||
'bgcolor-246': '#949494', 'bgcolor-247': '#9e9e9e', 'bgcolor-248': '#a8a8a8',
|
'bgcolor-246': '#949494', 'bgcolor-247': '#9e9e9e', 'bgcolor-248': '#a8a8a8',
|
||||||
'bgcolor-249': '#b2b2b2', 'bgcolor-250': '#bcbcbc', 'bgcolor-251': '#c6c6c6',
|
'bgcolor-249': '#b2b2b2', 'bgcolor-250': '#bcbcbc', 'bgcolor-251': '#c6c6c6',
|
||||||
'bgcolor-252': '#d0d0d0', 'bgcolor-253': '#dadada', 'bgcolor-254': '#e4e4e4',
|
'bgcolor-252': '#d0d0d0', 'bgcolor-253': '#dadada', 'bgcolor-254': '#e4e4e4',
|
||||||
'bgcolor-255': '#eeeeee'}
|
'bgcolor-255': '#eeeeee'
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
"""
|
||||||
|
The classes below exist to properly encapsulate text and other tag classes
|
||||||
|
because the order of how tags are opened and closed are important to display in godot.
|
||||||
|
"""
|
||||||
|
|
||||||
|
|
||||||
class RootTag(object):
|
class RootTag(object):
|
||||||
|
|
@ -256,7 +258,8 @@ class UrlTag(BBCodeTag):
|
||||||
|
|
||||||
class TextToBBCODEparser(TextToHTMLparser):
|
class TextToBBCODEparser(TextToHTMLparser):
|
||||||
"""
|
"""
|
||||||
This class describes a parser for converting from ANSI to html.
|
This class describes a parser for converting from ANSI to BBCode.
|
||||||
|
It inherits from the TextToHTMLParser and overrides the specifics for bbcode.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
def convert_urls(self, text):
|
def convert_urls(self, text):
|
||||||
|
|
@ -266,7 +269,7 @@ class TextToBBCODEparser(TextToHTMLparser):
|
||||||
def sub_mxp_links(self, match):
|
def sub_mxp_links(self, match):
|
||||||
"""
|
"""
|
||||||
Helper method to be passed to re.sub,
|
Helper method to be passed to re.sub,
|
||||||
replaces MXP links with HTML code.
|
replaces MXP links with bbcode.
|
||||||
|
|
||||||
Args:
|
Args:
|
||||||
match (re.Matchobject): Match for substitution.
|
match (re.Matchobject): Match for substitution.
|
||||||
|
|
@ -283,16 +286,16 @@ class TextToBBCODEparser(TextToHTMLparser):
|
||||||
def sub_mxp_urls(self, match):
|
def sub_mxp_urls(self, match):
|
||||||
"""
|
"""
|
||||||
Helper method to be passed to re.sub,
|
Helper method to be passed to re.sub,
|
||||||
replaces MXP links with HTML code.
|
replaces MXP links with bbcode.
|
||||||
Args:
|
Args:
|
||||||
match (re.Matchobject): Match for substitution.
|
match (re.Matchobject): Match for substitution.
|
||||||
Returns:
|
Returns:
|
||||||
text (str): Processed text.
|
text (str): Processed text.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
url, text = [grp.replace('"', "\\"") for grp in match.groups()]
|
url, text = [grp.replace('"', "\\"") for grp in match.groups()]
|
||||||
val = r"""<a id="mxplink" href="{url}" target="_blank">{text}</a>""".format(
|
val = f"[url={url}]{text}[/url]"
|
||||||
url=url, text=text
|
|
||||||
)
|
|
||||||
return val
|
return val
|
||||||
|
|
||||||
def sub_text(self, match):
|
def sub_text(self, match):
|
||||||
|
|
@ -519,8 +522,6 @@ class TextToBBCODEparser(TextToHTMLparser):
|
||||||
result = self.format_styles(result)
|
result = self.format_styles(result)
|
||||||
result = self.remove_backspaces(result)
|
result = self.remove_backspaces(result)
|
||||||
result = self.convert_urls(result)
|
result = self.convert_urls(result)
|
||||||
# clean out eventual ansi that was missed
|
|
||||||
## result = parse_ansi(result, strip_ansi=True)
|
|
||||||
|
|
||||||
return result
|
return result
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1,3 +1,8 @@
|
||||||
|
"""
|
||||||
|
Godot Websocket - ChrisLR 2022
|
||||||
|
|
||||||
|
This file contains the code necessary to dedicate a port to communicate with Godot via Websockets.
|
||||||
|
"""
|
||||||
import json
|
import json
|
||||||
|
|
||||||
from autobahn.twisted import WebSocketServerFactory
|
from autobahn.twisted import WebSocketServerFactory
|
||||||
|
|
@ -11,6 +16,11 @@ from evennia.settings_default import LOCKDOWN_MODE
|
||||||
|
|
||||||
|
|
||||||
class GodotWebSocketClient(webclient.WebSocketClient):
|
class GodotWebSocketClient(webclient.WebSocketClient):
|
||||||
|
"""
|
||||||
|
Implements the server-side of the Websocket connection specific to Godot.
|
||||||
|
It inherits from the basic Websocket implementation and changes only what is necessary.
|
||||||
|
|
||||||
|
"""
|
||||||
def __init__(self, *args, **kwargs):
|
def __init__(self, *args, **kwargs):
|
||||||
super().__init__(*args, **kwargs)
|
super().__init__(*args, **kwargs)
|
||||||
self.protocol_key = "godotclient/websocket"
|
self.protocol_key = "godotclient/websocket"
|
||||||
|
|
@ -18,17 +28,15 @@ class GodotWebSocketClient(webclient.WebSocketClient):
|
||||||
def send_text(self, *args, **kwargs):
|
def send_text(self, *args, **kwargs):
|
||||||
"""
|
"""
|
||||||
Send text data. This will pre-process the text for
|
Send text data. This will pre-process the text for
|
||||||
color-replacement, conversion to html etc.
|
color-replacement, conversion to bbcode etc.
|
||||||
|
|
||||||
Args:
|
Args:
|
||||||
text (str): Text to send.
|
text (str): Text to send.
|
||||||
|
|
||||||
Keyword Args:
|
Keyword Args:
|
||||||
options (dict): Options-dict with the following keys understood:
|
options (dict): Options-dict with the following keys understood:
|
||||||
- raw (bool): No parsing at all (leave ansi-to-html markers unparsed).
|
|
||||||
- nocolor (bool): Clean out all color.
|
- nocolor (bool): Clean out all color.
|
||||||
- screenreader (bool): Use Screenreader mode.
|
- send_prompt (bool): Send a prompt with parsed bbcode
|
||||||
- send_prompt (bool): Send a prompt with parsed html
|
|
||||||
|
|
||||||
"""
|
"""
|
||||||
if args:
|
if args:
|
||||||
|
|
@ -42,23 +50,10 @@ class GodotWebSocketClient(webclient.WebSocketClient):
|
||||||
flags = self.protocol_flags
|
flags = self.protocol_flags
|
||||||
|
|
||||||
options = kwargs.pop("options", {})
|
options = kwargs.pop("options", {})
|
||||||
raw = options.get("raw", flags.get("RAW", False))
|
|
||||||
client_raw = options.get("client_raw", False)
|
|
||||||
nocolor = options.get("nocolor", flags.get("NOCOLOR", False))
|
nocolor = options.get("nocolor", flags.get("NOCOLOR", False))
|
||||||
screenreader = options.get("screenreader", flags.get("SCREENREADER", False))
|
|
||||||
prompt = options.get("send_prompt", False)
|
prompt = options.get("send_prompt", False)
|
||||||
|
|
||||||
if screenreader:
|
|
||||||
# screenreader mode cleans up output
|
|
||||||
text = webclient.parse_ansi(text, strip_ansi=True, xterm256=False, mxp=False)
|
|
||||||
text = webclient._RE_SCREENREADER_REGEX.sub("", text)
|
|
||||||
cmd = "prompt" if prompt else "text"
|
cmd = "prompt" if prompt else "text"
|
||||||
if raw:
|
|
||||||
if client_raw:
|
|
||||||
args[0] = text
|
|
||||||
else:
|
|
||||||
args[0] = webclient.html.escape(text) # escape html!
|
|
||||||
else:
|
|
||||||
args[0] = parse_to_bbcode(text, strip_ansi=nocolor)
|
args[0] = parse_to_bbcode(text, strip_ansi=nocolor)
|
||||||
|
|
||||||
# send to client on required form [cmdname, args, kwargs]
|
# send to client on required form [cmdname, args, kwargs]
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue