Initial import from FreeBSD RELENG_4:
[dragonfly.git] / contrib / texinfo / makeinfo / docbook.c
1 /* docbook.c -- docbook output.
2    $Id: docbook.c,v 1.3 2001/12/31 16:52:17 karl Exp $
3
4    Copyright (C) 2001 Free Software Foundation, Inc.
5
6    This program is free software; you can redistribute it and/or modify
7    it under the terms of the GNU General Public License as published by
8    the Free Software Foundation; either version 2, or (at your option)
9    any later version.
10
11    This program is distributed in the hope that it will be useful,
12    but WITHOUT ANY WARRANTY; without even the implied warranty of
13    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14    GNU General Public License for more details.
15
16    You should have received a copy of the GNU General Public License
17    along with this program; if not, write to the Free Software Foundation,
18    Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.  */
19
20 #include "system.h"
21 #include "cmds.h"
22 #include "docbook.h"
23 #include "insertion.h"
24 #include "lang.h"
25 #include "makeinfo.h"
26 #include "macro.h"
27 #include "sectioning.h"
28
29 int docbook_version_inserted = 0;
30 int docbook_first_chapter_found = 0;
31 int docbook_must_insert_node_anchor = 0;
32 int docbook_begin_book_p = 0;
33 int docbook_no_new_paragraph = 0;
34
35 static int section_level = -1;
36 static int in_docbook_paragraph = 0;
37 static int in_list = 0;
38
39 static int in_table = 0;
40 static int in_term = 0;
41 static int in_entry = 0;
42 static int in_varlistitem = 0;
43
44 static int in_example = 0;
45
46 void
47 docbook_begin_section (level, cmd)
48      int level;
49      char *cmd;
50 {
51   int i, old_no_indent;
52   char *temp, *tem;
53   static char *last_chap = NULL;
54
55   close_paragraph ();
56   docbook_first_chapter_found = 1;
57   filling_enabled =  indented_fill = 0;
58   old_no_indent = no_indent;
59   no_indent = 1;
60
61   if (!docbook_begin_book_p)
62     docbook_begin_book ();
63
64   if (macro_expansion_output_stream && !executing_string)
65     append_to_expansion_output (input_text_offset + 1);
66
67   get_rest_of_line (0, &temp);
68
69   if (in_docbook_paragraph)
70     {
71       insert_string ("\n</para>\n\n");
72       adjust_braces_following (0, 10);
73     }
74   in_docbook_paragraph = 0;
75   docbook_no_new_paragraph++;
76
77   if (level > section_level + 1)
78     level = section_level + 1;
79
80   for (i = section_level;  i >= level ; i--)
81     {
82       if (i == 0)
83          {
84            if (last_chap && strcmp(last_chap, "appendix") == 0)
85              add_word ("</appendix>\n\n");
86            else
87              add_word ("</chapter>\n\n");
88          }
89       else
90         add_word_args ("</sect%d>\n\n", i);
91     }
92
93   section_level = level;
94
95   if (level == 0)
96     {
97       if (strcmp(cmd, "appendix") == 0)
98         add_word ("<appendix");
99       else
100         add_word ("<chapter");
101       last_chap = cmd;
102     }
103   else
104     add_word_args ("<sect%d", level);
105   
106   if (docbook_must_insert_node_anchor)
107     {
108       add_word (" id=\"");
109       tem = expansion (current_node, 0);
110       add_escaped_anchor_name (tem, 0);
111       free (tem);
112       add_word ("\"");
113       docbook_must_insert_node_anchor = 0;
114     }
115    add_word (">\n");
116    add_word ("<title>");
117
118   if (macro_expansion_output_stream && !executing_string)
119     {
120       char *temp1 = xmalloc (2 + strlen (temp));
121       sprintf (temp1, "%s", temp);
122       remember_itext (input_text, input_text_offset);
123       me_execute_string (temp1);
124       free (temp1);
125     }
126   else
127     execute_string ("%s", temp);
128
129   free (temp);
130
131   add_word ("</title>\n");
132
133   close_paragraph ();
134   filling_enabled = 1;
135   no_indent = old_no_indent;  
136   docbook_no_new_paragraph--;
137   insert_string("\n<para>");
138   in_docbook_paragraph = 1;
139 }
140
141 void
142 docbook_begin_paragraph ()
143 {
144   if (!docbook_first_chapter_found)
145     return;
146
147   if (in_example)
148     return;
149
150   if (in_table && !in_term)
151     {
152       if (!in_varlistitem)
153         insert_string ("\n<listitem><para>\n");
154       else
155         insert_string ("\n</para>\n\n<para>\n");
156       in_varlistitem = 1;
157       
158       return;
159     }
160   if (in_list)
161     return;
162   if (in_docbook_paragraph)
163     {
164        insert_string ("\n</para>\n\n");
165        adjust_braces_following (0, 10);
166     }
167
168 #if 0
169   if (docbook_must_insert_node_anchor)
170     {
171       char *tem;
172        insert_string ("<para id=\"");
173       adjust_braces_following (0, 10);
174       tem = expansion (current_node, 0);
175       add_escaped_anchor_name (tem, 0);
176       free (tem);
177       add_word ("\">\n");
178       docbook_must_insert_node_anchor = 0;
179     }
180   else
181 #endif
182     {
183       insert_string ("<para>\n");
184       adjust_braces_following (0, 7);
185     }
186   in_docbook_paragraph = 1;
187 }
188
189 void
190 docbook_begin_book ()
191 {
192   if (!docbook_begin_book_p)
193     docbook_begin_book_p = 1;
194   else
195     return;
196   
197   ++docbook_no_new_paragraph;
198   add_word_args ("<!DOCTYPE book PUBLIC \"-//Davenport//DTD DocBook V3.0//EN\">\n\
199 <book>\n<title>%s</title>\n", title);
200   --docbook_no_new_paragraph;
201 }
202
203 void
204 docbook_end_book ()
205 {
206   int i;
207   if (in_docbook_paragraph)
208     {
209       insert_string ("\n</para>\n\n");
210     }
211   
212   for (i = section_level;  i >= 0 ; i--)
213     {
214       if (i == 0)
215         add_word ("</chapter>\n");
216       else
217         add_word_args ("</sect%d>\n", i);
218     }
219
220   add_word ("</book>\n");
221 }
222
223 void
224 docbook_insert_tag (start_or_end, tag)
225      int start_or_end;
226      char *tag;
227 {
228   if (!paragraph_is_open && start_or_end == START)
229     docbook_begin_paragraph ();
230
231   add_char ('<');
232   if (start_or_end == START)
233     add_word (tag);
234   else
235     {
236       add_char ('/');
237       for (; *tag && *tag != ' '; tag++)
238         add_char(*tag);
239     }
240   add_meta_char ('>');
241 }
242
243 void 
244 docbook_xref1 (node_name)
245      char *node_name;
246 {
247   char *tem;
248   add_word ("<xref linkend=\"");
249   tem = expansion (node_name, 0);
250   add_escaped_anchor_name (tem, 1);
251   free (tem);
252   add_word ("\"/>");
253 }
254
255 void 
256 docbook_xref2 (node_name, ref_name)
257      char *node_name;
258      char *ref_name;
259 {
260   char *tem;
261   add_word ("<xref linkend=\"");
262   tem = expansion (node_name, 0);
263   add_escaped_anchor_name (tem, 1);
264   free (tem);
265   add_word ("\"/>");
266 }
267
268 int
269 docbook_quote (character)
270      int character;
271 {
272   switch (language_code)
273     {
274     case fr:
275       if (character == '`')
276         {
277           add_word ("«&nbsp");
278           return ';';
279         }
280       else
281         {
282           add_word ("&nbsp;");
283           return '»';
284         }
285       break;
286
287     default:
288       if (character == '`')
289         {
290           add_word ("&ldquo");
291           return ';';
292         }
293       else
294         {
295           add_word ("&rdquo");
296           return ';';
297         }
298       break;
299     }
300 }
301
302 #define IS_BLANK(c) (c == ' ' || c == '\t' || c == '\n')
303
304 int
305 docbook_is_punctuation (character, next)
306      int character;
307      int next;
308 {
309   return ( (character == ';'
310             || character == ':'
311             || character == '?'
312             || character == '!')
313            && IS_BLANK (next));
314 }
315
316 void
317 docbook_punctuation (character)
318      int character;
319 {
320   switch (language_code)
321     {
322     case fr:
323       while (output_paragraph[output_paragraph_offset-1] == ' ')
324         output_paragraph_offset--;
325       add_word ("&nbsp;");
326       break;
327     }
328 }
329
330 static int in_item = 0;
331
332 void
333 docbook_begin_itemize ()
334 {
335   if (in_docbook_paragraph)
336       insert_string ("\n</para>\n");
337
338   in_docbook_paragraph = 0;
339   insert_string ("\n<itemizedlist>\n");
340   in_item = 0;
341   in_list = 1;
342 }
343
344 void
345 docbook_end_itemize ()
346 {
347   if (in_item)
348     {
349       insert_string ("\n</para></listitem>\n");
350       in_item = 0;
351     }
352   insert_string ("\n</itemizedlist>\n\n<para>\n");
353   in_docbook_paragraph = 1;
354   in_list = 0;
355 }
356
357 void
358 docbook_begin_enumerate ()
359 {
360   if (in_docbook_paragraph)
361     insert_string ("\n</para>\n");
362   in_docbook_paragraph = 0;
363   insert_string ("\n<orderedlist>\n");
364   in_item = 0;
365   in_list = 1;
366 }
367
368 void
369 docbook_end_enumerate ()
370 {
371   if (in_item)
372     {
373       insert_string ("\n</para></listitem>\n");
374       in_item = 0;
375     }
376   insert_string ("\n</orderedlist>\n\n<para>\n");
377   in_docbook_paragraph = 1;
378   in_list = 0;
379 }
380
381 void
382 docbook_begin_table ()
383 {
384 #if 0
385   if (in_docbook_paragraph)
386     insert_string ("\n</para>\n\n");
387   in_docbook_paragraph = 0;
388 #endif
389   
390   add_word ("\n<variablelist>\n");
391   in_table ++;
392   in_varlistitem = 0;
393   in_entry = 0;
394 }
395
396 void
397 docbook_end_table ()
398 {
399   if (!in_varlistitem)
400     docbook_begin_paragraph ();
401   insert_string ("\n</para></listitem>\n</varlistentry>\n\n</variablelist>\n");
402 #if 0
403   if (in_table == 1)
404     {
405       insert_string ("\n</para>\n\n");
406       in_docbook_paragraph = 0;
407     }
408   else
409     {
410       insert_string ("\n<para>\n\n");
411       in_docbook_paragraph = 1;
412     }
413 #endif
414   in_table --;
415   in_list = 0;
416 }
417
418 void 
419 docbook_add_item ()
420 {
421   if (in_item)
422     insert_string ("\n</para></listitem>\n");
423   insert_string ("\n<listitem><para>\n");
424   in_docbook_paragraph = 1;
425   in_item = 1;
426 }
427
428 void 
429 docbook_add_table_item ()
430 {
431   if (in_varlistitem)
432     {
433       insert_string ("\n</para></listitem>\n</varlistentry>\n\n");
434       in_entry = 0;
435       in_varlistitem = 0;
436     }
437   if (!in_entry)
438     {
439       insert_string ("<varlistentry>\n");
440       in_entry = 1;
441     }
442   insert_string ("<term>");
443   in_list = 1;
444   in_term = 1;
445 }
446
447 void 
448 docbook_close_table_item ()
449 {
450   insert_string ("</term>");
451   in_list = 1;
452   in_term = 0;
453 }
454
455 void
456 docbook_add_anchor (anchor)
457      char *anchor;
458 {
459   add_word ("<anchor id=\"");
460   add_anchor_name (anchor, 0);
461   add_word ("\">");
462 }
463
464 void
465 docbook_footnote (note)
466      char *note;
467 {
468   /* add_word_args ("<footnote><para>\n%s\n</para></footnote>\n", note); */
469   add_word ("<footnote><para>\n");
470   execute_string("%s", note);
471   add_word("\n</para></footnote>\n");
472 }
473
474 void
475 docbook_begin_index ()
476 {
477   add_word ("<variablelist>\n");
478 }
479
480 void 
481 docbook_begin_example ()
482 {
483   add_word ("\n\n<screen>\n");
484   in_example = 1;
485 }
486
487 void 
488 docbook_end_example ()
489 {
490   in_example = 0;
491   add_word ("</screen>\n\n");
492 }