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