6.19.10.2.7. 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[12 /42 ] Next Prev First Last
   426: #line 577 "input_frame.ipk"
   427:   def insert_code(self,name):
   428:     "Insert code in an external file into the tangler stream"
   429:     ifdata = (self.depth+1,'code: '+self.current_tangler.language,name)
   430:     self.pass_frame.include_files.append(ifdata)
   431:     r = []
   432:     source = named_file_source(self.pass_frame,name, self.source.directory)
   433:     inpt = input_frame(
   434:       self.pass_frame,
   435:       source,
   436:       r,
   437:       self.current_weaver,
   438:       self.userdict.copy(),
   439:       self.depth+1)
   440:     r.append([inpt.any_line_re, inpt.do_web])
   441:     inpt.select(self.current_tangler)
   442:     inpt.file_pass()
   443: 
   444:   def include_code(self,name,current_tangler):
   445:     "Insert code in an external file into a nominated tangler stream"
   446:     ifdata = (self.depth+1,'code: '+current_tangler.language,name)
   447:     self.pass_frame.include_files.append(ifdata)
   448:     r = []
   449:     source = named_file_source(
   450:       self.pass_frame,
   451:       name,
   452:       self.source.directory)
   453:     inpt = input_frame(
   454:       self.pass_frame,
   455:       source,
   456:       r,
   457:       self.current_weaver,
   458:       self.userdict.copy(),
   459:       self.depth+1)
   460:     r.append([inpt.any_line_re, inpt.do_web])
   461:     inpt.select(current_tangler)
   462:     inpt.file_pass()
   463: 
   464:   def sink_verbatim(self,filename):
   465:     "Write code in an external file directly to the tanglers driver"
   466:     self.current_weaver.label_chunk(filename)
   467:     source = named_file_source(
   468:         self.pass_frame,
   469:         filename,
   470:         self.source.directory)
   471:     data = source.readlines()
   472:     self.current_tangler.sink.writelines(data)
   473: 
   474:   def define(self, macroname, language='data'):
   475:     "Name a chunk of code"
   476:     self.select(self.tangler(cache_sink(macroname, self.master), language))
   477: 
   478:   def expand(self,macroname):
   479:     "Write named chunk directly to tanglers driver"
   480:     self.current_weaver.label_chunk(macroname)
   481:     source = cache_source(macroname, self.master)
   482:     data = source.readlines()
   483:     self.current_tangler.sink.writelines(data)
   484: 
On-the-fly interscript for test 16 follows.
Start interscript section from input_frame.ipk
   636: @head(1,'The main program')
   637: This code includes and external file, chunk.tmp, defining
   638: the body of function f.
   639: You should see the #line directive for it on line 3.
   640: @select(tangler('interscript/tests/chunked.tmp','python'))
   641: def f():
   642: @insert_code('interscript/tests/chunk.tmp')
   643: def g(): pass
   644: def h():
   645: @sink_verbatim('interscript/tests/chunk.tmp')
   646: def k(): pass
   647: @head(1,'The included chunk')
   648: Here's the included chunk, defined after use.
   649: @select(tangler('interscript/tests/chunk.tmp','python'))
   650:   pass
   651: @doc()
   652: End example.
   653: @print_source_list(1)
   654: @print_include_list(1)
End interscript section from input_frame.ipk
Test output at ../tests/output/test_16.html. Logfile at ../tests/output/test_16.log.

   485: #line 656 "input_frame.ipk"
   486: 
End python section to interscript/frames/inputf.py[12]