File: py2htmldoc.py

package info (click to toggle)
zope-groupuserfolder 3.1.1-3
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 1,416 kB
  • ctags: 1,037
  • sloc: python: 6,755; sh: 1,365; makefile: 147
file content (991 lines) | stat: -rwxr-xr-x 38,556 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
#!/usr/local/bin/python -O

"""
    Automatic Python Documentation in HTML

    This tool will parse all files in a given directory and build an
    internal object structure closely resembling the code structure
    found in the files.

    Using this internal representation, the obejcts are then called
    to produce a readable output -- currently only HTML is supported.

    This module should probably make use of the standard module
    parser, I but didn't have the time back when I originally wrote
    this, to learn all about Pythons grammar and ast-trees. The
    regexp's I used work nice for most situations, though dynamically
    defined code (like code in if...else...-clauses) is not parsed.

    The doc-strings are processed by the doc_string-class and may
    contain special character sequences to enhance the output. Look
    at the as_HTML-method of that class to find out more about
    it's features.

    Caveats:

    - it will only work for doc-strings enclosed in triple double-quotes
      that appear /balanced/ in the source code (use \"\"\" if you have to
      include single occurences)
    - since the doc-strings are written more or less directly into
      the HTML-file you have to be careful about using <, > and &
      in them, since these could lead to unwanted results, e.g.
      like in 'if a<c then: print a>b'; writing 'if a < c then: print a > b'
      causes no problem; _note:_ this is a feature so you can use
      normal HTML-tags in your doc-strings; use the #-trick explained
      in the doc_string-class instead !
      [Maybe I should reverse this feature: make normal text the default
       and HTML the exception that needs escaping -- don't use any HTML
       in my current doc-strings anyway]
    - code could be made faster by using string.join and %s... oh well.
    - doc string highlighting isn't done nicely (but works fine for my code :-)
    - tuples in function/method declarations can get this little tool
      pretty confused...
    - this code is full of circular references; but then,
      I normally only use it as script -- not as module to other programs

    Notes:

    - you might want to take a look at gendoc and HTMLgen for doing
      a more elaborate job (see: www.python.org for more infos)
    - to get colorized HTML versions of yyour Python scripts have a look
      at py2html.py (downloadable from my Python pages on starship)

    History:

    - 0.5: minor fixes to the regexps (thanks to Tim Peters)
    - 0.6: fixed a buglet in rx_bodyindent[2] that sneaked
           in from 0.4 to 0.5 (thanks to Dinu Gherman) and
           added a few more /human/ formats :-)
    - 0.7: changed the parts-regexp to not break code at comments
           (this sometimes cut off some methods from classes)
    - 0.8: fixed bug that cut away first character from bullets;
           added __version__ parsing and source code highlighting
           provisions
    - 0.9: Added a fix to support spaces between class/function/method
           names and the parameter list (courtesy of Keith Davidson);
           added 'o' as list item
    - 0.9.1: Added sorting of methods, classes,
             functions; added special parameter formatting: an empty line
             truncates the remaining parameters from the docs

    The latest version is always available from my Python pages:

           http://starship.python.net/~lemburg/



-----------------------------------------------------------------------------
(c) Copyright by Marc-Andre Lemburg (mailto:mal@lemburg.com)

    Permission to use, copy, modify, and distribute this software and its
    documentation for any purpose and without fee or royalty is hereby granted,
    provided that the above copyright notice appear in all copies and that
    both that copyright notice and this permission notice appear in
    supporting documentation or portions thereof, including modifications,
    that you make.

    THE AUTHOR MARC-ANDRE LEMBURG DISCLAIMS ALL WARRANTIES WITH REGARD TO
    THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
    FITNESS, IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL,
    INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING
    FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT,
    NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION
    WITH THE USE OR PERFORMANCE OF THIS SOFTWARE !

"""

__version__  = '0.9.1'
__package_info__ = """
BEGIN PYTHON-PACKAGE-INFO 1.0
Title:                  Automatic Python Documentation in HTML
Current-Version:        0.9.1
Home-Page:              http://starship.python.net/~lemburg
Primary-Site:           http://starship.python.net/~lemburg/doc.py

This tool will parse all files in a given directory and build an
internal object structure closely resembling the code structure
found in the files.

Using this internal representation, the objects are then called
to produce a readable output -- currently only HTML is supported.
END PYTHON-PACKAGE-INFO
"""

