Skip to content

Commit a6c71d9

Browse files
committed
[Edit Tags] add ability to insert tag section in the middle of the text
1 parent bf8c2f6 commit a6c71d9

File tree

3 files changed

+103
-69
lines changed

3 files changed

+103
-69
lines changed

docs/Edit Tags.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,13 @@ If you press the button, you will be greeted with a new GUI.
7878

7979
You actually already know how to use this GUI because this is just a mini version of what we did earlier. The GUI only shows transformable tags but other than that the way you edit transforms is just similar to how you work with other tags. If there are multiple transforms, you can switch between those transforms using drop-down menu.
8080

81+
## Example 4: Inserting New Tag Section
82+
83+
If you want to add a new tag section in the middle of the text, simply add `{}`
84+
in the text box where you want a new tag section and click `Apply`. The GUI will
85+
close and reopen with that tag section inserted. Now you can navigate to that
86+
tag section and add tags to it.
87+
8188
# Multi-Line Mode
8289

8390
When you select multiple lines and run the script, this mode is run.

macros/phos.EditTags.moon

Lines changed: 36 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
export script_name = "Edit Tags"
22
export script_description = "Edit tags of current line."
3-
export script_version = "1.0.1"
3+
export script_version = "1.1.0"
44
export script_author = "PhosCity"
55
export script_namespace = "phos.EditTags"
66

