HTML-Code mit Aufzählungszeichen in Markdown in AppleScript konvertieren?
Ich muss diesen Text in AppleScript bearbeiten (z. B. den Text einer Variablen):
Example note exported from Apple.
<ul>
<li>Indent</li>
<ul>
<li>*Further* indent</li>
<ul>
<li>Even **further **indent. With a [link](https://duck.com).</li>
</ul>
</ul>
</ul>
End note.
Ich konvertiere alles von HTML nach Markdown. Ich muss dieses verbleibende Stück HTML, das die Aufzählungsliste ist, bereinigen, damit das Ergebnis lautet (mit echten Tabulatoren als Einrückungsbereich):
Example note exported from Apple.
- Indent
- *Further* indent
- Even **further **indent. With a [link](https://duck.com).
End note.
Es muss in der Lage sein, verschachtelte Einrückungen auf eine beliebige Anzahl von Ebenen und möglicherweise Rich Text in den Elementen wie diesem Beispiel zu berücksichtigen. Ich bevorzuge die Markdown-Ausgabe, um Bindestriche und eine Registerkarte für das Einrücken zu verwenden.
Es muss auch im Applescript enthalten sein - keine externen .pyDateien usw., und es muss kein homebrewTool eines Drittanbieters installiert sein.
Antworten
Das folgende Beispiel für den codierten AppleScript- Code funktioniert mit HTML- Code mit Aufzählungszeichen , wie im OP gezeigt. Das bedeutet, dass in der Variablen nur der relevante Code zum Definieren der Aufzählungsliste und kein anderer beliebiger HTML- Code übergeben wird .
Wie codiert, wird auch die entsprechende Ausgabe für Variationen des HTML- Codes mit Aufzählungszeichen erstellt , nicht nur für das hier gezeigte spezifische Beispiel . Dies wurde auf einer Vielzahl von anderen getesteten Proben nur enthalten Aufzählungsliste HTML - Code und erzeugt den entsprechenden Ausgang für sie wie das tut Beispiel hier.
-- # Define exportedNote variable containing the bulleted list HTML code.
set exportedNote to "<ul>
<li>Indent</li>
<ul>
<li>*Further* indent</li>
<ul>
<li>Even **further** indent. With a [link](https://duck.com).</li>
</ul>
</ul>
</ul>"
-- # Create an AppleScript list from the lines of bulleted list HTML code.
set exportedNoteList to paragraphs of exportedNote
-- # Process the list, acting only on items that contain "</li>"
-- # as they are the only ones relevant to converting the
-- # bulleted list HTML code to Markdown.
set tempList to {}
repeat with i from 1 to the number of items in exportedNoteList
if item i of exportedNoteList contains "<li>" then
set thisItem to item i of exportedNoteList
set thisItem to findAndReplaceInText(thisItem, "</li>", "")
set numberOfLeadingSpaces to ((offset of "<" in thisItem) - 1)
if numberOfLeadingSpaces is less than 4 then
set searchString to characters 1 thru numberOfLeadingSpaces of thisItem & "<li>" as text
set thisItem to findAndReplaceInText(thisItem, searchString, "- ")
set end of tempList to thisItem
end if
if numberOfLeadingSpaces is greater than 3 then
set searchString to characters 1 thru numberOfLeadingSpaces of thisItem & "<li>" as text
set thisItem to findAndReplaceInText(thisItem, searchString, "- ")
set numberOfLeadingTabs to (numberOfLeadingSpaces / 2) as integer
repeat with i from 1 to (numberOfLeadingTabs - 1)
set thisItem to tab & thisItem
end repeat
set end of tempList to thisItem
end if
end if
end repeat
-- # Update the contents of the exportedNoteList
-- # list to contain only the relevant list items.
set exportedNoteList to tempList
-- # Convert the exportedNoteList list to text.
set AppleScript's text item delimiters to linefeed
set convertedNote to text items of exportedNoteList
set convertedNote to convertedNote as text
set AppleScript's text item delimiters to {}
-- # 'return' is only used to show its value in Script Editor. Use
-- # the convertedNote variable as needed in the working code.
return convertedNote
-- # Handler(s)
-- # Note: Handlers may be placed as one chooses as appropriate.
-- # My preference is at the bottom of the rest of the code.
on findAndReplaceInText(theText, theSearchString, theReplacementString)
set AppleScript's text item delimiters to theSearchString
set theTextItems to every text item of theText
set AppleScript's text item delimiters to theReplacementString
set theText to theTextItems as string
set AppleScript's text item delimiters to ""
return theText
end findAndReplaceInText
Ergebnis:
"- Indent
- *Further* indent
- Even **further** indent. With a [link](https://duck.com)."
Was in einem Browser auf dieser Website wie folgt angezeigt wird:
- Einzug
- Weiterer Einzug
- Noch weiter einrücken. Mit einem Link .
- Weiterer Einzug
Ich werde diese Frage auch mit einer alternativen Methode beantworten, die ich ausgearbeitet habe:
set exportedNote to "Example note exported from Apple.
<ul>
<li>Indent</li>
<ul>
<li>*Further* indent</li>
<ul>
<li>Even **further **indent. With a [link](https://duck.com).</li>
</ul>
</ul>
</ul>
End note."
set deleteHTMLline to {"<ul>"}
set exportedNote to deleteLinesFromText(exportedNote, deleteHTMLline) of me as text
set deleteHTMLline to {"</ul>"}
set exportedNote to deleteLinesFromText(exportedNote, deleteHTMLline) of me as text
set exportedNote to replace_chars(exportedNote, " <li>", tab & tab & "- ") of me as text
set exportedNote to replace_chars(exportedNote, " <li>", tab & "- ") of me as text
set exportedNote to replace_chars(exportedNote, " <li>", "- ") of me as text
set exportedNote to replace_chars(exportedNote, "</li>", "") of me as text
return exportedNote
on replace_chars(this_text, search_string, replacement_string)
set AppleScript's text item delimiters to the search_string
set the item_list to every text item of this_text
set AppleScript's text item delimiters to the replacement_string
set this_text to the item_list as string
set AppleScript's text item delimiters to {""}
return this_text
end replace_chars
on deleteLinesFromText(theText, deletePhrase)
set newText to ""
try
set textList to paragraphs of theText
repeat with i from 1 to count of textList
set thisLine to item i of textList
if thisLine does not contain deletePhrase then
set newText to newText & thisLine & return
end if
end repeat
if newText is not "" then set newText to text 1 thru -2 of newText
on error
set newText to theText
end try
return newText
end deleteLinesFromText
Ergebnis:
Example note exported from Apple.
- Indent
- *Further* indent
- Even **further **indent. With a [link](https://duck.com).
End note.
Anmerkungen:
Ähnlich wie bei der Hauptantwort müssen Sie zusätzliche Zeilen hinzufügen, wenn Sie weitere Einrückungsstufen konvertieren möchten.
Dieser Beispielcode konvertiert nur drei Ebenen. Um ein viertes hinzuzufügen, können Sie oben im set exportedNote to replace_charsAbschnitt Folgendes hinzufügen :
set exportedNote to replace_chars(exportedNote, " <li>", tab & tab & tab & "- ") of me as text
Fügen Sie oben tiefere Ebenen hinzu, die in chronologischer Reihenfolge nach unten gehen, mit zwei weiteren Leerzeichen vor jedem <li>und einem weiteren tab &.
Wenn jemand Code hat, um eine beliebige nAnzahl von Einrückungsstufen konvertieren zu können, melden Sie sich bitte an oder senden Sie Änderungen.