UrlToLink - butscher/WikidPad GitHub Wiki

This script converts one or more urls created by Wikidpad when dropping files from the local file system over the editor to the format [url| name]

''Example''

dragged url: !rel:Somedir/url%20dragged%20to%20the%20editor.txt

generated link: [!rel:Somedir/url%20dragged%20to%20the%20editor.txt |url dragged to the editor]

Usage:

Select the url/urls to convert and press Ctrl-1

Install:

See here: HowToInstallGlobalScripts

<% 1:
# Urls to links for Wikidpad
# This scripts converts one or more urls created by Wikidpad when dropping
# files from the local file system over the editor to the format [url| name]
#
# Example
#
# dragged url: 
# rel://Workdir/url%2Bdragged%20to%20the%20editor.txt
#
# generated link: 
# [rel://Workdir/url%2Bdragged%20to%20the%20editor.txt |url+dragged to the editor]
#
# Use:
# Select the url/urls to convert and press Ctrl-1


from urllib import unquote_plus
import re

SelectedText = editor.GetSelectedText()	

#Trim spaces
SelectedText = SelectedText.strip()

# Every url is followed by a space, so we split them at the space position and assign them to a list
urls = SelectedText.rsplit(' ')


# Strip the path up to the last slash (/) and save the file names in a string separated by spaces
names = ""

for url in urls:
	names = names + re.sub(r'^[A-Za-z0-9/!#$%\'().-_]*\/', "", url) + " "
	
#remove trailing space and convert the string into a list
names = names.rstrip()
names = names.rsplit(' ')

#Format the name part of the link
links = ""

for id in range(len(names)):
	names[id] = unquote_plus(names[id])  # decode file name
	names[id] = re.sub(r'_', " ", names[id]) # replace underscores (_) with spaces
	
	#If file name has an extension, remove it
	match = re.search(r'\.[A-Za-z0-9]*$', names[id])
	if match:
		names[id] = re.sub(r'\.[A-Za-z0-9]*$', "", names[id])
	
	names[id] = names[id] + "]\n" # add new line (\n) so links will be each on its own line

#Format the url part of the link
for id in range(len(urls)):
	urls[id] = "[" + urls[id] + " |"
	
	#Join urls + names according to link syntax: [url |name]
	links = links + urls[id] + names[id]

editor.ReplaceSelection(links)
%>
⚠️ **GitHub.com Fallback** ⚠️