将文本片段移动到文档末尾

Moving snippets of text to end of document

我正在将 XML 格式的一本书转换为 EPUB。它有 67 章和 1000 多个脚注。我发现在 Epub 中创建脚注的一个好方法是将注释的内容移动到末尾的列表中,然后 link 在注释调用者和末尾的元素之间来回移动。我在 Textwrangler 中使用 Grep 搜索将标签更改为正确的 html-标签。但是我想不出一种方法来使用 Grep 查找一段文本并将其移动到文档的末尾?有没有其他简单的方法可以使用 Textwrangler 或其他文本编辑器来做到这一点?我假设 Applescript 可以 link 和 Textwrangler 为我做这件事(我正在使用 OS X)但我不确定如何。我不是程序员所以我更喜欢尽可能简单的解决方案,只要它不是手动剪切和粘贴:)

此脚本使用 grep 模式查找字符串并将其附加到文档末尾的新行

tell application "TextWrangler"
    tell text of window 1
        set cL to count lines
        select first line -- to start at the top
        repeat
            -- start from the selection, selection change when the "find" command found the search pattern in the document
            set r to find "[\d]+" options {search mode:grep} with selecting match -- change "[\d]+" to your search pattern 
            if found of r then
                if startLine of found object of r > cL then exit repeat -- the end of the original document, to not search in the appended lines
                set contents of found object of r to "" -- delete the found text
                make line with data found text of r -- append a new line + the found text
            else
                exit repeat
            end if
        end repeat
    end tell
end tell

重要提示:在 AppleScript 脚本中,您必须转义模式中的反斜杠。

示例:来自 TextWrangler 的模式 [\d]*\t 在 AppleScript

中必须是 [\d]*\t

已更新另一个问题。

此脚本搜索 <li id="......",如果它不是唯一的则添加一个后缀 --> - 和一个整数。

set uniq_ID_names to {}
tell application "TextWrangler"
    tell text of window 1
        select first line -- to start at the top
        repeat
            -- start from the selection, selection change when the "find" command found the search pattern in the document
            set r to find "<li id=\"[^\"]+" options {search mode:grep} with selecting match -- get character from <li id=" until the next double quote character
            if not found of r then exit repeat
            set t to found text of r
            if t is in uniq_ID_names then -- same ID's name
                set i to 1
                repeat -- add suffix to the found text
                    set t2 to t & "-" & i
                    if t2 is not in uniq_ID_names then
                        add suffix (found object of r) suffix ("-" & i)
                        set t to t2
                        exit repeat
                    end if
                    set i to i + 1
                end repeat
            end if
            set end of uniq_ID_names to t
        end repeat
    end tell
end tell