Skip to content

Commit

Permalink
all things signatures and notes
Browse files Browse the repository at this point in the history
  • Loading branch information
Carreau committed Nov 22, 2023
1 parent 3cf030d commit c192292
Show file tree
Hide file tree
Showing 9 changed files with 255 additions and 175 deletions.
95 changes: 61 additions & 34 deletions frontend/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,18 @@ import { MyST, DEFAULT_RENDERERS } from "myst-to-react";
import { fromMarkdown } from "mdast-util-from-markdown";

const Param = ({ node }) => {
return <>
<dt>
{node.param}: {node.type_}
</dt>
<dd>
{node.desc.map((sub) => (
<MyST ast={sub} />
))}
</dd>
</>;
return (
<>
<dt>
{node.param}: {node.type_}
</dt>
<dd>
{node.desc.map((sub) => (
<MyST ast={sub} />
))}
</dd>
</>
);
};
const Parameters = ({ node }) => {
return (
Expand Down Expand Up @@ -47,27 +49,51 @@ const DefList = ({ node }) => {
);
};

const ParameterNodeRenderer = ({ node }) => {
let acc = "";
if (node.kind === "VAR_POSITIONAL") {
acc += "*";
}
acc = acc + node.name;
if (node.default.type !== "Empty") {
acc += '='+node.default.data;
}
return acc;
};

const SignatureRenderer = ({ node }) => {
return (
<>
<div className="flex my-5 group">
<div className="flex-grow overflow-x-auto overflow-y-hidden" />
{node.value}
</div>
<div>
<MyST ast={node.children} />
</div>
</>
<div className="flex my-5 group">
ONAME (
<>
{node.parameters.map((parameter, index, array) => {
if (index + 1 == array.length) {
return <MyST ast={parameter} />;
} else {
return (
<>
<MyST ast={parameter} />
{", "}
</>
);
}
})}
</>
)
</div>
);
};

const Directive = ({ node }) => {
const dom = node.domain !== null ? ":"+node.domain : ""
const role = node.role !== null ? ":"+node.role+":" : ""
const dom = node.domain !== null ? ":" + node.domain : "";
const role = node.role !== null ? ":" + node.role + ":" : "";
return (
<>
<code className="not-implemented">
<span>{dom}{role}`{node.value}`</span>
<span>
{dom}
{role}`{node.value}`
</span>
</code>
</>
);
Expand All @@ -78,6 +104,7 @@ const LOC = {
Directive: Directive,
DefList: DefList,
Parameters: Parameters,
ParameterNode: ParameterNodeRenderer,
Param: Param,
};
const RENDERERS = { ...DEFAULT_RENDERERS, ...LOC };
Expand All @@ -87,18 +114,18 @@ function MyComponent({ node }) {
return <MyST ast={node.children} />;
}

const tree = fromMarkdown("Some *emphasis*, **strong**, and `code`.");
const mytree = {
type: "admonition",
children: [
{ type: "text", value: "myValue" },
{
type: "signature",
value: "Foo",
children: [{ type: "text", value: "Child" }],
},
],
};
//const tree = fromMarkdown("Some *emphasis*, **strong**, and `code`.");
//const mytree = {
// type: "admonition",
// children: [
// { type: "text", value: "myValue" },
// {
// type: "signature",
// value: "Foo",
// children: [{ type: "text", value: "Child" }],
// },
// ],
//};

console.log("Loading X");

Expand Down
3 changes: 2 additions & 1 deletion papyri/crosslink.py
Original file line number Diff line number Diff line change
Expand Up @@ -246,7 +246,8 @@ def _ingest_narrative(self, path, gstore: GraphStore) -> None:
version=None,
)
except Exception as e:
raise type(e)(f"at path: {document}")
e.add_note("at path: {document}")
raise
ref = document.name

module, version = path.name.split("_")
Expand Down
189 changes: 104 additions & 85 deletions papyri/miniserde.py
Original file line number Diff line number Diff line change
Expand Up @@ -153,91 +153,110 @@ def serialize(instance, annotation):

# type_ and annotation are _likely_ duplicate here as an annotation is likely a type, or a List, Union, ....)
def deserialize(type_, annotation, data):
# assert type_ is annotation
# assert annotation != {}
# assert annotation is not dict
# assert annotation is not None, "None is handled by nullable types"
if annotation is str:
# assert isinstance(data, str)
return data
if annotation is int:
# assert isinstance(data, int)
return data
if annotation is bool:
# assert isinstance(data, bool)
return data
orig = getattr(annotation, "__origin__", None)
if data is None:
return None
if orig:
if orig is tuple:
# assert isinstance(data, list)
inner_annotation = annotation.__args__
# assert len(inner_annotation) == 1, inner_annotation
return tuple(
deserialize(inner_annotation[0], inner_annotation[0], x) for x in data
)
elif orig is list:
# assert isinstance(data, list)
inner_annotation = annotation.__args__
# assert len(inner_annotation) == 1, inner_annotation
return [
deserialize(inner_annotation[0], inner_annotation[0], x) for x in data
]
elif orig is dict:
# assert isinstance(data, dict)
_, value_annotation = annotation.__args__
return {
k: deserialize(value_annotation, value_annotation, x)
for k, x in data.items()
}
elif orig is Union:
inner_annotation = annotation.__args__
if len(inner_annotation) == 2 and inner_annotation[1] == type(None):
# assert inner_annotation[0] is not None
if data is None:
return None
try:
# assert type_ is annotation
# assert annotation != {}
# assert annotation is not dict
# assert annotation is not None, "None is handled by nullable types"
if annotation is str:
# assert isinstance(data, str)
return data
if annotation is int:
# assert isinstance(data, int)
return data
if annotation is bool:
# assert isinstance(data, bool)
return data
orig = getattr(annotation, "__origin__", None)
if data is None:
return None
if orig:
if orig is tuple:
# assert isinstance(data, list)
inner_annotation = annotation.__args__
# assert len(inner_annotation) == 1, inner_annotation
return tuple(
deserialize(inner_annotation[0], inner_annotation[0], x)
for x in data
)
elif orig is list:
# assert isinstance(data, list)
inner_annotation = annotation.__args__
# assert len(inner_annotation) == 1, inner_annotation
return [
deserialize(inner_annotation[0], inner_annotation[0], x)
for x in data
]
elif orig is dict:
# assert isinstance(data, dict)
_, value_annotation = annotation.__args__
return {
k: deserialize(value_annotation, value_annotation, x)
for k, x in data.items()
}
elif orig is Union:
inner_annotation = annotation.__args__
if len(inner_annotation) == 2 and inner_annotation[1] == type(None):
# assert inner_annotation[0] is not None
if data is None:
return None
else:
return deserialize(
inner_annotation[0], inner_annotation[0], data
)
real_type = [t for t in inner_annotation if t.__name__ == data["type"]]
if len(real_type) == 0:
myst_type = f"M{data['type'][0].upper()}{data['type'][1:]}"
if data["type"] == "mystComment":
myst_type = "MComment"
elif data["type"] == "mystTarget":
myst_type = "MTarget"
elif data["type"] == "Transition":
myst_type = "thematicBreak"
real_type = [
t
for t in inner_annotation
if (t.__name__ == myst_type)
or (getattr(t, "type", None) == myst_type)
]
# assert len(real_type) == 1, real_type
try:
assert len(real_type) == 1, real_type
real_type = real_type[0]
except (IndexError, AssertionError) as e:
e.add_note(
f"""Index error as filters annotations are wrong myst_type={myst_type}, data[type]={data['type']},
accepted:{inner_annotation} `t.type`?= {[getattr(t,"type", None) for t in inner_annotation]}"""
)
raise
if data.get("data", _sentinel) is not _sentinel:
data_ = data["data"]
else:
return deserialize(inner_annotation[0], inner_annotation[0], data)
real_type = [t for t in inner_annotation if t.__name__ == data["type"]]
if len(real_type) == 0:
myst_type = f"M{data['type'][0].upper()}{data['type'][1:]}"
if data["type"] == "mystComment":
myst_type = "MComment"
elif data["type"] == "mystTarget":
myst_type = "MTarget"
real_type = [t for t in inner_annotation if t.__name__ == myst_type]
# assert len(real_type) == 1, real_type
try:
real_type = real_type[0]
except IndexError:
breakpoint()
raise
if data.get("data", _sentinel) is not _sentinel:
data_ = data["data"]
data_ = {k: v for k, v in data.items() if k != "type"}
return deserialize(real_type, real_type, data_)
else:
data_ = {k: v for k, v in data.items() if k != "type"}
return deserialize(real_type, real_type, data_)
else:
assert False
elif (type(annotation) is type) and annotation.__module__ not in (
"builtins",
"typing",
):
loc = {}
new_ann = get_type_hints(annotation).items()
# assert new_ann
for k, v in new_ann:
# assert k in data.keys(), f"{k} not int {data.keys()}"
# if data[k] != 0:
# assert data[k] != {}, f"{data}, {k}"
intermediate = deserialize(v, v, data[k])
# assert intermediate != {}, f"{v}, {data}, {k}"
loc[k] = intermediate
if hasattr(annotation, "_deserialise"):
return annotation._deserialise(**loc)
else:
return annotation(**loc)
assert False
elif (type(annotation) is type) and annotation.__module__ not in (
"builtins",
"typing",
):
loc = {}
new_ann = get_type_hints(annotation).items()
# assert new_ann
for k, v in new_ann:
# assert k in data.keys(), f"{k} not int {data.keys()}"
# if data[k] != 0:
# assert data[k] != {}, f"{data}, {k}"
intermediate = deserialize(v, v, data[k])
# assert intermediate != {}, f"{v}, {data}, {k}"
loc[k] = intermediate
if hasattr(annotation, "_deserialise"):
return annotation._deserialise(**loc)
else:
return annotation(**loc)

else:
assert False, f"{annotation!r}, {data}"
else:
assert False, f"{annotation!r}, {data}"
except Exception as e:
e.add_note(f"Deserializing {type_}, {annotation}")
raise
14 changes: 13 additions & 1 deletion papyri/myst_ast.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,11 @@ class MText(Node):
# position: Any
# data: Any

def __init__(self, value):
assert isinstance(value, str)
self.value = value
super().__init__()


@register(4047)
class MEmphasis(Node):
Expand Down Expand Up @@ -208,7 +213,14 @@ class MHeading(Node):
@register(4001)
class MRoot(Node):
type = "root"
children: List[Union["FlowContent", "take2.Parameters"]]
children: List[
Union[
"FlowContent",
"take2.Parameters",
"take2.Unimplemented",
"take2.SubstitutionDef",
]
]


StaticPhrasingContent = Union[
Expand Down
Loading

0 comments on commit c192292

Please sign in to comment.