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