import sys,os,regex,string,regsub,time

# list markers
list_markers = '\*+-o'

# reg. expressions used
rx_class = regex.compile('\( *\)class +\([^:( ]+\) *\((\([^)]*\))\)? *:')
rx_function = regex.compile('\( *\)def +\([^( ]+\) *'
                            '(\(\([^()]\|([^)]*)\)*\)) *:')
rx_method = rx_function
rx_bodyindent = regex.compile('^\( *\)[^ \n]')
rx_bodyindent_2 = regex.compile('\( *\)[^ \n]')
rx_version = regex.compile(' *__version__ *= *[\'\"]\([^\'\"]+\)[\'\"]')
# cache some common parts regexps
rx_parts = {}
for indent in range(0,17,4):
    si = indent * ' '
    rs = '^'+si+'def \|^'+si+'class \|^'+si+'\"\"\"\|^'+ \
         si+'import \|^'+si+'from \|^'+si+'__version__'#\|^'+si+'[^ \n]'
    rx = regex.compile(rs)
    rx_parts[indent] = rx

# used tabsize
tabsize = 8

# some gobal options
hrefprefix = '' # put infront of all external HTML-links... helps when
                # putting the pages onto some website.
#highlighted = 'http://starship.python.net/~lemburg/py2html.cgi/%s'
                # URL of the source code highlighting mediator to be used;
                # %s will be replaced by the hrefprefix+filename
highlighted = '%s.html'
                # If want to use static versions created with py2html.py,
                # use this version

# Errors
ParseError = 'ParseError'

# some tools

def parts(text,indent):

    """ return a list of tuples (from, to, type) delimiting different
        parts in text; the whole text has to be indented by indent spaces
        * text must be TAB-free
        * type can be: def, class, \"\"\", import, from, <none of these>
        * this will only find statically defined objects -- using
          if's to do conditional defining breaks this method
    """

    # get parts regexp -- from cache if possible
    try:
        rx = rx_parts[indent]
    except:
        si = indent * ' '
        rs = '^'+si+'def \|^'+si+'class \|^'+si+'\"\"\"\|^'+ \
                 si+'import \|^'+si+'from \|^'+si+'__version__'#\|^'+si+'[^ \n]'
        rx = regex.compile(rs)
        rx_parts[indent] = rx
    l = [[0,0,'']]
    start = 0
#    print '-- breaking into parts using indent',indent,':'
    while 1:
#       print '-- looking at...\n'+text[start:start+50]
        t = rx.search(text,start)
        if t == -1: break
        type = rx.group(0)[indent:]
        if type[-1] == ' ': type = type[:-1]
        l[-1][1] = t # the last part ends here ...
        l.append([t,t,type]) # ... while the new one starts here
#       print '-- found part type >',type,'<:',rx.regs[0],':',text[t:t+20],'...'
        start = rx.regs[0][1]
    l[-1][1] = len(text)
    l = map(tuple,l)
#    print '** returning with parts:',`l`
    return l

def calc_bodyindent(text,start=0):

    """ calculate the bodyindent of the code starting at text[start:] """

    # this one works in most cases
    rx = rx_bodyindent
    if rx.search(text,start) != -1:
        a,b = rx.regs[1]
        return b-a
    else:
        # maybe there are no new lines left (e.g. at the end of a file)
        rx = rx_bodyindent_2
        if rx.search(text,start) != -1:
            a,b = rx.regs[1]
            return b-a
        else:
            # didn't think of this one...
            print 'bodyindent failed for:\n--|'+text[start:]+'|-- why?'
            return 0

def subst(find,sub,text):

    """ substitute sub for every occurence of find in text """

    l = string.split(text,find)
    return string.joinfields(l,sub)

def fix_linebreaks(text):

    """ want to have Unix-style newlines everywhere (that is, no \r!) """

    text = subst('\r\n','\n',text)
    return subst('\r','\n',text)

def escape_long_strings(text):

    """ escape long string newlines so they don't disturb part-breaking
        * this only works, iff the long strings occur balanced everywhere !!!
    """

    l = string.split(text,'\"\"\"')
    for i in range(1,len(l),2):
        l[i] = subst('\n','\r',l[i])
    return string.joinfields(l,'\"\"\"')

