#1
  1. Mini me.
    Devshed Novice (500 - 999 posts)

    Join Date
    Nov 2003
    Location
    Cambridge, UK
    Posts
    783
    Rep Power
    13

    Markup CGI - please test


    In quiet moments I've been working on a python CGI script to do code markup. If you are interested enough to test it, here it is:
    Markup CGI
    Key features are:
    1. User control over format and colour
    2. Line numbering.
    3. Cookies to remember settings.

    Here's a sample:
    Code:
    [001] #Replace this code with your own
    [002] 
    [003] def Hello(name="World"):
    [004]     """A simple function"""
    [005]     print "Hello "+name #Classic
    [006]     a =1+1
    [007]     b=(6+7)-2
    [008]     if a < b :
    [009]         print "a=%s, b=%s"%(a,b)
    [010] 
    [011] Hello("you")
    [012] Hello()
    [013] Hello("The Universe")
    [014] 
    [015]
    You don't have to like my colours

    grim
    Last edited by Grim Archon; October 17th, 2004 at 08:22 AM. Reason: numbering changed
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2002
    Posts
    89
    Rep Power
    13
    that's really nice but I did get this error:

    Code:
    KeyError	Python 2.2.3: /usr/bin/python
    Fri Oct 15 10:27:32 2004
    
    A problem occurred in a Python script. Here is the sequence of function calls leading up to the error, in the order they occurred.
    
     /home/pharsc2/public_html/cgi-bin/syntax.py
      622                 oblock= cgi.escape(makeBlock(data),True)
      623                 #oblock=oblock%items
      624                 print html1%items,
      625                 fname = str(time.time()+random.random())
      626                 tf = file(os.sep+"tmp"+os.sep+fname,"w")
    html1 = '\n <head>\n <meta http-equiv="Conten...tle>\n </head>\n <body>\n <hr>\n ', items = {'com': 'Red', 'entity': '1', 'key': 'BLACK', 'markup': 'vbmarkup', 'num': 'BLACK', 'op': 'BLACK', 'reformat': '1', 'str': 'Green'}
    
    KeyError: res
          __doc__ = 'Mapping key not found.'
          __getitem__ = <bound method KeyError.__getitem__ of <exceptions.KeyError instance>>
          __init__ = <bound method KeyError.__init__ of <exceptions.KeyError instance>>
          __module__ = 'exceptions'
          __str__ = <bound method KeyError.__str__ of <exceptions.KeyError instance>>
          args = ('res',)
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2002
    Posts
    89
    Rep Power
    13
    line 624 ^^^
  6. #4
  7. Mini me.
    Devshed Novice (500 - 999 posts)

    Join Date
    Nov 2003
    Location
    Cambridge, UK
    Posts
    783
    Rep Power
    13
    Weird, learning something about cookies - should have realised it is stateless and you can't guarantee that if one is received then they are all received.

    Hopefully, now fixed with an update to a default dictionary.

    grim
  8. #5
  9. Hello World :)
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2003
    Location
    Hull, UK
    Posts
    2,537
    Rep Power
    69
    Tested it with normal Python code and code copied from the shell and it works perfectly . If you havn't posted a link to this in the tips and tricks thread do it already .

    Keep it up Paul,

    Mark.
    programming language development: www.netytan.com Hula

  10. #6
  11. Mini me.
    Devshed Novice (500 - 999 posts)

    Join Date
    Nov 2003
    Location
    Cambridge, UK
    Posts
    783
    Rep Power
    13
    Mark,
    Thanks for that

    I think I'll allow a little more testing before suggesting it for everyday use tho - I just changed it so line numbers start from a more logical 1.

    I haven't used cookies directly before so the bug that jimmy2k1 reported is a bit of a funny to me. I patched it by having defaults that get updated by whatever cookies are returned but I'm not happy with that. Anyone have any ideas?

    I might change it so that one cookie has all settings which is probably tidier anyway.

    grim
  12. #7
  13. Banned ;)
    Devshed Supreme Being (6500+ posts)

    Join Date
    Nov 2001
    Location
    Woodland Hills, Los Angeles County, California, USA
    Posts
    9,607
    Rep Power
    4247
    Nice stuff . One error I noticed was, when I enter something like this:
    Code:
    #!/usr/bin/env python
    x = "foo" + "bar" + "baz"
    the output is colourized, but the spaces between the + symbols are removed in the output.
    Code:
    #!/usr/bin/env python
    x = "foo"+"bar"+"baz"
    Also, your VBcode output has a space before the #!/usr/bin/env python line
    Up the Irons
    What Would Jimi Do? Smash amps. Burn guitar. Take the groupies home.
    "Death Before Dishonour, my Friends!!" - Bruce D ickinson, Iron Maiden Aug 20, 2005 @ OzzFest
    Down with Sharon Osbourne

    "I wouldn't hire a butcher to fix my car. I also wouldn't hire a marketing firm to build my website." - Nilpo
  14. #8
  15. Hello World :)
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2003
    Location
    Hull, UK
    Posts
    2,537
    Rep Power
    69
    You can fix the first example by selecting "No" in the reformat section . Not sure about the second though.
    programming language development: www.netytan.com Hula

  16. #9
  17. Mini me.
    Devshed Novice (500 - 999 posts)

    Join Date
    Nov 2003
    Location
    Cambridge, UK
    Posts
    783
    Rep Power
    13
    Yep Scorp, you got bit by formatting

    Reformat: No - means (hopefully) don't make any change to layout.

    Reformat: Style 1 Tries to impose some PEP 8 style guidelines to the code. This means ignoring spaces/tabs and putting them back in where guidelines suggest they should go. The rules are fairly simplistic but try to make the code layout consistent. (Looks like I need to modify the comment handling slightly for when it's at the beginning of a line.) I put it in because my code gets a little sloppy after editing and I wanted something to tidy it up. 9/10 times it does okay.

    Reformat Style 2 Is an alternative that some may prefer. (suck it an see)

    The mark-up decisions are made by the tokenizer module so it should be compatible with IDLE and other packages that use it.
    This might lead to funny colouration when marking up none-python code but seems to work for C/C++ okay as a lot of the reserved words are common.

    grim
  18. #10
  19. Mini me.
    Devshed Novice (500 - 999 posts)

    Join Date
    Nov 2003
    Location
    Cambridge, UK
    Posts
    783
    Rep Power
    13
    Fixed spurious space insertion when comment was in column 0 and Style 1 is used.

IMN logo majestic logo threadwatch logo seochat tools logo