From d6fc41fe05326706ea685abd7a586fe873839449 Mon Sep 17 00:00:00 2001 From: Patrick Nisble Date: Mon, 12 Jan 2026 08:37:31 +0100 Subject: [PATCH] extend possible in-/output --- rplugin/python3/mrpy/yaml.py | 51 +++++++++++++++++++++++++++++++----- 1 file changed, 45 insertions(+), 6 deletions(-) diff --git a/rplugin/python3/mrpy/yaml.py b/rplugin/python3/mrpy/yaml.py index 3853c30..ba60c97 100644 --- a/rplugin/python3/mrpy/yaml.py +++ b/rplugin/python3/mrpy/yaml.py @@ -17,7 +17,7 @@ def dump_scalar(entry: hints.JSONDataScalar) -> str: return f"{entry}\n" -def dump(obj: dict[str, hints.JSONDataScalar | list[hints.JSONDataScalar]]) -> str: +def dump(obj: hints.JSONDataMap) -> str: ret = "" for key, value in obj.items(): ret += key @@ -26,11 +26,32 @@ def dump(obj: dict[str, hints.JSONDataScalar | list[hints.JSONDataScalar]]) -> s match value: case []: ret += " []\n" + case {}: + ret += " {}\n" + case "": + ret += ' ""\n' case list() as entries: ret += "\n" for entry in entries: - ret += f" - {dump_scalar(entry)}" + match entry: + case dict(): + subdump = dump(entry) + ret += "\n".join(" " + l for l in subdump.splitlines()) + case list(): + pass + case _: + ret += f" - {dump_scalar(entry)}" + case dict() as substruct: + ret += "\n" + subdump = dump(substruct) + ret += "\n".join(" " + l for l in subdump.splitlines()) + "\n" + case str() as mlstring if key == "markdown_content": + if mlstring.strip() == "" or dump_scalar(mlstring).strip() == "null": + ret += ' ""\n' + else: + indented = "\n".join((" " + l) for l in dump_scalar(mlstring).splitlines()) + ret += f" >\n{indented}\n" case entry: ret += f" {dump_scalar(entry)}" @@ -56,6 +77,10 @@ def load(content: str) -> hints.JSONData: ... key10: ... - str ... - "str" + ... key11: > + ... * a + ... * b: + ... * c ... ''' >>> yaml_parsed = load(yaml) >>> compare = { @@ -69,8 +94,8 @@ def load(content: str) -> hints.JSONData: ... 'key8': False, ... 'key9': [1, 23.2], ... 'key10': ['str', 'str'], + ... 'key11': "* a\\n* b:\\n * c", ... } - >>> {k: yaml_parsed.get(k, None) for k in compare if compare[k] != yaml_parsed[k]} {} """ @@ -78,20 +103,34 @@ def load(content: str) -> hints.JSONData: ret = {} key = None value = None - for line in sub(r":[\s\n]*", ":\n ", dedent(content).strip()).splitlines(): - if line.endswith(":"): + mlstring = False + for line in sub(r"(:( >)?)[\s\n]*", r"\1\n ", dedent(content).strip()).splitlines(): + if line.startswith("#") or line.strip() == "---": + continue + elif line.endswith(":"): if key: ret[key] = value value = None + mlstring = False key = line.removesuffix(":") - elif line.startswith(" -"): + elif line.endswith(": >"): + if key: + ret[key] = value + value = None + mlstring = True + + key = line.removesuffix(": >") + elif line.startswith(" -") and not mlstring: value = value or [] try: parsed = loads(line.removeprefix(" -").strip()) except: parsed = loads('"' + line.removeprefix(" -").strip() + '"') value.append(parsed) + elif line.startswith(" ") and mlstring: + value = value or "" + value += line.removeprefix(" ") + "\n" else: try: value = loads(line.strip())