def unescape_long_strings(text):

    """ inverse of the above function """

    l = string.split(text,'\"\"\"')
    for i in range(1,len(l),2):
        l[i] = subst('\r','\n',l[i])
    return string.joinfields(l,'\"\"\"')

def extract_doc_string(text):

    """ extract and unescape doc-string from long-string-part """

    l = string.split(text,'\"\"\"')
    s = subst('\r','\n',l[1])
    return s

def extract_version_string(text):

    """ parse a __version__ string statement """

    if rx_version.match(text) >= 0:
        return rx_version.group(1)
    return 'unspecified'

def fullname(doc):

    """ return the full name of a doc-class, that is the names of its
        owners and itself, concatenated with dots
        -- the usual Python-fashion of naming things
    """

    t = doc.name
    while doc.owner is not None:
        doc = doc.owner
        t = doc.name + '.' + t
    return t

def get_parameters(paramstring,

                   split=string.split,strip=string.strip,
                   filter=filter,len=len,map=map):

    """ Returns a list of parameter strings.

        As a special feature, all parameters following an empty
        line are ignored in the list. This is useful for typical
        localization hacks like

        # func(a,b,c,
        #
        #      str=str,find=string.find):
        #     ...

        The localizations are not shown in the generated docs.

        XXX Tuples in the parameter string are not supported !

    """
    lines = split(paramstring,'\n')
    parameters = []
    for i in range(len(lines)):
        line = strip(lines[i])
        if not line:
            break
        parameters[len(parameters):] = map(strip,split(line,','))
    return filter(len,parameters)

# the doc-classes

class doc_string:

    def __init__(self,text,owner=None):

        """ interpret text as a doc-string; instance-variables:
            * text    - original doc-string
            * owner   - owner-class of this object: this object is a member
                        of its super-object, e.g. a function
        """
        self.text = text

    def __str__(self):

        return self.text

    def as_HTML(self):

        """ return a HTML-Version of the doc-string; tries to interpret certain
            human formatting styles in HTML:
            * single leading characters occuring in the global list_markers
              turn into list items (the doc-string is enclosed in a <UL> ...
              </UL> tag)
            * lines of - or = turn into horizontal rules
            * empty lines serve as paragraph separators
            * the first paragraph is written in italics

            * emphasized writing of single words:
                 ' *bold* ' turns out bold
                 ' _underlined_ ' turns out underlined
                 ' /italics/ ' comes out in italics

            * lines starting or ending with a # are written monospaced
              and the '#' signaling this is deleted; example:

              # def unescape_long_strings(text):
              #   l = string.split(text,'\"\"\"')
              #   for i in range(1,len(l),2):
              #       l[i] = subst('\r','\n',l[i])
              #   return string.joinfields(l,'\"\"\"')

              The '#' indicates the start-of-line, that is only spaces
              after the comment mark turn up as spaces ! Or use:

              def unescape_long_strings(text):                  #
                l = string.split(text,'\"\"\"')         #
                for i in range(1,len(l),2):                     #
                    l[i] = subst('\r','\n',l[i])                #
                return string.joinfields(l,'\"\"\"')            #

              In this case all spaces on the left are layouted as such.
              The lines are concatenated into one XMP-field,
              so HTML-tags won't work in here -- e.g.

              # <BODY>
              #  <B> Works ! </B>
              # </BODY>

              gives you an easy-to-apply alternative to using the
              XMP-tag directly.

            * If you plan to put verbatim HTML-code inline then you can
              use this syntax \<I>This doesn't come out in italics\</I>, i.e.
              put a backslash in front of the tag. (The tag must not
              contain embedded '>' characters.)

            * detects mailto, http and ftp URLs and converts them to
              HTML links

            * Note: 'lines' in this context refer to everything between
              two newlines

            * The formatting demonstrated here won't show up in the
              HTML-output of this doc-string, so you'll have to look
              at the source code to find out how it works...
        """
        t = string.strip(self.text) + '\n'
        # bullets:
        t = regsub.gsub('^ *['+list_markers+'] ','<LI TYPE=BULLET> ',t)
        # rules
        t = regsub.gsub('^ *[-=]+\n','<HR>\n',t)
        # empty lines become paragraphs
        t = regsub.gsub('^ *\n','<P>\n',t)
        # guess first paragraph
        t = regsub.gsub('\`\([^<]*\)','<P><I>\\1</I></P>\n',t)
        # monospaced stuff, e.g. code
        t = regsub.gsub('^ *#\(.*\)\n','<XMP>\\1</XMP>\n',t)
        t = regsub.gsub('^\(.*\)#\n','<XMP>\\1</XMP>\n',t)
        t = regsub.gsub('</XMP>\n<XMP>','\n',t)
        # inline HTML
        t = regsub.gsub('\\\\<\(/*[A-Za-z][^>]*\)>','<TT>&lt;\\1&gt;</TT>',t)
        # emphasizing
        t = regsub.gsub(' \*\([^ \*]+\)\* ',' <B>\\1</B> ',t)
        t = regsub.gsub(' _\([^ _]+\)_ ',' <U>\\1</U> ',t)
        t = regsub.gsub(' /\([^ /]+\)/ ',' <I>\\1</I> ',t)
        # URL detection
        t = regsub.gsub('mailto:\([a-zA-Z0-9@.%\-]+\)',
                        'mailto:<A HREF="mailto:\\1">\\1</A>',t)
        t = regsub.gsub('http://\([a-zA-Z0-9@.%\-\/?&+=~]+\)',
                        '<A HREF="http://\\1">http://\\1</A>',t)
        t = regsub.gsub('ftp://\([a-zA-Z0-9@.%\-\/?&+=~]+\)',
                        '<A HREF="ftp://\\1">ftp://\\1</A>',t)

        t = '<P> %s </P>' % t

        return t

