From: Bruno Haible Date: Wed, 14 Aug 2019 18:52:30 +0000 (+0200) Subject: libtextstyle: styled-ostream: Add hyperlink support. X-Git-Tag: v0.21~191 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=b117afc171263442b851cd0754184f266b4a4600;p=thirdparty%2Fgettext.git libtextstyle: styled-ostream: Add hyperlink support. * 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. --- diff --git a/libtextstyle/adhoc-tests/hello.c b/libtextstyle/adhoc-tests/hello.c index 1f827e57c..c94b175f3 100644 --- a/libtextstyle/adhoc-tests/hello.c +++ b/libtextstyle/adhoc-tests/hello.c @@ -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"); diff --git a/libtextstyle/gnulib-local/lib/html-styled-ostream.oo.c b/libtextstyle/gnulib-local/lib/html-styled-ostream.oo.c index 41aa170e1..945396499 100644 --- a/libtextstyle/gnulib-local/lib/html-styled-ostream.oo.c +++ b/libtextstyle/gnulib-local/lib/html-styled-ostream.oo.c @@ -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, "\n"); ostream_write_str (stream->destination, "\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, "\n"); /* HTML 4.01 or XHTML 1.0? diff --git a/libtextstyle/gnulib-local/lib/noop-styled-ostream.oo.c b/libtextstyle/gnulib-local/lib/noop-styled-ostream.oo.c index f24cffbe9..fb824b905 100644 --- a/libtextstyle/gnulib-local/lib/noop-styled-ostream.oo.c +++ b/libtextstyle/gnulib-local/lib/noop-styled-ostream.oo.c @@ -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; } diff --git a/libtextstyle/gnulib-local/lib/styled-ostream.oo.h b/libtextstyle/gnulib-local/lib/styled-ostream.oo.h index 2daa87c28..eed57f468 100644 --- a/libtextstyle/gnulib-local/lib/styled-ostream.oo.h +++ b/libtextstyle/gnulib-local/lib/styled-ostream.oo.h @@ -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 , 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. diff --git a/libtextstyle/gnulib-local/lib/term-styled-ostream.oo.c b/libtextstyle/gnulib-local/lib/term-styled-ostream.oo.c index 2cfd4a854..e9d4f0fd3 100644 --- a/libtextstyle/gnulib-local/lib/term-styled-ostream.oo.c +++ b/libtextstyle/gnulib-local/lib/term-styled-ostream.oo.c @@ -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) { diff --git a/libtextstyle/lib/textstyle.h b/libtextstyle/lib/textstyle.h index 00471d481..a80987a61 100644 --- a/libtextstyle/lib/textstyle.h +++ b/libtextstyle/lib/textstyle.h @@ -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 }