Script for automatic insertion of modified rows
-
I have an xml document that I need to edit. Using bookmarks I can extract certain lines marked <Text> and </Text>.
I’ll edit those lines in the adjacent document, but then I’d need to put them back in the same place, but somehow in bulk so I don’t have to do it manually line by line.Couldn’t this be solved with some script?Original document:
Edited text that needs to be returned:
Thank you for any help with my problem…
-
Theoretically yes, like this
- bookmark the lines that need to be edited ->the bookmarks should contain only one line
- Copy the bookmarked lines to another document and move them to the other view of Npp
- Edit the newly created data as you wish, but NO NEW lines should be inserted.
- Then run a script that replaces each line in the edited view with the corresponding line in the other view.
If this is NOT how you want it to work, then you need to explain your exact procedure so it can be understood if this is easily possible.
By the way, the PythonScript plugin would have to be installed if I were to provide a scripting solution.
-
@Ekopalypse
Yes, this looks like just what I need. I’ll bookmark the rows I need to edit. I copy these rows from the new NPP view, there they get edited (won’t change their number), I need to get these edited rows back into the original file. I have the PythonScript plugin installed.
Can you provide me a python script that can do this? -
OK, I’ll put something together, but since I have to prepare lunch now, I’ll post something in 1-2 hours.
-
-
As mentioned earlier, this script assumes that the number of bookmarked lines is identical to the number of lines edited in the other view.
For example, int this case when using copy bookmarked lines
you need to get rid of the 5 line in the second view.
from Npp import notepad, editor1, editor2 class BookmarkLineReplacer: def __init__(self): self.BOOKMARK_MASK = 1 << 20 # 20 = Npps' bookmark id # assuming these are the defaults self.ED1 = editor1 self.ED2 = editor2 def get_bookmarked_lines(self): self.bookmarked_lines = [] line_number = self.ED1.markerNext(0, self.BOOKMARK_MASK) while line_number != -1: self.bookmarked_lines.append(line_number) line_number = self.ED1.markerNext(line_number + 1, self.BOOKMARK_MASK) def replace_lines(self): # sanity check replacement_lines_count = self.ED2.getLineCount() if len(self.bookmarked_lines) != replacement_lines_count: msg = 'bookmarked lines: {} BUT the number of replacements is: {}'.format(len(self.bookmarked_lines), replacement_lines_count) notepad.messageBox(msg, "Number of lines do not match, aborting ...", 0) return replacement_lines = self.ED2.getText().splitlines() for i, line_number in enumerate(self.bookmarked_lines): self.ED1.replaceLine(line_number, replacement_lines[i]) def main(self): if notepad.isSingleView(): notepad.messageBox('This script NEEDS both views', "Aborting...", 0) return # check which view (0=editor1 and 1=editor2) has the bookmarks if editor1.markerNext(0, self.BOOKMARK_MASK) == -1: if editor2.markerNext(0, self.BOOKMARK_MASK) == -1: notepad.messageBox('No bookmarks found', "Aborting...", 0) return else: self.ED1 = editor2 self.ED2 = editor1 self.get_bookmarked_lines() self.replace_lines() BookmarkLineReplacer().main()
-
Tested. Yes, it works exactly as I needed. Thank you for your help. Nice work.
-
Hello, @martin-steiner, @ekopalypse and All,
Of course, the @ekopalypse’s python script seems the best bet ! However, you can just achieve it with the native regex engine of N++ !
I was able to download your
Test_File.zip
archive and extract theCooper_CH1_Call.xml
fileJust execute each point of the method below :
-
Open your
Cooper_CH1_Call.xml
file in N++ -
Open the Mark dialog (
Ctrl + M
) -
Uncheck all the box options
-
Check the
Wrap around
option -
SEARCH
(?x-si) (?<= <Text> ) .+ (?= </Text> )
-
Select the
Regular expression
search mode -
Click on the
Mark All
button -
Click on the
Copy Marked Text
button -
Move to the very end of the file
-
Add an
empty
new line -
Paste the bookmarked lines with
Ctrl + V
=> Near the end of the
XMl
file, you should get this text :</DialogCues> <Name>Cooper_CH1_Call</Name> </DialogCueSheetData> Ahoj. Tady je opět Cooper. You cannot open that door \nwith your current version of \nthe OMNI View software. You will need to update your \nsoftware at a Daemon server. Upgrade to version 1.0 and \nyou can open this door. Then, you can get the girl to the Library. Nejbližší Daemon se nachází v jídelně, na druhé straně kolejí. I will add this waypoint to your OMNI View. I will also highlight this location on your map. I will add this waypoint to your map. ... ... ... Zobrazíš ji výběrem ikony mapy v telefonu. Buď opatrný. The girl must feel a strong sense of deja vu, but fortunately you have been down this road before, too. Each confinement room contains a power unit in case of power failure. Find the unit to open the door. It is in a different location inside every room, so check every corner in OMNI-View. Tohle je pepřový sprej značky P-Zero. Equip it with the phone, and select a guard for her to attack. Knowing the girl, she'll defend herself even without the spray equipped. But be careful, each canister has a single shot. And guards will eventually return to patrol angrier than ever.
Note that I assume that your initial
XML
file does not contain any empty line !
Now, simply add the desired translation of every line, after some
space
characters. For instance :</DialogCues> <Name>Cooper_CH1_Call</Name> </DialogCueSheetData> Ahoj. Tady je opět Cooper. Line 001 You cannot open that door \nwith your current version of \nthe OMNI View software. Line 002 You will need to update your \nsoftware at a Daemon server. Line 003 Upgrade to version 1.0 and \nyou can open this door. Line 004 Then, you can get the girl to the Library. Line 005 Nejbližší Daemon se nachází v jídelně, Line 006 na druhé straně kolejí. Line 007 I will add this waypoint to your OMNI View. Line 008 I will also highlight this location on your map. Line 009 I will add this waypoint to your map. Line 010 ... ... ... Zobrazíš ji výběrem ikony mapy v telefonu. Buď opatrný. Line 244 The girl must feel a strong sense of deja vu, but fortunately you have been down this road before, too. Line 245 Each confinement room contains a power unit in case of power failure. Line 246 Find the unit to open the door. Line 247 It is in a different location inside every room, so check every corner in OMNI-View. Line 248 Tohle je pepřový sprej značky P-Zero. Line 249 Equip it with the phone, and select a guard for her to attack. Line 250 Knowing the girl, she'll defend herself even without the spray equipped. Line 251 But be careful, each canister has a single shot. Line 252 And guards will eventually return to patrol angrier than ever. Line 253
Then :
-
Open the Replace dialog
Ctrl + H
-
Uncheck all the box options
-
Check the
Wrap around
option -
SEARCH
(?x-si) (?<= <Text> ) ( .+ ) (?= </Text> (?s: .+ ) ^ \R (?-s: .+ \R )* \1 \x20{2,} ( .+ ) ) | (?s) ^ \R .+
-
REPLACE
?2\2
-
Select the
Regular expression
search mode -
Click on the
Replace All
button
Here you are ! All the lines
<Text>..........</Text>
should now contain their corresponding translationLine ###
!Best Regards
guy038
P.S. :
In case, your
XML
file contains empty lines, change, for instance, the uniq empty separation line with a line of@
characters and use the regex replacement :-
SEARCH
(?x-si) (?<= <Text> ) ( .+ ) (?= </Text> (?s: .+ ) ^ @+ \R (?-s: .+ \R )* \1 \x20{2,} ( .+ ) ) | (?s) ^ @+ \R .+
-
REPLACE
?2\2
-