output: Expand footnote support.
[pspp] / src / output / odt.c
index a29c045d0b67e8920344a0e3e0b6f6a5f99f4db8..153cd393c116f357f615e3d113ea9e336d8292e5 100644 (file)
@@ -36,6 +36,7 @@
 #include "libpspp/temp-file.h"
 #include "libpspp/version.h"
 #include "libpspp/zip-writer.h"
+#include "data/file-handle-def.h"
 #include "output/driver-provider.h"
 #include "output/message-item.h"
 #include "output/options.h"
@@ -68,12 +69,6 @@ struct odt_driver
 
   /* Number of tables so far. */
   int table_num;
-
-  /* Name of current command. */
-  char *command_name;
-
-  /* Number of footnotes so far. */
-  int n_footnotes;
 };
 
 static const struct output_driver_class odt_driver_class;
@@ -148,7 +143,7 @@ write_style_data (struct odt_driver *odt)
                               _xml ("urn:oasis:names:tc:opendocument:xmlns:xsl-fo-compatible:1.0") );
 
   xmlTextWriterWriteAttribute (w, _xml ("office:version"),  _xml ("1.1"));
-                              
+
 
 
   xmlTextWriterStartElement (w, _xml ("office:styles"));
@@ -244,7 +239,7 @@ write_meta_data (struct odt_driver *odt)
   xmlTextWriterStartElement (w, _xml ("office:meta"));
   {
     xmlTextWriterStartElement (w, _xml ("meta:generator"));
-    xmlTextWriterWriteString (w, _xml (stat_version));
+    xmlTextWriterWriteString (w, _xml (version));
     xmlTextWriterEndElement (w);
   }
 
@@ -290,12 +285,13 @@ write_meta_data (struct odt_driver *odt)
 }
 
 static struct output_driver *
-odt_create (const char *file_name, enum settings_output_devices device_type,
+odt_create (struct file_handle *fh, enum settings_output_devices device_type,
             struct string_map *o UNUSED)
 {
   struct output_driver *d;
   struct odt_driver *odt;
   struct zip_writer *zip;
+  const char *file_name = fh_get_file_name (fh);
 
   zip = zip_writer_create (file_name);
   if (zip == NULL)
@@ -303,6 +299,7 @@ odt_create (const char *file_name, enum settings_output_devices device_type,
 
   odt = xzalloc (sizeof *odt);
   d = &odt->driver;
+
   output_driver_init (d, &odt_driver_class, file_name, device_type);
 
   odt->zip = zip;
@@ -382,9 +379,8 @@ odt_destroy (struct output_driver *driver)
 
       zip_writer_close (odt->zip);
     }
-  
+
   free (odt->file_name);
-  free (odt->command_name);
   free (odt);
 }
 
@@ -423,55 +419,58 @@ write_xml_with_line_breaks (struct odt_driver *odt, const char *line_)
 }
 
 static void
-write_footnote (struct odt_driver *odt, const char *footnote)
+write_footnote (struct odt_driver *odt, const struct footnote *f)
 {
-  char marker[16];
-
   xmlTextWriterStartElement (odt->content_wtr, _xml("text:note"));
   xmlTextWriterWriteAttribute (odt->content_wtr, _xml("text:note-class"),
                                _xml("footnote"));
 
   xmlTextWriterStartElement (odt->content_wtr, _xml("text:note-citation"));
-  str_format_26adic (++odt->n_footnotes, false, marker, sizeof marker);
-  if (strlen (marker) > 1)
+  if (strlen (f->marker) > 1)
     xmlTextWriterWriteFormatAttribute (odt->content_wtr, _xml("text:label"),
-                                       "(%s)", marker);
+                                       "(%s)", f->marker);
   else
     xmlTextWriterWriteAttribute (odt->content_wtr, _xml("text:label"),
-                                 _xml(marker));
+                                 _xml(f->marker));
   xmlTextWriterEndElement (odt->content_wtr);
 
   xmlTextWriterStartElement (odt->content_wtr, _xml("text:note-body"));
   xmlTextWriterStartElement (odt->content_wtr, _xml("text:p"));
-  write_xml_with_line_breaks (odt, footnote);
+  write_xml_with_line_breaks (odt, f->content);
   xmlTextWriterEndElement (odt->content_wtr);
   xmlTextWriterEndElement (odt->content_wtr);
 
   xmlTextWriterEndElement (odt->content_wtr);
 }
 
