Browse files

New: option to remove attributes from tags on document or selection(s…

…). + Some stylistic tweaks.
  • Loading branch information...
1 parent c13a1fd commit 07a0cfc23cd19af5d4221971d6cf9ac92cc497fb @titoBouzout committed Feb 9, 2012
View
70 Main.sublime-menu
@@ -1,4 +1,74 @@
[
+{
+ "caption": "Edit",
+ "mnemonic": "E",
+ "id": "edit",
+ "children":
+ [
+
+ {
+ "caption": "Tag",
+ "id": "tag",
+ "children":
+ [
+ {"caption":"-"},
+ {
+ "command": "tag_indent",
+ "id":"tag-indent-selection",
+ "caption":"Indent Tags on Selection"
+ },
+ {
+ "command": "tag_indent_document",
+ "id":"tag-indent-document",
+ "caption":"Indent Tags on Document"
+ },
+ {"caption":"-"},
+ {
+ "command": "tag_remove_picked_in_selection",
+ "id":"tag-remove-picked-in-selection",
+ "caption":"Remove Picked Tags in Selection"
+ },
+ {
+ "command": "tag_remove_picked_in_document",
+ "id":"tag-remove-picked-in-document",
+ "caption":"Remove Picked Tags in Document"
+ },
+ {
+ "command": "tag_remove_all_in_selection",
+ "id":"tag-remove-all-in-selection",
+ "caption":"Remove All Tags in Selection"
+ },
+ {
+ "command": "tag_remove_all_in_document",
+ "id":"tag-remove-all-in-document",
+ "caption":"Remove All Tags in Document"
+ },
+ {"caption":"-"},
+ {
+ "command": "tag_remove_picked_attributes_in_selection",
+ "id":"tag-remove-picked-attributes-in-selection",
+ "caption":"Remove Picked Attributes From Tags in Selection"
+ },
+ {
+ "command": "tag_remove_picked_attributes_in_document",
+ "id":"tag-remove-picked-attributes-in-document",
+ "caption":"Remove Picked Attributes From Tags in Document"
+ },
+ {
+ "command": "tag_remove_all_attributes_in_selection",
+ "id":"tag-remove-all-attributes-in-selection",
+ "caption":"Remove All Attributes From Tags in Selection"
+ },
+ {
+ "command": "tag_remove_all_attributes_in_document",
+ "id":"tag-remove-all-attributes-in-document",
+ "caption":"Remove All Attributes From Tags in Document"
+ },
+ {"caption":"-"}
+ ]
+ }
+ ]
+ },
{
"caption": "Preferences",
"mnemonic": "n",
View
4 Tag Indent/Commands.sublime-commands
@@ -1,10 +1,10 @@
[
{
- "caption": "HTML: Indent Tags on Selection",
+ "caption": "Tag: Indent Tags on Selection",
"command": "tag_indent"
},
{
- "caption": "HTML: Indent Tags on Document",
+ "caption": "Tag: Indent Tags on Document",
"command": "tag_indent_document"
}
]
View
30 Tag Indent/Main.sublime-menu
@@ -1,30 +0,0 @@
-[
- {
- "caption": "Edit",
- "mnemonic": "E",
- "id": "edit",
- "children":
- [
-
- {
- "caption": "Tag",
- "id": "tag",
- "children":
- [
- {"caption":"-"},
- {
- "command": "tag_indent",
- "id":"tag-indent-selection",
- "caption":"Indent Tags on Selection"
- },
- {
- "command": "tag_indent_document",
- "id":"tag-indent-document",
- "caption":"Indent Tags on Document"
- },
- {"caption":"-"}
- ]
- }
- ]
- }
-]
View
18 Tag Remove Attributes/Commands.sublime-commands
@@ -0,0 +1,18 @@
+[
+ {
+ "caption": "Tag: Remove Picked Attributes From Tags in Selection",
+ "command": "tag_remove_picked_attributes_in_selection"
+ },
+ {
+ "caption": "Tag: Remove Picked Attributes From Tags in Document",
+ "command": "tag_remove_picked_attributes_in_document"
+ },
+ {
+ "caption": "Tag: Remove All Attributes From Tags in Selection",
+ "command": "tag_remove_all_attributes_in_selection"
+ },
+ {
+ "caption": "Tag: Remove All Attributes From Tags in Document",
+ "command": "tag_remove_all_attributes_in_document"
+ }
+]
View
8 Tag Remove/Commands.sublime-commands
@@ -1,18 +1,18 @@
[
{
- "caption": "Remove Picked Tags in Selection",
+ "caption": "Tag: Remove Picked Tags in Selection",
"command": "tag_remove_picked_in_selection"
},
{
- "caption": "Remove Picked Tags in Document",
+ "caption": "Tag: Remove Picked Tags in Document",
"command": "tag_remove_picked_in_document"
},
{
- "caption": "Remove All Tags in Selection",
+ "caption": "Tag: Remove All Tags in Selection",
"command": "tag_remove_all_in_selection"
},
{
- "caption": "Remove All Tags in Document",
+ "caption": "Tag: Remove All Tags in Document",
"command": "tag_remove_all_in_document"
}
]
View
40 Tag Remove/Main.sublime-menu
@@ -1,40 +0,0 @@
-[
- {
- "caption": "Edit",
- "mnemonic": "E",
- "id": "edit",
- "children":
- [
-
- {
- "caption": "Tag",
- "id": "tag",
- "children":
- [
- {"caption":"-"},
- {
- "command": "tag_remove_picked_in_selection",
- "id":"tag-remove-picked-in-selection",
- "caption":"Remove Picked Tags in Selection"
- },
- {
- "command": "tag_remove_picked_in_document",
- "id":"tag-remove-picked-in-document",
- "caption":"Remove Picked Tags in Document"
- },
- {
- "command": "tag_remove_all_in_selection",
- "id":"tag-remove-all-in-selection",
- "caption":"Remove All Tags in Selection"
- },
- {
- "command": "tag_remove_all_in_document",
- "id":"tag-remove-all-in-document",
- "caption":"Remove All Tags in Document"
- },
- {"caption":"-"}
- ]
- }
- ]
- }
-]
View
59 tag_remove_attributes.py
@@ -0,0 +1,59 @@
+import sublime, sublime_plugin
+import re
+
+def TagRemoveAttributesClean(data):
+ regexp = re.compile(r'(<([a-z0-9-]+)\s+>)');
+ data = regexp.sub('<\\2>', data);
+ return data
+
+def TagRemoveAttributesAll(data, view):
+ return TagRemoveAttributesClean(re.sub(r'(<([a-z0-9-]+)\s+[^>]+>)', '<\\2>', data));
+
+def TagRemoveAttributesSelected(data, attributes, view):
+ attributes = attributes.replace(',', ' ').replace(';', ' ').replace('|', ' ')+' '
+ for attribute in attributes.split(' '):
+ if attribute:
+ regexp = re.compile(r'(<([a-z0-9-]+\s+)([^>]*)\s*'+re.escape(attribute)+'="[^"]+"\s*([^>]*)>)')
+ data = regexp.sub('<\\2\\3\\4>', data);
+ data = TagRemoveAttributesClean(data);
+ return data;
+
+class TagRemoveAllAttributesInSelectionCommand(sublime_plugin.TextCommand):
+ def run(self, edit):
+ for region in self.view.sel():
+ if region.empty():
+ continue
+ dataRegion = sublime.Region(region.begin(), region.end())
+ data = TagRemoveAttributesAll(self.view.substr(dataRegion), self.view)
+ self.view.replace(edit, dataRegion, data);
+
+class TagRemoveAllAttributesInDocumentCommand(sublime_plugin.TextCommand):
+ def run(self, edit):
+ dataRegion = sublime.Region(0, self.view.size())
+ data = TagRemoveAttributesAll(self.view.substr(dataRegion), self.view)
+ self.view.replace(edit, dataRegion, data);
+
+class TagRemovePickedAttributesInSelectionCommand(sublime_plugin.TextCommand):
+ def run(self, edit):
+ import functools
+ self.view.window().run_command('hide_panel');
+ self.view.window().show_input_panel("Remove the following attributes:", '', functools.partial(self.on_done, edit), None, None)
+
+ def on_done(self, edit, attributes):
+ for region in self.view.sel():
+ if region.empty():
+ continue
+ dataRegion = sublime.Region(region.begin(), region.end())
+ data = TagRemoveAttributesSelected(self.view.substr(dataRegion), attributes, self.view)
+ self.view.replace(edit, dataRegion, data);
+
+class TagRemovePickedAttributesInDocumentCommand(sublime_plugin.TextCommand):
+ def run(self, edit):
+ import functools
+ self.view.window().run_command('hide_panel');
+ self.view.window().show_input_panel("Remove the following attributes:", '', functools.partial(self.on_done, edit), None, None)
+
+ def on_done(self, edit, attributes):
+ dataRegion = sublime.Region(0, self.view.size())
+ data = TagRemoveAttributesSelected(self.view.substr(dataRegion), attributes, self.view)
+ self.view.replace(edit, dataRegion, data);

0 comments on commit 07a0cfc

Please sign in to comment.