我目前在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如果有人可以帮我编辑脚本@ lri的建议那就太棒了。提前谢谢。
答案 0 :(得分: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"
答案 1 :(得分:1)
的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"))
这些假设以(空格和<
开头的任何内容都是标记。