Initial version of donated sources by Avertec, 3.4p5.
[tas-yagle.git] / distrib / share / etc / xml.awk
1 #!/usr/local/bin/gawk -f
2
3 # This script creates an .xml file from a .txt file
4 # The .txt file should come from an MS-Word .doc file
5 # The .txt file must be saved with the 'LF only' option
6 # (Even with this option, ^M may remain in the .txt file. In this case,
7 # it is necessary to remove them manually).
8 # The .doc file must be well-formed
9
10 BEGIN {
11 line = 0;
12 chapter_opened = 0;
13 section_opened = 0;
14 list_opened = 0;
15 level = 0;
16 }
17 {
18 filexml = "YAG_introduction.xml";
19
20 if (line == 0)
21 printf ("<?xml version=\"1.0\" encoding=\"ISO-8859-1\" ?>\n") > filexml;
22
23 #chapter or section
24 if ((int ($1) >= 1) && (int ($1) <= 9)) {
25
26 #level
27 level = split ($1, word, ".");
28
29 #chapter
30 if (level == 1) {
31 while (section_opened > 0) {
32 printf ("</section>\n\n") > filexml;
33 section_opened--;
34 }
35 if (chapter_opened == 1) {
36 printf ("</chapter>\n\n\n") > filexml;
37 chapter_opened = 0;
38 }
39 printf ("<chapter>\n") > filexml;
40 chapter_opened = 1;
41 }
42
43 #section
44 if (list_opened == 1) {
45 printf ("</list>\n") > filexml;
46 list_opened = 0;
47 }
48 while (section_opened > level - 1) {
49 printf ("</section>\n\n") > filexml;
50 section_opened--;
51 }
52 printf ("<section niv='%d'><title>", level) > filexml;
53 for (i = 2; i < NF; i++) printf ("%s ", $i) > filexml;
54 printf ("%s</title>\n", $NF) > filexml;
55 section_opened++;
56 }
57
58 #body
59 else {
60 if (length ($0) > 0) {
61
62 #list
63 if ($1 == "*") {
64 if (list_opened == 0) {
65 printf ("<list>\n") > filexml;
66 list_opened = 1;
67 }
68 printf ("<item>", level) > filexml;
69 for (i = 2; i < NF; i++) printf ("%s ", $i) > filexml;
70 printf ("%s</item>\n", $NF) > filexml;
71 }
72
73 #other
74 else {
75 if (list_opened == 1) {
76 printf ("</list>\n") > filexml;
77 list_opened = 0;
78 }
79 printf ("<p>%s</p>\n", $0) > filexml;
80 }
81 }
82 }
83
84 line++;
85 }
86
87
88 END {
89 while (section_opened > 0) {
90 printf ("</section>\n\n") > filexml;
91 section_opened--;
92 }
93 if (chapter_opened == 1) {
94 printf ("</chapter>\n\n\n") > filexml;
95 chapter_opened = 0;
96 }
97 }