Applescript 用 HTML 标签换行和 MarsEdit.app 脚本问题 [英] Applescript wrap lines with HTML tags and MarsEdit.app script problem
问题描述
我目前在 MarsEdit.app 中使用一个有缺陷的脚本.它会检查 HTML 文档中是否有段落被 标签包裹的情况,如下所示:
Im currently using a script in MarsEdit.app which has a flaw. It checks the HTML document for cases where paragraphs are wrapped with <p>
tags as follows:
-- 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>代码> 标签.
The problem here is that if the paragraph (or single line) is wrapped with any other HTML tag other than a <p>
tag the script wraps <p>
tags across the board.
脚本的另一部分,检查段落末尾的结束标记,其作用几乎相同.
Another portion of the script, which checks for ending tags at the end of the paragraph does pretty much the same thing.
-- 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
示例:
Foobar
<h5>
Foobar </h5>
变成
Foobar
在另一个问题 Applescript 和开始于"中操作员,@lri 很友好地为我提供了与之相关的解决方案.
In another question Applescript and "starts with" operator, @lri was kind enough to provide me a solution related to it.
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
他的另一个建议也可以在这个网站上找到在 applescript 上用标签换行
and another of his recommendations can be found on this website as well Wrap lines with tags on applescript
使用 MarsEdit.app 实施这些建议对我来说是另一个问题.
Implementing these recommendations with MarsEdit.app is another issue for me.
我将整个脚本上传到了 pastebin.Pastebin:MarsEdit.app,用 标签脚本换行
I uploaded the entire script on pastebin. Pastebin: MarsEdit.app, wrap line with
tags script If anyone can help me edit the script to @lri's recommendations that would be great. Thanks in advance.
推荐答案
AppleScript:
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"))
<小时>
这些假设任何以(空格和)<
开头的都是一个标签.
These assume that anything starting with (white space and) <
is a tag.
这篇关于Applescript 用 HTML 标签换行和 MarsEdit.app 脚本问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!