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