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