6.13.10.2.5. Include/insert code

The @insert_code command inserts a code file into the input stream of the current tangler. It is used to yank code fragments from external files.

The external file can contain any data, all of which is interpreted as literal program code: lines beginning with @, for example, will not be processed as commands.

Note that the file is not written directly to the tangler's sink driver, it is written to the tangler. Therefore, if the code contains embedded documentation constructions in a form the tangler understands, documentation can still be woven. For example if a Perl module containing POD constructions is imported, a perl_tangler will still recognize these constructions and send appropriate documentation commens to its associated weaver.

The @include_code command is the same as the @insert_code command except that the tangler is passed as an argument, rather than the current tangler being used.

Start python section to interscript/frames/inputf.py[10 /40 ] Next Prev Last
   337: #line 482 "input_frame.ipk"
   338:   def insert_code(self,name):
   339:     ifdata = (self.depth+1,'code: '+self.current_tangler.language,name)
   340:     self.pass_frame.include_files.append(ifdata)
   341:     r = []
   342:     source = named_file_source(self.pass_frame,name, self.source.directory)
   343:     inpt = input_frame(
   344:       self.pass_frame,
   345:       source,
   346:       r,
   347:       self.current_weaver,
   348:       self.userdict.copy(),
   349:       self.depth+1)
   350:     r.append([inpt.any_line_re, inpt.do_web])
   351:     inpt.select(self.current_tangler)
   352:     inpt.file_pass()
   353: 
   354:   def include_code(self,name,current_tangler):
   355:     ifdata = (self.depth+1,'code: '+self.current_tangler.language,name)
   356:     self.pass_frame.include_files.append(ifdata)
   357:     r = []
   358:     source = named_file_source(
   359:       self.pass_frame,
   360:       name,
   361:       self.source.directory)
   362:     inpt = input_frame(
   363:       self.pass_frame,
   364:       source,
   365:       r,
   366:       self.current_weaver,
   367:       self.userdict.copy(),
   368:       self.depth+1)
   369:     r.append([inpt.any_line_re, inpt.do_web])
   370:     inpt.select(tangler)
   371:     inpt.file_pass()
   372: 
   373:   def sink_verbatim(self,filename):
   374:     self.current_weaver.label_chunk(filename)
   375:     source = named_file_source(
   376:         self.pass_frame,
   377:         filename,
   378:         self.source.directory)
   379:     data = source.readlines()
   380:     self.current_tangler.sink.writelines(data)
   381: 
   382:   def define(self, macroname, language='data'):
   383:     self.select(self.output(cache_sink(macroname, self.master), language))
   384: 
   385:   def expand(self,macroname):
   386:     self.current_weaver.label_chunk(macroname)
   387:     source = cache_source(macroname, self.master)
   388:     data = source.readlines()
   389:     self.current_tangler.sink.writelines(data)
   390: 
On-the-fly interscript for test 14 follows.
Start interscript section from input_frame.ipk
   536: @head(1,'The main program')
   537: This code includes and external file, chunk.tmp, defining
   538: the body of function f.
   539: You should see the #line directive for it on line 3.
   540: @select(tangler('chunked.tmp','python'))
   541: def f():
   542: @insert_code('chunk.tmp')
   543: def g(): pass
   544: def h():
   545: @sink_verbatim('chunk.tmp')
   546: def k(): pass
   547: @head(1,'The included chunk')
   548: Here's the included chunk, defined after use.
   549: @select(tangler('chunk.tmp','python'))
   550:   pass
   551: @doc()
   552: End example.
   553: @print_source_list(1)
   554: @print_include_list(1)
End interscript section from input_frame.ipk
Test output at ../tests/output/test_14.html. Logfile at ../tests/output/test_14.log.

   391: #line 556 "input_frame.ipk"
   392: 
End python section to interscript/frames/inputf.py[10]