OSDN Git Service

c165686075384f9805ae98c266167e4d12722b20
[hmh/hhml.git] / lib / motoroutput.cc
1 #include "motoroutput.h"
2 #include "ml.h"
3 #include "util_string.h"
4 #include "filemacro.h"
5 #include "ustring.h"
6 #include "utf8.h"
7 #include "motorconst.h"
8 #include <iostream>
9
10 /* ============================================================ */
11 static uregex  re_encode ("(<)|(>)|(&)|(\")|(')");
12 static uregex  re_encode_br ("(<)|(>)|(&)|(\")|(')|(\n)");
13 static uregex  re_encode_br_a ("(<)|(>)|(&)|(\")|(')|(\n)|(" rHTML ")");
14
15 MotorOutput*  MotorOutput::outamp (uiterator b, uiterator e, uregex* re) {
16     umatch  m;
17
18     while (b != e && usearch (b, e, m, *re)) {
19         if (b != m[0].first) {
20             ustring  t (b, m[0].first);
21             out_toText (t);
22         }
23         b = m[0].second;
24
25         if (m[1].matched) {             // <
26             out_raw (CharConst ("&lt;"));
27         } else if (m[2].matched) {      // >
28             out_raw (CharConst ("&gt;"));
29         } else if (m[3].matched) {      // &
30             out_raw (CharConst ("&amp;"));
31         } else if (m[4].matched) {      // "
32             out_raw (CharConst ("&quot;"));
33         } else if (m[5].matched) {      // '
34             out_raw (CharConst ("&#39;"));
35         } else if (m[6].matched) {      // \n
36             out_raw (CharConst ("<br />\n"));
37         } else if (m[7].matched) {      // http...
38             out_raw (CharConst ("<a href=\""));
39             outamp (m[7].first, m[7].second, &re_encode);
40             out_raw (CharConst ("\">"));
41             outamp (m[7].first, m[7].second, &re_encode);
42             out_raw (CharConst ("</a>"));
43         } else {
44             assert (0);
45         }
46     }
47     if (b != e) {
48         ustring  t (b, e);
49         out_toText (t);
50     }
51     return this;
52 }
53
54 MotorOutput*  MotorOutput::out_templateText (const ustring& str) {
55     return out_raw (fixUTF8 (str));
56 }
57
58 MotorOutput*  MotorOutput::out_toHTML (const ustring& str) {
59     ustring  t = fixUTF8 (str);
60     outamp (t.begin (), t.end (), &re_encode);
61     return this;
62 }
63
64 MotorOutput*  MotorOutput::out_toHTML_br (const ustring& str) {
65     ustring  t = fixUTF8 (str);
66     outamp (t.begin (), t.end (), &re_encode_br);
67     return this;
68 }
69
70 MotorOutput*  MotorOutput::out_toHTML_br_a (const ustring& str) {
71     ustring  t = fixUTF8 (str);
72     outamp (t.begin (), t.end (), &re_encode_br_a);
73     return this;
74 }
75
76 MotorOutput*  MotorOutput::out_toHTML_wbr (const ustring& str) {
77     ustring  t = fixUTF8 (str);
78     outamp (t.begin (), t.end (), &re_encode);
79     return this;
80     /* *** */
81     assert (0);
82 }
83
84 MotorOutput*  MotorOutput::out_toHTML_noCtrl (const ustring& str) {
85     out_toHTML (omitCtrl (str));
86     return this;
87 }
88
89 MotorOutput*  MotorOutput::out_toJS (const ustring& str) {
90     out_raw (jsEncode (str));
91     return this;
92 }
93
94 MotorOutput*  MotorOutput::out_noCtrl (const ustring& str) {
95     out_raw (omitCtrl (str));
96     return this;
97 }
98
99 MotorOutput*  MotorOutput::outNbsp () {
100     out_raw (uNbsp);
101     return this;
102 }
103
104 MotorOutput*  MotorOutput::out_file (const ustring& src) {
105     FileMacro  f;
106     char*  b;
107     ssize_t  s;
108
109     if (f.openRead (src.c_str ())) {
110         b = new char[65536];
111         while ((s = f.read (b, 65536)) > 0) {
112             out (b, s);
113         }
114         delete b;
115     }
116     return this;
117 }
118
119 MotorOutput*  MotorOutput::outamp (const ustring& t) {
120     outamp (t.begin (), t.end (), &re_encode);
121     return this;
122 }
123
124 MotorOutput*  MotorOutput::outamp_br (const ustring& t) {
125     outamp (t.begin (), t.end (), &re_encode_br);
126     return this;
127 }
128
129 MotorOutput*  MotorOutput::outamp_wbr (const ustring& t) {
130     outamp (t.begin (), t.end (), &re_encode);
131     return this;
132     /* *** */
133     assert (0);
134 }
135
136 MotorOutput*  MotorOutput::outamp_nw (const ustring& t) {
137     if (t.size () > 0) {
138         outamp (t);
139     } else {
140         outNbsp ();
141     }
142 }
143
144 MotorOutput*  MotorOutput::outamp_c3 (const ustring& t) {
145     outamp (c3 (t));
146 }
147
148 /* ============================================================ */
149 MotorOutput*  MotorOutputCOut::out (const ustring::value_type* s, size_t len) {
150     std::cout.write (s, len);
151     return this;
152 }
153
154 /* ============================================================ */
155 MotorOutput*  MotorOutputString::out (const ustring::value_type* s, size_t len) {
156     ans.append (s, len);
157     return this;
158 }
159
160 /* ============================================================ */
161 MotorOutput*  MotorOutputOStream::out (const ustring::value_type* s, size_t len) {
162     ostream->write (s, len);
163     return this;
164 }
165
166 /* ============================================================ */