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