~ubuntu-branches/ubuntu/trusty/aegisub/trusty

« back to all changes in this revision

Viewing changes to automation/include/cleantags.lua

  • Committer: Package Import Robot
  • Author(s): Sebastian Reichel
  • Date: 2012-03-16 22:58:00 UTC
  • Revision ID: package-import@ubuntu.com-20120316225800-yfb8h9e5n04rk46a
Tags: upstream-2.1.9
ImportĀ upstreamĀ versionĀ 2.1.9

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
--[[ 
 
2
"Clean Tags" -- An Auto4 LUA script for cleaning up ASS subtitle lines of badly-formed override 
 
3
blocks and redundant/duplicate tags
 
4
* Designed to work for Aegisub 2.0 and above
 
5
* include()'ed this file from any auto4 script to use the cleantags() function below
 
6
* Might change from time to time so look out for cleantags_version below
 
7
 
 
8
Copyright (c) 2007-2009 Muhammad Lukman Nasaruddin (aka ai-chan, Aegisub's forum member)
 
9
 
 
10
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and 
 
11
associated documentation files (the "Software"), to deal in the Software without restriction, 
 
12
including without limitation the rights to use, copy, modify, merge, publish, distribute, 
 
13
sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is 
 
14
furnished to do so, subject to the following conditions:
 
15
 
 
16
The above copyright notice and this permission notice shall be included in all copies or substantial 
 
17
portions of the Software.
 
18
 
 
19
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT 
 
20
NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 
 
21
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES 
 
22
OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 
 
23
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 
24
]]
 
25
 
 
26
cleantags_version = "1.301"
 
27
cleantags_modified = "13 November 2009"
 
28
 
 
29
ktag = "\\[kK][fo]?%d+"
 
30
 
 
31
--[[ The main function that performs the cleaning up
 
32
Takes: text
 
33
Returns: cleaned up text
 
34
]]
 
35
function cleantags(text)
 
36
        --[[ Combine adjacent override override blocks into one ]]
 
37
        function combineadjacentnotks(block1, block2)
 
38
                if string.find(block1, ktag) and string.find(block2, ktag) then
 
39
                        -- if both adjacent override blocks have \k , let them be
 
40
                        return "{" .. block1 .. "}" .. string.char(1) .. "{" .. block2 .. "}" -- char(1) prevents infinite loop
 
41
                else
 
42
                        -- either one or both override blocks don't have \k , so combine them into one override block
 
43
                        return "{" .. block1 .. block2 .. "}"
 
44
                end
 
45
        end
 
46
        repeat
 
47
                if aegisub.progress.is_cancelled() then return end
 
48
                text, replaced = string.gsub(text,"{(.-)}{(.-)}", combineadjacentnotks)
 
49
        until replaced == 0
 
50
        text = string.gsub(text, string.char(1), "") -- removes all char(1) we inserted
 
51
 
 
52
        --[[ Move first \k tag in override blocks to the front ]]
 
53
        text = string.gsub(text, "{([^{}]-)(" .. ktag .. ")(.-)}", "{%2%1%3}") 
 
54
 
 
55
        --[[ For some reasons if one override block has more than one \k tag, 
 
56
        push those to behind the first \k tag (which has been pushed to front already) ]]
 
57
        repeat
 
58
                if aegisub.progress.is_cancelled() then return end
 
59
                text, replaced = string.gsub(text, "{([^{}]-)(" .. ktag .. ")(\\[^kK][^}]-)(" .. ktag .. ")(.-)}", "{%1%2%4%3%5}")
 
60
        until replaced == 0
 
61
                                
 
62
        --[[ Move to the front all tags that affect the whole line (i.e. not affected by their positions in the line) ]]
 
63
        local linetags = ""
 
64
        function first(pattern)
 
65
                local p_s, _, p_tag = string.find(text, pattern)
 
66
                if p_s then
 
67
                        text = string.gsub(text, pattern, "")
 
68
                        linetags = linetags .. p_tag                            
 
69
                end
 
70
        end
 
71
        function firstoftwo(pattern1, pattern2)
 
72
                local p1_s, _, p1_tag = string.find(text, pattern1)
 
73
                local p2_s, _, p2_tag = string.find(text, pattern2)
 
74
                text = string.gsub(text, pattern1, "")
 
75
                text = string.gsub(text, pattern2, "")
 
76
                if p1_s and (not p2_s or p1_s < p2_s) then
 
77
                        linetags = linetags .. p1_tag
 
78
                elseif p2_s then
 
79
                        linetags = linetags .. p2_tag                           
 
80
                end
 
81
        end
 
82
        -- \an or \a
 
83
        first("(\\an?%d+)")
 
84
        -- \org
 
85
        first("(\\org%([^,%)]*,[^,%)]*%))")
 
86
        -- \move and \pos (the first one wins)
 
87
        firstoftwo("(\\move%([^,%)]*,[^,%)]*,[^,%)]*,[^,%)]*%))", "(\\pos%([^,%)]*,[^,%)]*%))")
 
88
        -- \fade and \fad (the first one wins)
 
89
        firstoftwo("(\\fade%([^,%)]*,[^,%)]*,[^,%)]*,[^,%)]*,[^,%)]*,[^,%)]*,[^,%)]*%))", "(\\fad%([^,%)]*,[^,%)]*%))")
 
90
        -- integrate
 
91
        if string.len(linetags) > 0 then
 
92
                if string.sub(text, 1, 1) == "{" then
 
93
                        text = "{" .. linetags .. string.sub(text, 2)
 
94
                else
 
95
                        text = "{" .. linetags .. "}" .. text
 
96
                end
 
97
        end
 
98
 
 
99
        --[[ Remove any spaces within parenteses within override blocks except for \clip tags ]]
 
100
        local comb = function(a,b,c,d,e) 
 
101
                if c ~= "\\i?clip" or d:sub(-1):find("[,%({]") or e:sub(1,1):find("[,%)}]") then return a..b..d..e 
 
102
                else return a..b..d..string.char(2)..e end 
 
103
        end
 
104
    repeat
 
105
        text, replaced2 = string.gsub(text, "({[^}\\]*)([^}%s]*(\\[^%(}\\%s]*))%s*(%([^%s%)}]*)%s+([^}]*)", comb)
 
106
    until replaced2 == 0
 
107
    text, _ = text:gsub(string.char(2)," ")
 
108
                
 
109
        --[[ Remove all empty override blocks ==> {} ]]
 
110
        text = string.gsub(text, "{%s*}", "")
 
111
 
 
112
        --[[ Finally, return the cleaned up text ]]
 
113
        return text
 
114
end