четверг, 21 июня 2012 г.

Скрипт на Python Script для Notepad++

Для редактора Notepad++ есть плагин Python Script. Я решил попробовать написать пару скриптов.

Первый скрипт простой - создается новый файл формата HTML с уже заполненным  содержимым. Для вызова этого скрипта создал кнопку на панели инструментов.
# -*- coding: utf8 -*-
notepad.new()
editor.addText('<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">\n')
editor.addText('<html>\n')
editor.addText('<head>\n')
editor.addText('<meta http-equiv="Content-Type" content="text/html; charset=utf-8">\n')
editor.addText('<meta name="author" content="tkachenkosi@gmail.com">\n')
editor.addText('<link href="css/style.css" type="text/css" rel="stylesheet">\n')
editor.addText('<title>Документ</title>\n')
editor.addText('</head>\n')
editor.addText('<body>\n')
editor.addText('\n')
editor.addText('</body>\n')
editor.addText('</html>\n')
notepad.save()


Второй скрипт добавляет строчные комментарии, которые различаются в зависимости от типа файла (py, html, js, css). Скрипт вызывается по комбинации Ctrl+\ как в IDE Delphi. Повторный вызов скрипта удаляет комментарий в текущей строки. Скрипт получился не совершенным но я привык и пользуюсь регулярно.
# -*- coding: utf8 -*-
import os

text = editor.getCurLine()
pos = editor.getCurrentPos()
num = editor.lineFromPosition(pos)

text = text.rstrip()

x=os.path.basename(notepad.getCurrentFilename()).split('.')[1]

if x=='css':
    if text.find('/*')>-1:
        text = text.replace('/*','')
        text = text.replace('*/','')
    else:
        text = '/*' + text + ' */'
       
elif x=='py':
    if text.find('# ',0,4)>-1:
        text = text.replace('# ','',1)
    elif text.find('#',0,4)>-1:
        text = text.replace('#','',1)
    else:
        text = '# ' + text
   
    # if text[0]=='#':
        # text = text[1:]

elif x=='js':
    if text.find('//',0,8)>-1:
        text = text.replace('//','',1)
    else:
        text = '//' + text
       
elif x=='html':
    if text.find('<!--')>-1:
        text = text.replace('<!--','')
        text = text.replace('-->','')
    else:
        text = '<!--' + text + ' -->'
       
text = text.rstrip()
  
# editor.replaceLine(num,text)
editor.replaceWholeLine(num,text+'\n')
editor.gotoPos(pos)

Комментариев нет:

Отправить комментарий