Convertir le code HTML de la liste à puces en Markdown dans AppleScript?
J'ai ce texte à manipuler dans AppleScript (par exemple le texte d'une variable):
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.
Je convertis tout en Markdown à partir du HTML. J'ai besoin de nettoyer ce bit restant de HTML qui est la liste à puces, afin que le résultat soit (avec de vrais tabulations comme espace de retrait):
Example note exported from Apple.
- Indent
- *Further* indent
- Even **further **indent. With a [link](https://duck.com).
End note.
Il doit être capable de prendre en charge l'indentation imbriquée à n'importe quel nombre 'n' de niveaux, et éventuellement avec du texte riche à l'intérieur des éléments tels que cet exemple. Je préfère que la sortie Markdown utilise des tirets et un onglet pour l'indentation.
Il doit également pouvoir être contenu dans l'applescript - pas de .pyfichiers externes , etc., et ne pas nécessiter l' homebrewinstallation d'un outil tiers.
Réponses
L' exemple de code AppleScript suivant , tel que codé, est destiné à fonctionner avec le code HTML de liste à puces comme indiqué dans l' OP , ce qui signifie que ce qui est passé dans la variable est simplement le code approprié pour définir la liste à puces et non un autre code HTML arbitraire .
Tel que codé, il produira également la sortie appropriée pour les variations du code HTML de liste à puces , pas seulement l' exemple spécifique montré ici. Cela a été testé sur une variété d'autres exemples ne contenant que du code HTML de liste à puces et produit la sortie appropriée, comme le fait l' exemple ci-dessous.
-- # 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
Résultat:
"- Indent
- *Further* indent
- Even **further** indent. With a [link](https://duck.com)."
Qui s'affiche comme suit dans un navigateur sur ce site Web:
- Retrait
- Retrait
supplémentaire
- Encore plus indent. Avec un lien .
- Retrait
supplémentaire
Je répondrai également à cette question avec une méthode alternative que j'ai élaborée:
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
Résultat:
Example note exported from Apple.
- Indent
- *Further* indent
- Even **further **indent. With a [link](https://duck.com).
End note.
Remarques:
Comme pour la réponse principale, si vous souhaitez convertir d'autres niveaux d'indentation, vous devrez ajouter des lignes supplémentaires pour cela.
Cet exemple de code ne convertit que trois niveaux. Pour en ajouter un quatrième, vous pouvez ajouter en haut de la set exportedNote to replace_charssection:
set exportedNote to replace_chars(exportedNote, " <li>", tab & tab & tab & "- ") of me as text
Ajoutez des niveaux plus profonds en haut dans l'ordre chronologique avec deux espaces supplémentaires avant chacun <li>et un autre tab &.
Si quelqu'un a du code pour convertir un nnombre illimité de niveaux d'indentation, veuillez le signaler ou soumettre des modifications.