New to the forum, but have been amazed by KM since I discovered it a year ago, and this is the reason I decided to come here today and create an account.
I wanted to take an URL and insert it in a document, e.g. LibreOffice, as a link with the title of the website as the text. I searched and found that similar questions have been asked a few times, to name the main ones I used for inspiration:
(It is always prudent to have complete paths IMHO, don't forget to chmod u+x on the the Python script).
The Python file is the reason for this post, I donate it to public domain, and it depends on the requests library.
# -*- mode: python; coding: utf-8 -*-
import requests # python -m pip install requests
lang = ""
if m := re.search("https?://.*$", url):
url = m.group(0).strip()
r = requests.get(url)
if m := re.search("""<html[^>]+lang=["']?([a-z][a-z])""", r.text, flags=re.IGNORECASE):
lang = m.group(1)
if m := re.search("<title.*?>(.*?)</title.*?>", r.text, flags=re.DOTALL|re.IGNORECASE):
title = m.group(1).strip()
url = "No link"
url = "Error link" # Not good to ignore errors but so very many thing can go wrong
One can argue if hreflang makes any difference.
I have a similar function in Emacs (I assume you can tell I use Emacs, can't you...?) and I wanted something similar for LibreOffice and friends.
No point in doing with just one foot, right? I now realise I maybe should've attached the script instead of copy the text of it; different forums, different cultures (I obviously missed this when I browsed the FAQ and newbie guides). I thought it would be a bit overkill with adding the macro itself, hence I described it in text instead.
I need to study the "Markdown Link" macro you mention, it seemed to be very complex when I gave it a glance just now. I think though it tries to solve a different problem from the one I wanted to solve, but might be handy once I understand what it does.
I wanted to keep the solution simple, the macros I found that did something similar also queried the browser for link and title. Even if I would prefer that option to access the web again, they usually needed to do special handling of Firefox, which is the main browser for me. Since I am ... not found of kludges I stayed away from those solutions. The benefit of this solution is also that all it needs is the URL – regardless of where it comes from. The reason I wanted to share this was to show to others that it can be made pretty simple.