Can a plugin add new directives?

29 views
Skip to first unread message

Aaron Digulla

unread,
Aug 8, 2016, 9:58:38 AM8/8/16
to leo-editor
I'd like to add multi-language support to my Leo documents. For that, I thought I'd use "@locale ??" but now the question is: Can a plugin add directives?

That said, I'm wondering how directives work. Is there a way to get a list of directives from a node? Or is there a parser which detects them for me?

Regards,

A. Digulla

Edward K. Ream

unread,
Aug 8, 2016, 10:41:40 AM8/8/16
to leo-editor


On Sun, Aug 7, 2016 at 3:50 PM, Aaron Digulla <adig...@gmail.com> wrote:
>
> I'd like to add multi-language support to my Leo documents. For that, I thought I'd use "@locale ??" but now the question is: Can a plugin add directives?


Plugins can do anything Leo can do, and in general Leo can do anything ;-)


> Is there a way to get a list of directives from a node? Or is there a parser which detects them for me?


Both, and more:

1. g.globalDirectiveList contains a list of all known directives.

leoGlobals.py--><< define g.
​​
globalDirectiveList >> inits this list.  Any code, including plugin code can add items to the list.  Not a good idea to remove items.

2. @file leoGlobals.py-->g.Directives contains lots of global functions that allow quick scanning of directives.  Typically, code starts with:

aList = g.get_directives_dict_list(p)

and then filters the list.  For example:

def scanAtWrapDirectives(aList, issue_error_flag=False
):
    '''Scan aList for @wrap and @nowrap directives.'''
    for d in aList:
        if d.get('wrap') is not None:
            return True
        elif d.get('nowrap') is not None:
            return False
    return None

def scanAllAtWrapDirectives(c, p):
    '''Scan p and all ancestors looking for
        @wrap/@nowrap directives.
    '''
    if c and p:
        default = c and c.config.getBool("body_pane_wraps")
        aList = g.get_directives_dict_list(p)
        val = g.scanAtWrapDirectives(aList)
        ret = default if val is None else val
    else:
        ret = None
    return ret


In other words, g.get_directives_dict_list does the parsing, returning a list of dicts describing the directives (in order, from p through all the parents of p).  This ordered list is the way Leo handles the "inheritance" of directives.

g.get_directives_dict_list
 uses g.​globalDirectiveList (via g.compute_directives_re). If you update g.​globalDirectiveList,  the g.get_directives_dict_list method will return dicts for your new directive.

Yes, this is a bit complicated, but the complications ensure fast parsing, which is important.  Take a look at these methods, and feel free to ask more questions.

Edward
Reply all
Reply to author
Forward
0 new messages