]> git.lyx.org Git - lyx.git/blob - lib/examples/noweb2lyx.lyx
d3b743131dc4f6642f924d07b44aeddab2241ff3
[lyx.git] / lib / examples / noweb2lyx.lyx
1 #LyX 2.2 created this file. For more info see http://www.lyx.org/
2 \lyxformat 504
3 \begin_document
4 \begin_header
5 \save_transient_properties true
6 \origin /systemlyxdir/examples/
7 \textclass article
8 \begin_preamble
9 %
10 % This relaxes the noweb constraint that chunks are
11 % never broken across pages.
12 %
13 % This is from the noweb FAQ
14 %
15 \def\nwendcode{\endtrivlist \endgroup}
16 \let\nwdocspar=\smallbreak
17 \end_preamble
18 \use_default_options false
19 \begin_modules
20 noweb
21 \end_modules
22 \maintain_unincluded_children false
23 \language english
24 \language_package default
25 \inputencoding default
26 \fontencoding global
27 \font_roman "default" "default"
28 \font_sans "default" "default"
29 \font_typewriter "default" "default"
30 \font_math "auto" "auto"
31 \font_default_family default
32 \use_non_tex_fonts false
33 \font_sc false
34 \font_osf false
35 \font_sf_scale 100 100
36 \font_tt_scale 100 100
37 \graphics default
38 \default_output_format default
39 \output_sync 0
40 \bibtex_command default
41 \index_command default
42 \paperfontsize default
43 \spacing single
44 \use_hyperref false
45 \papersize default
46 \use_geometry false
47 \use_package amsmath 0
48 \use_package amssymb 0
49 \use_package cancel 0
50 \use_package esint 0
51 \use_package mathdots 1
52 \use_package mathtools 0
53 \use_package mhchem 1
54 \use_package stackrel 0
55 \use_package stmaryrd 0
56 \use_package undertilde 0
57 \cite_engine basic
58 \cite_engine_type default
59 \biblio_style plain
60 \use_bibtopic false
61 \use_indices false
62 \paperorientation portrait
63 \suppress_date false
64 \justification true
65 \use_refstyle 0
66 \index Index
67 \shortcut idx
68 \color #008000
69 \end_index
70 \secnumdepth 3
71 \tocdepth 3
72 \paragraph_separation indent
73 \paragraph_indentation default
74 \quotes_language english
75 \papercolumns 1
76 \papersides 1
77 \paperpagestyle default
78 \tracking_changes false
79 \output_changes false
80 \html_math_output 0
81 \html_css_as_file 0
82 \html_be_strict false
83 \end_header
84
85 \begin_body
86
87 \begin_layout Title
88
89 \noun on
90 noweb2lyx
91 \end_layout
92
93 \begin_layout Author
94 Kayvan A.
95  Sylvan <kayvan@sylvan.com>
96 \end_layout
97
98 \begin_layout Date
99 May 6, 1999
100 \end_layout
101
102 \begin_layout Abstract
103 This document describes and implements a perl script for importing noweb
104  files into \SpecialCharNoPassThru LyX
105 \end_layout
106
107 \begin_layout Standard
108 \begin_inset CommandInset toc
109 LatexCommand tableofcontents
110
111 \end_inset
112
113
114 \end_layout
115
116 \begin_layout Standard
117 \begin_inset Newpage newpage
118 \end_inset
119
120
121 \end_layout
122
123 \begin_layout Section
124 Introduction
125 \end_layout
126
127 \begin_layout Standard
128 Since version 1.0.1, \SpecialCharNoPassThru LyX
129  now supports Literate Programming using 
130 \noun on
131 noweb
132 \noun default
133 .
134  This addition to \SpecialCharNoPassThru LyX
135  made it very pleasant to write programs in the literate
136  style (like this one).
137  In addition to being able to write new literate programs, it would be quite
138  useful if old 
139 \noun on
140 noweb
141 \noun default
142  code could be imported into \SpecialCharNoPassThru LyX
143  in some fashion.
144  That's where this program comes in.
145 \end_layout
146
147 \begin_layout Standard
148 The purpose of 
149 \noun on
150 noweb2lyx
151 \noun default
152  is to convert a 
153 \noun on
154 noweb
155 \noun default
156  file to \SpecialCharNoPassThru LyX
157 .
158 \end_layout
159
160 \begin_layout Standard
161 \begin_inset Flex Chunk
162 status open
163
164 \begin_layout Plain Layout
165
166 \begin_inset Argument 1
167 status open
168
169 \begin_layout Plain Layout
170 noweb2lyx.in
171 \end_layout
172
173 \end_inset
174
175 #!@PERL@
176 \end_layout
177
178 \begin_layout Plain Layout
179
180
181 \end_layout
182
183 \begin_layout Plain Layout
184
185 # Copyright (C) 1999 Kayvan A.
186  Sylvan <kayvan@sylvan.com>
187 \end_layout
188
189 \begin_layout Plain Layout
190
191 # You are free to use and modify this code under the terms of
192 \end_layout
193
194 \begin_layout Plain Layout
195
196 # the GNU General Public Licence version 2 or later.
197 \end_layout
198
199 \begin_layout Plain Layout
200
201 #
202 \end_layout
203
204 \begin_layout Plain Layout
205
206 # Written with assistance from:
207 \end_layout
208
209 \begin_layout Plain Layout
210
211 #   Edmar Wienskoski Jr.
212  <edmar-w-jr@technologist.com>
213 \end_layout
214
215 \begin_layout Plain Layout
216
217 #   Amir Karger <karger@post.harvard.edu>
218 \end_layout
219
220 \begin_layout Plain Layout
221
222 #
223 \end_layout
224
225 \begin_layout Plain Layout
226
227 # $Id: noweb2lyx.lyx,v 1.5 2005/07/18 09:42:27 jamatos Exp $
228 \end_layout
229
230 \begin_layout Plain Layout
231
232 #
233 \end_layout
234
235 \begin_layout Plain Layout
236
237 # NOTE: This file was automatically generated from noweb2lyx.lyx using noweb.
238 \end_layout
239
240 \begin_layout Plain Layout
241
242 #
243 \end_layout
244
245 \begin_layout Plain Layout
246
247 <<Setup variables from user supplied args>>
248 \end_layout
249
250 \begin_layout Plain Layout
251
252 <<Subroutines>>
253 \end_layout
254
255 \begin_layout Plain Layout
256
257 <<Convert noweb to \SpecialCharNoPassThru LyX
258 >>
259 \end_layout
260
261 \end_inset
262
263
264 \end_layout
265
266 \begin_layout Section
267 The Noweb file defined
268 \end_layout
269
270 \begin_layout Standard
271
272 \noun on
273 noweb
274 \noun default
275  file is a collection of documentation and code chunks.
276  Documentation chunks simply start with an ``@'' and have no name:
277 \end_layout
278
279 \begin_layout LyX-Code
280 @ Here is some documentation.
281 \end_layout
282
283 \begin_layout Standard
284 \begin_inset ERT
285 status open
286
287 \begin_layout Plain Layout
288
289 We can do arbitrary \SpecialCharNoPassThru LaTeX
290  code here.
291 \end_layout
292
293 \begin_layout Plain Layout
294
295 [...
296  blah blah blah ...]
297 \end_layout
298
299 \end_inset
300
301
302 \end_layout
303
304 \begin_layout Standard
305 Code chunks look like this:
306 \end_layout
307
308 \begin_layout LyX-Code
309 <
310 \begin_inset ERT
311 status collapsed
312
313 \begin_layout Plain Layout
314
315 {}
316 \end_layout
317
318 \end_inset
319
320 <Name of chunk here>
321 \begin_inset ERT
322 status collapsed
323
324 \begin_layout Plain Layout
325
326 {}
327 \end_layout
328
329 \end_inset
330
331 >=
332 \begin_inset Newline newline
333 \end_inset
334
335 {...
336  code for the chunk goes here ...}
337 \begin_inset Newline newline
338 \end_inset
339
340
341 \end_layout
342
343 \begin_layout Standard
344 The ``@'' is a necessary delimiter to end the code chunk.
345  The other form that the ``@'' line takes is as follows:
346 \end_layout
347
348 \begin_layout LyX-Code
349 <
350 \begin_inset ERT
351 status collapsed
352
353 \begin_layout Plain Layout
354
355 {}
356 \end_layout
357
358 \end_inset
359
360 <Name of chunk here>
361 \begin_inset ERT
362 status collapsed
363
364 \begin_layout Plain Layout
365
366 {}
367 \end_layout
368
369 \end_inset
370
371 >=
372 \begin_inset Newline newline
373 \end_inset
374
375 {...
376  code for the chunk ...}
377 \begin_inset Newline newline
378 \end_inset
379
380 @ %def identifier1 identifier2
381 \end_layout
382
383 \begin_layout Standard
384 In the latter form, we are declaring to 
385 \noun on
386 noweb
387 \noun default
388  that this code chunk defines identifier1, identifier2, etc.
389 \end_layout
390
391 \begin_layout Standard
392 When first tackling this problem, I spoke with members of the \SpecialCharNoPassThru LyX
393  team that
394  knew about the literate programming extensions and re\SpecialCharNoPassThru LyX
395  (the \SpecialCharNoPassThru LaTeX
396  importing
397  code).
398 \end_layout
399
400 \begin_layout Standard
401 One of the first ideas was to extend the re\SpecialCharNoPassThru LyX
402  code to understand the 
403 \noun on
404 noweb
405 \noun default
406  code chunks.
407  This proved to be too hard and presents other problems
408 \begin_inset Foot
409 status collapsed
410
411 \begin_layout Plain Layout
412 Not the least of these problems is the fact that << is a quote in French.
413 \end_layout
414
415 \end_inset
416
417 .
418  On the other hand, it turns out that re\SpecialCharNoPassThru LyX
419  contains a very useful literal
420  quoting mechanism.
421  If the input file contains the construct
422 \end_layout
423
424 \begin_layout LyX-Code
425
426 \backslash
427 begin{re\SpecialCharNoPassThru LyX
428 skip}
429 \begin_inset Newline newline
430 \end_inset
431
432 {...
433  \SpecialCharNoPassThru LaTeX
434  stuff ...}
435 \begin_inset Newline newline
436 \end_inset
437
438
439 \backslash
440 end{re\SpecialCharNoPassThru LyX
441 skip}
442 \end_layout
443
444 \begin_layout Standard
445 then re\SpecialCharNoPassThru LyX
446  will copy the surrounded code to the output file verbatim.
447  Given this, the first part of the translation is easy; we simply have to
448  copy the code chunks into an intermediate file that surrounds them with
449  
450 \family typewriter
451
452 \backslash
453 begin{re\SpecialCharNoPassThru LyX
454 skip}
455 \family default
456  and 
457 \family typewriter
458
459 \backslash
460 end{re\SpecialCharNoPassThru LyX
461 skip}
462 \family default
463 .
464 \end_layout
465
466 \begin_layout Standard
467 Once re\SpecialCharNoPassThru LyX
468  is done with the input file, the problem is reduced to changing
469  the code chunks from \SpecialCharNoPassThru LyX
470 's \SpecialCharNoPassThru LaTeX
471  layout to the Chunk layout.
472 \end_layout
473
474 \begin_layout Standard
475 There is one final constraint on 
476 \noun on
477 noweb2lyx
478 \noun default
479 .
480  We want to be able to run it as a simple pre-processor and post-processor
481  from within re\SpecialCharNoPassThru LyX
482 .
483  We can accomplish this by setting the flags 
484 \family roman
485 \series medium
486 \shape up
487 \size normal
488 \emph off
489 \bar no
490 \noun off
491 \color none
492
493 \begin_inset ERT
494 status collapsed
495
496 \begin_layout Plain Layout
497
498 [[pre_only]]
499 \end_layout
500
501 \end_inset
502
503
504 \family default
505 \series default
506 \shape default
507 \size default
508 \emph default
509 \bar default
510 \noun default
511  
512 \color inherit
513 and
514 \color none
515  
516 \family roman
517 \series medium
518 \shape up
519 \size normal
520 \emph off
521 \bar no
522 \noun off
523
524 \begin_inset ERT
525 status collapsed
526
527 \begin_layout Plain Layout
528
529 [[post_only]]
530 \end_layout
531
532 \end_inset
533
534
535 \family default
536 \series default
537 \shape default
538 \size default
539 \emph default
540 \bar default
541 \noun default
542  
543 \color inherit
544 before we reach the main conversion code.
545 \end_layout
546
547 \begin_layout Standard
548 With all that preamble out of the way, we now have the basic high-level
549  outline for our code:
550 \end_layout
551
552 \begin_layout Standard
553 \begin_inset Flex Chunk
554 status open
555
556 \begin_layout Plain Layout
557
558 \begin_inset Argument 1
559 status open
560
561 \begin_layout Plain Layout
562 Convert noweb to \SpecialCharNoPassThru LyX
563 \end_layout
564
565 \end_inset
566
567 if (!$post_only) {
568 \end_layout
569
570 \begin_layout Plain Layout
571
572   <<Transform noweb for re\SpecialCharNoPassThru LyX
573 >>
574 \end_layout
575
576 \begin_layout Plain Layout
577
578 }
579 \end_layout
580
581 \begin_layout Plain Layout
582
583 if ((!$pre_only) && (!$post_only)) {
584 \end_layout
585
586 \begin_layout Plain Layout
587
588   <<Run re\SpecialCharNoPassThru LyX
589  on intermediate file>>
590 \end_layout
591
592 \begin_layout Plain Layout
593
594 }
595 \end_layout
596
597 \begin_layout Plain Layout
598
599 if (!$pre_only) {
600 \end_layout
601
602 \begin_layout Plain Layout
603
604   <<Fix up \SpecialCharNoPassThru LyX
605  file>>
606 \end_layout
607
608 \begin_layout Plain Layout
609
610 }
611 \end_layout
612
613 \begin_layout Plain Layout
614
615 <<Clean up>>
616 \end_layout
617
618 \end_inset
619
620
621 \end_layout
622
623 \begin_layout Section
624 Making a file that re\SpecialCharNoPassThru LyX
625  can process
626 \end_layout
627
628 \begin_layout Standard
629 In this section, we present the code that performs the task of creating
630  the intermediate file that re\SpecialCharNoPassThru LyX
631  can process, using the algorithm that
632  we just outlined.
633  This algorithm is outlined in the code that follows:
634 \end_layout
635
636 \begin_layout Standard
637 \begin_inset Flex Chunk
638 status open
639
640 \begin_layout Plain Layout
641
642 \end_layout
643
644 \begin_layout Plain Layout
645
646 <<Transform noweb for re\SpecialCharNoPassThru LyX
647 >>=
648 \end_layout
649
650 \begin_layout Plain Layout
651
652 <<Setup INPUT and OUTPUT>>
653 \end_layout
654
655 \begin_layout Plain Layout
656
657 inputline: while(<INPUT>)
658 \end_layout
659
660 \begin_layout Plain Layout
661
662 {
663 \end_layout
664
665 \begin_layout Plain Layout
666
667   if (/^
668 \backslash
669 s*
670 \backslash
671 <
672 \backslash
673 <.*
674 \backslash
675 >
676 \backslash
677 >=/) { # Beginning of a noweb chunk
678 \end_layout
679
680 \begin_layout Plain Layout
681
682     <<Read in and output the noweb code chunk>>
683 \end_layout
684
685 \begin_layout Plain Layout
686
687   } elsif (/^@
688 \backslash
689 s+(.*)/) { # Beginning of a documentation chunk
690 \end_layout
691
692 \begin_layout Plain Layout
693
694     print OUTPUT $1; # We do not need the ``@'' part
695 \end_layout
696
697 \begin_layout Plain Layout
698
699   } elsif (/
700 \backslash
701 [
702 \backslash
703 [.+
704 \backslash
705 ]
706 \backslash
707 ]/) { # noweb quoted code
708 \end_layout
709
710 \begin_layout Plain Layout
711
712     <<Perform special input quoting of [[var]]>>
713 \end_layout
714
715 \begin_layout Plain Layout
716
717   } else {
718 \end_layout
719
720 \begin_layout Plain Layout
721
722     print OUTPUT; # Just let the line pass through
723 \end_layout
724
725 \begin_layout Plain Layout
726
727   }
728 \end_layout
729
730 \begin_layout Plain Layout
731
732 }
733 \end_layout
734
735 \begin_layout Plain Layout
736
737 <<Close INPUT and OUTPUT>>
738 \end_layout
739
740 \end_inset
741
742
743 \end_layout
744
745 \begin_layout Standard
746 In the code above, we do some pre-processing of the noweb ``[[...]]'' construct.
747  This avoids some problems with re\SpecialCharNoPassThru LyX
748  confusing lists composed of ``[[...]]''
749  constructs.
750 \end_layout
751
752 \begin_layout Standard
753 \begin_inset ERT
754 status open
755
756 \begin_layout Plain Layout
757
758 <<Perform special input quoting of [[var]]>>=
759 \end_layout
760
761 \begin_layout Plain Layout
762
763 s/
764 \backslash
765 [
766 \backslash
767 [.+?
768 \backslash
769 ]{2,}/{$&}/g;
770 \end_layout
771
772 \begin_layout Plain Layout
773
774 print OUTPUT;
775 \end_layout
776
777 \begin_layout Plain Layout
778
779 @
780 \end_layout
781
782 \end_inset
783
784
785 \end_layout
786
787 \begin_layout Standard
788 While reading in the 
789 \family roman
790 \series medium
791 \shape up
792 \size normal
793 \emph off
794 \bar no
795 \noun off
796 \color none
797
798 \begin_inset ERT
799 status collapsed
800
801 \begin_layout Plain Layout
802
803 [[INPUT]]
804 \end_layout
805
806 \end_inset
807
808
809 \family default
810 \series default
811 \shape default
812 \size default
813 \emph default
814 \bar default
815 \noun default
816  
817 \color inherit
818 file, once we have identified a 
819 \noun on
820 noweb
821 \noun default
822  code chunk, we transform it into a form that is usable by re\SpecialCharNoPassThru LyX
823 .
824 \end_layout
825
826 \begin_layout Standard
827 \begin_inset Flex Chunk
828 status open
829
830 \begin_layout Plain Layout
831
832 \begin_inset Argument 1
833 status open
834
835 \begin_layout Plain Layout
836 Read in and output the noweb code chunk
837 \end_layout
838
839 \end_inset
840
841 <<Save the beginning of the chunk to savedchunk>>
842 \end_layout
843
844 \begin_layout Plain Layout
845
846 <<Concatenate the rest of the chunk>>
847 \end_layout
848
849 \begin_layout Plain Layout
850
851 <<print out the chunk in a re\SpecialCharNoPassThru LyX
852 skip block>>
853 \end_layout
854
855 \end_inset
856
857
858 \end_layout
859
860 \begin_layout Subsection
861 File input and output for the pre-processing step
862 \end_layout
863
864 \begin_layout Standard
865 In 
866 \noun on
867 noweb2lyx
868 \noun default
869 , we will use 
870 \family roman
871 \series medium
872 \shape up
873 \size normal
874 \emph off
875 \bar no
876 \noun off
877 \color none
878
879 \begin_inset ERT
880 status collapsed
881
882 \begin_layout Plain Layout
883
884 [[INPUT]]
885 \end_layout
886
887 \end_inset
888
889
890 \family default
891 \series default
892 \shape default
893 \size default
894 \emph default
895 \bar default
896 \noun default
897  
898 \color inherit
899 and
900 \color none
901  
902 \family roman
903 \series medium
904 \shape up
905 \size normal
906 \emph off
907 \bar no
908 \noun off
909
910 \begin_inset ERT
911 status collapsed
912
913 \begin_layout Plain Layout
914
915 [[OUTPUT]]
916 \end_layout
917
918 \end_inset
919
920
921 \family default
922 \series default
923 \shape default
924 \size default
925 \emph default
926 \bar default
927 \noun default
928  
929 \color inherit
930 to read and write files.
931  In the code fragment above, we need to read from the input file and write
932  to a file that will be later transformed by re\SpecialCharNoPassThru LyX
933 .
934  If we are being called only to pre-process the input file, then there is
935  no need to create a temporary file.
936 \end_layout
937
938 \begin_layout Standard
939 \begin_inset Flex Chunk
940 status open
941
942 \begin_layout Plain Layout
943
944 \begin_inset Argument 1
945 status open
946
947 \begin_layout Plain Layout
948 Setup INPUT and OUTPUT
949 \end_layout
950
951 \end_inset
952
953 if ($pre_only) {
954 \end_layout
955
956 \begin_layout Plain Layout
957
958   &setup_files($input_file, $output_file);
959 \end_layout
960
961 \begin_layout Plain Layout
962
963 } else {
964 \end_layout
965
966 \begin_layout Plain Layout
967
968   $relyx_file = "temp$$";
969 \end_layout
970
971 \begin_layout Plain Layout
972
973   &setup_files($input_file, $relyx_file);
974 \end_layout
975
976 \begin_layout Plain Layout
977
978 }
979 \end_layout
980
981 \end_inset
982
983
984 \end_layout
985
986 \begin_layout Standard
987 This code uses a small perl subroutine, 
988 \family roman
989 \series medium
990 \shape up
991 \size normal
992 \emph off
993 \bar no
994 \noun off
995 \color none
996
997 \begin_inset ERT
998 status collapsed
999
1000 \begin_layout Plain Layout
1001
1002 [[setup_files]]
1003 \end_layout
1004
1005 \end_inset
1006
1007
1008 \family default
1009 \series default
1010 \shape default
1011 \size default
1012 \emph default
1013 \bar default
1014 \noun default
1015 \color inherit
1016 , which we define below:
1017 \end_layout
1018
1019 \begin_layout Standard
1020 \begin_inset ERT
1021 status open
1022
1023 \begin_layout Plain Layout
1024
1025 <<Subroutines>>=
1026 \end_layout
1027
1028 \begin_layout Plain Layout
1029
1030 sub setup_files {
1031 \end_layout
1032
1033 \begin_layout Plain Layout
1034
1035   my($in, $out) = @_;
1036 \end_layout
1037
1038 \begin_layout Plain Layout
1039
1040   open(INPUT, "<$in") || die "Cannot read $in: $!
1041 \backslash
1042 n";
1043 \end_layout
1044
1045 \begin_layout Plain Layout
1046
1047   open(OUTPUT, ">$out") || die "Cannot write $out: $!
1048 \backslash
1049 n";
1050 \end_layout
1051
1052 \begin_layout Plain Layout
1053
1054 }
1055 \end_layout
1056
1057 \begin_layout Plain Layout
1058
1059 @ %def setup_files
1060 \end_layout
1061
1062 \end_inset
1063
1064
1065 \end_layout
1066
1067 \begin_layout Subsection
1068 Reading in the 
1069 \noun on
1070 noweb
1071 \noun default
1072  chunk
1073 \end_layout
1074
1075 \begin_layout Standard
1076 After we see the beginning of the chunk, we need to read in and save the
1077  rest of the chunk for output.
1078 \end_layout
1079
1080 \begin_layout Standard
1081 \begin_inset Flex Chunk
1082 status open
1083
1084 \begin_layout Plain Layout
1085
1086 \begin_inset Argument 1
1087 status open
1088
1089 \begin_layout Plain Layout
1090 Save the beginning of the chunk to savedchunk
1091 \end_layout
1092
1093 \end_inset
1094
1095 $savedchunk = $_;
1096 \end_layout
1097
1098 \begin_layout Plain Layout
1099
1100 $endLine = "";
1101 \end_layout
1102
1103 \end_inset
1104
1105
1106 \end_layout
1107
1108 \begin_layout Standard
1109 \begin_inset ERT
1110 status open
1111
1112 \begin_layout Plain Layout
1113
1114 <<Concatenate the rest of the chunk>>=
1115 \end_layout
1116
1117 \begin_layout Plain Layout
1118
1119 chunkline: while (<INPUT>) {
1120 \end_layout
1121
1122 \begin_layout Plain Layout
1123
1124  last chunkline if /^@
1125 \backslash
1126 s+/;
1127 \end_layout
1128
1129 \begin_layout Plain Layout
1130
1131  $savedchunk .= $_;
1132 \end_layout
1133
1134 \begin_layout Plain Layout
1135
1136 };
1137 \end_layout
1138
1139 \begin_layout Plain Layout
1140
1141 switch: {
1142 \end_layout
1143
1144 \begin_layout Plain Layout
1145
1146  if (/^@
1147 \backslash
1148 s+$/) {$savedchunk .= $_; last switch; }
1149 \end_layout
1150
1151 \begin_layout Plain Layout
1152
1153  if (/^@
1154 \backslash
1155 s+%def.*$/) {$savedchunk .= $_; last switch; }
1156 \end_layout
1157
1158 \begin_layout Plain Layout
1159
1160  if (/^@
1161 \backslash
1162 s+(.*)$/) {$savedchunk .= "@
1163 \backslash
1164 n"; $endLine = "$1
1165 \backslash
1166 n"; }
1167 \end_layout
1168
1169 \begin_layout Plain Layout
1170
1171 }
1172 \end_layout
1173
1174 \begin_layout Plain Layout
1175
1176 @
1177 \end_layout
1178
1179 \end_inset
1180
1181
1182 \end_layout
1183
1184 \begin_layout Subsection
1185 Printing out the chunk
1186 \end_layout
1187
1188 \begin_layout Standard
1189 The final piece of the first pass of the conversion is done by this code.
1190 \end_layout
1191
1192 \begin_layout Standard
1193 \begin_inset Flex Chunk
1194 status open
1195
1196 \begin_layout Plain Layout
1197
1198 \begin_inset Argument 1
1199 status open
1200
1201 \begin_layout Plain Layout
1202 print out the chunk in a re\SpecialCharNoPassThru LyX
1203 skip block
1204 \end_layout
1205
1206 \end_inset
1207
1208 print OUTPUT "
1209 \backslash
1210
1211 \backslash
1212 begin{re\SpecialCharNoPassThru LyX
1213 skip}
1214 \backslash
1215 n";
1216 \end_layout
1217
1218 \begin_layout Plain Layout
1219
1220 print OUTPUT $savedchunk;
1221 \end_layout
1222
1223 \begin_layout Plain Layout
1224
1225 print OUTPUT "
1226 \backslash
1227
1228 \backslash
1229 end{re\SpecialCharNoPassThru LyX
1230 skip}
1231 \backslash
1232 n
1233 \backslash
1234 n";
1235 \end_layout
1236
1237 \begin_layout Plain Layout
1238
1239 print OUTPUT "$endLine";
1240 \end_layout
1241
1242 \end_inset
1243
1244
1245 \end_layout
1246
1247 \begin_layout Standard
1248 Finally, we need to close the 
1249 \family roman
1250 \series medium
1251 \shape up
1252 \size normal
1253 \emph off
1254 \bar no
1255 \noun off
1256 \color none
1257
1258 \begin_inset ERT
1259 status collapsed
1260
1261 \begin_layout Plain Layout
1262
1263 [[INPUT]]
1264 \end_layout
1265
1266 \end_inset
1267
1268
1269 \family default
1270 \series default
1271 \shape default
1272 \size default
1273 \emph default
1274 \bar default
1275 \noun default
1276  
1277 \color inherit
1278 and
1279 \color none
1280  
1281 \family roman
1282 \series medium
1283 \shape up
1284 \size normal
1285 \emph off
1286 \bar no
1287 \noun off
1288
1289 \begin_inset ERT
1290 status collapsed
1291
1292 \begin_layout Plain Layout
1293
1294 [[OUTPUT]]
1295 \end_layout
1296
1297 \end_inset
1298
1299
1300 \family default
1301 \series default
1302 \shape default
1303 \size default
1304 \emph default
1305 \bar default
1306 \noun default
1307  
1308 \color inherit
1309 files.
1310 \end_layout
1311
1312 \begin_layout Standard
1313 \begin_inset Flex Chunk
1314 status open
1315
1316 \begin_layout Plain Layout
1317
1318 \begin_inset Argument 1
1319 status open
1320
1321 \begin_layout Plain Layout
1322 Close INPUT and OUTPUT
1323 \end_layout
1324
1325 \end_inset
1326
1327 close(INPUT);
1328 \end_layout
1329
1330 \begin_layout Plain Layout
1331
1332 close(OUTPUT);
1333 \end_layout
1334
1335 \end_inset
1336
1337
1338 \end_layout
1339
1340 \begin_layout Section
1341 Running re\SpecialCharNoPassThru LyX
1342 \end_layout
1343
1344 \begin_layout Standard
1345 In this section, we describe and implement the code that runs re\SpecialCharNoPassThru LyX
1346  on the
1347  intermediate file 
1348 \family roman
1349 \series medium
1350 \shape up
1351 \size normal
1352 \emph off
1353 \bar no
1354 \noun off
1355 \color none
1356
1357 \begin_inset ERT
1358 status collapsed
1359
1360 \begin_layout Plain Layout
1361
1362 [[relyx_file]]
1363 \end_layout
1364
1365 \end_inset
1366
1367
1368 \family default
1369 \series default
1370 \shape default
1371 \size default
1372 \emph default
1373 \bar default
1374 \noun default
1375 \color inherit
1376 .
1377  
1378 \end_layout
1379
1380 \begin_layout Subsection
1381 Selecting the document class
1382 \end_layout
1383
1384 \begin_layout Standard
1385 In order to run re\SpecialCharNoPassThru LyX
1386 , we need to know the article class of the input document
1387  (to choose the corresponding literate document layout).
1388  For this, we need to parse the intermediate file.
1389 \end_layout
1390
1391 \begin_layout Standard
1392 \begin_inset Flex Chunk
1393 status open
1394
1395 \begin_layout Plain Layout
1396
1397 \begin_inset Argument 1
1398 status open
1399
1400 \begin_layout Plain Layout
1401 Run re\SpecialCharNoPassThru LyX
1402  on intermediate file
1403 \end_layout
1404
1405 \end_inset
1406
1407 <<Parse for document class>>
1408 \end_layout
1409
1410 \begin_layout Plain Layout
1411
1412 <<Run re\SpecialCharNoPassThru LyX
1413  with document class>>
1414 \end_layout
1415
1416 \end_inset
1417
1418
1419 \end_layout
1420
1421 \begin_layout Standard
1422 In the code below, you'll see a strange regular expression to search for
1423  the document class.
1424  The reason for this kludge is that without it, we can't run 
1425 \noun on
1426 noweb2lyx
1427 \noun default
1428  on the 
1429 \emph on
1430 noweb2lyx.nw
1431 \emph default
1432  file that is generated by \SpecialCharNoPassThru LyX
1433 \begin_inset Foot
1434 status collapsed
1435
1436 \begin_layout Plain Layout
1437 re\SpecialCharNoPassThru LyX
1438  searches for 
1439 \backslash
1440
1441 \backslash
1442 doc
1443 \family roman
1444 \series medium
1445 \shape up
1446 \size normal
1447 \emph off
1448 \bar no
1449 \noun off
1450 \color none
1451
1452 \begin_inset ERT
1453 status collapsed
1454
1455 \begin_layout Plain Layout
1456
1457 {}
1458 \end_layout
1459
1460 \end_inset
1461
1462
1463 \family default
1464 \series default
1465 \shape default
1466 \size default
1467 \emph default
1468 \bar default
1469 \noun default
1470 \color inherit
1471 ument
1472 \family roman
1473 \series medium
1474 \shape up
1475 \size normal
1476 \emph off
1477 \bar no
1478 \noun off
1479 \color none
1480
1481 \begin_inset ERT
1482 status collapsed
1483
1484 \begin_layout Plain Layout
1485
1486 {}
1487 \end_layout
1488
1489 \end_inset
1490
1491
1492 \family default
1493 \series default
1494 \shape default
1495 \size default
1496 \emph default
1497 \bar default
1498 \noun default
1499 \color inherit
1500 class and gets confused, so we have to obfuscate it slightly.
1501 \end_layout
1502
1503 \end_inset
1504
1505 .
1506  With the regular expression as it is, we can actually run 
1507 \noun on
1508 noweb2lyx
1509 \noun default
1510  on itself and a produce a quite reasonable \SpecialCharNoPassThru LyX
1511  file.
1512 \end_layout
1513
1514 \begin_layout Standard
1515 \begin_inset Flex Chunk
1516 status open
1517
1518 \begin_layout Plain Layout
1519
1520 \begin_inset Argument 1
1521 status open
1522
1523 \begin_layout Plain Layout
1524 Parse for document class
1525 \end_layout
1526
1527 \end_inset
1528
1529 open(INPUT, "<$relyx_file") ||
1530 \end_layout
1531
1532 \begin_layout Plain Layout
1533
1534   die "Cannot read $relyx_file: $!
1535 \backslash
1536 n";
1537 \end_layout
1538
1539 \begin_layout Plain Layout
1540
1541 $class = "article"; # default if none found
1542 \end_layout
1543
1544 \begin_layout Plain Layout
1545
1546 parse: while(<INPUT>) {
1547 \end_layout
1548
1549 \begin_layout Plain Layout
1550
1551   if (/
1552 \backslash
1553
1554 \backslash
1555 docu[m]entclass{(.*)}/) {
1556 \end_layout
1557
1558 \begin_layout Plain Layout
1559
1560     $class = $1;
1561 \end_layout
1562
1563 \begin_layout Plain Layout
1564
1565     last parse;
1566 \end_layout
1567
1568 \begin_layout Plain Layout
1569
1570   }
1571 \end_layout
1572
1573 \begin_layout Plain Layout
1574
1575 }
1576 \end_layout
1577
1578 \begin_layout Plain Layout
1579
1580 close(INPUT);
1581 \end_layout
1582
1583 \end_inset
1584
1585
1586 \end_layout
1587
1588 \begin_layout Subsection
1589 Running re\SpecialCharNoPassThru LyX
1590  with the corresponding literate document layout
1591 \end_layout
1592
1593 \begin_layout Standard
1594 Now that we know what the document class ought to be, we do:
1595 \end_layout
1596
1597 \begin_layout Standard
1598 \begin_inset ERT
1599 status open
1600
1601 \begin_layout Plain Layout
1602
1603 <<Run re\SpecialCharNoPassThru LyX
1604  with document class>>= 
1605 \end_layout
1606
1607 \begin_layout Plain Layout
1608
1609 $doc_class = "literate-" .
1610  $class;
1611 \end_layout
1612
1613 \begin_layout Plain Layout
1614
1615 die "re\SpecialCharNoPassThru LyX
1616  returned non-zero: $!
1617 \backslash
1618 n"
1619 \end_layout
1620
1621 \begin_layout Plain Layout
1622
1623   if (system("re\SpecialCharNoPassThru LyX
1624  -c $doc_class $relyx_file"));
1625 \end_layout
1626
1627 \begin_layout Plain Layout
1628
1629 @
1630 \end_layout
1631
1632 \end_inset
1633
1634
1635 \end_layout
1636
1637 \begin_layout Standard
1638 re\SpecialCharNoPassThru LyX
1639  performs the main bulk of the translation work.
1640  Note that if the ``literate-
1641 \emph on
1642 class
1643 \emph default
1644 '' document layout is not found, then re\SpecialCharNoPassThru LyX
1645  will fail with an error.
1646  In that case, you may need to modify your 
1647 \noun on
1648 noweb
1649 \noun default
1650  input file to a supported document type.
1651 \end_layout
1652
1653 \begin_layout Section
1654 Fixing the re\SpecialCharNoPassThru LyX
1655  output
1656 \end_layout
1657
1658 \begin_layout Standard
1659 We need to perform some post-processing of what re\SpecialCharNoPassThru LyX
1660  produces in order
1661  to have the best output for our literate document.
1662  The outline of the post-processing steps are:
1663 \end_layout
1664
1665 \begin_layout Standard
1666 \begin_inset Flex Chunk
1667 status open
1668
1669 \begin_layout Plain Layout
1670
1671 \begin_inset Argument 1
1672 status open
1673
1674 \begin_layout Plain Layout
1675 Fix up \SpecialCharNoPassThru LyX
1676  file
1677 \end_layout
1678
1679 \end_inset
1680
1681 <<Setup INPUT and OUTPUT for the final output>>
1682 \end_layout
1683
1684 \begin_layout Plain Layout
1685
1686 line: while(<INPUT>)
1687 \end_layout
1688
1689 \begin_layout Plain Layout
1690
1691 {
1692 \end_layout
1693
1694 \begin_layout Plain Layout
1695
1696   <<Fix code chunks in latex layout>>
1697 \end_layout
1698
1699 \begin_layout Plain Layout
1700
1701   <<Fix [[var]] noweb construct>>
1702 \end_layout
1703
1704 \begin_layout Plain Layout
1705
1706   print OUTPUT; # default
1707 \end_layout
1708
1709 \begin_layout Plain Layout
1710
1711
1712 \end_layout
1713
1714 \begin_layout Plain Layout
1715
1716 <<Close INPUT and OUTPUT>>
1717 \end_layout
1718
1719 \end_inset
1720
1721
1722 \end_layout
1723
1724 \begin_layout Standard
1725 Note that in the perl code that is contained in the 
1726 \family roman
1727 \series medium
1728 \shape up
1729 \size normal
1730 \emph off
1731 \bar no
1732 \noun off
1733 \color none
1734
1735 \begin_inset ERT
1736 status collapsed
1737
1738 \begin_layout Plain Layout
1739
1740 [[while(<INPUT>)]]
1741 \end_layout
1742
1743 \end_inset
1744
1745
1746 \family default
1747 \series default
1748 \shape default
1749 \size default
1750 \emph default
1751 \bar default
1752 \noun default
1753  
1754 \color inherit
1755 loop above, the perl construct
1756 \color none
1757  
1758 \family roman
1759 \series medium
1760 \shape up
1761 \size normal
1762 \emph off
1763 \bar no
1764 \noun off
1765
1766 \begin_inset ERT
1767 status collapsed
1768
1769 \begin_layout Plain Layout
1770
1771 [[next line]]
1772 \end_layout
1773
1774 \end_inset
1775
1776
1777 \family default
1778 \series default
1779 \shape default
1780 \size default
1781 \emph default
1782 \bar default
1783 \noun default
1784  
1785 \color inherit
1786 is sufficient to restart the loop.
1787  We can use this construct to do some relatively complex parsing of the
1788  re\SpecialCharNoPassThru LyX
1789  generated file.
1790 \end_layout
1791
1792 \begin_layout Subsection
1793 File input and output for the post-processing
1794 \end_layout
1795
1796 \begin_layout Standard
1797 Setting up the 
1798 \family roman
1799 \series medium
1800 \shape up
1801 \size normal
1802 \emph off
1803 \bar no
1804 \noun off
1805 \color none
1806
1807 \begin_inset ERT
1808 status collapsed
1809
1810 \begin_layout Plain Layout
1811
1812 [[INPUT]]
1813 \end_layout
1814
1815 \end_inset
1816
1817
1818 \family default
1819 \series default
1820 \shape default
1821 \size default
1822 \emph default
1823 \bar default
1824 \noun default
1825  
1826 \color inherit
1827 and
1828 \color none
1829  
1830 \family roman
1831 \series medium
1832 \shape up
1833 \size normal
1834 \emph off
1835 \bar no
1836 \noun off
1837
1838 \begin_inset ERT
1839 status collapsed
1840
1841 \begin_layout Plain Layout
1842
1843 [[OUTPUT]]
1844 \end_layout
1845
1846 \end_inset
1847
1848
1849 \family default
1850 \series default
1851 \shape default
1852 \size default
1853 \emph default
1854 \bar default
1855 \noun default
1856  
1857 \color inherit
1858 is taken care of by this code:
1859 \end_layout
1860
1861 \begin_layout Standard
1862 \begin_inset Flex Chunk
1863 status open
1864
1865 \begin_layout Plain Layout
1866
1867 \begin_inset Argument 1
1868 status open
1869
1870 \begin_layout Plain Layout
1871 Setup INPUT and OUTPUT for the final output
1872 \end_layout
1873
1874 \end_inset
1875
1876 if ($post_only) {
1877 \end_layout
1878
1879 \begin_layout Plain Layout
1880
1881   &setup_files("$input_file", "$output_file");
1882 \end_layout
1883
1884 \begin_layout Plain Layout
1885
1886 } else {
1887 \end_layout
1888
1889 \begin_layout Plain Layout
1890
1891   &setup_files("$relyx_file.lyx", "$output_file");
1892 \end_layout
1893
1894 \begin_layout Plain Layout
1895
1896 }
1897 \end_layout
1898
1899 \end_inset
1900
1901
1902 \end_layout
1903
1904 \begin_layout Subsection
1905 Making sure the code chunks are in the Chunk layout
1906 \end_layout
1907
1908 \begin_layout Standard
1909 Now, as we outlined before, the final step is transforming the code-chunks
1910  which have been put into a \SpecialCharNoPassThru LaTeX
1911  layout by \SpecialCharNoPassThru LyX
1912  into the Chunk layout.
1913 \end_layout
1914
1915 \begin_layout Standard
1916 \begin_inset Flex Chunk
1917 status open
1918
1919 \begin_layout Plain Layout
1920
1921 \begin_inset Argument 1
1922 status open
1923
1924 \begin_layout Plain Layout
1925 Fix code chunks in latex layout
1926 \end_layout
1927
1928 \end_inset
1929
1930 if (/
1931 \backslash
1932
1933 \backslash
1934 latex latex/) { # Beginning of some latex code
1935 \end_layout
1936
1937 \begin_layout Plain Layout
1938
1939   if (($line = <INPUT>) =~ /^
1940 \backslash
1941 s*<</) { # code chunk
1942 \end_layout
1943
1944 \begin_layout Plain Layout
1945
1946     <<Transform this chunk into layout chunk>>
1947 \end_layout
1948
1949 \begin_layout Plain Layout
1950
1951   } else {
1952 \end_layout
1953
1954 \begin_layout Plain Layout
1955
1956     # print the 
1957 \backslash
1958 latex latex line + next line
1959 \end_layout
1960
1961 \begin_layout Plain Layout
1962
1963     print OUTPUT "$_$line";
1964 \end_layout
1965
1966 \begin_layout Plain Layout
1967
1968   }
1969 \end_layout
1970
1971 \begin_layout Plain Layout
1972
1973   next line;
1974 \end_layout
1975
1976 \begin_layout Plain Layout
1977
1978 }
1979 \end_layout
1980
1981 \end_inset
1982
1983
1984 \end_layout
1985
1986 \begin_layout Standard
1987 When we are sure that we are in a code chunk, we must read in the rest of
1988  the code chunk and output a chunk layout for it:
1989 \end_layout
1990
1991 \begin_layout Standard
1992 \begin_inset ERT
1993 status open
1994
1995 \begin_layout Plain Layout
1996
1997 <<Transform this chunk into layout chunk>>=
1998 \end_layout
1999
2000 \begin_layout Plain Layout
2001
2002 \end_layout
2003
2004 \begin_layout Plain Layout
2005
2006 $savedchunk = "
2007 \backslash
2008
2009 \backslash
2010 layout Chunk
2011 \backslash
2012 n
2013 \backslash
2014 n$line";
2015 \end_layout
2016
2017 \begin_layout Plain Layout
2018
2019 codeline: while (<INPUT>) {
2020 \end_layout
2021
2022 \begin_layout Plain Layout
2023
2024  $savedchunk .= $_;
2025 \end_layout
2026
2027 \begin_layout Plain Layout
2028
2029  last codeline if /^@
2030 \backslash
2031 s+/;
2032 \end_layout
2033
2034 \begin_layout Plain Layout
2035
2036 };
2037 \end_layout
2038
2039 \begin_layout Plain Layout
2040
2041 print OUTPUT $savedchunk;
2042 \end_layout
2043
2044 \begin_layout Plain Layout
2045
2046 <<Slurp up to the end of the latex layout>>
2047 \end_layout
2048
2049 \begin_layout Plain Layout
2050
2051 @
2052 \end_layout
2053
2054 \end_inset
2055
2056
2057 \end_layout
2058
2059 \begin_layout Standard
2060 Okay, now we just need to eat the rest of the latex layout.
2061  There should only be a few different types of lines for us to match:
2062 \end_layout
2063
2064 \begin_layout Standard
2065 \begin_inset Flex Chunk
2066 status open
2067
2068 \begin_layout Plain Layout
2069
2070 \begin_inset Argument 1
2071 status open
2072
2073 \begin_layout Plain Layout
2074 Slurp up to the end of the latex layout
2075 \end_layout
2076
2077 \end_inset
2078
2079 slurp: while (<INPUT>) {
2080 \end_layout
2081
2082 \begin_layout Plain Layout
2083
2084   last slurp if /
2085 \backslash
2086
2087 \backslash
2088 latex /;
2089 \end_layout
2090
2091 \begin_layout Plain Layout
2092
2093   next slurp if /
2094 \backslash
2095
2096 \backslash
2097 newline/;
2098 \end_layout
2099
2100 \begin_layout Plain Layout
2101
2102   next slurp if /^
2103 \backslash
2104 s*$/;
2105 \end_layout
2106
2107 \begin_layout Plain Layout
2108
2109   warn "confused by line: $_";
2110 \end_layout
2111
2112 \begin_layout Plain Layout
2113
2114 }
2115 \end_layout
2116
2117 \end_inset
2118
2119
2120 \end_layout
2121
2122 \begin_layout Subsection
2123 Taking care of the 
2124 \noun on
2125 noweb
2126 \noun default
2127  
2128 \emph on
2129 [[quoted code]]
2130 \emph default
2131  construct
2132 \end_layout
2133
2134 \begin_layout Standard
2135
2136 \noun on
2137 noweb
2138 \noun default
2139  allows the user to use a special code quoting mechanism in documentation
2140  chunks.
2141  Fixing this ``[[quoted-code]]'' 
2142 \noun on
2143 noweb
2144 \noun default
2145  syntax means putting the ``[[quoted-code]]'' in a \SpecialCharNoPassThru LaTeX
2146  layout in the \SpecialCharNoPassThru LyX
2147  file.
2148  Otherwise, \SpecialCharNoPassThru LyX
2149  will backslash-quote the brackets, creating ugly output.
2150  The quoted-code is transformed by 
2151 \noun on
2152 noweb
2153 \noun default
2154  when it generates the final \SpecialCharNoPassThru LaTeX
2155  code.
2156 \end_layout
2157
2158 \begin_layout Standard
2159 \begin_inset Flex Chunk
2160 status open
2161
2162 \begin_layout Plain Layout
2163
2164 \begin_inset Argument 1
2165 status open
2166
2167 \begin_layout Plain Layout
2168 Fix [[var]] noweb construct
2169 \end_layout
2170
2171 \end_inset
2172
2173 if (/
2174 \backslash
2175 [
2176 \backslash
2177 [.+
2178 \backslash
2179 ]
2180 \backslash
2181 ]/) { # special code for [[var]]
2182 \end_layout
2183
2184 \begin_layout Plain Layout
2185
2186   s/
2187 \backslash
2188 [
2189 \backslash
2190 [.+?
2191 \backslash
2192 ]{2,}/
2193 \backslash
2194 n
2195 \backslash
2196
2197 \backslash
2198 latex latex
2199 \backslash
2200 n$&
2201 \backslash
2202 n
2203 \backslash
2204
2205 \backslash
2206 latex default
2207 \backslash
2208 n/g;
2209 \end_layout
2210
2211 \begin_layout Plain Layout
2212
2213   print OUTPUT;
2214 \end_layout
2215
2216 \begin_layout Plain Layout
2217
2218   next line;
2219 \end_layout
2220
2221 \begin_layout Plain Layout
2222
2223 }
2224 \end_layout
2225
2226 \end_inset
2227
2228
2229 \end_layout
2230
2231 \begin_layout Section
2232 Cleaning up intermediate files
2233 \end_layout
2234
2235 \begin_layout Standard
2236 The cleanup code is very simple:
2237 \end_layout
2238
2239 \begin_layout Standard
2240 \begin_inset Flex Chunk
2241 status open
2242
2243 \begin_layout Plain Layout
2244
2245 \begin_inset Argument 1
2246 status open
2247
2248 \begin_layout Plain Layout
2249 Clean up
2250 \end_layout
2251
2252 \end_inset
2253
2254 system("rm -f $relyx_file*") unless ($post_only || $pre_only);
2255 \end_layout
2256
2257 \end_inset
2258
2259
2260 \end_layout
2261
2262 \begin_layout Section
2263 User supplied arguments
2264 \end_layout
2265
2266 \begin_layout Standard
2267 The 
2268 \noun on
2269 noweb2lyx
2270 \noun default
2271  script understands two arguments, input-file and output-file.
2272  It is also set up to be used internally by re\SpecialCharNoPassThru LyX
2273  to pre-process or postprocess
2274  files in the import pipeline.
2275 \end_layout
2276
2277 \begin_layout Standard
2278 \begin_inset ERT
2279 status open
2280
2281 \begin_layout Plain Layout
2282
2283 <<Setup variables from user supplied args>>=
2284 \end_layout
2285
2286 \begin_layout Plain Layout
2287
2288 &usage() if ($#ARGV < 1); # zero or one argument 
2289 \end_layout
2290
2291 \begin_layout Plain Layout
2292
2293 if ($ARGV[0] eq "-pre") {
2294 \end_layout
2295
2296 \begin_layout Plain Layout
2297
2298   &usage unless ($#ARGV == 2);
2299 \end_layout
2300
2301 \begin_layout Plain Layout
2302
2303   $input_file = $ARGV[1]; $output_file = $ARGV[2]; $pre_only = 1;
2304 \end_layout
2305
2306 \begin_layout Plain Layout
2307
2308 } elsif ($ARGV[0] eq "-post") {
2309 \end_layout
2310
2311 \begin_layout Plain Layout
2312
2313   &usage unless ($#ARGV == 2);
2314 \end_layout
2315
2316 \begin_layout Plain Layout
2317
2318   $input_file = $ARGV[1]; $output_file = $ARGV[2]; $post_only = 1;
2319 \end_layout
2320
2321 \begin_layout Plain Layout
2322
2323 } else {
2324 \end_layout
2325
2326 \begin_layout Plain Layout
2327
2328   &usage unless ($#ARGV == 1);
2329 \end_layout
2330
2331 \begin_layout Plain Layout
2332
2333   $input_file = $ARGV[0]; $output_file = $ARGV[1];
2334 \end_layout
2335
2336 \begin_layout Plain Layout
2337
2338   $pre_only = 0; $post_only = 0;
2339 \end_layout
2340
2341 \begin_layout Plain Layout
2342
2343 }
2344 \end_layout
2345
2346 \begin_layout Plain Layout
2347
2348 @ %def input_file output_file pre_only post_only
2349 \end_layout
2350
2351 \begin_layout Plain Layout
2352
2353 <<Subroutines>>=
2354 \end_layout
2355
2356 \begin_layout Plain Layout
2357
2358 sub usage() {
2359 \end_layout
2360
2361 \begin_layout Plain Layout
2362
2363   print "Usage: noweb2lyx [-pre | -post] input-file output-file
2364 \end_layout
2365
2366 \begin_layout Plain Layout
2367
2368 \end_layout
2369
2370 \begin_layout Plain Layout
2371
2372 If -pre is specified, only pre-processes the input-file for re\SpecialCharNoPassThru LyX
2373 .
2374 \end_layout
2375
2376 \begin_layout Plain Layout
2377
2378 Similarly, in the case of -post, post-processes re\SpecialCharNoPassThru LyX
2379  output.
2380 \end_layout
2381
2382 \begin_layout Plain Layout
2383
2384 In case of bugs, Email Kayvan Sylvan <kayvan
2385 \backslash
2386 @sylvan.com>.
2387 \backslash
2388 n";
2389 \end_layout
2390
2391 \begin_layout Plain Layout
2392
2393   exit;
2394 \end_layout
2395
2396 \begin_layout Plain Layout
2397
2398 }
2399 \end_layout
2400
2401 \begin_layout Plain Layout
2402
2403 @ %def usage
2404 \end_layout
2405
2406 \end_inset
2407
2408
2409 \end_layout
2410
2411 \begin_layout Section
2412 Generating the 
2413 \noun on
2414 noweb2lyx
2415 \noun default
2416  script
2417 \end_layout
2418
2419 \begin_layout Standard
2420 The noweb2lyx script can be tangled from \SpecialCharNoPassThru LyX
2421  if you set 
2422 \family typewriter
2423
2424 \backslash
2425 build_command
2426 \family default
2427  to call a generic script that always extracts a chunk named 
2428 \family typewriter
2429 build-script
2430 \family default
2431  and executes it.
2432  Here is an example of such a script:
2433 \end_layout
2434
2435 \begin_layout LyX-Code
2436 #!/bin/sh
2437 \begin_inset Newline newline
2438 \end_inset
2439
2440 notangle -Rbuild-script $1 | env NOWEB_SOURCE=$1 NOWEB_OUTPUT_DIR=$2 sh
2441 \end_layout
2442
2443 \begin_layout Standard
2444 \begin_inset Flex Chunk
2445 status open
2446
2447 \begin_layout Plain Layout
2448
2449 \begin_inset Argument 1
2450 status open
2451
2452 \begin_layout Plain Layout
2453 build-script
2454 \end_layout
2455
2456 \end_inset
2457
2458 PREFIX=/usr
2459 \end_layout
2460
2461 \begin_layout Plain Layout
2462
2463 notangle -Rnoweb2lyx.in noweb2lyx.nw > noweb2lyx.in
2464 \end_layout
2465
2466 \begin_layout Plain Layout
2467
2468 sed -e "s=@PERL@=$PREFIX/bin/perl=" noweb2lyx.in > noweb2lyx
2469 \end_layout
2470
2471 \begin_layout Plain Layout
2472
2473 chmod +x noweb2lyx
2474 \end_layout
2475
2476 \end_inset
2477
2478
2479 \end_layout
2480
2481 \begin_layout Standard
2482 \begin_inset Newpage newpage
2483 \end_inset
2484
2485
2486 \end_layout
2487
2488 \begin_layout Section*
2489 Macros
2490 \end_layout
2491
2492 \begin_layout Standard
2493
2494 \family roman
2495 \series medium
2496 \shape up
2497 \size normal
2498 \emph off
2499 \bar no
2500 \noun off
2501 \color none
2502 \begin_inset ERT
2503 status collapsed
2504
2505 \begin_layout Plain Layout
2506
2507
2508 \backslash
2509 nowebchunks
2510 \end_layout
2511
2512 \end_inset
2513
2514
2515 \end_layout
2516
2517 \begin_layout Section*
2518 Identifiers
2519 \end_layout
2520
2521 \begin_layout Standard
2522
2523 \family roman
2524 \series medium
2525 \shape up
2526 \size normal
2527 \emph off
2528 \bar no
2529 \noun off
2530 \color none
2531 \begin_inset ERT
2532 status collapsed
2533
2534 \begin_layout Plain Layout
2535
2536
2537 \backslash
2538 nowebindex
2539 \end_layout
2540
2541 \end_inset
2542
2543
2544 \end_layout
2545
2546 \end_body
2547 \end_document