4
$\begingroup$

I see that there is limited support for "HTMLFragment" in Export, but I figured with all the work that went into porting Mathematica into the Wolfram Cloud there should be a way to Import "HTMLFragments".

Here is the fragment I'd like to import into a cell/box expression:

    <div style=\"text-align: center;\">
    This is <span style=\"font-size: 21px;\">
    <i>an example</i></span> of a <u>
    <font face=\"Georgia\" style=\"font-size: 19px;\">text cell</font></u> with 
<font color=\"#c14dff\" style=\"\">complex styles</font> applied.</div>

So this would look like this: enter image description here

$\endgroup$
3
  • $\begingroup$ p.s. Import it as XML and define rules for your transformation. There is also a tutorial about working with XML. $\endgroup$
    – Kuba
    Commented May 11, 2015 at 19:05
  • $\begingroup$ @Kuba I added an image of what it should render to. $\endgroup$
    – M.R.
    Commented May 11, 2015 at 19:27
  • $\begingroup$ You can start with this: Block[{XMLElement, f}, XMLElement[tag_, opt_, content_] := Tooltip[Row[content, " "], f[opt]]; xml[[2]] ] $\endgroup$
    – Kuba
    Commented May 14, 2015 at 6:55

1 Answer 1

3
$\begingroup$

OP's stuff:

html = "<div style=\"text-align: center;\">
      This is <span style=\"font-size: 21px;\">
      <i>an example</i></span> of a <u>
      <font face=\"Georgia\" style=\"font-size: 19px;\">text \
cell</font></u> with 
  <font color=\"#c14dff\" style=\"\">complex styles</font> \
applied.</div>";

Simon Woods color converter:

hexToRGB = RGBColor @@ (IntegerDigits[ ToExpression@StringReplace[#, "#" -> "16^^"], 256, 3]/255.) &

Auxiliary functions, you have to extend them for proper interpretation of all rules...

lhs[name_String] := Switch[name,
   "face", FontFamily,
   "font-size", FontSize,
   "color", FontColor,
   "text-align", TextAlignment,
   "center", Center,
   _, name
   ];

rhs[string_String] :=  Composition[ToExpression, lhs, 
    StringReplace[#, {"px" -> "", color : ("#" ~~ __) :> ToString[hexToRGB[color]]}] &,  StringTrim][string];

style[opt : {__}] := (style /@ opt);
style[{} | ""] := {};
style["style" -> (dir_)] := style[dir];
style[(l_) -> (r_)] := lhs[l] -> rhs[r];
style[s_String] := Sequence @@ (lhs[#] -> rhs[#2] & @@@ Partition[StringSplit[s, ";" | ":"], 2]);

Procedure:

xml = ImportString[html, "XML"];


Block[{XMLElement},

 XMLElement["div", opt_, content_] := Cell[BoxData@RowBox@content, style@opt];
 XMLElement["i", opt_, content_] := StyleBox[RowBox@content, Italic];
 XMLElement["u", opt_, content_] := StyleBox[RowBox@content, Underlined];
 XMLElement[tag_, opt_, content_] := StyleBox[RowBox[Riffle[content, " "]], style@opt];

 CellPrint@xml[[2]]]

enter image description here

$\endgroup$
4
  • $\begingroup$ A bit telegraphic, but it seems to do the job :-) +1 $\endgroup$
    – chris
    Commented May 15, 2015 at 8:27
  • $\begingroup$ @chris You mean I should add explanations? Let's say I will answer any question instead :) There is to much t write to make nice tutorial. Shortly, those are just DownValues for XMLElements and style parser. $\endgroup$
    – Kuba
    Commented May 15, 2015 at 8:30
  • $\begingroup$ Its perfectly fine. It just feels you were in a bit of a rush. $\endgroup$
    – chris
    Commented May 15, 2015 at 8:34
  • $\begingroup$ @chris I always am, I should try to learn better time management :/ $\endgroup$
    – Kuba
    Commented May 15, 2015 at 8:40

Your Answer

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

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