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 style
s 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.
string
, seems not the same as that in my notebook, I hope you've understand my question, that's the most important. $\endgroup$"\" s"
is not a good pattern, since sometimes style is not just afterheight=\"30\" (other) style=...
. $\endgroup$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). $\endgroup$