Changes between Version 1 and Version 2 of GenshiRecipes/SconsXIncludeScanner
- Timestamp:
- Sep 12, 2006, 12:31:00 PM (18 years ago)
Legend:
- Unmodified
- Added
- Removed
- Modified
-
GenshiRecipes/SconsXIncludeScanner
v1 v2 1 = [GenshiRecipes Genshi Recipes]: XInclude scanner for SCons = 2 3 == Motivation == 4 5 You need to integrate Genshi sources into an [http://www.scons.org/ SCons] based build system and you would like implicit dependencies for `xi:include` directives. 6 7 == Code == 8 1 9 {{{ 2 #!rst 10 #!python 11 import sys 12 from genshi.filters import IncludeFilter 13 from genshi.template import Template,TemplateLoader 3 14 4 **why would you want this recipe ?** You need to integrate markup sources into a `scons <http://www.scons.org/>`_ based build system and you would like implicit dependencies for ``xi:include`` directives. 15 def genshi_xinclude_scan(node, env, path, arg): 16 t = Template(node.get_contents()) 17 loader = TemplateLoader('.'.split()) 18 t.filters.insert(0, IncludeFilter(loader)) 19 t.generate().render() 20 return loader._cache.keys() 5 21 6 file SConscript:: 22 markup_scanner = Scanner( 23 name='genshi', 24 function=genshi_xinclude_scan, 25 argument=None, 26 skeys = '.xhtml .html .xml'.split()) 27 scanners = Environment().Dictionary('SCANNERS') 28 env = Environment(SCANNERS = scanners + [genshi_scanner]) 7 29 8 import sys 9 from markup.filters import IncludeFilter 10 from markup.template import Template,TemplateLoader 30 env.Command('page-out', 'page.html', './render $SOURCES $TARGET') 31 }}} 11 32 12 def markup_xinclude_scan(node, env, path, arg): 13 t=Template(node.get_contents()) 14 loader=TemplateLoader('.'.split()) 15 t.filters.insert(0, IncludeFilter(loader)) 16 t.generate().render() 17 return loader._cache.keys() 33 So, assuming `page.html` references another file, say `base.html`, using the XInclude recipe, any edit to `base.html` will cause scons to consider `page-out` to be out of date. After an edit of either `page.html` or `base.html` `scons -f SConscript` will be sufficient to bring `page-out` up to date. 18 34 19 markup_scanner = Scanner( 20 name='markup', 21 function=markup_xinclude_scan, 22 argument=None, 23 skeys = '.xhtml .html .xml'.split()) 24 scanners = Environment().Dictionary('SCANNERS') 25 env = Environment(SCANNERS = scanners + [markup_scanner]) 35 `render` is a trivial wrapper script that renders the final output using Genshi for a single source file. Mine looks like this:: 26 36 27 env.Command('page-out', 'page.html', './render $SOURCES $TARGET') 37 {{{ 38 #!python 39 #!/usr/bin/python 40 import sys 41 try: 42 import wingdbstub 43 except ImportError: 44 pass 45 from genshi.filters import IncludeFilter 46 from genshi.template import Template, TemplateLoader 28 47 29 So, assuming page.html references another file, say ``base.html``, using the Xinclude recipe, any edit to base.html will cause scons to consider ``page-out`` to be out of date. After an edit of either page.html or base.html ``scons -f SConscript`` will be sufficient to bring ``page-out`` up to date. 30 31 render is a trivial wrapper script that renders the final output using markup for a single source file. Mine looks like this:: 32 33 #!/usr/bin/python 34 import sys 35 try: import wingdbstub 36 except ImportError: pass 37 from markup.filters import IncludeFilter 38 from markup.template import Template,TemplateLoader 39 t=Template(file(sys.argv[1]).read()) 40 t.filters.insert(0, IncludeFilter(TemplateLoader('.'.split()))) 41 outf = sys.stdout 42 if len(sys.argv) > 2: 43 outf=file(sys.argv[2], 'w') 44 print >> outf, t.generate().render() 48 t = Template(file(sys.argv[1]).read()) 49 t.filters.insert(0, IncludeFilter(TemplateLoader('.'.split()))) 50 outf = sys.stdout 51 if len(sys.argv) > 2: 52 outf = file(sys.argv[2], 'w') 53 print >> outf, t.generate() 54 }}} 45 55 46 56 There may be slicker ways of doing this. If you know them then please update this recipe. 47 57 48 49 }}} 58 ---- 59 See also: GenshiRecipes, GenshiRecipes/RecursiveIncludeScanner