class doc_type:

    def __cmp__(self,other):

        return cmp(self.fullname,other.fullname)

class doc_class(doc_type):

    def __init__(self,text,sx,sy,owner=None):

        """ parse the text part [sx:sy] for a class definition and all its
            members; instance-variables:
            * text    - original code
            * slice   - the part of text where the class def is supposed to be
                        found
            * owner   - owner-class of this object: this object is a member
                        of its super-object, e.g. a module
            * indent  - indent of this def
            * name    - the class name
            * fullname - the full name of this class
            * baseclasses - list of names of baseclasses
            * doc     - doc-string as doc_string-object
            * methods - list of methods as doc_method-objects
            * classes - list of classes as doc_class-objects
            * bodyindent - indent of the definitions body
            * parts   - definition body, broken into parts
        """
        self.text = text
        self.slice = (sx,sy)
        self.owner = owner
        rx = rx_class
        start = rx.match(text,sx)
        if start < 0:
            # we've got a problem here
            print "-- can't find the class definition in:"
            print text[sx:sy]
            raise ParseError,"couldn't parse class definition"
        start = start + sx
        self.indent = len(rx.group(1))
        self.name = rx.group(2)
        if rx.group(3) is not None:
            self.baseclasses = string.split(rx.group(4),',')
        else:
            self.baseclasses = []
        self.doc = doc_string('',self)
        self.methods = []
        self.classes = []
        self.fullname = fullname(self)
        # calc body-indent
        self.bodyindent = calc_bodyindent(text,start)
        # break into parts
        self.parts = parts(text[start:sy],self.bodyindent)
        try:
            for x,y,type in self.parts:
                if  type == 'def':
                    # got a method
                    self.methods.append(doc_method(text,start+x,start+y,self))
                elif type == 'class':
                    # got a class
                    self.classes.append(doc_class(text,start+x,start+y,self))
                elif type == '#':
                    # got a comment
                    pass
                elif type == '\"\"\"':
                    # got a doc-string
                    self.doc = doc_string(extract_doc_string(text[start+x:start+y]),self)
                else:
                    # something else
                    pass
        except ParseError,reason:
            print '-- ParseError:',reason
            print '-- was looking at:\n',text[start+x:start+y]
            print '-- Skipping the rest of this class (%s)'%self.fullname
        except regex.error,reason:
            print '-- RegexError:',reason
            print '-- was looking at:\n',text[start+x:start+y]
            print '-- Skipping the rest of this class (%s)'%self.fullname
        self.classes.sort()
        self.methods.sort()

    def as_HTML(self):

        """ give a HTML-version of the class and its members """

        output = '<LI TYPE=SQUARE><A NAME="'+self.fullname+'"><I>class</I>&nbsp;<B>'+self.name+'</B>&nbsp;('+ \
                 string.joinfields(self.baseclasses,',')+') <P><UL>'+\
                 self.doc.as_HTML() + '<P><FONT COLOR=EE9900 SIZE=-2>'+self.fullname+'</FONT></P></UL></P>'
        if self.classes:
            output = output + '<P>Classes: ' + \
                     string.join(
                         map(lambda m: \
                             '<A HREF="#'+m.fullname+'">'+m.name+'</A>',
                             self.classes),' : ') + \
                     '</P>'
        if self.methods:
            output = output + '<P>Methods: (' + \
                     string.join(
                         map(lambda m: \
                             '<A HREF="#'+m.fullname+'">'+m.name+'</A>',
                             self.methods),', ') + \
                     ')</P>'
        if self.classes:
            output = output + '<UL>'
            for m in self.classes:
                output = output + m.as_HTML()
            output = output + '</UL>'
        if self.methods:
            output = output + '<UL>'
            for m in self.methods:
                output = output + m.as_HTML()
            output = output + '</UL>'
        return output + '</LI>'

