- I needed something to clean my HTML content when using in public/AI sources
- I initially tried Sublime Text's "macros" but they apparently can't do search/replace which is insane (imo)
- Finally got a Sublime Text "plugin" version working - it took bit of time to hash out because all online sources (including/especially AI) were wrong and gave back/non-functioning code.
- Sharing in case others find useful.
The command in the py file * * MUST * * be exactly camel case like this or it will silently fail and never show up in the cmd palette: If the file name is my_cool_plugin.py the PY class def should be like: MyCoolPluginCommand. So: class MyCoolPluginCommand(sublime_plugin...):
Also there can be just one Default.sublime-commands file with all your commands in it you dont need a sublime-commands file for each plugin/command. Example:
[
{"caption": "HTML Cleaner: Clean HTML","command": "html_cleaner"},
{"caption": "SQL Cleaner: Clean SQL","command": "sql_cleaner" },
]
Working in Sublime Text Build 4180 (9/4/2024)
FILE/PATH: C:\Users\##USERNAME##\AppData\Roaming\Sublime Text 3\Packages\HTMLCleaner\html_cleaner.py
import sublime
import sublime_plugin
class HtmlCleanerCommand(sublime_plugin.TextCommand):
def run(self, edit):
# operation: Replace content between tags with "lorem ipsum"
regions = self.view.find_all(r"(?<=>)[^<>\t\r\n]+?(?=<)")
for region in reversed(regions):
self.view.replace(edit, region, "lorem ipsum")
# operation: Remove all comments
regions = self.view.find_all(r"<!--.*?-->")
for region in reversed(regions):
self.view.replace(edit, region, "")
# operation: Replace "mydomain.com" with "acme.com"
regions = self.view.find_all("mydomain\\.com")
for region in reversed(regions):
self.view.replace(edit, region, "acme.com")
# operation: Replace "mycompany" with "acme"
regions = self.view.find_all("mycompany")
for region in reversed(regions):
self.view.replace(edit, region, "acme")
# operation: Replace "MYCOMPANY" with "ACME"
regions = self.view.find_all("MYCOMPANY")
for region in reversed(regions):
self.view.replace(edit, region, "ACME")
# operation: Replace "ORANGES" with "PRODUCT1"
regions = self.view.find_all("ORANGES")
for region in reversed(regions):
self.view.replace(edit, region, "PRODUCT1")
# operation: Replace "APPLES" with "PRODUCT2"
regions = self.view.find_all("APPLES")
for region in reversed(regions):
self.view.replace(edit, region, "PRODUCT2")
# operation: Replace "apples" with "product2"
regions = self.view.find_all("apples")
for region in reversed(regions):
self.view.replace(edit, region, "product2")
# operation: Replace "subdomain1" with "sbd1"
regions = self.view.find_all("subdomain1")
for region in reversed(regions):
self.view.replace(edit, region, "sbd1")
# operation: Replace "otherword" with "owrd"
regions = self.view.find_all("otherword")
for region in reversed(regions):
self.view.replace(edit, region, "owrd")
# operation: Replace "CeoName" with "Person1"
regions = self.view.find_all("CeoName")
for region in reversed(regions):
self.view.replace(edit, region, "Person1")
# Inform the user that the operation is complete
sublime.status_message("HTML Cleaner: Replacements completed.")
class HtmlCleanerEventListener(sublime_plugin.EventListener):
def on_post_save_async(self, view):
# Automatically run the HTML Cleaner command after saving an HTML file
if view.file_name().endswith(".html"):
view.run_command("html_cleaner")
# FILE 2: C:\Users\##USERNAME##\AppData\Roaming\Sublime Text 3\Packages\User\HTML Cleaner.sublime-commands
[
{
"caption": "HTML Cleaner: Clean HTML",
"command": "html_cleaner"
}
]