|
202 | 202 | { |
203 | 203 | "cell_type": "code", |
204 | 204 | "execution_count": null, |
205 | | - "id": "e89496fb", |
| 205 | + "id": "254c8ff3", |
| 206 | + "metadata": {}, |
| 207 | + "outputs": [], |
| 208 | + "source": [ |
| 209 | + "#| export\n", |
| 210 | + "def _escape(s): return '' if s is None else escape(s) if isinstance(s, str) else s" |
| 211 | + ] |
| 212 | + }, |
| 213 | + { |
| 214 | + "cell_type": "code", |
| 215 | + "execution_count": null, |
| 216 | + "id": "0255b96f", |
206 | 217 | "metadata": {}, |
207 | 218 | "outputs": [], |
208 | 219 | "source": [ |
|
211 | 222 | " if isinstance(v,bool):\n", |
212 | 223 | " if v==True : return str(k)\n", |
213 | 224 | " if v==False: return ''\n", |
214 | | - " return f'{k}=\"{escape(str(v), quote=False)}\"'" |
| 225 | + " return f'{k}=\"{escape(str(v), quote=True)}\"'" |
215 | 226 | ] |
216 | 227 | }, |
217 | 228 | { |
|
224 | 235 | "#| export\n", |
225 | 236 | "def to_xml(elm, lvl=0):\n", |
226 | 237 | " \"Convert `xt` element tree into an XML string\"\n", |
| 238 | + " if elm is None: return ''\n", |
227 | 239 | " if isinstance(elm, tuple): return '\\n'.join(to_xml(o) for o in elm)\n", |
228 | 240 | " if hasattr(elm, '__xt__'): elm = elm.__xt__()\n", |
229 | 241 | " sp = ' ' * lvl\n", |
230 | | - " if not isinstance(elm, list):\n", |
231 | | - " if isinstance(elm, str): elm = escape(elm)\n", |
232 | | - " return f'{elm}\\n'\n", |
| 242 | + " if not isinstance(elm, list): return f'{_escape(elm)}\\n'\n", |
233 | 243 | "\n", |
234 | 244 | " tag,cs,attrs = elm\n", |
235 | 245 | " stag = tag\n", |
|
239 | 249 | "\n", |
240 | 250 | " cltag = '' if tag in voids else f'</{tag}>'\n", |
241 | 251 | " if not cs: return f'{sp}<{stag}>{cltag}\\n'\n", |
| 252 | + " if len(cs)==1 and not isinstance(cs[0],(list,tuple)) and not hasattr(cs[0],'__xt__'):\n", |
| 253 | + " return f'{sp}<{stag}>{_escape(cs[0])}{cltag}\\n'\n", |
242 | 254 | " res = f'{sp}<{stag}>\\n'\n", |
243 | 255 | " res += ''.join(to_xml(c, lvl=lvl+2) for c in cs)\n", |
244 | 256 | " if tag not in voids: res += f'{sp}{cltag}\\n'\n", |
|
257 | 269 | "text": [ |
258 | 270 | "<html>\n", |
259 | 271 | " <head>\n", |
260 | | - " <title>\n", |
261 | | - "Some page\n", |
262 | | - " </title>\n", |
| 272 | + " <title>Some page</title>\n", |
263 | 273 | " </head>\n", |
264 | 274 | " <body>\n", |
265 | 275 | " <div class=\"myclass\">\n", |
|
0 commit comments