class doc_method(doc_type):

    def __init__(self,text,sx,sy,owner=None):

        """ parse the text part [sx:sy] for a method definition and all its
            members; instance-variables:
            * text    - original code
            * slice   - the part of text where the class def is supposed to be
                        found
            * owner   - owner-class of this object: this object is a member
                        of its super-object, e.g. a module
            * indent  - indent of this def
            * name    - the class name
            * fullname - the full name of this class
            * parameters - list of parameters needed for this method (without self)
            * doc     - doc-string as doc_string-object
            * functions - list of functions as doc_function-objects
            * classes - list of classes as doc_class-objects
            * bodyindent - indent of the definitions body
            * parts   - definition body, broken into parts
        """
        self.text = text
        self.slice = (sx,sy)
        self.owner = owner
        rx = rx_method
        start = rx.match(text,sx)
        if start < 0:
            # we've got a problem here
            print "-- can't find the method definition in:"
            print text[sx:sy]
            raise ParseError,"couldn't parse method definition"
        start = start + sx
        self.indent = len(rx.group(1))
        self.name = rx.group(2)
        self.parameters = get_parameters(rx.group(3))[1:]
        self.doc = doc_string('',self)
        self.functions = [] # don't think these are really needed...
        self.classes = []   # -"-
        self.fullname = fullname(self)
        # calc body-indent
        self.bodyindent = calc_bodyindent(text,start)
        # break into parts
        self.parts = parts(text[start:sy],self.bodyindent)
        try:
            for x,y,type in self.parts:
                if  type == 'def':
                    # got a function
                    self.functions.append(doc_function(text,start+x,start+y,self))
                elif type == 'class':
                    # got a class
                    self.classes.append(doc_class(text,start+x,start+y,self))
                elif type == '#':
                    # got a comment
                    pass
                elif type == '\"\"\"':
                    # got a doc-string
                    self.doc = doc_string(extract_doc_string(text[start+x:start+y]),self)
                else:
                    # something else
                    pass
        except ParseError,reason:
            print '-- ParseError:',reason
            print '-- was looking at:\n',text[start+x:start+y]
            print '-- Skipping the rest of this method (%s)'%self.fullname
        except regex.error,reason:
            print '-- RegexError:',reason
            print '-- was looking at:\n',text[start+x:start+y]
            print '-- Skipping the rest of this method (%s)'%self.fullname
        self.classes.sort()
        self.functions.sort()

    def as_HTML(self):

        """ give a HTML-version of the method and its members """

        output = '<P><A NAME="'+self.fullname+'">-&nbsp;<I>method</I>&nbsp;<B>'+self.name+'</B>&nbsp;('+\
                 string.joinfields(self.parameters,', ')+') <P><UL>'+\
                 self.doc.as_HTML() + '<P><FONT COLOR=EE9900 SIZE=-2>'+self.fullname+'</FONT></P></UL></P>'
        if self.classes:
            output = output + '<P>Classes: ' + \
                     string.join(
                         map(lambda m: \
                             '<A HREF="#'+m.fullname+'">'+m.name+'</A>',
                             self.classes),' : ') + \
                     '</P>'
        if self.functions:
            output = output + '<P>Functions: (' + \
                     string.join(
                         map(lambda m: \
                             '<A HREF="#'+m.fullname+'">'+m.name+'</A>',
                             self.functions),', ') + \
                     ')</P>'
        if self.classes:
            output = output + '<UL>'
            for m in self.classes:
                output = output + m.as_HTML()
            output = output + '</UL>'
        if self.functions:
            output = output + '<UL>'
            for m in self.functions:
                output = output + m.as_HTML()
            output = output + '</UL>'
        return output + '</P>'

