]> git.ipfire.org Git - thirdparty/gettext.git/commitdiff
libtextstyle: styled-ostream: Add hyperlink support.
authorBruno Haible <bruno@clisp.org>
Wed, 14 Aug 2019 18:52:30 +0000 (20:52 +0200)
committerBruno Haible <bruno@clisp.org>
Wed, 14 Aug 2019 18:52:30 +0000 (20:52 +0200)
* libtextstyle/gnulib-local/lib/styled-ostream.oo.h (struct styled_ostream): Add
methods get_hyperlink_ref, get_hyperlink_id, set_hyperlink.
* libtextstyle/gnulib-local/lib/term-styled-ostream.oo.c
(term_styled_ostream::get_hyperlink_ref, term_styled_ostream::get_hyperlink_id,
term_styled_ostream::set_hyperlink): New functions.
* libtextstyle/gnulib-local/lib/html-styled-ostream.oo.c
(struct html_styled_ostream): Add field hyperlink_id.
(html_styled_ostream::free): Free the hyperlink_id field.
(html_styled_ostream::get_hyperlink_ref, html_styled_ostream::get_hyperlink_id,
html_styled_ostream::set_hyperlink): New functions.
(html_styled_ostream_create): Initialize the hyperlink_id field.
* libtextstyle/gnulib-local/lib/noop-styled-ostream.oo.c
(struct noop_styled_ostream): Add fields hyperlink_ref, hyperlink_id.
(noop_styled_ostream::free): Free the hyperlink_ref and hyperlink_id fields.
(noop_styled_ostream::get_hyperlink_ref, noop_styled_ostream::get_hyperlink_id,
noop_styled_ostream::set_hyperlink): New functions.
(noop_styled_ostream_create): Initialize the hyperlink_ref and hyperlink_id
fields.
* libtextstyle/lib/textstyle.h (styled_ostream_get_hyperlink_ref,
styled_ostream_get_hyperlink_id, styled_ostream_set_hyperlink,
term_styled_ostream_get_hyperlink_ref, term_styled_ostream_get_hyperlink_id,
term_styled_ostream_set_hyperlink, html_styled_ostream_get_hyperlink_ref,
html_styled_ostream_get_hyperlink_id, html_styled_ostream_set_hyperlink,
noop_styled_ostream_get_hyperlink_ref, noop_styled_ostream_get_hyperlink_id,
noop_styled_ostream_set_hyperlink): New declarations.
* libtextstyle/adhoc-tests/hello.c (main): Invoke styled_ostream_set_hyperlink.

libtextstyle/adhoc-tests/hello.c
libtextstyle/gnulib-local/lib/html-styled-ostream.oo.c
libtextstyle/gnulib-local/lib/noop-styled-ostream.oo.c
libtextstyle/gnulib-local/lib/styled-ostream.oo.h
libtextstyle/gnulib-local/lib/term-styled-ostream.oo.c
libtextstyle/lib/textstyle.h

index 1f827e57c18d613cebe62d442b66ccaf27942fd5..c94b175f36225c684dd8e86b137e899722006a15 100644 (file)
@@ -85,9 +85,13 @@ main (int argc, char *argv[])
 
   ostream_write_str (stream, "Dr. ");
   styled_ostream_begin_use_class (stream, "boy-name");
+  /* Start a hyperlink.  */
+  styled_ostream_set_hyperlink (stream, "https://en.wikipedia.org/wiki/Linus_Pauling", NULL);
   ostream_write_str (stream, "Linus");
   styled_ostream_end_use_class (stream, "boy-name");
   ostream_write_str (stream, " Pauling");
+  /* End the current hyperlink.  */
+  styled_ostream_set_hyperlink (stream, NULL, NULL);
 
   /* Terminate the name.  */
   styled_ostream_end_use_class (stream, "name");
index 41aa170e13595951bb567b54ca4f9075009fe40b..945396499241376d237cd71edb64ac6e81ac3bff 100644 (file)
@@ -47,6 +47,8 @@ fields:
   ostream_t destination;
   /* A HTML aware wrapper around the destination stream.  */
   html_ostream_t html_destination;
+  /* The current hyperlink id.  */
+  char *hyperlink_id;
 };
 
 /* Implementation of ostream_t methods.  */
@@ -70,6 +72,7 @@ html_styled_ostream::free (html_styled_ostream_t stream)
   html_ostream_free (stream->html_destination);
   ostream_write_str (stream->destination, "</body>\n");
   ostream_write_str (stream->destination, "</html>\n");
+  free (stream->hyperlink_id);
   free (stream);
 }
 
@@ -89,6 +92,29 @@ html_styled_ostream::end_use_class (html_styled_ostream_t stream,
   html_ostream_end_span (stream->html_destination, classname);
 }
 
