summaryrefslogtreecommitdiff
path: root/doc-i18n-tool/doc-i18n-tool.c
blob: 64cac50cfffb066565368b71aa6647803b0e3baa (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
/*
 * doc-i18n-tool.c : load an XML or SGML DocBook document and extract text strings from
 * the document content.
 *
 * See Copyright for the status of this software.
 *
 * daniel@veillard.com
 */



#include <stdio.h>
#include <string.h>
#include <stdarg.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdlib.h>
#include <libintl.h>
#include <popt.h>
#include <libxml/xmlversion.h>
#include <libxml/xmlmemory.h>
#include <locale.h>

#ifdef LIBXML_DOCB_ENABLED
#include <libxml/DOCBparser.h>
#endif
#include <libxml/tree.h>
#include <libxml/debugXML.h>

#ifdef LIBXML_DOCB_ENABLED
static int sgml = 0;
static char *encoding = NULL;
#endif
static int noent = 0;
static int output_pot_file = 0;
static char *package = NULL;
static char *localedir = ".";
static char *alternate_file_name = NULL;
struct poptOption options [] =
  {
#ifdef LIBXML_DOCB_ENABLED
    { "sgml", 's', POPT_ARG_NONE, &sgml, 0, "parse as docbook sgml", "SGML" },
    { "encoding", 'e', POPT_ARG_STRING, &encoding, 0, "encoding for sgml files", "ENCODING" },
#endif
    { "noent", 'n', POPT_ARG_NONE, &noent, 0, "ignore entities in document", "NOENT" },
    { "output-pot-file", 'p', POPT_ARG_NONE, &output_pot_file, 0, "output a pot file for the document", "OUTPUT_POT_FILE" },
    { "package", 'g', POPT_ARG_STRING, &package, 0, "output a pot file for the document", "PACKAGE" },
    { "localedir", 'l', POPT_ARG_STRING, &localedir, 0, "localedir for the document", "LOCALEDIR" },
    { "filename", 'f', POPT_ARG_STRING, &alternate_file_name, 0, "alternate filename", "FILENAME" },
    { NULL, '\0', 0, NULL, 0}
  };
 
/*
 * Null terminated list of elements which should be considered
 * presentational and should not break strings.
 *
 * Add elements names as needed.
 */
const char *presentation_elements[] = {
    "accel",
    NULL
};


xmlChar *
processString (const xmlChar *value,
	       const char    *filename, 
	       int            line_no)
{
  if (output_pot_file)
    {
      const char *ptr;
      char *ptr2;
      char *new_line;
      int new_len = 0;
      
      for (ptr = value; *ptr != '\000'; ptr++)
	{
	  if (*ptr == '\n')
	    new_len += 5;
	  else if (*ptr == '\"')
	    new_len += 2;
	  else
	    new_len++;
	}
      new_line = malloc (sizeof (char) *(new_len + 1));
      for (ptr = value, ptr2 = new_line; *ptr != '\000'; ptr++)
	{
	  if (*ptr == '\n')
	    {
	      *ptr2++ = '\\';
	      *ptr2++ = 'n';
	      *ptr2++ = '\"';
	      *ptr2++ = '\n';
	      *ptr2++ = '\"';
	    }
	  else if (*ptr == '\"')
	    {
	      *ptr2++ = '\\';
	      *ptr2++ = '\"';
	    }
	  else
	    *ptr2++ = *ptr;
	}
      *ptr2 = '\000';
      if (alternate_file_name)
	printf ("#: %s:%d\n", alternate_file_name, line_no);
      else
	printf ("#: %s:%d\n", filename, line_no);
      printf ("msgid \"%s\"\n", new_line);
      printf ("msgstr \"\"\n\n");
      free (new_line);
    }
  else
    {
      return xmlStrdup (gettext (value));
    }
  return NULL;
}

static void
processNode (xmlNodePtr  node,
	     const char *filename,
	     int         line)
{
  if (node->type == XML_TEXT_NODE)
    {
      xmlChar *res;

      res = processString (node->content, filename, line);

      if (res != NULL)
	{
	  xmlFree (node->content);
	  node->content = res;
	}
    }
  else if (node->type == XML_ELEMENT_NODE)
    {
      int pres;
      xmlNodePtr children = node->children;
      const xmlChar *name;
      const char **tst;

      if (children == NULL)
	  return;

      pres = 1;
      /* check to see if all children are only text or presentation elements */
      while ((children != NULL) && (pres != 0)) {
	  if (children->type == XML_ELEMENT_NODE) {
	      name = children->name;
	      tst = presentation_elements;
	      while (*tst != NULL) {
		  if (!xmlStrcmp((const xmlChar *)*tst, name))
		      break;
		  tst++;
	      }
	      if (*tst == NULL) {
		  pres = 0;
		  break;
	      }
	  }
	  children = children->next;
      }
      if (pres == 1) {
	  xmlChar *content;

	  /*
	   * get the value, scrap the node list, replace with 
	   * the modified text content. Let the tree walking
	   * process the text node at the next step.
	   */
	  content = xmlNodeGetContent(node);
	  children = node->children;
	  node->last = node->children = NULL;
	  xmlFreeNodeList(children);
	  children = xmlNewDocText(node->doc, content);
	  xmlAddChild(node, children);
	  xmlFree(content);
      }
    }
}

static void
processSubtree (xmlNodePtr  node,
		const char *filename)
{
  xmlNodePtr cur;
  int line = 0;

  if (node == NULL)
    return;

  cur = node;
  while (cur != NULL)
    {
      if (node->type == XML_ELEMENT_NODE) {
	line = (int) node->content;
      }
      processNode (cur, filename, line);

      /*
       * skip to next node
       */
      if (cur->type == XML_ENTITY_REF_NODE)
	{
	  processSubtree (cur->children, filename);
	}
      if ((cur->children != NULL) && (cur->type != XML_ENTITY_REF_NODE))
	{
	  cur = cur->children;
	}
      else if (cur->type == XML_ENTITY_DECL)
	{
	  return;
	}
      else if (cur->next != NULL)
	{
	  cur = cur->next;
	}
      else
	{
	  do
	    {
	      cur = cur->parent;
	      if (cur == NULL)
		break;
	      if (cur->next != NULL) {
		cur = cur->next;
		break;
	      }
	    } while ((cur != NULL) && (cur != node));
	}
      if (cur == node)
	return;
    }
}

static void
output_pot_header (const char *filename)
{
  printf ("# SOME DESCRIPTIVE TITLE\n");
  printf ("# Copyright (C) YEAR Free Software Foundation, Inc.\n");
  printf ("# FIRST AUTHOR <EMAIL@ADDRESS>, YEAR.\n");
  if (alternate_file_name)
    printf ("#\n# %s\n\n", alternate_file_name);
  else
    printf ("#\n# %s\n\n", filename);
}

static void
processDocument (xmlDocPtr   doc,
		 const char *filename)
{
  xmlNodePtr cur;

  if (doc == NULL)
    return;

  if (output_pot_file)
    output_pot_header (filename);
  cur = xmlDocGetRootElement (doc);
  processSubtree (cur, filename);
}

static void
processFile (const char *filename)
{
  xmlDocPtr doc;

#ifdef LIBXML_DOCB_ENABLED
  if (sgml)
    doc = docbParseFile (filename, encoding);
  else
    doc = xmlParseFile (filename);
#else
  doc = xmlParseFile (filename);
#endif
  processDocument (doc, filename);
  if (!output_pot_file)
    xmlSaveFile ("-", doc);
  xmlFreeDoc (doc);
}

int main (int argc, char **argv)
{
  poptContext context;
  const char **files;
  const char *file_name;

  context = poptGetContext ("doc-i18n-tool", argc, (const char**)argv, options, 0);
  while (poptGetNextOpt (context) != -1);

  setlocale (LC_ALL, "");
  if (!output_pot_file)
    {
      bindtextdomain (package, localedir);
      bind_textdomain_codeset (package, "UTF-8");
      textdomain (package);
    }
  xmlLineNumbersDefault (1);
  if (noent == 0) xmlSubstituteEntitiesDefault(1);

  files = poptGetArgs (context);
  if (files == NULL)
    exit (0);

  while ((file_name = *files++) != NULL)
    {
      struct stat s;
      if ((stat (file_name, &s) < 0) ||
	  (! S_ISREG (s.st_mode)))
	{
	  printf ("File %s doesn't exist\n", file_name);
	  exit (1);
	}
      processFile (file_name);
    }

  poptFreeContext (context);
  xmlCleanupParser ();
  xmlMemoryDump ();

  return 0;
}