class doc_function(doc_type):

    def __init__(self,text,sx,sy,owner=None):

        """ parse the text part [sx:sy] for a method definition and all its
            members; instance-variables:
            * text    - original code
            * slice   - the part of text where the class def is supposed to be
                        found
            * owner   - owner-class of this object: this object is a member
                        of its super-object, e.g. a module
            * indent  - indent of this def
            * name    - the class name
            * fullname - the full name of this class
            * parameters - list of parameters needed for this function
            * doc     - doc-string as doc_string-object
            * functions - list of functions as doc_function-objects
            * classes - list of classes as doc_class-objects
            * bodyindent - indent of the definitions body
            * parts   - definition body, broken into parts
        """
        self.text = text
        self.slice = (sx,sy)
        self.owner = owner
        rx = rx_function
        start = rx.match(text[sx:sy])
        if start < 0:
            # we've got a problem here
            print "-- can't find the function definition in:"
            print text[sx:sy]
            raise ParseError,"couldn't parse function definition"
        start = start + sx
        self.indent = len(rx.group(1))
        self.name = rx.group(2)
        self.parameters = get_parameters(rx.group(3))
        self.doc = doc_string('',self)
        self.functions = []
        self.classes = []
        self.fullname = fullname(self)
        # calc body-indent
        self.bodyindent = calc_bodyindent(text,start)
        # break into parts
        self.parts = parts(text[start:sy],self.bodyindent)
#       print ' - function',self.parts,':\n',text[start:sy]
        try:
            for x,y,type in self.parts:
                if  type == 'def':
                    # got a function
                    self.functions.append(doc_function(text,start+x,start+y,self))
                elif type == 'class':
                    # got a class
                    self.classes.append(doc_class(text,start+x,start+y,self))
                elif type == '#':
                    # got a comment
                    pass
                elif type == '\"\"\"':
                    # got a doc-string
                    self.doc = doc_string(extract_doc_string(text[start+x:start+y]),self)
                else:
                    # something else
                    pass
        except ParseError,reason:
            print '-- ParseError:',reason
            print '-- was looking at:\n',text[start+x:start+y]
            print '-- Skipping the rest of this function (%s)'%self.fullname
        except regex.error,reason:
            print '-- RegexError:',reason
            print '-- was looking at:\n',text[start+x:start+y]
            print '-- Skipping the rest of this function (%s)'%self.fullname
        self.classes.sort()
        self.functions.sort()

    def as_HTML(self):

        """ give a HTML-version of the function and its members """

        output = '<LI TYPE=CIRCLE><A NAME="'+self.fullname+'"><I>function</I>&nbsp;<B>'+self.name+'</B>&nbsp;('+\
                 string.join(self.parameters,', ')+') <P><UL>'+\
                 self.doc.as_HTML() + '<P><FONT COLOR=EE9900 SIZE=-2>'+self.fullname+'</FONT></P></UL></P>'
        if self.classes:
            output = output + '<P>Classes: ' + \
                     string.join(
                         map(lambda m: \
                             '<A HREF="#'+m.fullname+'">'+m.name+'</A>',
                             self.classes),' : ') + \
                     '</P>'
        if self.functions:
            output = output + '<P>Functions: (' + \
                     string.join(
                         map(lambda m: \
                             '<A HREF="#'+m.fullname+'">'+m.name+'</A>',
                             self.functions),', ') + \
                     ')</P>'
        if self.classes:
            output = output + '<UL>'
            for m in self.classes:
                output = output + m.as_HTML()
            output = output + '</UL>'
        if self.functions:
            output = output + '<UL>'
            for m in self.functions:
                output = output + m.as_HTML()
            output = output + '</UL>'
        return output + '</LI>'


