AppleScript: подстрока в строку или формат html - PullRequest
0 голосов
/ 25 августа 2011

Я сейчас работаю над своим яблочным сценарием, и я застрял здесь .. Давайте возьмем этот фрагмент в качестве примера HTML-кода

<body><div>Apple don't behave accordingly <a href = "http://apple.com>apple</a></div></body>

Теперь мне нужно вернуть слово безHTML-теги.Либо, удалив скобку со всем в нем, либо, может быть, есть какой-либо другой способ переформатировать html в простой текст.

Результат должен быть:

Apple don 'т вести себя соответственно яблоко

Ответы [ 3 ]

1 голос
/ 18 ноября 2015

Думал, что добавлю дополнительный ответ из-за проблемы, с которой столкнулся.Если вы хотите, чтобы символы UTF-8 не терялись, вам нужно:

set plain_text to do shell script "echo " & quoted form of ("<!DOCTYPE HTML PUBLIC><meta charset=\"UTF-8\">" & html_string) & space & "| textutil  -convert txt  -stdin -stdout"

Вам нужно добавить метатег <meta charset=\"UTF-8\">, чтобы textutil воспринимал это как документ utf-8.

0 голосов
/ 25 августа 2011

Как насчет использования textutil ?

on run -- example (don't forget to escape quotes)
    removeMarkup from "<body><div>Apple don't behave accordingly <a href = \"http://apple.com\">apple</a></div></body>"
end run

to removeMarkup from someText -- strip HTML using textutil
    set someText to quoted form of ("<!DOCTYPE HTML PUBLIC>" & someText) -- fake a HTML document header
    return (do shell script "echo " & someText & " | /usr/bin/textutil -stdin -convert txt -stdout") -- strip HTML
end removeMarkup
0 голосов
/ 25 августа 2011
on findStrings(these_strings, search_string)
    set the foundList to {}
    repeat with this_string in these_strings
        considering case
            if the search_string contains this_string then set the end of the foundList to this_string
        end considering
    end repeat
    return the foundList
end findStrings

findStrings({"List","Of","Strings","To","find..."}, "...in String to search")
...