forked from Ekopalypse/NppPythonScripts
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathReformatChordsAndLyrics.py
More file actions
45 lines (35 loc) · 1.49 KB
/
Copy pathReformatChordsAndLyrics.py
File metadata and controls
45 lines (35 loc) · 1.49 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
from Npp import editor
import re
# it is assumed that chords and text is correctly aligned
# and that the ordering is always one line of chords followed by one line of lyric
# reformats from
# A B C
# Some text to sing
# to
# [A]Some [B] text to [C] sing
in_text_line = False
new_content = ''
chords = []
for line in editor.getText().splitlines():
if line.strip(): # skip empty lines
if in_text_line:
if len(chords) == 1:
new_content += '[{0}]{1}\r\n'.format(chords[0][1], line)
else:
for i, chord in enumerate(chords[:-1]):
_line = line[chords[i][0][0]:chords[i+1][0][0]]
new_content += '[{0}]{1} '.format(chords[i][1], _line).replace('[]','')
# last element in list
_line = line[chords[-1][0][0]:]
new_content += '[{0}]{1}\r\n'.format(chords[-1][1], _line).replace('[]','')
# next line must be a chord line
chords = [] # .clear()
in_text_line = False
else: # in chord lines
chords = [(m.span(), m.group()) for m in re.finditer('[^ ]+', line)]
# add a dummy entry if the chord does not start at the beginning of the line
if chords[0][0][0] > 0:
chords.insert(0, ((0,0), ''))
# next line must be a text line
in_text_line = True
editor.setText(new_content)