How to modify this script?

chaouche yacine yacinechaouche at
Wed Jan 9 11:23:08 CET 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 :)

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 
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: 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
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <>

More information about the Python-list mailing list