File: format_c_api_docs.py

package info (click to toggle)
graphicsmagick 1.4%2Breally1.3.46-2
  • links: PTS
  • area: main
  • in suites: sid
  • size: 41,896 kB
  • sloc: ansic: 250,504; cpp: 18,843; sh: 9,308; perl: 3,634; makefile: 2,568; tcl: 2,209; python: 1,250
file content (127 lines) | stat: -rwxr-xr-x 3,698 bytes parent folder | download | duplicates (3)
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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# vim: ts=4:sw=4:expandtab:

# Copyright 2008 Mark Mitchell
# License: Same as GraphicsMagick.

__doc__ = """Runs format_c_api_doc.py on all files in the whatis list,
producing reStructuredText files.  Then runs rst2htmldeco.py on all the
reStructuredText files, producing HTML files.

Usage:
    format_c_api_docs.py whatisfile [GMTOPDIR] [OUTDIR]

    GMTOPDIR is used to construct filesystem paths to the 'magick'
    directory of the .c input files, i.e. GMTOPDIR/magick.

    If OUTDIR is not specified, it defaults to GMTOPDIR/www/api

    Example:
    ./format_c_api_docs.py  whatis.txt /usr/local/src/GraphicsMagick/ ../www/api

    would expect to find the .c files listed in whatis.txt in the
    directory /usr/local/src/GraphicsMagick/magick, and will write
    the HTML files into the directory ../www/api, which is relative
    to the current working directory.

    The value of --url-prefix passed to rst2htmldeco.py is hard-wired
    as '../../', meaning this script assumes the standard GM directory
    layout.

    The value of --link-stylesheet passed to rst2htmldeco.py is hard-wired
    as '../../www/docutils-api.css'.
"""

import sys
import os, os.path
import shutil

import format_c_api_doc
import rst2htmldeco


# assume standard GM directory layout
stylesheet_file = 'docutils-api.css'
url_prefix = '../../'
stylesheet_url = '%swww/%s' % (url_prefix, stylesheet_file)



def main(argv=None):
    if argv is None:
        argv = sys.argv

    if len(argv) < 2:
        print >> sys.stderr, __doc__
        return 1

    if argv[1] in ('-h', '--help'):
        print __doc__
        return 0

    whatis_file = argv[1]

    gm_topdir = None
    if len(argv) >= 3:
        gm_topdir = argv[2]

    outfile_dir = None
    if len(argv) >= 4:
        outfile_dir = argv[3]

    current_dir = os.path.dirname(os.path.abspath(__file__))

    if not gm_topdir:
        # NOTE: the following is predicated on the directory structure of the
        # GraphicsMagick source tree.  It assumes this script resides in
        # TOPDIR/scripts and the .c files referred to by the whatis file
        # reside in TOPDIR/magick
        gm_topdir = os.path.normpath(os.path.join(current_dir, '..'))

    srcfile_dir = os.path.join(gm_topdir, 'magick')

    if not outfile_dir:
        outfile_dir = os.path.join(gm_topdir, 'www', 'api')

    f = file(whatis_file, 'r')
    cnt = 0
    for line in f:
        cnt += 1
        if not line.strip():
            continue
        if line.startswith('#'):
            continue
        try:
            cfile, whatis = line.split(None, 1)
        except ValueError:
            print >> sys.stderr, "Line %u of %s: improper format" % (cnt, whatis_file)
            return 1

        srcfile_path = os.path.join(srcfile_dir, cfile)
        srcfile = os.path.basename(srcfile_path)
        base, ext = os.path.splitext(srcfile)
        rstfile = base + '.rst'
        rstfile_path = os.path.join(outfile_dir, rstfile)
        htmlfile = base + '.html'
        htmlfile_path = os.path.join(outfile_dir, htmlfile)

        args = ['-w', whatis_file, srcfile_path, rstfile_path]
        print 'format_c_api_doc.py', ' '.join(args)
        format_c_api_doc.main(args)

        # Now generate HTML from the .rst files
        args = ['--link-stylesheet=%s' % stylesheet_url,
                '--url-prefix=%s' % url_prefix,
                rstfile_path,
                htmlfile_path]
        print 'rst2htmldeco.py %s' % ' '.join(args)
        rst2htmldeco.main(args)
        print 'rm', rstfile_path
        os.unlink(rstfile_path)
    f.close()



if __name__ == '__main__':
    sys.exit(main(sys.argv))