summaryrefslogtreecommitdiff
path: root/enc/trans/escape.trans
blob: 544e1ace4a0abe1b10de148b319b6111418b6fa1 (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
#include "transcode_data.h"

static int
fun_so_escape_html_chref(void *statep, const unsigned char *s, size_t l, unsigned char *o)
{
    switch (*s) {
      case '&':
        o[0] = '&';
        o[1] = 'a';
        o[2] = 'm';
        o[3] = 'p';
        o[4] = ';';
        return 5;

      case '<':
        o[0] = '&';
        o[1] = 'l';
        o[2] = 't';
        o[3] = ';';
        return 4;

      case '>':
        o[0] = '&';
        o[1] = 'g';
        o[2] = 't';
        o[3] = ';';
        return 4;

      case '"':
        o[0] = '&';
        o[1] = 'q';
        o[2] = 'u';
        o[3] = 'o';
        o[4] = 't';
        o[5] = ';';
        return 6;

      default:
        rb_bug("unexpected char");
    }
} 
<%
  map_amp = {}
  map_amp["{00-25,27-FF}"] = :nomap
  map_amp["26"] = :func_so
  transcode_generate_node(ActionMap.parse(map_amp), "escape_amp_as_chref")

  map_html_text = {}
  map_html_text["{00-25,27-3B,3D,3F-FF}"] = :nomap
  map_html_text["26"] = :func_so
  map_html_text["3C"] = :func_so
  map_html_text["3E"] = :func_so
  transcode_generate_node(ActionMap.parse(map_html_text), "escape_html_text")

  map_html_attr = {}
  map_html_attr["{00-FF}"] = :func_so
  transcode_generate_node(ActionMap.parse(map_html_attr), "escape_html_attr")
%>

<%= transcode_generated_code %>

static const rb_transcoder
rb_escape_amp_as_chref = {
    "", "amp-escaped", escape_amp_as_chref,
    TRANSCODE_TABLE_INFO,
    1, /* input_unit_length */
    1, /* max_input */
    5, /* max_output */
    stateless_converter, /* stateful_type */
    0, NULL, NULL,
    NULL, NULL, NULL, &fun_so_escape_html_chref
};

static const rb_transcoder
rb_escape_html_text = {
    "", "html-text-escaped", escape_html_text,
    TRANSCODE_TABLE_INFO,
    1, /* input_unit_length */
    1, /* max_input */
    5, /* max_output */
    stateless_converter, /* stateful_type */
    0, NULL, NULL,
    NULL, NULL, NULL, &fun_so_escape_html_chref
};

#define END 0
#define NORMAL  1

static int
escape_html_attr_init(void *statep)
{
    unsigned char *sp = statep;
    *sp = END;
    return 0;
}

static VALUE
fun_so_escape_html_attr(void *statep, const unsigned char *s, size_t l, unsigned char *o)
{
    unsigned char *sp = statep;
    int n = 0;
    if (*sp == END) {
        *sp = NORMAL;
        o[n++] = '"';
    }
    switch (s[0]) {
      case '&':
      case '<':
      case '>':
      case '"':
        n += fun_so_escape_html_chref(statep, s, l, o+n);
        break;

      default:
        o[n++] = s[0];
        break;
    }
    return n;
}

static int
escape_html_attr_finish(void *statep, unsigned char *o)
{
    unsigned char *sp = statep;
    int n = 0;

    if (*sp == END) {
        o[n++] = '"';
    }

    o[n++] = '"';
    *sp = END;

    return n;
}

static const rb_transcoder
rb_escape_html_attr = {
    "", "html-attr-escaped", escape_html_attr,
    TRANSCODE_TABLE_INFO,
    1, /* input_unit_length */
    1, /* max_input */
    7, /* max_output */
    stateful_encoder, /* stateful_type */
    1, escape_html_attr_init, escape_html_attr_init,
    NULL, NULL, NULL, fun_so_escape_html_attr,
    escape_html_attr_finish
};

void
Init_escape(void)
{
    rb_register_transcoder(&rb_escape_amp_as_chref);
    rb_register_transcoder(&rb_escape_html_text);
    rb_register_transcoder(&rb_escape_html_attr);
}