Imported Robodoc.
[robodoc.git] / Source / xmldocbook_generator.c
1 /*
2 Copyright (C) 1994-2007  Frans Slothouber, Jacco van Weert, Petteri Kettunen,
3 Bernd Koesling, Thomas Aglassinger, Anthon Pang, Stefan Kost, David Druffner,
4 Sasha Vasko, Kai Hofmann, Thierry Pierron, Friedrich Haase, and Gergely Budai.
5
6 This file is part of ROBODoc
7
8 ROBODoc is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3 of the License, or
11 (at your option) any later version.
12
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 GNU General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with this program.  If not, see <http://www.gnu.org/licenses/>.
20
21 */
22
23
24 #include <stdio.h>
25 #include <string.h>
26 #include <assert.h>
27 #include <ctype.h>
28 #include "xmldocbook_generator.h"
29 #include "globals.h"
30 #include "util.h"
31
32
33 char               *RB_XMLDB_Get_Default_Extension(
34     void )
35 {
36     return ".xml";
37 }
38
39 void RB_XMLDB_Generate_String(
40     FILE *dest_doc,
41     char *a_string )
42 {
43     int                 i;
44     int                 l = strlen( a_string );
45     unsigned char       c;
46
47     for ( i = 0; i < l; ++i )
48     {
49         c = a_string[i];
50         RB_XMLDB_Generate_Char( dest_doc, c );
51     }
52 }
53
54 /* TODO Documentation */
55
56 void RB_XMLDB_Generate_Label(
57     FILE *dest_doc,
58     char *name )
59 {
60     int                 i;
61     int                 l = strlen( name );
62     unsigned char       c;
63
64     fprintf( dest_doc, "<anchor id=\"" );
65     for ( i = 0; i < l; ++i )
66     {
67         c = name[i];
68         if ( utf8_isalnum( c ) )
69         {
70             RB_XMLDB_Generate_Char( dest_doc, c );
71         }
72         else
73         {
74             char                buf[4];
75
76             sprintf( buf, "%02x", c );
77             RB_XMLDB_Generate_Char( dest_doc, buf[0] );
78             RB_XMLDB_Generate_Char( dest_doc, buf[1] );
79         }
80     }
81     fprintf( dest_doc, "\"/>\n" );
82 }
83
84
85 /****f* Generator/RB_XMLDB_Generate_Char
86  * NAME
87  *   RB_XMLDB_Generate_Char
88  * SYNOPSIS
89  *   void RB_XMLDB_Generate_Char( FILE * dest_doc, int c )
90  * FUNCTION
91  *   Switchboard to RB_XMLDB_Generate_Char
92  * SOURCE
93  */
94
95 void RB_XMLDB_Generate_Char(
96     FILE *dest_doc,
97     int c )
98 {
99     switch ( c )
100     {
101     case '\n':
102         assert( 0 );
103         break;
104     case '\t':
105         assert( 0 );
106         break;
107     case '<':
108         fprintf( dest_doc, "&lt;" );
109         break;
110     case '>':
111         fprintf( dest_doc, "&gt;" );
112         break;
113     case '&':
114         fprintf( dest_doc, "&amp;" );
115         break;
116     default:
117         // All others are printed literally
118         fputc( c, dest_doc );
119     }
120 }
121
122 /*****/
123
124 void RB_XMLDB_Generate_Header_Start(
125     FILE *dest_doc,
126     struct RB_header *cur_header )
127 {
128     fprintf( dest_doc, "<section>\n" );
129     /*    fprintf( dest_doc, "<section id=\"%s\">\n", cur_header->unique_name ); */
130     fprintf( dest_doc, "<title>\n" );
131     RB_XMLDB_Generate_String( dest_doc, cur_header->name );
132     fprintf( dest_doc, "</title>\n" );
133 }
134
135 void RB_XMLDB_Generate_Header_End(
136     FILE *dest_doc,
137     struct RB_header *cur_header )
138 {
139     USE( cur_header );
140     fprintf( dest_doc, "</section>\n" );
141 }
142
143 void RB_XMLDB_Generate_Link(
144     FILE *dest,
145     char *dest_name,
146     char *filename,
147     char *labelname,
148     char *linkname )
149 {
150     USE( dest_name );
151     USE( filename );
152
153     fprintf( dest, "<link linkend=\"%s\">", labelname );
154     RB_XMLDB_Generate_String( dest, linkname );
155     fprintf( dest, "</link>" );
156 }
157
158 void RB_XMLDB_Generate_Doc_Start(
159     struct RB_Document *document,
160     FILE *dest_doc,
161     char *charset )
162 {
163     if ( course_of_action.do_headless )
164     {
165         /* The user does not want the document head. */
166     }
167     else
168     {
169         fprintf( dest_doc, "<?xml version=\"1.0\" encoding=\"%s\"?>\n",
170                  charset ? charset : DEFAULT_CHARSET );
171         if ( document->doctype_name && document->doctype_location )
172         {
173             fprintf( dest_doc, "<!DOCTYPE article PUBLIC \"%s\"\n\"%s\">\n",
174                      document->doctype_name, document->doctype_location );
175             fprintf( dest_doc, "%s", "<article lang=\"en\">\n" );
176         }
177         else
178         {
179             fprintf( dest_doc, "%s",
180                      "<!DOCTYPE article PUBLIC \"-//OASIS//DTD DocBook XML V4.2//EN\" \n"
181                      "\"http://www.oasis-open.org/docbook/xml/4.2/docbookx.dtd\">\n"
182                      "<article lang=\"en\">\n" );
183         }
184         fprintf( dest_doc,
185                  "<articleinfo>\n  <title>%s</title>\n</articleinfo>\n",
186                  document_title ? document_title : DEFAULT_DOCTITILE );
187     }
188 }
189
190
191 void RB_XMLDB_Generate_Doc_End(
192     FILE *dest_doc,
193     char *name )
194 {
195     USE( name );
196
197     if ( course_of_action.do_footless )
198     {
199         /* The user does not want the foot of the
200          * document.
201          */
202     }
203     else
204     {
205         fprintf( dest_doc, "%s", "</article>\n" );
206     }
207 }
208
209
210 void RB_XMLDB_Generate_Item_Name(
211     FILE *dest_doc,
212     char *name )
213 {
214     fprintf( dest_doc, "<formalpara><title>" );
215     RB_XMLDB_Generate_String( dest_doc, name );
216     fprintf( dest_doc, "</title><para></para></formalpara>\n" );
217 }
218
219 void RB_XMLDB_Generate_Item_Begin(
220     FILE *dest_doc )
221 {
222     USE( dest_doc );
223     /* Empty */
224 }
225
226
227 void RB_XMLDB_Generate_Item_End(
228     FILE *dest_doc )
229 {
230     USE( dest_doc );
231     /* Empty */
232 }
233
234
235 void RB_XMLDB_Generate_BeginSection(
236     FILE *dest_doc,
237     int depth,
238     char *name )
239 {
240     USE( depth );
241
242     fprintf( dest_doc, "%s", "<section>\n<title>" );
243     RB_XMLDB_Generate_String( dest_doc, name );
244     fprintf( dest_doc, "%s", "</title>\n" );
245 }
246
247 void RB_XMLDB_Generate_EndSection(
248     FILE *dest_doc,
249     int depth,
250     char *name )
251 {
252     USE( depth );
253     USE( name );
254     fprintf( dest_doc, "%s", "</section>\n" );
255 }
256
257
258 void RB_XMLDB_Generate_False_Link(
259     FILE *dest_doc,
260     char *name )
261 {
262     RB_XMLDB_Generate_String( dest_doc, name );
263 }
264
265
266
267 void XMLDB_Generate_Begin_Paragraph(
268     FILE *dest_doc )
269 {
270     fprintf( dest_doc, "%s", "<para>\n" );
271 }
272
273 void XMLDB_Generate_End_Paragraph(
274     FILE *dest_doc )
275 {
276     fprintf( dest_doc, "%s", "</para>\n" );
277 }
278
279
280 void XMLDB_Generate_Begin_Preformatted(
281     FILE *dest_doc )
282 {
283     fprintf( dest_doc, "%s", "<literallayout class=\"monospaced\">\n" );
284 }
285
286 void XMLDB_Generate_End_Preformatted(
287     FILE *dest_doc )
288 {
289     fprintf( dest_doc, "%s", "</literallayout>\n" );
290 }
291
292
293 void XMLDB_Generate_Begin_List(
294     FILE *dest_doc )
295 {
296     fprintf( dest_doc, "%s", "<itemizedlist>" );
297 }
298
299 void XMLDB_Generate_End_List(
300     FILE *dest_doc )
301 {
302     fprintf( dest_doc, "%s", "</itemizedlist>" );
303 }
304
305 void XMLDB_Generate_Begin_List_Item(
306     FILE *dest_doc )
307 {
308     fprintf( dest_doc, "%s", "<listitem><para>" );
309 }
310
311 void XMLDB_Generate_End_List_Item(
312     FILE *dest_doc )
313 {
314     fprintf( dest_doc, "%s", "</para></listitem>" );
315 }