Utente:Qualc1/microEditor templateR

Istruzioni d'uso modifica

È un micro editor che aiuta a inserire il template r in automatico. Per usarlo bisogna copia-incollare la poesia nell'editor, inserire nell'apposito campo ogni quante righe si vuole che venga inserito il template, e cliccare sul bottone inserisci. Vengono inseriti in automatico i template, quindi si può ri-copia-incollare la poesia nel wiki.


Istruzioni di installazione modifica

Funziona (o almeno dovrebbe funzionare) su windows, mac e linux; ma richiede l'installazione di python (v. 2.5) e wxPython (v. 2.6). Dopo aver installato il software richiesto bisogna salvare il codice del programmino in un file con estensione .py; così dovrebbe essere possibile eseguire il file salvato.


Futuro modifica

Cercherò di di aggiungere questa funzionalità negli script di IPork in modo che sia comoda da usare direttamente nel browser (niente copia-incolla da una parte all'altra)...


#!/usr/bin/env python
# -*- coding: utf-8 -*-
# generated by wxGlade 0.6.2 on Fri Feb 22 20:33:11 2008

import wx
import re

# begin wxGlade: extracode
# end wxGlade

global TEMPLATE_R
TEMPLATE_R = '{{R|%s}}'

class Editor(wx.Frame):
    def __init__(self, *args, **kwds):
        # begin wxGlade: Editor.__init__
        kwds["style"] = wx.DEFAULT_FRAME_STYLE
        wx.Frame.__init__(self, *args, **kwds)
        self.textPoesia = wx.TextCtrl(self, -1, "", style=wx.TE_MULTILINE|wx.HSCROLL)
        self.static_line_2 = wx.StaticLine(self, -1)
        self.buttonStrofe = wx.Button(self, -1, "Inserisci numeri strofe")
        self.static_line_1 = wx.StaticLine(self, -1)
        self.labelOgni = wx.StaticText(self, -1, "Ogni quante righe")
        self.textOgni = wx.TextCtrl(self, -1, "")
        self.buttonInserisci = wx.Button(self, -1, "Inserisci")

        self.__set_properties()
        self.__do_layout()

        self.Bind(wx.EVT_BUTTON, self.inserisciStrofe, self.buttonStrofe)
        self.Bind(wx.EVT_BUTTON, self.inserisciR, self.buttonInserisci)
        # end wxGlade

    def __set_properties(self):
        # begin wxGlade: Editor.__set_properties
        self.SetTitle("microEditor")
        self.SetSize((410, 340))
        self.textOgni.SetMinSize((30, 21))
        # end wxGlade

    def __do_layout(self):
        # begin wxGlade: Editor.__do_layout
        sizer_2 = wx.BoxSizer(wx.VERTICAL)
        sizer_3 = wx.BoxSizer(wx.VERTICAL)
        sizer_4 = wx.BoxSizer(wx.HORIZONTAL)
        sizer_1 = wx.BoxSizer(wx.VERTICAL)
        sizer_3.Add(self.textPoesia, 7, wx.EXPAND|wx.ADJUST_MINSIZE, 0)
        sizer_1.Add(self.static_line_2, 0, wx.TOP|wx.BOTTOM|wx.EXPAND, 4)
        sizer_1.Add(self.buttonStrofe, 0, wx.ALIGN_CENTER_HORIZONTAL|wx.ADJUST_MINSIZE, 0)
        sizer_1.Add(self.static_line_1, 0, wx.TOP|wx.BOTTOM|wx.EXPAND, 4)
        sizer_3.Add(sizer_1, 0, wx.EXPAND, 0)
        sizer_4.Add(self.labelOgni, 0, wx.LEFT|wx.RIGHT|wx.TOP|wx.ALIGN_CENTER_VERTICAL|wx.ADJUST_MINSIZE, 5)
        sizer_4.Add(self.textOgni, 0, wx.LEFT|wx.RIGHT|wx.TOP|wx.ALIGN_CENTER_VERTICAL|wx.ADJUST_MINSIZE, 5)
        sizer_4.Add(self.buttonInserisci, 0, wx.LEFT|wx.RIGHT|wx.TOP|wx.ALIGN_CENTER_VERTICAL|wx.ADJUST_MINSIZE, 5)
        sizer_3.Add(sizer_4, 0, wx.ALIGN_CENTER_HORIZONTAL|wx.ADJUST_MINSIZE, 0)
        sizer_2.Add(sizer_3, 1, wx.ALL|wx.EXPAND, 5)
        self.SetSizer(sizer_2)
        self.Layout()
        self.SetSize((410, 340))
        # end wxGlade

    def inserisciR(self, event): # wxGlade: Editor.<event_handler>
        print "Event handler `inserisciR' not implemented!"
        ogni = self.textOgni.GetLineText(0)
        try:
            ogni = int(ogni)
        except ValueError:
            print "voglio un numero" # TODO cercare come visualizzarlo in un dialog.
            wx.MessageDialog(self, 'Voglio un numero nel campo "Ogni quante righe"',
                        "Campo non valido", wx.OK).ShowModal()
        else:
            lines = self.getLineePoesia()
            lineNum = 0
            newText = ''
            for line in lines:
                if not re.match(r'^\s*$', line):
                    lineNum += 1
                    if (lineNum % ogni) == 0:
                        if line[0] == ':':
                            line = line[1:]
                            newText += ':'
                        newText += TEMPLATE_R % str(lineNum)
                newText += line + '\n'
            self.setPoesia(newText[:-1])
        event.Skip()

    def inserisciStrofe(self, event): # wxGlade: Editor.<event_handler>
        lines = self.getLineePoesia()
        strofaNum = 0
        newText = ''
        trovataLineaVuota = True
        for line in lines:
            if (not re.match(r'^\s*$', line)) and trovataLineaVuota:
                strofaNum += 1
                if line[0] == ':':
                    line = line[1:]
                    newText += ':'
                newText += TEMPLATE_R % str(strofaNum)
            trovataLineaVuota = (re.match(r'^\s*$', line))
            newText += line + '\n'
        self.setPoesia(newText[:-1])
        event.Skip()
    
    def getLineePoesia(self):
        """Restituisce una lista delle linee della poesia."""
        text = self.textPoesia.GetString(0, self.textPoesia.GetLastPosition())
        return text.split("\n")
    
    def setPoesia(self, poesia):
        """Setta il testo passato nella textArea."""
        self.textPoesia.Replace(0, self.textPoesia.GetLastPosition(), poesia)

# end of class Editor


if __name__ == "__main__":
    app = wx.PySimpleApp(0)
    wx.InitAllImageHandlers()
    frame_1 = Editor(None, -1, "")
    app.SetTopWindow(frame_1)
    frame_1.Show()
    app.MainLoop()