¿Convertir código de hipervínculo HTML a Markdown (MD) en AppleScript puro?

Oct 24 2020

Tengo un archivo de texto o algún texto para procesar a través del portapapeles. Contiene algunas líneas de código HTML y me gustaría convertirlo a Markdown puro.

Por ejemplo, de:

This is a link: <a href="https://duck.com">link</a>

a:

This is a link: [link](https://duck.com)

¿Existe un código AppleScript puro (que usa la sustitución de expresiones regulares) que pueda hacer esto?

Alternativamente, estoy contento con que AppleScript llame a cualquier herramienta CLI para procesar el texto a través de do shell scriptsi es un texto incorporado que no requiere la instalación de homebrewuna herramienta de terceros.

Respuestas

1 user3439894 Oct 23 2020 at 23:33

He aquí un ejemplo :

set htmlString to "This is a link: <a href=\"https://duck.com\">link</a>"

set mdString to do shell script "/usr/bin/sed -E -e 's|<a href=\"|[link](|g' -e 's|\">link</a>|)|g' <<< " & htmlString's quoted form

Resultado:

"This is a link: [link](https://duck.com)"

Esto también puede estar inactivo sin el uso del do shell script comando , como en este ejemplo :

set htmlString to "This is a link: <a href=\"https://duck.com\">link</a>"

set htmlString to findAndReplaceInText(htmlString, "<a href=\"", "[link](")
set htmlString to findAndReplaceInText(htmlString, "\">link</a>", ")")

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

Resultado:

"This is a link: [link](https://duck.com)"

Si This is a link: <a href="https://duck.com">link</a>está en un archivo o en el portapapeles , el escape se realiza automáticamente al asignarlo a una variable . Luego, solo necesita escapar "del sed comando como se muestra en el ejemplo anterior.


Otros ejemplos :

Si está This is a link: <a href="https://duck.com">link</a>en un archivo:

set htmlFile to "/path/to/filename/ext"
set htmlString to read htmlFile
set mdString to do shell script "/usr/bin/sed -E -e 's|<a href=\"|[link](|g' -e 's|\">link</a>|)|g' <<< " & htmlString's quoted form

O procesando el archivo directamente:

set htmlFile to "/path/to/filename.ext"
set mdString to do shell script "/usr/bin/sed -E -e 's|<a href=\"|[link](|g' -e 's|\">link</a>|)|g'" & space & htmlFile's quoted form

Si This is a link: <a href="https://duck.com">link</a>está en el portapapeles :

set htmlString to (the clipboard as text)
set mdString to do shell script "/usr/bin/sed -E -e 's|<a href=\"|[link](|g' -e 's|\">link</a>|)|g' <<< " & htmlString's quoted form

Nota: El uso del findAndReplaceInText() controlador también se puede utilizar en lugar del do shell script comando en estos otros ejemplos .

  • Consulte también Manipulación de texto en la Guía de secuencias de comandos de automatización de Mac .