0

我目前在 MarsEdit.app 中使用一个有缺陷的脚本。它会检查 HTML 文档中是否有段落被标签包裹的情况,<p>如下所示:

-- If already starts with <p>, don't prepend another one

if not {oneParagraph starts with "<p>"} then
           set newBodyText to newBodyText & "<p>"
end if
set newBodyText to newBodyText & oneParagraph

这里的问题是,如果段落(或单行)被任何其他 HTML 标记而不是<p>标记包裹,则脚本<p>会全面包裹标记。

脚本的另一部分,它检查段落末尾的结束标签,它的作用几乎相同。

-- If already ends with </p>, don't append another one

if not (oneParagraph ends with "</p>") then
    set newBodyText to newBodyText & "</p>"
end if

set newBodyText to newBodyText & return

例子:

<h5>富吧</h5>

变成

<p><h5>富吧</h5></p>

在另一个问题Applescript and "starts with" operator中,@lri 很友好地为我提供了一个与之相关的解决方案。

on startswith(txt, l)
repeat with v in l
    if txt starts with v then return true
end repeat
false
end startswith

startswith("abc", {"a", "d", "e"}) -- true

他的另一个建议也可以在这个网站上找到

使用 MarsEdit.app 实施这些建议对我来说是另一个问题。

我将整个脚本上传到了 pastebin。Pastebin:MarsEdit.app,换行

标记脚本如果有人可以帮助我将脚本编辑为@lri 的建议,那就太好了。提前致谢。

4

2 回答 2

1

您可以通过在 applescript 中运行 shell 命令来使用另一种更强大的语言来完成此过程

基本上你可以像这样在终端窗口中运行任何你想要的东西

假设您的桌面上有一个 test.txt 文件,您可以运行它,它会用 p 标签包装所有行

set dir to quoted form of POSIX path of (path to desktop)
set results to do shell script "cd " & dir & "
awk ' { print \"<p>\"$0\"</p>\" } ' test.txt"

如果你想运行一个 php 文件,你就做

set dir to quoted form of POSIX path of 'path:to:php_folder")
set results to do shell script "cd " & dir & "
php test.php"
于 2011-05-09T19:01:42.720 回答
1

苹果脚本:

tell application "MarsEdit" to set txt to current text of document 1
set paras to paragraphs of txt

repeat with i from 1 to (count paras)
    set v to item i of paras
    ignoring white space
        if not (v is "" or v starts with "<") then
            set item i of paras to "<p>" & v & "</p>"
        end if
    end ignoring
end repeat

set text item delimiters to ASCII character 10
tell application "MarsEdit" to set current text of document 1 to paras as text

红宝石应用脚本

require 'appscript'; include Appscript

doc = app('MarsEdit').documents[0]
lines = doc.current_text.get.gsub(/\r\n?/, "\n").split("\n")

for i in 0...lines.size
    next if lines[i] =~ /^\s*$/ or lines[i] =~ /^\s*</
    lines[i] = "<p>#{lines[i]}</p>"
end

doc.current_text.set(lines.join("\n"))

这些假设任何以 (white space and) 开头的东西<都是一个标签。

于 2011-05-10T13:49:18.653 回答