+static const char *
+html_styled_ostream::get_hyperlink_ref (html_styled_ostream_t stream)
+{
+  return html_ostream_get_hyperlink_ref (stream->html_destination);
+}
+
+static const char *
+html_styled_ostream::get_hyperlink_id (html_styled_ostream_t stream)
+{
+  return stream->hyperlink_id;
+}
+
+static void
+html_styled_ostream::set_hyperlink (html_styled_ostream_t stream,
+                                    const char *ref, const char *id)
+{
+  char *id_copy = (id != NULL ? xstrdup (id) : NULL);
+
+  html_ostream_set_hyperlink_ref (stream->html_destination, ref);
+  free (stream->hyperlink_id);
+  stream->hyperlink_id = id_copy;
+}
+
 static void
 html_styled_ostream::flush_to_current_style (html_styled_ostream_t stream)
 {
@@ -106,6 +132,7 @@ html_styled_ostream_create (ostream_t destination, const char *css_filename)
   stream->base.base.vtable = &html_styled_ostream_vtable;
   stream->destination = destination;
   stream->html_destination = html_ostream_create (destination);
+  stream->hyperlink_id = NULL;
 
   ostream_write_str (stream->destination, "<?xml version=\"1.0\"?>\n");
   /* HTML 4.01 or XHTML 1.0?
index f24cffbe9cd003c9e46cffb94f5fc02e678cbb18..fb824b9057ad295dabd0a47ea952d8968dacbdb7 100644 (file)
@@ -29,6 +29,9 @@ fields:
   /* The destination stream.  */
   ostream_t destination;
   bool own_destination;
+  /* The current hyperlink ref and id.  */
+  char *hyperlink_ref;
+  char *hyperlink_id;
 };
 
 /* Implementation of ostream_t methods.  */
@@ -52,6 +55,8 @@ noop_styled_ostream::free (noop_styled_ostream_t stream)
 {
   if (stream->own_destination)
     ostream_free (stream->destination);
+  free (stream->hyperlink_ref);
+  free (stream->hyperlink_id);
   free (stream);
 }
 
