"QuoteUtils" by NickDMax

Rating: (0 votes)

Report this Script

Name: QuoteUtils
Uploaded By: NickDMax
Date Created: May 4, 2009 3:13 p.m.
Date Modified: May 4, 2009 3:13 p.m.
Description: Utilities for dealing with quotation marks in programming languages. Switch between single and double quotes. Escape quotation marks.
Num Views: 663
Num Downloads: 278

Contents

Download this script

###############################################################################
## QuoteTools.py scripts -- Provides some quick access to regex operations on
## selected text. Specifically dealing with quotation marks.
## By: NickDMax

import pn
import scintilla
from pypn.decorators import script

@script("Escape quotes", "Text")
def EscapeQuotes():
    """ Escape quotation marks within current selection """
    doc = pn.CurrentDoc()
    if doc is not None:     #Lets try not to crash pn too often...
        editor = scintilla.Scintilla(doc)
        start = editor.SelectionStart
        editor.BeginUndoAction()
        editor.ReplaceAllInRange("\"", "\\\"", editor.SelectionStart, editor.SelectionEnd)
        editor.SetSel(start, start)
        editor.EndUndoAction() 
    
@script("UnEscape quotes", "Text")
def UnEscapeQuotes():
    """ Unescape quotation marks within current selection """
    doc = pn.CurrentDoc()
    if doc is not None:     #Lets try not to crash pn too often...
        editor = scintilla.Scintilla(doc)
        start = editor.SelectionStart
        editor.BeginUndoAction()
        editor.ReplaceAllInRange("\\\"", "\"", editor.SelectionStart, editor.SelectionEnd)
        editor.SetSel(start, start)
        editor.EndUndoAction()

@script("Single2Double", "Text")
def Single2Double():
    """ Converts single quotation marks to doubles within current selection """
    doc = pn.CurrentDoc()
    if doc is not None:     #Lets try not to crash pn too often...
        editor = scintilla.Scintilla(doc)
        start = editor.SelectionStart
        editor.BeginUndoAction()
        editor.ReplaceAllInRange("\'", "\"", editor.SelectionStart, editor.SelectionEnd)
        editor.SetSel(start, start)
        editor.EndUndoAction()

@script("Double2Single", "Text")
def Double2Single():
    """ Converts double quotation marks to singles within current selection """
    doc = pn.CurrentDoc()
    if doc is not None:     #Lets try not to crash pn too often...
        editor = scintilla.Scintilla(doc)
        start = editor.SelectionStart
        editor.BeginUndoAction()
        editor.ReplaceAllInRange("\"", "\'", editor.SelectionStart, editor.SelectionEnd)
        editor.SetSel(start, start)
        editor.EndUndoAction()

@script("XML Escape", "Text")
def XMLEscape():
    """ Preforms XML escapes for the current selection """
    doc = pn.CurrentDoc()
    if doc is not None:     #Lets try not to crash pn too often...
        editor = scintilla.Scintilla(doc)
        start = editor.SelectionStart
        editor.BeginUndoAction()
        editor.ReplaceAllInRange("&", "&", editor.SelectionStart, editor.SelectionEnd)
        editor.ReplaceAllInRange("\"", """, editor.SelectionStart, editor.SelectionEnd)
        editor.ReplaceAllInRange("\'", "'", editor.SelectionStart, editor.SelectionEnd)
        editor.ReplaceAllInRange("<", "&lt;", editor.SelectionStart, editor.SelectionEnd)
        editor.ReplaceAllInRange(">", "&gt;", editor.SelectionStart, editor.SelectionEnd)
        editor.SetSel(start, start)
        editor.EndUndoAction()

@script("XML UnEscape", "Text")
def UNXMLEscape():
    """ Unescape XML sequences in the current selection """
    doc = pn.CurrentDoc()
    if doc is not None:     #Lets try not to crash pn too often...
        editor = scintilla.Scintilla(doc)
        start = editor.SelectionStart
        editor.BeginUndoAction()
        editor.ReplaceAllInRange("&quot;", "\"", editor.SelectionStart, editor.SelectionEnd)
        editor.ReplaceAllInRange("&apos;", "\'", editor.SelectionStart, editor.SelectionEnd)
        editor.ReplaceAllInRange("&lt;", "<", editor.SelectionStart, editor.SelectionEnd)
        editor.ReplaceAllInRange("&gt;", ">", editor.SelectionStart, editor.SelectionEnd)
        editor.ReplaceAllInRange("&amp;", "&", editor.SelectionStart, editor.SelectionEnd)
        editor.SetSel(start, start)
        editor.EndUndoAction()

Comments: