[Vim-l] Template:Help - Simple fixes

John Beckett johnb.beckett at gmail.com
Tue Jun 26 11:57:33 UTC 2007


D'oh! I asked for you to post your help-dev script without
noticing that it was already attached.

Please see what I have attached for my first Python attempt.

I have proposed a few changes. If you use this, please remove my
comments at the top - they are just to record my thoughts.

There is some confusion about urlencode.

Here is what I think:
 A VimTip should urlencode the components of the URL.
 So the help tag should be urlencoded.
 Therefore the CGI script has to urlDECODE the tag.

Consider the example ':help <tab>'.

Template:Help should create link:
  http://vimplugin.sourceforge.net/cgi-bin/help?tag=%3Ctab%3E

The CGI script would calculate:
  rawtag = form["tag"].value          # "%3Ctab%3E"
  tag = urllib.unquote_plus(rawtag)   # "<tab>"

The CGI script would redirect to:
  http://vimdoc.sourceforge.net/htmldoc/motion.html#<tab>

John
-------------- next part --------------
#!/usr/bin/python
# JB untested code.
# We want to urlDECODE tag (because the URL components
# we receive should be urlencoded - I think).
# This version avoids the unnecessary overhead of dict().
# TODO: What does 'tag.lower' do if 'tag' is numeric?

import sys
import cgi
import os
import urllib

#global variables
#TODO: tags-file needs to be updated from time to time?
tagsfile="./tags"
baseurl="http://vimdoc.sourceforge.net/htmldoc/"
ext=".html"

#defaults to false
urlencode = False

if __name__ == '__main__':
    form = cgi.FieldStorage()

    #if urlencode parameter is 1, the tag is encoded (we must decode)
    if form.has_key("urlencode"):
        urlencode = form["urlencode"]==1

    if form.has_key("tag") and form["tag"].value != "":
        tag = form["tag"].value
        if urlencode:
            tag = urllib.unquote_plus(tag)

        #read tags file
        if not os.path.exists(tagsfile):
            print "Content-type: text/html\n"
            print "<h1>Error! Tagfile not found.</h1>"
            #TODO: exit function.
        f = open(tagsfile)
        try:
           gotit = False
           for line in f:
               a,b,c = line.split()
               if a.lower == tag.lower:
                   gotit = True
                   break
        finally:
           f.close()

        #do the actual redirect or print error
        if gotit:
            print "Status: 302 Found"
            print 'Location: '+baseurl+b.replace('.txt',ext)+'#'+tag
        else:
            print "Content-type: text/html\n"
            print "<h1>Error! Tag \"%s\" not found in help!</h1>"%tag

    else:
        print "Content-type: text/html\n"
        print "<h1>Error! No tag given!</h1>"




More information about the Vim-l mailing list