@@ -69,6 +74,31 @@ noop_styled_ostream::end_use_class (noop_styled_ostream_t stream,
 {
 }
 
+static const char *
+noop_styled_ostream::get_hyperlink_ref (noop_styled_ostream_t stream)
+{
+  return stream->hyperlink_ref;
+}
+
+static const char *
+noop_styled_ostream::get_hyperlink_id (noop_styled_ostream_t stream)
+{
+  return stream->hyperlink_id;
+}
+
+static void
+noop_styled_ostream::set_hyperlink (noop_styled_ostream_t stream,
+                                    const char *ref, const char *id)
+{
+  char *ref_copy = (ref != NULL ? xstrdup (ref) : NULL);
+  char *id_copy = (id != NULL ? xstrdup (id) : NULL);
+
+  free (stream->hyperlink_ref);
+  stream->hyperlink_ref = ref_copy;
+  free (stream->hyperlink_id);
+  stream->hyperlink_id = id_copy;
+}
+
 static void
 noop_styled_ostream::flush_to_current_style (noop_styled_ostream_t stream)
 {
@@ -86,6 +116,8 @@ noop_styled_ostream_create (ostream_t destination, bool pass_ownership)
   stream->base.base.vtable = &noop_styled_ostream_vtable;
   stream->destination = destination;
   stream->own_destination = pass_ownership;
+  stream->hyperlink_ref = NULL;
+  stream->hyperlink_id = NULL;
 
   return stream;
 }
index 2daa87c28e9ae4e0e481c351ec19a8e6df5c8a71..eed57f468965fd1b2308f98e05714f7431c49a1c 100644 (file)
@@ -1,5 +1,5 @@
 /* Abstract output stream for CSS styled text.
-   Copyright (C) 2006 Free Software Foundation, Inc.
+   Copyright (C) 2006, 2019 Free Software Foundation, Inc.
    Written by Bruno Haible <bruno@clisp.org>, 2006.
 
    This program is free software: you can redistribute it and/or modify
@@ -39,6 +39,12 @@ methods:
      The begin_use_class / end_use_class calls must match properly.  */
   void end_use_class (styled_ostream_t stream, const char *classname);
 
+  /* Get/set the hyperlink attribute and its id.  */
+  const char * get_hyperlink_ref (styled_ostream_t stream);
+  const char * get_hyperlink_id (styled_ostream_t stream);
+  void         set_hyperlink (styled_ostream_t stream,
+                              const char *ref, const char *id);
+
   /* Like styled_ostream_flush (first_arg, FLUSH_THIS_STREAM), except that it
      leaves the destination with the current text style enabled, instead
      of with the default text style.
index 2cfd4a8546ef3790c258616d1b08e314e26a2a23..e9d4f0fd349e356e8e0d8e69edd8a9886c745443 100644 (file)
@@ -595,6 +595,25 @@ term_styled_ostream::end_use_class (term_styled_ostream_t stream,
   stream->curr_attr = (attributes_t *) found;
 }
 
+static const char *
+term_styled_ostream::get_hyperlink_ref (term_styled_ostream_t stream)
+{
+  return term_ostream_get_hyperlink_ref (stream->destination);
+}
+
+static const char *
+term_styled_ostream::get_hyperlink_id (term_styled_ostream_t stream)
+{
+  return term_ostream_get_hyperlink_id (stream->destination);
+}
+
+static void
+term_styled_ostream::set_hyperlink (term_styled_ostream_t stream,
+                                    const char *ref, const char *id)
+{
+  term_ostream_set_hyperlink (stream->destination, ref, id);
+}
+
 static void
 term_styled_ostream::flush_to_current_style (term_styled_ostream_t stream)
 {
index 00471d481fb9c949afa1fd7c63f92c2879c48dc3..a80987a619a7a41543d662ce6b16345d07c6069c 100644 (file)
@@ -93,6 +93,9 @@ extern void styled_ostream_flush (styled_ostream_t first_arg, ostream_flush_scop
 extern void styled_ostream_free (styled_ostream_t first_arg);
 extern void styled_ostream_begin_use_class (styled_ostream_t first_arg, const char *classname);
 extern void styled_ostream_end_use_class (styled_ostream_t first_arg, const char *classname);
+extern const char *styled_ostream_get_hyperlink_ref (styled_ostream_t first_arg);
+extern const char *styled_ostream_get_hyperlink_id (styled_ostream_t first_arg);
+extern void styled_ostream_set_hyperlink (styled_ostream_t first_arg, const char *ref, const char *id);
 /* Like styled_ostream_flush (first_arg, FLUSH_THIS_STREAM), except that it
    leaves the destination with the current text style enabled, instead
    of with the default text style.
@@ -404,6 +407,9 @@ extern void term_styled_ostream_flush (term_styled_ostream_t first_arg, ostream_
 extern void term_styled_ostream_free (term_styled_ostream_t first_arg);
 extern void term_styled_ostream_begin_use_class (term_styled_ostream_t first_arg, const char *classname);
 extern void term_styled_ostream_end_use_class (term_styled_ostream_t first_arg, const char *classname);
+extern const char *term_styled_ostream_get_hyperlink_ref (term_styled_ostream_t first_arg);
+extern const char *term_styled_ostream_get_hyperlink_id (term_styled_ostream_t first_arg);
+extern void term_styled_ostream_set_hyperlink (term_styled_ostream_t first_arg, const char *ref, const char *id);
 extern void term_styled_ostream_flush_to_current_style (term_styled_ostream_t first_arg);
 #ifdef __cplusplus
 }
@@ -444,6 +450,9 @@ extern void html_styled_ostream_flush (html_styled_ostream_t first_arg, ostream_
 extern void html_styled_ostream_free (html_styled_ostream_t first_arg);
 extern void html_styled_ostream_begin_use_class (html_styled_ostream_t first_arg, const char *classname);
 extern void html_styled_ostream_end_use_class (html_styled_ostream_t first_arg, const char *classname);
+extern const char *html_styled_ostream_get_hyperlink_ref (html_styled_ostream_t first_arg);
+extern const char *html_styled_ostream_get_hyperlink_id (html_styled_ostream_t first_arg);
+extern void html_styled_ostream_set_hyperlink (html_styled_ostream_t first_arg, const char *ref, const char *id);
 extern void html_styled_ostream_flush_to_current_style (html_styled_ostream_t first_arg);
 #ifdef __cplusplus
 }
@@ -481,6 +490,9 @@ extern void noop_styled_ostream_flush (noop_styled_ostream_t first_arg, ostream_
 extern void noop_styled_ostream_free (noop_styled_ostream_t first_arg);
 extern void noop_styled_ostream_begin_use_class (noop_styled_ostream_t first_arg, const char *classname);
 extern void noop_styled_ostream_end_use_class (noop_styled_ostream_t first_arg, const char *classname);
+extern const char *noop_styled_ostream_get_hyperlink_ref (noop_styled_ostream_t first_arg);
+extern const char *noop_styled_ostream_get_hyperlink_id (noop_styled_ostream_t first_arg);
+extern void noop_styled_ostream_set_hyperlink (noop_styled_ostream_t first_arg, const char *ref, const char *id);
 extern void noop_styled_ostream_flush_to_current_style (noop_styled_ostream_t first_arg);
 #ifdef __cplusplus
 }