Team Ai
Datasetpublic

codekingpro/portable-devtools

sourceHugging Faceupdated 5mo agoView on Hugging Face
1likes15kdownloads
_view_javascript.py69 linesDownload Raw Back to contentviews
1import io2import re3 4from mitmproxy.contentviews._api import Contentview5from mitmproxy.contentviews._api import Metadata6from mitmproxy.utils import strutils7 8DELIMITERS = "{};\n"9SPECIAL_AREAS = (10    r"(?<=[^\w\s)])\s*/(?:[^\n/]|(?<!\\)(?:\\\\)*\\/)+?/(?=[gimsuy]{0,6}\s*(?:[;,).\n]|$))",11    r"'" + strutils.MULTILINE_CONTENT_LINE_CONTINUATION + strutils.NO_ESCAPE + "'",12    r'"' + strutils.MULTILINE_CONTENT_LINE_CONTINUATION + strutils.NO_ESCAPE + '"',13    r"`" + strutils.MULTILINE_CONTENT + strutils.NO_ESCAPE + "`",14    r"/\*" + strutils.MULTILINE_CONTENT + r"\*/",15    r"//" + strutils.SINGLELINE_CONTENT + "$",16    r"for\(" + strutils.SINGLELINE_CONTENT + r"\)",17)18 19 20def beautify(data):21    data = strutils.escape_special_areas(data, SPECIAL_AREAS, DELIMITERS)22 23    data = re.sub(r"\s*{\s*(?!};)", " {\n", data)24    data = re.sub(r"\s*;\s*", ";\n", data)25    data = re.sub(r"(?<!{)\s*}(;)?\s*", r"\n}\1\n", data)26 27    beautified = io.StringIO()28    indent_level = 029 30    for line in data.splitlines(True):31        if line.endswith("{\n"):32            beautified.write(" " * 2 * indent_level + line)33            indent_level += 134        elif line.startswith("}"):35            indent_level -= 136            beautified.write(" " * 2 * indent_level + line)37        else:38            beautified.write(" " * 2 * indent_level + line)39 40    data = strutils.unescape_special_areas(beautified.getvalue())41    return data42 43 44class JavaScriptContentview(Contentview):45    syntax_highlight = "javascript"46    __content_types = (47        "application/x-javascript",48        "application/javascript",49        "text/javascript",50    )51 52    def prettify(53        self,54        data: bytes,55        metadata: Metadata,56    ) -> str:57        data_str = data.decode("utf-8", "replace")58        return beautify(data_str)59 60    def render_priority(61        self,62        data: bytes,63        metadata: Metadata,64    ) -> float:65        return float(bool(data) and metadata.content_type in self.__content_types)66 67 68javascript = JavaScriptContentview()69 
codekingpro/portable-devtools · Team Ai