Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save cemerson/01e3a57436e796187e2602c16de4e344 to your computer and use it in GitHub Desktop.
Save cemerson/01e3a57436e796187e2602c16de4e344 to your computer and use it in GitHub Desktop.
Sublime Text: Obfuscate/Mangle Private HTML Content (User Plugin)

Summary

  • 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.

Important

Camel Case Command

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...):

Sublime-commands file

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 1: Main Plugin Code

Location:

FILE/PATH: C:\Users\##USERNAME##\AppData\Roaming\Sublime Text 3\Packages\HTMLCleaner\html_cleaner.py

Code:

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: Command Palette

Location:

# FILE 2: C:\Users\##USERNAME##\AppData\Roaming\Sublime Text 3\Packages\User\HTML Cleaner.sublime-commands

Code:

[
    {
        "caption": "HTML Cleaner: Clean HTML",
        "command": "html_cleaner"
    }
]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment