clld / clld-markdown-plugin

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

Requirements #1

Closed xrotwang closed 1 year ago

xrotwang commented 2 years ago
xrotwang commented 2 years ago

Here's an initial prototypical function from ACD:

def markdown(req, s):
    def repl(ml):
        comp_to_route = {
            'LanguageTable': 'language',
            'Source': 'source',
        }
        if ml.is_cldf_link:
            try:
                ml.url = req.route_url(comp_to_route[ml.component()], id=ml.objid)
            except:
                print(ml.component(), ml)
        return ml

    md = Markdown(extensions=[
        TocExtension(permalink=True),
        'markdown.extensions.fenced_code',
        'markdown.extensions.tables'])
    return md.convert(CLDFMarkdownLink.replace(s, repl)), md
fmatter commented 2 years ago

How to best accommodate custom models?

fmatter commented 2 years ago

I think I've covered your points 2/3/4, as well as my custom model requirement.

Items are retrieved with DBSession and the objid. To just link entities, a dict is used in combination with a function:

model_map = {
    "LanguageTable": {"route": "language", "model": Language},
    "ExampleTable": {"route": "sentence", "model": Sentence},
    "sources.bib": {"route": "source", "model": Source},
}

def link_entity(req, objid, route, model, decorate=None):
    entity = DBSession.query(model).filter(model.id == objid)[0]
    url = req.route_url(route, id=objid)
    md_str = f"[{entity.name}]({url})"
    if decorate is None:
        return md_str
    else:
        return decorate(md_str)

If a table is in function_map, a custom function is used. If a file clld_own_markdownpy exists, custom_model_map and custom_function_map are imported from there.

I am confident that some aspects are hacky, but what can you do...

fmatter commented 2 years ago

must work with custom ID prefixes to accomodate clld apps serving aggregated CLDF content such as Dictionaria

Question: how do these prefixes exactly work? Are CLLD database IDs just prefix + individual CLDF ID?

xrotwang commented 1 year ago

After a bit more thinking, I'd say it is the responsibility of the clld app to

In any case, it's only the clld app that knows how to do that, so it shouldn't be pushed down to the plugin.