clld / clld-markdown-plugin

Render CLDF markdown in clld apps
Apache License 2.0
0 stars 0 forks source link

Renderer functions #3

Closed xrotwang closed 1 year ago

xrotwang commented 1 year ago

How are renderer functions supposed to work? While render_ex returns an HTML string or a list of HTML strings, render_cogset seems to return an f-string containing Mako markup. https://github.com/clld/clld-markdown-plugin/blob/dbf9f6584a065b10f46e9790d4ed908042466408/src/clld_markdown_plugin/__init__.py#L68-L81

Do you intend to pass this on to some sort of adapter, based on https://github.com/clld/clld/blob/c56f69dd4108d3db9ec933694638c86cdc33db1e/src/clld/web/adapters/base.py#L59-L62 ?

fmatter commented 1 year ago

Huh, I hadn't thought about that. Since link_entity returns a string with HTML code, I guess render functions should do the same.

The reason neither of them do is that 1) for examples, there was already the nicely working rendered_sentence that I just used, 2) I implemented cogsets in a hurry and I think something was not working with "plain" HTML (?). I am not committed to either implementation and I think all functions returning entities to be inserted into the text should do so with strings.

What do you think?

xrotwang commented 1 year ago

OK, I think once we have integrated cogsets in the test suite, this will be easier to polish. I just couldn't imagine the mako markup returned right now to work as expected.

fmatter commented 1 year ago

Ah, it probably doesn't! I forgot. I implemented them in a custom function in a CLLD app, and I did end up generating an HTML string to be integrated into markdown:

def render_cogset(req, objid, table, ids=None, inline=False):
    ctx = DBSession.query(Cognateset).get(objid)
    ctx = DBSession.query(Cognateset).filter(Cognateset.id == objid).first()
    if inline:
        return link(req, ctx)
    reflexes = sorted(
        list(ctx.reflexes), key=lambda x: order[x.counterpart.language.id]
    )
    rows = []
    for cog in reflexes:
        rows.append(
            f"""<tr><td>{link(req, cog.counterpart)}</td>
    <td>{link(req, cog.counterpart.language)}</td>
    <td>
        <span class="alignment">{cog.alignment}</span>
    </td></tr>"""
        )
    body = "\n".join(rows)
    return f"""<table class="alignment-table"><thead>
            <tr>
        <th>Form</th>
        <th>Language</th>
        <th>Alignment</th>
    </tr>
        </thead><tbody>{body}</tbody></table>"""

Together with the alignment.* files in https://github.com/tupian-language-resources/tular/tree/8f1b79ea56ddf2972035edb4c4e27a50884bf0ec/tular/static, I was able to get nicely colored and aligned cogset tables in running text.

I think I left the render_cogset function as a placeholder (note the custom order dict used for sorting languages in the function above).

fmatter commented 1 year ago

So: renderer functions return text which possibly contains HTML.