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
body.
"""
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
'\\(chapter|section|subsection|subsubsection|paragraph|subparagraph)(\[.*?\])?({.*})'
,
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]
else:
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=
dirtyVnodeList)
p.v.setDirty()
changed += 1
if changed:
u.afterChangeTree(c.p, undoType, bunch)
g.es('Changed %s node%s.' % (changed, g.plural(changed)))
else:
g.es('No nodes changed')
else:
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.
Edward
--
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 [email protected].
To post to this group, send email to [email protected].
Visit this group at https://groups.google.com/group/leo-editor.
For more options, visit https://groups.google.com/d/optout.