+static void
+write_table_item_text (struct odt_driver *odt,
+                       const struct table_item_text *text)
+{
+  if (!text)
+    return;
+
+  xmlTextWriterStartElement (odt->content_wtr, _xml("text:h"));
+  xmlTextWriterWriteFormatAttribute (odt->content_wtr,
+                                     _xml("text:outline-level"), "%d", 2);
+  xmlTextWriterWriteString (odt->content_wtr, _xml (text->content) );
+  for (size_t i = 0; i < text->n_footnotes; i++)
+    write_footnote (odt, text->footnotes[i]);
+  xmlTextWriterEndElement (odt->content_wtr);
+}
+
 static void
 write_table (struct odt_driver *odt, const struct table_item *item)
 {
   const struct table *tab = table_item_get_table (item);
-  const char *caption = table_item_get_caption (item);
-  const char *title = table_item_get_title (item);
   int r, c;
 
   /* Write a heading for the table */
-  if (title != NULL)
-    {
-      xmlTextWriterStartElement (odt->content_wtr, _xml("text:h"));
-      xmlTextWriterWriteFormatAttribute (odt->content_wtr,
-                                         _xml("text:outline-level"), "%d", 2);
-      xmlTextWriterWriteString (odt->content_wtr,
-                                _xml (table_item_get_title (item)) );
-      xmlTextWriterEndElement (odt->content_wtr);
-    }
+  write_table_item_text (odt, table_item_get_title (item));
 
   /* Start table */
   xmlTextWriterStartElement (odt->content_wtr, _xml("table:table"));
-  xmlTextWriterWriteFormatAttribute (odt->content_wtr, _xml("table:name"), 
+  xmlTextWriterWriteFormatAttribute (odt->content_wtr, _xml("table:name"),
                                     "TABLE-%d", odt->table_num++);
 
 
@@ -484,7 +483,7 @@ write_table (struct odt_driver *odt, const struct table_item *item)
   /* Deal with row headers */
   if ( table_ht (tab) > 0)
     xmlTextWriterStartElement (odt->content_wtr, _xml("table:table-header-rows"));
-    
+
 
   /* Write all the rows */
   for (r = 0 ; r < table_nr (tab); ++r)
@@ -523,25 +522,19 @@ write_table (struct odt_driver *odt, const struct table_item *item)
                   const struct cell_contents *contents = &cell.contents[i];
                   int j;
 
-                  if (contents->text)
-                    {
-                      xmlTextWriterStartElement (odt->content_wtr, _xml("text:p"));
-
-                      if ( r < table_ht (tab) || c < table_hl (tab) )
-                        xmlTextWriterWriteAttribute (odt->content_wtr, _xml("text:style-name"), _xml("Table_20_Heading"));
-                      else
-                        xmlTextWriterWriteAttribute (odt->content_wtr, _xml("text:style-name"), _xml("Table_20_Contents"));
+                  xmlTextWriterStartElement (odt->content_wtr, _xml("text:p"));
 
-                      write_xml_with_line_breaks (odt, contents->text);
+                  if ( r < table_ht (tab) || c < table_hl (tab) )
+                    xmlTextWriterWriteAttribute (odt->content_wtr, _xml("text:style-name"), _xml("Table_20_Heading"));
+                  else
+                    xmlTextWriterWriteAttribute (odt->content_wtr, _xml("text:style-name"), _xml("Table_20_Contents"));
 
-                      for (j = 0; j < contents->n_footnotes; j++)
-                        write_footnote (odt, contents->footnotes[j]);
+                  write_xml_with_line_breaks (odt, contents->text);
 
-                      xmlTextWriterEndElement (odt->content_wtr); /* text:p */
-                    }
-                  else if (contents->table)
-                    write_table (odt, contents->table);
+                  for (j = 0; j < contents->n_footnotes; j++)
+                    write_footnote (odt, contents->footnotes[j]);
 
+                  xmlTextWriterEndElement (odt->content_wtr); /* text:p */
                 }
               xmlTextWriterEndElement (odt->content_wtr); /* table:table-cell */
            }
@@ -553,7 +546,7 @@ write_table (struct odt_driver *odt, const struct table_item *item)
 
           table_cell_free (&cell);
        }
-  
+
       xmlTextWriterEndElement (odt->content_wtr); /* row */
 
       if ( table_ht (tab) > 0 && r == table_ht (tab) - 1)
@@ -563,16 +556,7 @@ write_table (struct odt_driver *odt, const struct table_item *item)
   xmlTextWriterEndElement (odt->content_wtr); /* table */
 
   /* Write a caption for the table */
-  if (caption != NULL)
-    {
-      xmlTextWriterStartElement (odt->content_wtr, _xml("text:h"));
-      xmlTextWriterWriteFormatAttribute (odt->content_wtr,
-                                         _xml("text:outline-level"), "%d", 2);
-      xmlTextWriterWriteString (odt->content_wtr,
-                                _xml (table_item_get_caption (item)) );
-      xmlTextWriterEndElement (odt->content_wtr);
-    }
-
+  write_table_item_text (odt, table_item_get_caption (item));
 }
 
 static void
@@ -590,8 +574,6 @@ odt_submit (struct output_driver *driver,
 {
   struct odt_driver *odt = odt_driver_cast (driver);
 
-  output_driver_track_current_command (output_item, &odt->command_name);
-
   if (is_table_item (output_item))
     write_table (odt, to_table_item (output_item));
   else if (is_text_item (output_item))
@@ -605,7 +587,7 @@ odt_submit (struct output_driver *driver,
     {
       const struct message_item *message_item = to_message_item (output_item);
       const struct msg *msg = message_item_get_msg (message_item);
-      char *s = msg_to_string (msg, odt->command_name);
+      char *s = msg_to_string (msg, message_item->command_name);
       odt_output_text (odt, s);
       free (s);
     }