@@ -376,6 +376,8 @@ applyGUItoLine = (res, data, existingTagTable, sectionTable, count, tagSection,
376376
generateGUI = (data, sectionTable, count, transforms) ->
377377
--------------------------------------------------------
378378
str = getGUIstring!
379+
parseLine = false
380+
379381

380382
local tagSection, textSection, defaultTags
381383
sectionPair = sectionTable[count]
@@ -421,7 +423,7 @@ generateGUI = (data, sectionTable, count, transforms) ->
421423
aegisub.cancel! unless pressed
422424

423425
if pressed == "Text Mode"
424-
return nil, true, nil, true
426+
return nil, true, nil, true, false
425427

426428
if res["tagSection"]
427429
sectionTable[count]["toRemove"] = false if tagSection
@@ -435,10 +437,15 @@ generateGUI = (data, sectionTable, count, transforms) ->
435437
count = tonumber(count)
436438
exitLoop = false
437439

440+
if res["textvalue"]\match "{}"
441+
parseLine = true
442+
data\commit!
443+
exitLoop = false
444+
438445
if pressed == "Transform"
439446
handleTransform transforms
440447

441-
count, exitLoop, sectionTable, textMode
448+
count, exitLoop, sectionTable, textMode, parseLine
442449

443450

444451
-----------------------------------
@@ -574,33 +581,35 @@ textModeMain = (sub, sel, lines) ->
574581
-------------------------------------
575582
singleLineMain = (sub, sel, lines) ->
576583
-------------------------------------
577-
local textMode
578-
line = lines[1]
584+
local textMode, sectionTable, data, count, transforms
585+
parseLine = true
579586

580-
data = ASS\parse line
587+
while true
588+
if parseLine
589+
line = lines[1]
590+
data = ASS\parse line
581591

582-
transforms = data\getTags "transform"
592+
transforms = data\getTags "transform"
583593

584-
data\cleanTags 0
594+
data\cleanTags 0
585595

586-
sectionTable = {}
587-
local tagIndex
588-
data\callback (section, _, j) ->
589-
return if section.class == ASS.Section.Comment
590-
if section.class == ASS.Section.Tag
591-
tagIndex = j
592-
else
593-
local text
594-
if section.class == ASS.Section.Text
595-
text = section\getString!
596-
else
597-
text = section\toString!
598-
sectionTable[#sectionTable + 1] = tagIndex: tagIndex, textIndex: j, text: text, toRemove: false
599-
tagIndex = nil
600-
601-
count = 1
602-
while true
603-
count, exitLoop, sectionTable, textMode = generateGUI data, sectionTable, count, transforms
596+
sectionTable = {}
597+
local tagIndex
598+
data\callback (section, _, j) ->
599+
return if section.class == ASS.Section.Comment
600+
if section.class == ASS.Section.Tag
601+
tagIndex = j
602+
else
603+
local text
604+
if section.class == ASS.Section.Text
605+
text = section\getString!
606+
else
607+
text = section\toString!
608+
sectionTable[#sectionTable + 1] = tagIndex: tagIndex, textIndex: j, text: text, toRemove: false
609+
tagIndex = nil
610+
611+
count = 1
612+
count, exitLoop, sectionTable, textMode, parseLine = generateGUI data, sectionTable, count, transforms
604613
break if exitLoop
605614

606615
if textMode
@@ -738,4 +747,4 @@ main = (sub, sel) ->
738747
multipleLineMain sub, sel, lines
739748

740749

741-
depctrl\registerMacro main
750+
depctrl\registerMacro main

source/phos.EditTags.norg

Lines changed: 60 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ tangle: {
1818
@code moon
1919
export script_name = "Edit Tags"
2020
export script_description = "Edit tags of current line."
21-
export script_version = "1.0.1"
21+
export script_version = "1.1.0"
2222
export script_author = "PhosCity"
2323
export script_namespace = "phos.EditTags"
2424
@end
@@ -627,6 +627,7 @@ tangle: {
627627
generateGUI = (data, sectionTable, count, transforms) ->
628628
--------------------------------------------------------
629629
str = getGUIstring!
630+
parseLine = false
630631
@end
631632

632633
*** Get different sections
@@ -713,7 +714,7 @@ tangle: {
713714
Ignores all the changes made in the gui if text mode is selected.
714715
@code moon
715716
if pressed == "Text Mode"
716-
return nil, true, nil, true
717+
return nil, true, nil, true, false
717718
@end
718719

719720
*** Apply GUI to Lines
@@ -736,6 +737,15 @@ tangle: {
736737
exitLoop = false
737738
@end
738739

740+
*** Check if user has inserted a new tag section
741+
@code moon
742+
if res["textvalue"]\match "{}"
743+
parseLine = true
744+
data\commit!
745+
exitLoop = false
746+
@end
747+
748+
739749
*** Transforms
740750
If the user has selected transform button, then we go to the main function that handles transform.
741751
@code moon
@@ -745,7 +755,7 @@ tangle: {
745755

746756
*** Returns
747757
@code moon
748-
count, exitLoop, sectionTable, textMode
758+
count, exitLoop, sectionTable, textMode, parseLine
749759

750760
@end
751761

@@ -920,67 +930,75 @@ tangle: {
920930
-------------------------------------
921931
singleLineMain = (sub, sel, lines) ->
922932
-------------------------------------
923-
local textMode
924-
line = lines[1]
933+
local textMode, sectionTable, data, count, transforms
934+
parseLine = true
935+
@end
936+
937+
** Start a while loop that will continue until exitLoop variable is true
938+
@code moon
939+
while true
925940
@end
926941

942+
*** Parse Line
943+
If the variable parseLine is true, which is always the case in the beginning, we parse the line. The variable is also true when user adds new tag section and we have to reparse the line again.
944+
927945
** Collect line data using ASSFoundation
928946
@code moon
929-
data = ASS\parse line
947+
if parseLine
948+
line = lines[1]
949+
data = ASS\parse line
930950
@end
931951

932-
** Gather transforms
952+
*** Gather transforms
933953

934-
It's kinda easier to get transform this way than get their values from effective tags. Actually effective tags don't provide transforms at tll. So we're doing this here now while we get all other tags later.
954+
It's kinda easier to get transform this way than get their values from effective tags. Actually effective tags don't provide transforms at tll. So we're doing this here now while we get all other tags later.
935955
@code moon
936-
transforms = data\getTags "transform"
956+
transforms = data\getTags "transform"
937957
@end
938958

939-
** Clean Tags
959+
*** Clean Tags
940960

941-
Here we don't want to change tags willy nilly. We only want to merge consecutive tag sections so that we have clear distinction of tag, text and drawing sections.
961+
Here we don't want to change tags willy nilly. We only want to merge consecutive tag sections so that we have clear distinction of tag, text and drawing sections.
942962
@code moon
943-
data\cleanTags 0
963+
data\cleanTags 0
944964
@end
945965

946-
** Loop through all the sections of a line
947-
We loop through all the sections of a line and collect all the information we need from the line.
966+
*** Loop through all the sections of a line
967+
We loop through all the sections of a line and collect all the information we need from the line.
948968

949-
The first thing we do is we ignore the comment sections. We aren't touching it in this script. Our goal here is to find effective tags of text or drawing section which we can do but at the same time I also want to find out which tags actually exist in the line itself. These tags will be checked in the GUI so that user can easily differentiate which tags are coming from the line and which tags are effectively applied.
969+
The first thing we do is we ignore the comment sections. We aren't touching it in this script. Our goal here is to find effective tags of text or drawing section which we can do but at the same time I also want to find out which tags actually exist in the line itself. These tags will be checked in the GUI so that user can easily differentiate which tags are coming from the line and which tags are effectively applied.
950970

951-
I do not know any way to find existing tags in text and drawing section currently which is why when we find a tag section, we collect all the tags that we find in that tag section and save it in a variable called *tempExistingTable*. Since we merged consecutive tag sections, these tags will apply to the next text or drawing sections we find which means when we find any text or drawing section, we save these existing tags as the tags that applies to that section. Then we clear the variable *tempExistingTable* for next tag section.
971+
I do not know any way to find existing tags in text and drawing section currently which is why when we find a tag section, we collect all the tags that we find in that tag section and save it in a variable called *tempExistingTable*. Since we merged consecutive tag sections, these tags will apply to the next text or drawing sections we find which means when we find any text or drawing section, we save these existing tags as the tags that applies to that section. Then we clear the variable *tempExistingTable* for next tag section.
952972

953-
After this loop is finished, here's what we get:
954-
- *tagTable* is table of tables where key is the index of that text or drawing section and its value is table of effective tags for that section.
955-
- *existingtagTable* is table just like *tagTable* but in this case only the tags that already exist in the line are available.
956-
- *textTable* is table of tables where key is the index of that text/drawing section and its value is the text of that section.
973+
After this loop is finished, here's what we get:
974+
- *tagTable* is table of tables where key is the index of that text or drawing section and its value is table of effective tags for that section.
975+
- *existingtagTable* is table just like *tagTable* but in this case only the tags that already exist in the line are available.
976+
- *textTable* is table of tables where key is the index of that text/drawing section and its value is the text of that section.
957977

958-
The indexes are important because they are assigned by ASSFoundation to those sections. So, whatever changes the user makes on the GUI will be applied on the basis of those indexes.
978+
The indexes are important because they are assigned by ASSFoundation to those sections. So, whatever changes the user makes on the GUI will be applied on the basis of those indexes.
959979

960-
Resource: {https://github.com/TypesettingTools/ASSFoundation/blob/ba2cace60efc39edfdedce1747b2b68aeff0af01/l0/ASSFoundation/LineContents.moon#L174-L203}[Fansubbing Wiki]
980+
Resource: {https://github.com/TypesettingTools/ASSFoundation/blob/ba2cace60efc39edfdedce1747b2b68aeff0af01/l0/ASSFoundation/LineContents.moon#L174-L203}[Fansubbing Wiki]
961981

962-
@code moon
963-
sectionTable = {}
964-
local tagIndex
965-
data\callback (section, _, j) ->
966-
return if section.class == ASS.Section.Comment
967-
if section.class == ASS.Section.Tag
968-
tagIndex = j
969-
else
970-
local text
971-
if section.class == ASS.Section.Text
972-
text = section\getString!
973-
else
974-
text = section\toString!
975-
sectionTable[#sectionTable + 1] = tagIndex: tagIndex, textIndex: j, text: text, toRemove: false
976-
tagIndex = nil
977-
@end
982+
@code moon
983+
sectionTable = {}
984+
local tagIndex
985+
data\callback (section, _, j) ->
986+
return if section.class == ASS.Section.Comment
987+
if section.class == ASS.Section.Tag
988+
tagIndex = j
989+
else
990+
local text
991+
if section.class == ASS.Section.Text
992+
text = section\getString!
993+
else
994+
text = section\toString!
995+
sectionTable[#sectionTable + 1] = tagIndex: tagIndex, textIndex: j, text: text, toRemove: false
996+
tagIndex = nil
997+
@end
978998

979-
** Parse the information we collected to use in GUI
980999
@code moon
981-
count = 1
982-
while true
983-
count, exitLoop, sectionTable, textMode = generateGUI data, sectionTable, count, transforms
1000+
count = 1
1001+
count, exitLoop, sectionTable, textMode, parseLine = generateGUI data, sectionTable, count, transforms
9841002
break if exitLoop
9851003
@end
9861004

0 commit comments

Comments
 (0)