On Thursday, March 8, 2018 at 10:48:28 AM UTC-6, Arjan wrote:

Reviving this old topic. I made a crude attempt, hard-coded etc., to just 
> rewrite the section definitions via regex search and replace...
> How would I add Undo to this? Any other obvious improvements to make?

Here is a version with undo:

@language python

Changes LaTeX section definition levels in the subtree of an @clean file 
node to their subtree level.
Only one LaTeX section level can therefore be used within a single node 

import re

section_levels = {
    1: 'chapter',
    2: 'section',
    3: 'subsection',
    4: 'subsubsection',
    5: 'paragraph',
    6: 'subparagraph'

def latex_convert_section_levels(p, adjusted_level_name):
    """ Replaces LaTeX section definition levels found on a single line (re 
multiline mode).
    Returns the modified node body."""
    return re.sub(r
        r'\\'+adjusted_level_name+r'\g<2>\g<3>', p.b, re.M)

u, undoType = c.undoer, 'change-latex'
h = p.h.strip()
if g.match_word(h, 0, '@clean') and h.endswith('.tex'):
    bunch = u.beforeChangeTree(c.p)
    changed, dirtyVnodeList = 0, []
    root_level = p.level()
    for p in p.subtree():
        level = p.level() - root_level
        if level < 7:
            level_name = section_levels[level]
            level_name = 'subparagraph'
        s = latex_convert_section_levels(p, level_name)
        if s != p.b:
            bunch2 = u.beforeChangeNodeContents(p)
            p.b = s
            u.afterChangeNodeContents(p, undoType, bunch2, dirtyVnodeList=
            changed += 1
    if changed:
        u.afterChangeTree(c.p, undoType, bunch)
        g.es('Changed %s node%s.' % (changed, g.plural(changed)))
        g.es('No nodes changed')
    g.es('not a LaTeX file node?')

The only important change besides the addition of undo code was to test for 
.tex files with the following instead of regex.

 if g.match_word(h, 0, '@clean') and h.endswith('.tex'):

The regex will fail in some cases, especially on Windows:

And that's all.  It's a good @button node.


You received this message because you are subscribed to the Google Groups 
"leo-editor" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to leo-editor+unsubscr...@googlegroups.com.
To post to this group, send email to leo-editor@googlegroups.com.
Visit this group at https://groups.google.com/group/leo-editor.
For more options, visit https://groups.google.com/d/optout.

Reply via email to