6.23.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.

Comienzo python section to interscript/frames/inputf.py[11 /41 ] Siguiente Previo Primero Ăšltimo
   377: #line 527 "input_frame.ipk"
   378:   def insert_code(self,name):
   379:     ifdata = (self.depth+1,'code: '+self.current_tangler.language,name)
   380:     self.pass_frame.include_files.append(ifdata)
   381:     r = []
   382:     source = named_file_source(self.pass_frame,name, self.source.directory)
   383:     inpt = input_frame(
   384:       self.pass_frame,
   385:       source,
   386:       r,
   387:       self.current_weaver,
   388:       self.userdict.copy(),
   389:       self.depth+1)
   390:     r.append([inpt.any_line_re, inpt.do_web])
   391:     inpt.select(self.current_tangler)
   392:     inpt.file_pass()
   393: 
   394:   def include_code(self,name,current_tangler):
   395:     ifdata = (self.depth+1,'code: '+current_tangler.language,name)
   396:     self.pass_frame.include_files.append(ifdata)
   397:     r = []
   398:     source = named_file_source(
   399:       self.pass_frame,
   400:       name,
   401:       self.source.directory)
   402:     inpt = input_frame(
   403:       self.pass_frame,
   404:       source,
   405:       r,
   406:       self.current_weaver,
   407:       self.userdict.copy(),
   408:       self.depth+1)
   409:     r.append([inpt.any_line_re, inpt.do_web])
   410:     inpt.select(current_tangler)
   411:     inpt.file_pass()
   412: 
   413:   def sink_verbatim(self,filename):
   414:     self.current_weaver.label_chunk(filename)
   415:     source = named_file_source(
   416:         self.pass_frame,
   417:         filename,
   418:         self.source.directory)
   419:     data = source.readlines()
   420:     self.current_tangler.sink.writelines(data)
   421: 
   422:   def define(self, macroname, language='data'):
   423:     self.select(self.output(cache_sink(macroname, self.master), language))
   424: 
   425:   def expand(self,macroname):
   426:     self.current_weaver.label_chunk(macroname)
   427:     source = cache_source(macroname, self.master)
   428:     data = source.readlines()
   429:     self.current_tangler.sink.writelines(data)
   430: 
On-the-fly interscript for test 17 follows.
Start interscript section from input_frame.ipk
   581: @head(1,'The main program')
   582: This code includes and external file, chunk.tmp, defining
   583: the body of function f.
   584: You should see the #line directive for it on line 3.
   585: @select(tangler('interscript/tests/chunked.tmp','python'))
   586: def f():
   587: @insert_code('interscript/tests/chunk.tmp')
   588: def g(): pass
   589: def h():
   590: @sink_verbatim('interscript/tests/chunk.tmp')
   591: def k(): pass
   592: @head(1,'The included chunk')
   593: Here's the included chunk, defined after use.
   594: @select(tangler('interscript/tests/chunk.tmp','python'))
   595:   pass
   596: @doc()
   597: End example.
   598: @print_source_list(1)
   599: @print_include_list(1)
End interscript section from input_frame.ipk
Test output at ../tests/output/test_17.html. Logfile at ../tests/output/test_17.log.

   431: #line 601 "input_frame.ipk"
   432: 
End python section to interscript/frames/inputf.py[11]