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