class doc_module(doc_type):

    version = None

    def __init__(self,file,owner=None):

        """ parse the source code in file as Python module
            * owner   - owner-class of this object: this object is a member
                        of its super-object, e.g. a project
            * name    - the module name
            * doc     - doc-string as doc_string-object
            * functions - list of funtions as doc_function-objects
            * methods - list of methods as doc_method-objects
            * classes - list of classes as doc_class-objects
            * parts   - definition body, broken into parts
        """
        # read file, expand tabs, fix line breaks and long strings
        self.file = file
        self.text = open(self.file).read()
        self.text = string.expandtabs(self.text,tabsize)
        self.text = fix_linebreaks(self.text)
        self.text = escape_long_strings(self.text)
        # I need a linebreak at the file end to make things easier:
        if self.text[-1:] != '\n':
            self.text = self.text + '\n'
        self.name = os.path.split(file)[1][:-3] # strip path and .py
        self.owner = owner
        # break into parts
        self.parts = parts(self.text,0)
#       print ' - module',self.parts
        # parse
        self.functions = []
        self.classes = []
        self.doc = doc_string('',self)
        try:
            for x,y,type in self.parts:
                if  type == 'def':
                    # got a function
                    self.functions.append(doc_function(self.text,x,y,self))
                elif type == 'class':
                    # got a class
                    self.classes.append(doc_class(self.text,x,y,self))
                elif type == '#':
                    # got a comment
                    pass
                elif type == '\"\"\"':
                    # got a doc-string
                    self.doc = doc_string(extract_doc_string(self.text[x:y]),self)
                elif type == '__version__':
                    # got a version-string
                    self.version = extract_version_string(self.text[x:y])
                else:
                    # something else
                    pass
        except ParseError,reason:
            print '-- ParseError:',reason
            print '-- was looking at:\n',self.text[x:y]
            print '-- Skipping the rest of this module (%s)'%file
        except regex.error,reason:
            print '-- RegexError:',reason
            print '-- was looking at:\n',self.text[x:y]
            print '-- Skipping the rest of this module (%s)'%file
        self.classes.sort()
        self.functions.sort()

    def as_HTML(self):

        """ give a HTML-version of the module and its members """

        output = '<A NAME="'+self.name+'"><HR SIZE=6><H2>Module: '+self.name+'</H2> <P><UL>'+\
                 self.doc.as_HTML()+'</UL></P>'
        if self.file[:2] == os.curdir + os.sep:
            filename = self.file[2:]
        else:
            filename = self.file
        if self.version:
            output = output + '<P>Version: <FONT COLOR=#FF0000>%s</FONT>' %\
                     self.version
#        output = output + '<P>Sourcecode: \
#                 <A HREF="%s" TARGET="source-window">plaintext</A>,\
#                 <A HREF="%s" TARGET="source-window">highlighted</A>' %\
#                 (hrefprefix+filename,highlighted % (hrefprefix+filename))
        output = output + '<DL>'
        if self.classes:
            output = output + '<DT><I>Classes:</I><DD>' + \
                     string.join(
                         map(lambda m: \
                             '<A HREF="#'+m.fullname+'">'+m.name+'</A>',
                             self.classes),' : ') + \
                     '</DD>'
        if self.functions:
            output = output + '<DT><I>Functions:</I><DD>' + \
                     string.join(
                         map(lambda m: \
                             '<A HREF="#'+m.fullname+'">'+m.name+'</A>',
                             self.functions),' : ') + \
                     '</DD>'
        output = output + '</DL>'
        if self.classes:
            output = output + '<HR><UL>'
            for m in self.classes:
                output = output + m.as_HTML()
            output = output + '</UL>'
        if self.functions:
            output = output + '<HR><UL>'
            for m in self.functions:
                output = output + m.as_HTML()
            output = output + '</UL>'
        return output


