6
$\begingroup$

Is there a native Mathematica way to convert a string like "<select></select>" into "&lt;select&gt;&lt;/select&gt;" and similarly for other reserved characters in html (or another encoding that would work in a webpage)?.

I'm working in webMathematica, but I can't find a good way to print the literal string "<select></select>" into a page - it either gets converted into an node, or it gets wrapped in a <pre> tag.

$\endgroup$

2 Answers 2

12
$\begingroup$

The different HTML entities are stored in System`Convert`MLStringDataDump`$HTMLEntities on version 9 and from here, it's a simple StringReplace:

StringReplace["<select></select>", System`Convert`MLStringDataDump`$HTMLEntities]
(* "&lt;select&gt;&lt;/select&gt;" *)
$\endgroup$
2
  • $\begingroup$ The problem with this is that the necessary definitions are not present in a fresh kernel. We need to figure out what triggers auto-loading them. ExportString["asd", "HTML"] certainly does but maybe it's more than necessary. $\endgroup$
    – Szabolcs
    Feb 19, 2014 at 20:53
  • $\begingroup$ This is the line registering the exporter: ImportExport`RegisterExport[ "HTML", System`Convert`HTMLDump`exportUsingHTMLSave, "Sources" -> {"Convert`ConvertCommon`", "Convert`MLStringData`", "Convert`HTMLConvert`"}, "Unevaluated" -> False ] I'm not sure what the "Sources" option does exactly. $\endgroup$
    – Szabolcs
    Feb 19, 2014 at 20:59
9
$\begingroup$

Something like:

ExportString[Cell[TextData["<select></select>"],"Text"],"HTML","FullDocument" -> False]

produces:

<p class="Text">
 &lt;select&gt;&lt;/select&gt;
</p>

which might also be a good start.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.