How to modify this script?

chaouche yacine yacinechaouche at yahoo.com
Wed Jan 9 05:23:08 EST 2013


I figrued it out. Copy/paste exactly these lines in the snippets tool. You can bind it to a key as you may know, I bound it to Ctrl-E. So paste it in a new snippet (keep the original in a safe place), bind to a key, select the text you want to html-tableize and hit the key binding. In my case it worked.

$<
def addline(line):
    return "<tr\>%s</tr\>\n" % line

def addcolumn(item,nb_columns):
    if nb_columns != 3:
        return "<td colspan='%s'\>%s</td\>" % (3 - nb_columns + 1, item)
    return "<td\>%s</td\>" % item

output = "<table\>\n"
for line in """$GEDIT_SELECTED_TEXT""".split("\n"):
    items = line.strip().split("\t")
    columns = ""
    for item in items :
        columns += addcolumn(item,len(items))
    output  += addline(columns)


output += "</table\>"
return output>


Here's a screenshit, sorry screenshot :) http://h.dropcanvas.com/521xc/gedit.png


The python support in gedit snippets is very poor when it comes to debugging because there are traceback printed in the console, that means gedit actually breaks without even noticing the user about what went wrong (ex. : your snippet is malformed or has errors). I had to debug it using pdb.set_trace directly inside its source code to figure out what was wrong in the snippet.

If this doesn't work for you, please let me know.





________________________________
 From: Kurt Hansen <kurt at ugyldig.invalid>
To: python-list at python.org 
Sent: Wednesday, January 9, 2013 10:07 AM
Subject: Re: How to modify this script?
 
Den 08/01/13 16.31, chaouche yacine skrev:
> Well tell me how do you use this script in gedit, are you using it as a
> plugin ?

"Snippets" is a plugin, yes. It's included in the .app for Mac (v. 2.30.2), but not activated af default.

Open "Tools" in the menu line and click "Manage snippets...". Here you can organize, add and edit snippets of texts. The feature olså has the ability to work with Python code inside the snippet content.

I am re-building a 15 years old homepage. The HTML code is handmade over the years and very varying, buggy etc., så I would like to renew the HTML for the table structure in an easy way.

Example: On this page: http://www.danacord.dk/frmsets/records/732-r.html I mark the content of the CD, copy it to the clipboard and paste it into the editing area in Gedit. cmd-a marks it all again and then I "run" the snippet upon the text, either using my self-defined hotkey or by pushing ctrl+space and select my snippet from a list.

The copied text is inserted as clean text without any HTML. The Python-snippet we are discussing recognizes tabs to separate the columns and adds the apprpriate HTML-code to it.
-- Regards
Kurt Hansen
-- http://mail.python.org/mailman/listinfo/python-list
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20130109/4d623ab2/attachment.html>


More information about the Python-list mailing list