class doc_project:

    def __init__(self,path,name='test'):

        """ find all Python-files in path and parse them via doc_module
            * name    - project name
            * path    - pathname
            * files   - filenames found
            * modules - list of modules as doc_module-objects
        """
        self.name = name
        self.path = path
        self.files = filter(lambda f: f[-3:]=='.py', os.listdir(path))
        self.files.sort()
        self.modules = []
        for f in self.files:
            print ' scanning',f
            self.modules.append(doc_module(os.path.join(path,f)))

    def make_HTML(self):

        """ create a HTML-version of the project and its members using
            FRAMES and three files: the frameset-file, the module index and the
            content file
        """

        print 'generating files...'

        # set filenames
        self.main = self.name+'.html'
        self.index = self.name+'.idx.html'
        self.content = self.name+'.docs.html'

        # open files
        main = open(self.main,'w')
        index = open(self.index,'w')
        content = open(self.content,'w')

        # write main-file
        main.write("""
      <HTML>
      <HEAD>
      <TITLE>Docs::Project::%s</TITLE>
      <!--Created by the Python Doc-Tool V%s on %s-->
      <META NAME="KEYWORDS" CONTENT="%s, doc, documentation, python">
      </HEAD>
      <FRAMESET COLS="200,*">
      <FRAME NAME="index" SRC="%s" MARGINHEIGHT=0 MARGINWIDTH=0>
      <FRAME NAME="content" SRC="%s" MARGINHEIGHT=0 MARGINWIDTH=0>
      </FRAMESET>
      </HTML>
        """ % (self.name, __version__, time.ctime(time.time()),
        self.name, hrefprefix+self.index, hrefprefix+self.content))
        main.close()

        # make index-file
        print '',self.index
        output = """
                    <HTML>
                    <HEAD>
                    <TITLE>DocIndex::Project::%s</TITLE>
                    <META NAME="KEYWORDS" CONTENT="%s, index, documentation, python">
                    </HEAD>
                    <BODY BGCOLOR=#FFFFFF>
                 """ % (self.name, self.name)
        output = output + '<HR SIZE=7><H2>Project:&nbsp;'+self.name+'</H2>'
        for m in self.modules:
            output = output + '<B>Module&nbsp;<A HREF="'+hrefprefix+self.content+\
                     '#'+m.name+'" TARGET="content">'+m.name+'</A></B><BR>'
            if m.classes:
                output = output + '<UL><I>Classes</I><BR><BR>'
                for c in m.classes:
                    output = output + '<LI TYPE=DISC>' +\
                             '<A HREF="'+hrefprefix+self.content+\
                             '#'+c.fullname+'" TARGET="content">'+c.name+'</A><BR>'
                    if c.methods != []:
                        output = output + '<FONT SIZE=-1>'
                        for me in c.methods:
                            output = output + '-&nbsp;<A HREF="'+hrefprefix+self.content+\
                                     '#'+me.fullname+'" TARGET="content">'+me.name+'</A><BR>'
                        output = output + '</FONT>'
                    output = output + '<BR>'
                output = output + '</UL>'
            if m.functions:
                output = output + '<UL><I>Functions</I><BR><BR>'
                for f in m.functions:
                    output = output + '<LI TYPE=DISC>' +\
                             '<A HREF="'+hrefprefix+self.content+\
                             '#'+f.fullname+'" TARGET="content">'+f.name+'</A>'
                output = output + '</UL>'

        output = output + '</BODY></HTML>'
        index.write(output)
        index.close()

        # make content-file
        print '',self.content
        output = """
                    <HTML>
                    <HEAD>
                    <TITLE>Docs::Project::%s</TITLE
                    <META NAME="KEYWORDS" CONTENT="%s, docs, documentation, python, doc-tool">
                    </HEAD>
                    <BODY BGCOLOR=#FFFFFF>
                 """ % (self.name,self.name)
        for m in self.modules:
            output = output + m.as_HTML()
        output = output + "<HR><P ALIGN=CENTER><FONT SIZE=-1>Created by Doc-Tool V"+__version__+\
                 " on "+time.ctime(time.time())+"</FONT></P>"
        output = output + '</BODY></HTML>'
        content.write(output)
        content.close()

# simple interface:

def main():

    global hrefprefix

    print 'Doc-Tool V'+__version__+' (c) Marc-Andre Lemburg; mailto:mal@lemburg.com'
    print
    if len(sys.argv) < 3:
        print 'Syntax:',sys.argv[0],'project-name project-dir [HREF-prefix]'
        print
        print 'The tool will create HTML-files with prefix <project-name> containing'
        print 'the doc-strings of the found Python-files in a structured form.'
        print 'The links in these files will be prepended with HREF-prefix, if'
        print 'given, to simplify the upload to a website.'
        print
        print 'Enjoy !'
        sys.exit()
    project,dir = sys.argv[1:3]
    print 'Working on project:',project
    print ' project directory:',dir
    print
    print 'Creating files...'
    if len(sys.argv) >= 4:
        hrefprefix = sys.argv[3]
    else:
        hrefprefix = ''
    p = doc_project(dir,project)
    p.make_HTML()
    print 'Done. Point your browser at',hrefprefix+sys.argv[1]+'.html'

if __name__ == '__main__':
    main()