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.

how to change the width and height in the HTML code into some other values decided by ratio and a fixed width?

The background is that you can adjust a image in your web-page to fit the fixed-width.


string="<img src=\"/HTMLFiles\\Help\\Help\\Help_1.gif\" alt=\"Help_1.gif\" width=\"882\" height=\"382\" style=\"vertical-align:middle\" /><img src=\"/HTMLFiles\\Help\\Help\\spikeyIcon.png\" alt=\"Spikey\" width=\"20\" height=\"21\" style=\"padding-right:2px; border:0px solid white; vertical-align:middle;\" />";

Here is my tries, a snippet of my terrible codes.

I want to change the $\text{width}>800$ into $800$, here it is $882$. And height becomes the corresponding value.

StringCases[string,(*strT:*) imgTag : Shortest["<img" ~~ __ ~~ ">"] :>   StringReplace[imgTag,    widthString :        Shortest["width=\"" ~~ width__ ~~ "\" "] ~~ (heightSring :         Shortest["height=\"" ~~ height__ ~~ "\" s"(*Here,          there is a strange thing I felt, try delete the 's'.*)]) ~~       pp__ /; (heightE =         ToExpression[         ToExpression[(ttt =             StringDrop[StringDrop[heightSring, 7], -1])]];        widthE =         ToExpression[         ToExpression[(tt = StringDrop[widthString, 6])]]) > 800 :>     "width=\"800\" height=\"" <> ToString[N[800 heightE/widthE]] <>      "\" " <> pp]]

(*
    {<img src="/HTMLFiles\Help\Help\Help_1.gif" alt="Help_1.gif" width="800" height="346.485" 
tyle="vertical-align:middle" />,<img src="/HTMLFiles\Help\Help\spikeyIcon.png" alt="Spikey
" width="20" height="21" style="padding-right:2px; border:0px solid white; vertical-align:
middle;" />}
*)

The value of ttt is

ttt

(*
    "21" 
*)

It's Ok, but there is a problem, see the comment of the above code.

question1:


in the heightString pattern, delete the "s" of "style"(I think/hope "\" " is enough to work well like that in widthString, but why it dosn't work well?)

StringCases[string,(*strT:*) imgTag : Shortest["<img" ~~ __ ~~ ">"] :>   StringReplace[imgTag,    widthString :        Shortest["width=\"" ~~ width__ ~~ "\" "] ~~ (heightSring :         Shortest["height=\"" ~~ height__ ~~ "\" "]) ~~       pp__ /; (heightE =         ToExpression[         ToExpression[(ttt =             StringDrop[StringDrop[heightSring, 7], -1])]];        widthE =         ToExpression[         ToExpression[(tt = StringDrop[widthString, 6])]]) > 800 :>     "width=\"800\" height=\"" <> ToString[N[800 heightE/widthE]] <>      "\" " <> pp]]

(*
    {<img src="/HTMLFiles\Help\Help\Help_1.gif" alt="Help_1.gif" width="800" height="346.485" 
style="vertical-align:middle" />,<img src="/HTMLFiles\Help\Help\spikeyIcon.png" alt="Spike
y" width="20" height="21" style="padding-right:2px; border:0px solid white; vertical-align
:middle;" />}
*)

ttt

(*
    "21" style="padding-right:2px; border:0px solid white; vertical-align:middle;"
*)

question2


of course, my codes are urgly, as you see, too many ToExpression,and StringDrop.

I definitely know you can think up a simpler method? how to optimize my codes?

Welcome to comment, if you're interested with the question and feel hard to understand and badly I'm always poor at expressing.

here is my page in GitHub with code formatted better

One problem need under consideration is the spaces, and that in my method it's sensitive, for example: maybe not all styles are after width=\"20\" height=\"21\" [other...?]style=\"padding... .


@ssch's answer is good, but I wanna see some StringManipulation answers, since I'm learning StringManipulation via this example. Welcome to post your answers.

share|improve this question
1  
I like that you added a link to your GitHub page. The string posted there works, the one above contains for example \s which Mathematica does not recognize. –  Jacob Akkerboom Sep 9 '13 at 7:33
    
@JacobAkkerboom I found there is a problem of my string, seems not the same as that in my notebook, I hope you've understand my question, that's the most important. –  HyperGroups Sep 9 '13 at 8:00
    
"\" s" is not a good pattern, since sometimes style is not just after height=\"30\" (other) style=.... –  HyperGroups Sep 9 '13 at 8:42
    
@JacobAkkerboom thanks, I think now the string in SE it's OK, But the code in GitHub, there is a problem of word-wrap, width=\" 882\" height=\" 382\" style= style is in the new line, that is not intended(in solving). –  HyperGroups Sep 9 '13 at 9:07
    
HyperGroups, I hope I have more time to look at it later :). (temporary message) –  Jacob Akkerboom Sep 9 '13 at 9:29

1 Answer 1

up vote 5 down vote accepted

I figure it's good to avoid trying to parse the String manually when we can have Mathematica turn it into an XMLObject for us with ImportString[string, {"HTML","XMLObject"}] which lends itself to more reliable parsing. It's not really simpler but should give less headaches down the line.

Here is a quick demonstration, modifyXMLAttributes takes an XMLElement and a rule. The elements (xml)attributes are put in a head with Attributes Orderless before the rule is applied for easier specification of rules.

Attributes[OrderlessList] = {Orderless};

modifyXMLAttributes[
  XMLElement[type_, List[oattr___], data___],
  orule_
  ] :=
 Module[{
   attr = OrderlessList[oattr],
   (* This part needs some tuning to be robust
      note that r_ matches Condition below and not RuleDelayed *)
   rule = orule /. r_[List[lhs___], rhs_] :> r[OrderlessList[lhs], rhs]
   },
  XMLElement[type, attr /. rule /. OrderlessList -> List, data]
  ]

xmlobj = ImportString[string, {"HTML", "XMLObject"}];
xmlobj /. img : XMLElement["img", ___] :>
  modifyXMLAttributes[img,
    {"width" -> w_, "height" -> h_, rest___} /; ToExpression[w] > 800 :>
    {"width" -> "800", 
     "height" -> ToString@Round[ToExpression[h]/(800./ToExpression[w])], 
     rest}];
ExportString[%, "XML"]

Gives:

"<?xml version='1.0' standalone='yes'?>
<html version='-//W3C//DTD HTML 4.01 Transitional//EN'
    xmlns='http://www.w3.org/1999/xhtml'>
 <body>
  <img width='800'
      height='346'
      alt='Help_1.gif'
      src='/HTMLFiles\\Help\\Help\\Help_1.gif'
      style='vertical-align:middle' />
  <img alt='Spikey'
      height='21'
      src='/HTMLFiles\\Help\\Help\\spikeyIcon.png'
      style='padding-right:2px; border:0px  solid white; \
vertical-align:middle;'
      width='20' />
 </body>
</html>"
share|improve this answer
    
yes, this is a good way, I'll use it if I have more time to develop my html package. :) Now one guideline of mine is to practice String Manipulation, so I use StringExpression as much as possible. Feel free to play with String whenever your time seems ripe if you are interested. –  HyperGroups Sep 10 '13 at 14:21

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.