summaryrefslogtreecommitdiff
path: root/gettext-tools/src/filter-quote.c
blob: 23967a28cc68bd128734fdca46053564e1396c2e (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
/* Convert ASCII quotations to Unicode quotations.
   Copyright (C) 2014-2016 Free Software Foundation, Inc.
   Written by Daiki Ueno <ueno@gnu.org>, 2014.

   This program is free software: you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 3 of the License, or
   (at your option) any later version.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program.  If not, see <http://www.gnu.org/licenses/>.  */

#ifdef HAVE_CONFIG_H
# include <config.h>
#endif

/* Specification.  */
#include "filters.h"

#include "quote.h"
#include <stdbool.h>
#include <stdlib.h>
#include <string.h>
#include "xalloc.h"

#define BOLD_START "\x1b[1m"
#define BOLD_END "\x1b[0m"

struct result
{
  char *output;
  char *offset;
  bool bold;
};

static void
convert_quote_callback (char quote, const char *quoted, size_t quoted_length,
                        void *data)
{
  struct result *result = data;

  switch (quote)
    {
    case '\0':
      memcpy (result->offset, quoted, quoted_length);
      result->offset += quoted_length;
      break;

    case '"':
      /* U+201C: LEFT DOUBLE QUOTATION MARK */
      memcpy (result->offset, "\xe2\x80\x9c", 3);
      result->offset += 3;
      if (result->bold)
        {
          memcpy (result->offset, BOLD_START, 4);
          result->offset += 4;
        }
      memcpy (result->offset, quoted, quoted_length);
      result->offset += quoted_length;
      if (result->bold)
        {
          memcpy (result->offset, BOLD_END, 4);
          result->offset += 4;
        }
      /* U+201D: RIGHT DOUBLE QUOTATION MARK */
      memcpy (result->offset, "\xe2\x80\x9d", 3);
      result->offset += 3;
      break;

    case '\'':
      /* U+2018: LEFT SINGLE QUOTATION MARK */
      memcpy (result->offset, "\xe2\x80\x98", 3);
      result->offset += 3;
      if (result->bold)
        {
          memcpy (result->offset, BOLD_START, 4);
          result->offset += 4;
        }
      memcpy (result->offset, quoted, quoted_length);
      result->offset += quoted_length;
      if (result->bold)
        {
          memcpy (result->offset, BOLD_END, 4);
          result->offset += 4;
        }
      /* U+2019: RIGHT SINGLE QUOTATION MARK */
      memcpy (result->offset, "\xe2\x80\x99", 3);
      result->offset += 3;
      break;
    }
}

/* This is a direct translation of po/quot.sed and po/boldquot.sed.  */
static void
convert_ascii_quote_to_unicode (const char *input, size_t input_len,
                                char **output_p, size_t *output_len_p,
                                bool bold)
{
  const char *p;
  size_t quote_count;
  struct result result;

  /* Count the number of quotation characters.  */
  quote_count = 0;
  for (p = input; p < input + input_len; p++)
    {
      size_t len;

      p = strpbrk (p, "`'\"");
      if (!p)
        break;

      len = strspn (p, "`'\"");
      quote_count += len;
      p += len;
    }

  /* Large enough.  */
  result.output = XNMALLOC (input_len - quote_count
                            + (bold ? 7 : 3) * quote_count + 1,
                            char);
  result.offset = result.output;
  result.bold = bold;

  scan_quoted (input, input_len, convert_quote_callback, &result);

  *output_p = result.output;
  *output_len_p = result.offset - result.output;
}

void
ascii_quote_to_unicode (const char *input, size_t input_len,
                        char **output_p, size_t *output_len_p)
{
  convert_ascii_quote_to_unicode (input, input_len,
                                  output_p, output_len_p,
                                  false);
}

void
ascii_quote_to_unicode_bold (const char *input, size_t input_len,
                             char **output_p, size_t *output_len_p)
{
  convert_ascii_quote_to_unicode (input, input_len,
                                  output_p, output_len_p,
                                  true);
}