Add a script for getting reference Markdown links
- ID
9235be3- date
2024-01-12 06:59:04+00:00- author
Alex Chan <alex@alexwlchan.net>- parent
3762875- message
Add a script for getting reference Markdown links- changed files
2 files, 67 additions, 1 deletion
Changed files
textexpander/README.md (3719) → textexpander/README.md (4131)
diff --git a/textexpander/README.md b/textexpander/README.md
index f59df1f..54d531e 100644
--- a/textexpander/README.md
+++ b/textexpander/README.md
@@ -30,6 +30,12 @@ scripts = [
"""
},
{
+ "name": "get_markdown_link.py",
+ "description": """
+ print a reference-style Markdown link to my frontmost Safari URL
+ """,
+ },
+ {
"name": "get_mastodon_text.py",
"description": """
print a Markdown-formatted blockquote of a Mastodon I've got open in Safari, suitable for saving in Obsidian
@@ -83,6 +89,15 @@ cog_helpers.create_description_table(
</dd>
<dt>
+ <a href="https://github.com/alexwlchan/scripts/blob/main/textexpander/get_markdown_link.py">
+ <code>get_markdown_link.py</code>
+ </a>
+ </dt>
+ <dd>
+ print a reference-style Markdown link to my frontmost Safari URL
+ </dd>
+
+ <dt>
<a href="https://github.com/alexwlchan/scripts/blob/main/textexpander/get_mastodon_text.py">
<code>get_mastodon_text.py</code>
</a>
@@ -119,4 +134,4 @@ cog_helpers.create_description_table(
get URL and first paragraph of a Wikipedia entry I have open in Safari, suitable for saving in Obsidian.
</dd>
</dl>
-<!-- [[[end]]] (checksum: ad35004c0b69b4421e1d303061102eb9) -->
+<!-- [[[end]]] (checksum: bde87b2a782ccef4c17c11eb80e2f099) -->
textexpander/get_markdown_link.py (0) → textexpander/get_markdown_link.py (1341)
diff --git a/textexpander/get_markdown_link.py b/textexpander/get_markdown_link.py
new file mode 100755
index 0000000..f5e5db0
--- /dev/null
+++ b/textexpander/get_markdown_link.py
@@ -0,0 +1,51 @@
+#!/usr/bin/env python3
+"""
+Create a reference-style Markdown link to my frontmost Safari URL.
+
+This assumes I've already typed the link label, and it adds the reference.
+It tries to deduce a sensible name for the reference, or prompts for input
+if it can't pick one.
+"""
+
+import subprocess
+
+import hyperlink
+
+from get_safari_url import normalise_url
+from urls import get_safari_url
+
+
+def get_reference_label(url: str) -> str:
+ """
+ Get the reference label that should accompany this URL in the link.
+ """
+ u = hyperlink.DecodedURL.from_text(url)
+
+ # If it's a package on PyPI, the link label is the project name.
+ #
+ # e.g. "https://pypi.org/project/eyed3/" ~> "eyed3"
+ if u.host == "pypi.org" and len(u.path) >= 2 and u.path[0] == "project":
+ return u.path[1]
+
+ # If we can't deduce a default link name, prompt the user for input.
+ cmd = [
+ "osascript",
+ "-e",
+ f"""
+ set theResponse to display dialog "The URL is {url}.\n\nWhat’s the link label?" default answer ""
+ get text returned of theResponse
+ """,
+ ]
+
+ label = subprocess.check_output(cmd).decode("utf8").strip()
+
+ return label
+
+
+if __name__ == "__main__":
+ url = get_safari_url()
+ url = normalise_url(url)
+
+ label = get_reference_label(url)
+
+ print(f"[{label}]\n\n[{label}]: {url}")