Applescript wrap lines with HTML tags and MarsEdit.app script problem

牧云@^-^@ 提交于 2019-12-13 05:41:36

问题


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

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

Example:

<h5>Foobar </h5>

becomes

<p><h5>Foobar </h5></p>

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

and another of his recommendations can be found on this website as well Wrap lines with tags on applescript

Implementing these recommendations with MarsEdit.app is another issue for me.

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.


回答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

Ruby appscript:

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.




回答2:


you could do this process using another stronger language by running shell commands in applescript

basiclly you can run anything that you would in a terminal window like this

lets assume you have a test.txt file on your desktop you could run this and it would wrap all the lines with p tag

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"

and if you want to run a php file you just do

set dir to quoted form of POSIX path of 'path:to:php_folder")
set results to do shell script "cd " & dir & "
php test.php"


来源:https://stackoverflow.com/questions/5932752/applescript-wrap-lines-with-html-tags-and-marsedit-app-script-problem

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!