-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathReplaceWithTemplate.py
More file actions
37 lines (28 loc) · 1.28 KB
/
ReplaceWithTemplate.py
File metadata and controls
37 lines (28 loc) · 1.28 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
from Npp import notepad, editor
# Wim Gielis
# Oct. 2025
#
# ReplaceWithTemplate script (Alt-Shift-t):
# - The entire file is converted, line by line, following a template
# - Easy to insert text in front and/or at the end
# - Source: https://isc.sans.edu/diary/31240
def Substitute(contents, lineNumber, totalLines=None):
contents = str(contents).rstrip('\n\r')
if contents != '':
editor.replaceLine(lineNumber, template.replace(token, contents))
def forEachSelectedLine(callback):
start_line = editor.lineFromPosition(editor.getSelectionStart())
end_line = editor.lineFromPosition(editor.getSelectionEnd())
for line_num in range(start_line, end_line + 1):
line_text = editor.getLine(line_num)
callback(line_text, line_num)
# # token = notepad.prompt('Provide a token', 'Substitute token', '#' )
# token = '#'
# template = notepad.prompt('Provide a template', 'Substitute template', '#')
# if token != None and template != None:
# editor.forEachLine(Substitute)
# token = notepad.prompt('Provide a token', 'Substitute token', '#' )
token = '#'
template = notepad.prompt('Provide a template', 'Substitute template', '#')
if token != None and template != None:
forEachSelectedLine(Substitute)