我目前在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
这里的问题是,如果段落(或单行)包含除<p>
标记之外的任何其他HTML标记,则脚本会在整个板上包装<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>
Foobar </h5>
变
<p><h5>
Foobar </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
他的另一个建议也可以在这个网站上找到Wrap lines with tags on applescript
使用MarsEdit.app实现这些建议对我来说是另一个问题。
我在pastebin上传了整个脚本。 Pastebin: MarsEdit.app, wrap line with tags script
AppleScript的:
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"))
这些假设以(白色空格和)<
开头的任何东西都是标签。
你可以通过在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"