1# Copyright David Abrahams 2004. Distributed under the Boost
2# Software License, Version 1.0. (See accompanying
3# file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
4
5# Support for docutils ReStructuredText processing.
6
7import type ;
8import scanner ;
9import generators ;
10import os ;
11import common ;
12import toolset ;
13import path ;
14import feature : feature ;
15import property ;
16import errors ;
17
18.initialized = ;
19
20type.register ReST : rst ;
21
22class rst-scanner : common-scanner
23{
24    rule __init__ ( paths * )
25    {
26        common-scanner.__init__ . $(paths) ;
27    }
28
29    rule pattern ( )
30    {
31         return "^[ 	]*\\.\\.[ 	]+include::[ 	]+([^
32]+)"
33        "^[ 	]*\\.\\.[ 	]+image::[ 	]+([^
34]+)"
35        "^[ 	]*\\.\\.[ 	]+figure::[ 	]+([^
36]+)"
37        ;
38    }
39}
40
41scanner.register rst-scanner : include ;
42type.set-scanner ReST : rst-scanner ;
43
44generators.register-standard docutils.html : ReST : HTML ;
45
46rule init ( docutils-dir ? : tools-dir ? )
47{
48    docutils-dir ?= [ modules.peek : DOCUTILS_DIR ] ;
49    tools-dir ?= $(docutils-dir)/tools ;
50
51    if ! $(.initialized)
52    {
53        .initialized = true ;
54        if $(docutils-dir)
55        {
56            .docutils-dir = $(docutils-dir) ;
57            .tools-dir = $(tools-dir:R="") ;
58
59            .setup = [
60                common.prepend-path-variable-command PYTHONPATH
61                    : $(.docutils-dir) $(.docutils-dir)/extras ] ;
62            RST2XXX = [ common.find-tool rst2html ] ;
63        }
64        else
65        {
66            RST2XXX_PY = [ common.find-tool rst2html.py ] ;
67        }
68    }
69}
70
71rule html ( target : source : properties *  )
72{
73    if ! [ on $(target) return $(RST2XXX) ]
74    {
75        local python-cmd = [ property.select <python.interpreter> : $(properties) ] ;
76        if ! $(.tools-dir) && ! $(RST2XXX_PY) {
77            errors.user-error
78            "The docutils module is used, but not configured. "
79            : ""
80            : "Please modify your user-config.jam or project-config.jam to contain:"
81            : ""
82            : "    using docutils : <docutils-dir> ;"
83            : ""
84            : "On Ubuntu, 'docutils-common' package will create /usr/share/docutils."
85            : "Other flavours of Linux likely have docutils as package as well."
86            : "On Windows, you can install from http://docutils.sourceforge.net/."
87            ;
88        }
89
90        if $(RST2XXX_PY)
91        {
92            if $(RST2XXX_PY:D)
93            {
94                # If we have a path to the rst2html.py script, we need to use
95                # the python interpreter to load it up.
96                RST2XXX on $(target) = $(python-cmd:G=:E="python") $(RST2XXX_PY) ;
97            }
98            else
99            {
100                # Otherwise, bare rst2html.py, we can just exec that directly.
101                # This work for both Nix, and the standard Windows Python installs.
102                RST2XXX on $(target) = $(RST2XXX_PY) ;
103            }
104        }
105        else
106        {
107            RST2XXX on $(target) = $(python-cmd:G=:E="python") $(.tools-dir)/rst2html.py ;
108        }
109    }
110}
111
112
113feature docutils : : free ;
114feature docutils-html : : free ;
115feature docutils-cmd : : free ;
116toolset.flags docutils COMMON-FLAGS : <docutils> ;
117toolset.flags docutils HTML-FLAGS : <docutils-html> ;
118toolset.flags docutils RST2XXX : <docutils-cmd> ;
119
120actions html
121{
122    $(.setup)
123    "$(RST2XXX)" $(COMMON-FLAGS) $(HTML-FLAGS) $(>) $(<)
124}
125
126