1#include "transcode_data.h"
2
3<%
4  def hexstr(str)
5    str.unpack("H*")[0]
6  end
7
8  transcode_tblgen("", "amp_escape", [
9    ["{00-25,27-FF}", :nomap], 
10    ["26", hexstr("&amp;")]
11  ], nil)
12
13  transcode_tblgen("", "xml_text_escape", [
14    ["{00-25,27-3B,3D,3F-FF}", :nomap], 
15    ["26", hexstr("&amp;")],
16    ["3C", hexstr("&lt;")],
17    ["3E", hexstr("&gt;")]
18  ], nil)
19
20  transcode_tblgen("", "xml_attr_content_escape", [
21    ["{00-21,23-25,27-3B,3D,3F-FF}", :nomap], 
22    ["22", hexstr("&quot;")],
23    ["26", hexstr("&amp;")],
24    ["3C", hexstr("&lt;")],
25    ["3E", hexstr("&gt;")]
26  ], nil)
27
28  map_xml_attr_quote = {}
29  map_xml_attr_quote["{00-FF}"] = :func_so
30  transcode_generate_node(ActionMap.parse(map_xml_attr_quote), "escape_xml_attr_quote")
31%>
32
33<%= transcode_generated_code %>
34
35#define END 0
36#define NORMAL  1
37
38static int
39escape_xml_attr_quote_init(void *statep)
40{
41    unsigned char *sp = statep;
42    *sp = END;
43    return 0;
44}
45
46static ssize_t
47fun_so_escape_xml_attr_quote(void *statep, const unsigned char *s, size_t l, unsigned char *o, size_t osize)
48{
49    unsigned char *sp = statep;
50    int n = 0;
51    if (*sp == END) {
52        *sp = NORMAL;
53        o[n++] = '"';
54    }
55    o[n++] = s[0];
56    return n;
57}
58
59static ssize_t
60escape_xml_attr_quote_finish(void *statep, unsigned char *o, size_t osize)
61{
62    unsigned char *sp = statep;
63    int n = 0;
64
65    if (*sp == END) {
66        o[n++] = '"';
67    }
68
69    o[n++] = '"';
70    *sp = END;
71
72    return n;
73}
74
75static const rb_transcoder
76rb_escape_xml_attr_quote = {
77    "", "xml_attr_quote", escape_xml_attr_quote,
78    TRANSCODE_TABLE_INFO,
79    1, /* input_unit_length */
80    1, /* max_input */
81    7, /* max_output */
82    asciicompat_encoder, /* asciicompat_type */
83    1, escape_xml_attr_quote_init, escape_xml_attr_quote_init,
84    NULL, NULL, NULL, fun_so_escape_xml_attr_quote,
85    escape_xml_attr_quote_finish
86};
87
88TRANS_INIT(escape)
89{
90<%= transcode_register_code %>
91    rb_register_transcoder(&rb_escape_xml_attr_quote);
92}
93
94