Sublime Forum

Replace all href by base64

#1

hi, i’m trying to build a plugin that edit all href in the current file, and replace them with base64 of them content.
But the plugin work only for the first href.
I really don’t know why, here is the code :

import sublime
import sublime_plugin
from base64 import b64encode
import re

class HrefToB64Command(sublime_plugin.TextCommand):
	def run(self, edit):
		# all the href in the file
		hrefRegions = self.view.find_all('href="([^"]*)"')

		# once we get all the regions
		# we loop on them
		for href in hrefRegions:
			# the full text (href="...")
			text = self.view.substr(href)

			# the link in the text
			link = re.match('href="([^"]*)"', text).group(1)

			# the link base64 encoded
			link = str(b64encode(bytes(link, 'utf-8')), 'utf-8')
			print(link)

			# the final href encoded
			finalHref = 'href="'+link+'"'

			# the replace
			self.view.replace(edit, href, finalHref)
0 Likes

#2

probably you should replace the regions in the opposite (i.e. reverse) order, otherwise the regions get messed up when your replacement string is a different size to what it is replacing

2 Likes

#3

it works !!! thank you !!!

0 Likes