From 59f856c7e6d195ac6ed64b1d6fbd2dc55e0c2292 Mon Sep 17 00:00:00 2001 From: Kegan Dougal Date: Wed, 27 May 2015 16:30:11 +0100 Subject: [PATCH] Factor out ImageInfo into a core type. Refer to that in other msgtypes. Add templating for msgtypes. ImageInfo core type is not referred to for m.image in order for the ImageInfo table to render for it. --- .../examples/v1/m.room.message#m.location | 2 +- event-schemas/schema/v1/core | 25 +++++++++++++++++ event-schemas/schema/v1/m.room.message#m.file | 23 +++------------ .../schema/v1/m.room.message#m.image | 21 ++------------ .../schema/v1/m.room.message#m.location | 5 +++- .../schema/v1/m.room.message#m.video | 5 +++- templating/matrix_templates/sections.py | 17 ++++++++++- .../matrix_templates/templates/msgtypes.tmpl | 23 +++++++++++++++ templating/matrix_templates/units.py | 28 +++++++++++++++++-- 9 files changed, 106 insertions(+), 43 deletions(-) create mode 100644 templating/matrix_templates/templates/msgtypes.tmpl diff --git a/event-schemas/examples/v1/m.room.message#m.location b/event-schemas/examples/v1/m.room.message#m.location index b192b832..54198acb 100644 --- a/event-schemas/examples/v1/m.room.message#m.location +++ b/event-schemas/examples/v1/m.room.message#m.location @@ -17,4 +17,4 @@ "room_id": "!jEsUZKDJdhlrceRyVU:localhost", "type": "m.room.message", "user_id": "@example:localhost" -} \ No newline at end of file +} diff --git a/event-schemas/schema/v1/core b/event-schemas/schema/v1/core index e7a24803..ed374a69 100644 --- a/event-schemas/schema/v1/core +++ b/event-schemas/schema/v1/core @@ -58,6 +58,31 @@ } }, "required": ["state_key"] + }, + "msgtype_infos": { + "image_info": { + "type": "object", + "title": "ImageInfo", + "description": "Metadata about an image.", + "properties": { + "size": { + "type": "integer", + "description": "Size of the image in bytes." + }, + "w": { + "type": "integer", + "description": "The width of the image in pixels." + }, + "h": { + "type": "integer", + "description": "The height of the image in pixels." + }, + "mimetype": { + "type": "string", + "description": "The mimetype of the image, e.g. ``image/jpeg``." + } + } + } } } } diff --git a/event-schemas/schema/v1/m.room.message#m.file b/event-schemas/schema/v1/m.room.message#m.file index c07f17b3..cf6d6552 100644 --- a/event-schemas/schema/v1/m.room.message#m.file +++ b/event-schemas/schema/v1/m.room.message#m.file @@ -2,7 +2,7 @@ "$schema": "http://json-schema.org/draft-04/schema#", "type": "object", "title": "FileMessage", - "description": "TODO.", + "description": "This message represents a generic file.", "allOf": [{ "$ref": "core#/definitions/room_event" }], @@ -49,24 +49,9 @@ "type": "object", "title": "ImageInfo", "description": "Metadata about the image referred to in ``thumbnail_url``.", - "properties": { - "size": { - "type": "integer", - "description": "Size of the image in bytes." - }, - "w": { - "type": "integer", - "description": "The width of the image in pixels." - }, - "h": { - "type": "integer", - "description": "The height of the image in pixels." - }, - "mimetype": { - "type": "string", - "description": "The mimetype of the image, e.g. ``image/jpeg``." - } - } + "allOf": [{ + "$ref": "core#/definitions/msgtype_infos/image_info" + }] } }, "required": ["msgtype", "body", "url", "filename"] diff --git a/event-schemas/schema/v1/m.room.message#m.image b/event-schemas/schema/v1/m.room.message#m.image index 29552452..8c010b0d 100644 --- a/event-schemas/schema/v1/m.room.message#m.image +++ b/event-schemas/schema/v1/m.room.message#m.image @@ -30,24 +30,9 @@ "type": "object", "title": "ImageInfo", "description": "Metadata about the image referred to in ``thumbnail_url``.", - "properties": { - "size": { - "type": "integer", - "description": "Size of the image in bytes." - }, - "w": { - "type": "integer", - "description": "The width of the image in pixels." - }, - "h": { - "type": "integer", - "description": "The height of the image in pixels." - }, - "mimetype": { - "type": "string", - "description": "The mimetype of the image, e.g. ``image/jpeg``." - } - } + "allOf": [{ + "$ref": "core#/definitions/msgtype_infos/image_info" + }] }, "info": { "type": "object", diff --git a/event-schemas/schema/v1/m.room.message#m.location b/event-schemas/schema/v1/m.room.message#m.location index d1b95157..cd15f31e 100644 --- a/event-schemas/schema/v1/m.room.message#m.location +++ b/event-schemas/schema/v1/m.room.message#m.location @@ -28,7 +28,10 @@ }, "thumbnail_info": { "type": "object", - "title": "ImageInfo" + "title": "ImageInfo", + "allOf": [{ + "$ref": "core#/definitions/msgtype_infos/image_info" + }] } }, "required": ["msgtype", "body", "geo_uri"] diff --git a/event-schemas/schema/v1/m.room.message#m.video b/event-schemas/schema/v1/m.room.message#m.video index 960126b8..22266506 100644 --- a/event-schemas/schema/v1/m.room.message#m.video +++ b/event-schemas/schema/v1/m.room.message#m.video @@ -53,7 +53,10 @@ }, "thumbnail_info": { "type": "object", - "title": "ImageInfo" + "title": "ImageInfo", + "allOf": [{ + "$ref": "core#/definitions/msgtype_infos/image_info" + }] } } } diff --git a/templating/matrix_templates/sections.py b/templating/matrix_templates/sections.py index e42bdf7b..5e210371 100644 --- a/templating/matrix_templates/sections.py +++ b/templating/matrix_templates/sections.py @@ -20,7 +20,22 @@ class MatrixSections(Sections): schemas = self.units.get("event_schemas") sections = [] for event_name in sorted(schemas): - if not event_name.startswith("m.room"): + if (not event_name.startswith("m.room") or + event_name.startswith("m.room.message#m.")): + continue + sections.append(template.render( + example=examples[event_name], + event=schemas[event_name] + )) + return "\n\n".join(sections) + + def render_msgtype_events(self): + template = self.env.get_template("msgtypes.tmpl") + examples = self.units.get("event_examples") + schemas = self.units.get("event_schemas") + sections = [] + for event_name in sorted(schemas): + if not event_name.startswith("m.room.message#m."): continue sections.append(template.render( example=examples[event_name], diff --git a/templating/matrix_templates/templates/msgtypes.tmpl b/templating/matrix_templates/templates/msgtypes.tmpl new file mode 100644 index 00000000..633615a7 --- /dev/null +++ b/templating/matrix_templates/templates/msgtypes.tmpl @@ -0,0 +1,23 @@ +``{{event.msgtype}}`` +{{(4 + event.msgtype | length) * '~'}} +{{event.desc | wrap(80)}} +{% for table in event.content_fields -%} +{{"``"+table.title+"``" if table.title else "" }} + +================== ================= =========================================== + {{table.title or "Content"}} Key Type Description +================== ================= =========================================== +{% for row in table.rows -%} +{# -#} +{# Row type needs to prepend spaces to line up with the type column (19 ch) -#} +{# Desc needs to prepend the required text (maybe) and prepend spaces too -#} +{# It also needs to then wrap inside the desc col (43 ch width) -#} +{# -#} +{{row.key}}{{row.type|indent(19-row.key|length)}}{{row.desc|wrap(43,row.req_str | indent(18 - (row.type|length))) |indent_block(37)}} +{% endfor -%} +================== ================= =========================================== + +{% endfor %} +Example:: + + {{example | jsonify(4, 4)}} diff --git a/templating/matrix_templates/units.py b/templating/matrix_templates/units.py index 28fc3f9b..109223de 100644 --- a/templating/matrix_templates/units.py +++ b/templating/matrix_templates/units.py @@ -14,6 +14,8 @@ class MatrixUnits(Units): with open(path, "r") as f: core_json = json.loads(f.read()) for event_type in core_json["definitions"]: + if "event" not in event_type: + continue # filter ImageInfo and co event_info = core_json["definitions"][event_type] table = { "title": event_info["title"], @@ -68,7 +70,19 @@ class MatrixUnits(Units): } tables = [fields] - props = obj["properties"] + props = obj.get("properties") + parents = obj.get("allOf") + if not props and not parents: + raise Exception( + "Object %s has no properties or parents." % obj + ) + if not props: # parents only + return [{ + "title": obj["title"], + "parent": parents[0]["$ref"], + "no-table": True + }] + for key_name in sorted(props): value_type = None required = key_name in required_keys @@ -87,7 +101,9 @@ class MatrixUnits(Units): enforce_title=True ) value_type = "{%s}" % nested_object[0]["title"] - tables += nested_object + + if not nested_object[0].get("no-table"): + tables += nested_object elif props[key_name]["type"] == "array": # if the items of the array are objects then recurse if props[key_name]["items"]["type"] == "object": @@ -133,6 +149,7 @@ class MatrixUnits(Units): "type": None, "title": None, "desc": None, + "msgtype": None, "content_fields": [ # { # title: " key" @@ -168,6 +185,13 @@ class MatrixUnits(Units): Units.prop(json_schema, "properties/content") ) + # grab msgtype if it is the right kind of event + msgtype = Units.prop( + json_schema, "properties/content/properties/msgtype/enum" + ) + if msgtype: + schema["msgtype"] = msgtype[0] # enum prop + # Assign state key info if schema["typeof"] == "State Event": skey_desc = Units.prop(