summaryrefslogtreecommitdiffstats
path: root/src/engine/SCons/Tool/tex.py
blob: adb6c06ec287f6e6ea8163b1ca87b3d8efb16b2f (plain)
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
"""SCons.Tool.tex

Tool-specific initialization for TeX.

There normally shouldn't be any need to import this module directly.
It will usually be imported through the generic SCons.Tool.Tool()
selection method.

"""

#
# __COPYRIGHT__
#
# Permission is hereby granted, free of charge, to any person obtaining
# a copy of this software and associated documentation files (the
# "Software"), to deal in the Software without restriction, including
# without limitation the rights to use, copy, modify, merge, publish,
# distribute, sublicense, and/or sell copies of the Software, and to
# permit persons to whom the Software is furnished to do so, subject to
# the following conditions:
#
# The above copyright notice and this permission notice shall be included
# in all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY
# KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
# WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
# LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
# OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
# WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
#

__revision__ = "__FILE__ __REVISION__ __DATE__ __DEVELOPER__"

import os.path
import re
import string

import SCons.Action
import SCons.Defaults
import SCons.Node
import SCons.Node.FS
import SCons.Util

# Define an action to build a generic tex file.  This is sufficient for all 
# tex files.
TeXAction = SCons.Action.Action("$TEXCOM", "$TEXCOMSTR")

# Define an action to build a latex file.  This action might be needed more
# than once if we are dealing with labels and bibtex
LaTeXAction = SCons.Action.Action("$LATEXCOM", "$LATEXCOMSTR")

# Define an action to run BibTeX on a file.
BibTeXAction = SCons.Action.Action("$BIBTEXCOM", "$BIBTEXCOMSTR")

def LaTeXAuxAction(target = None, source= None, env=None):
    """A builder for LaTeX files that checks the output in the aux file
    and decides how many times to use LaTeXAction, and BibTeXAction."""
    # Get the base name of the target
    basename, ext = os.path.splitext(str(target[0]))
    # Run LaTeX once to generate a new aux file.
    LaTeXAction(target,source,env)
    # Now if bibtex will need to be run.
    content = open(basename + ".aux","rb").read()
    if string.find(content, "bibdata") != -1:
        bibfile = env.fs.File(basename)
        BibTeXAction(None,bibfile,env)
    # Now check if latex needs to be run yet again.
    for trial in range(3):
        content = open(basename + ".log","rb").read()
        if not re.search("^LaTeX Warning:.*Rerun",content,re.MULTILINE):
            break
        LaTeXAction(target,source,env)
    return 0

LaTeX_re = re.compile("\\\\document(style|class)")

def is_LaTeX(flist):
    # Scan a file list to decide if it's TeX- or LaTeX-flavored.
    for f in flist:
        content = f.get_contents()
        if LaTeX_re.search(content):
            return 1
    return 0

def TeXLaTeXFunction(target = None, source= None, env=None):
    """A builder for TeX and LaTeX that scans the source file to
    decide the "flavor" of the source and then executes the appropriate
    program."""
    if is_LaTeX(source):
        LaTeXAuxAction(target,source,env)
    else:
        TeXAction(target,source,env)
    return 0

TeXLaTeXAction = SCons.Action.Action(TeXLaTeXFunction,
                                     strfunction=None)

def generate(env):
    """Add Builders and construction variables for TeX to an Environment."""
    try:
        bld = env['BUILDERS']['DVI']
    except KeyError:
        bld = SCons.Defaults.DVI()
        env['BUILDERS']['DVI'] = bld
        
    bld.add_action('.tex', TeXLaTeXAction)

    env['TEX']      = 'tex'
    env['TEXFLAGS'] = SCons.Util.CLVar('')
    env['TEXCOM']   = '$TEX $TEXFLAGS $SOURCES'

    # Duplicate from latex.py.  If latex.py goes away, then this is still OK.
    env['LATEX']      = 'latex'
    env['LATEXFLAGS'] = SCons.Util.CLVar('')
    env['LATEXCOM']   = '$LATEX $LATEXFLAGS $SOURCES'

    env['BIBTEX']      = 'bibtex'
    env['BIBTEXFLAGS'] = SCons.Util.CLVar('')
    env['BIBTEXCOM']   = '$BIBTEX $BIBTEXFLAGS $SOURCES'


def exists(env):
    return env.Detect('tex')