summaryrefslogtreecommitdiffstats
blob: c7596d67555248aad8c1877ef608bd92a1b8645d (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
import SCons.Util
import xml.dom.minidom, re

################################################################################
# XSL-FO builder
################################################################################

def generate(env) :
  def generate_actions(source, target, env, for_signature) :
    if len(env["FOCFG"]) > 0 :
      cmd = "$FO -c $FOCFG $FOFLAGS $SOURCE $TARGET"
    else :
      cmd = "$FO $FOFLAGS $SOURCE $TARGET"
    return SCons.Action.Action(cmd, cmdstr = "$FOCOMSTR")

  def modify_sources(target, source, env) :
    if len(env["FOCFG"]) > 0 :
      source.append(env["FOCFG"])
    return target, source

  def scan_fo(node, env, path) :
    dependencies = set()
    try :
      document = xml.dom.minidom.parseString(node.get_contents())
    except xml.parsers.expat.ExpatError:
      return []
    for include in document.getElementsByTagNameNS("http://www.w3.org/1999/XSL/Format", "external-graphic") :
      m = re.match("url\((.*)\)", include.getAttribute("src"))
      if m :
        dependencies.add(m.group(1))
    return list(dependencies)

  env["FO"] = "fop"
  env["FOFLAGS"] = ""
  env["FOCFG"] = ""
  env["BUILDERS"]["FO"] = SCons.Builder.Builder(
        generator = generate_actions,
        emitter = modify_sources,
        source_scanner = SCons.Scanner.Scanner(function = scan_fo, skeys = [".fo"]),
        suffix = ".pdf",
        src_suffix = ".fo"
      )

def exists(env) :
  return True