Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

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.

share|improve this question
add comment

2 Answers

up vote 7 down vote accepted

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;" *)
share|improve this answer
    
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. –  Szabolcs Feb 19 at 20:53
    
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. –  Szabolcs Feb 19 at 20:59
add comment

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.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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