Insert Tag - surroung selection with HTML tag

vptn
2012-11-30
2013-01-25
  • vptn
    vptn
    2012-11-30

    Similar to "Insert HTML/XML tag" in Notepad2-mod.
    1. Asks user for tag with attributes.
    2. Surrounds current selection with tag provided by user.
    3. Works without selection.
    4. Knows tags without closing pair.

    def openingTag(tagStr):
        return "<" + tagStr + ">"
    def closingTag(tagStr):
        pureTag = tagStr.partition(" ")[0]
        unclosedTags = ["br", "input", "img", "link", "hr", "area",
                        "frame", "wbr", "meta", "base", "col",
                        "isindex", "param", "basefont"]
        if pureTag in unclosedTags:
            return ""
        return "</" + pureTag + ">"
    tagStr = notepad.prompt("Tag with attributes:", "Insert HTML/XML tag")
    if not tagStr is None:
        editor.beginUndoAction()
        
        selStr = editor.getSelText()
        editor.replaceSel(openingTag(tagStr) + selStr + closingTag(tagStr))
        
        editor.endUndoAction()