WebDialog.set_html() Gotchas
-
It is very convenient to keep small HTML segments in your Ruby, rather than making a separate file. However, you should know this:
The WebDialog.set_html() writes your HTML into a temp file and hands the browser the URI of that file. During the file writing process, character substitution is performed. I'd call it a bug, not a feature. It means the string in this bit of script:
crlf = '\x0d\x0a';
is reduced to two characters, an unterminated string error at load time. This works:
crlf = String.fromCharCode( 13 ) + String.fromCharCode( 10 );
If you want a backslash in a regular expression, its probably best to put the HTML in its own file. This is from the console I'm working on:
# escape the backslashes; pathname.gsub!( /\\/, "\\\\\\\\" ) # huh? dunno. experiment.
-
Can you post a failing (small, complete) example?
-
What if you use the standard escape sequences for regular expressions, which both Ruby and Js share.
%(#8000BF)[crlf = '\r\n';]
Does the writing to the Temp file replace the escape sequences?
-
.
And a good example of why I like Ruby so much:Js:
%(#8000BF)[crlf = String.fromCharCode( 13 ) + String.fromCharCode( 10 );]
Ruby:
crlf = 13.chr<<10.chr
-
Works for me on a Mac - no errors shown. You are missing a semi-colon in your javascript.
On Windows, I do get the unterminated string error, but changing your line from this:
crlf = '\x0d\x0a' ;
to this:
crlf = '\\x0d\\x0a' ;
resolves the error. That's the nature of the beast (ruby). You gotta double your backslashes if you want backslashes.
-
@unknownuser said:
Can you post a failing (small, complete) example?
require 'sketchup' html=" <html> <body> <script> crlf = '\x0d\x0a' </script> </body> </html> " wd = UI;;WebDialog.new( "test", true, "test", 500, 500, 0, 0, true ) wd.set_html( html ) wd.show()
Code typed, not run, but should be close. On error, note the name of the file. Load it in your editor and note the difference between your html variable's content and the file's content.
@dan rathbun said:
.
And a good example of why I like Ruby so much:Js:
%(#8000BF)[crlf = String.fromCharCode( 13 ) + String.fromCharCode( 10 );]
Ruby:
crlf = 13.chr<<10.chr
I couldn't tell if you were being serious or sarcastic. The Ruby's terse but it wins no prizes for readability. I'd replace either of these with
crlf = ASC(13) + ASC(10)
.And watch what you say on this as Monty, my pet Python, also reads these posts. Trust me, you want to stay on his good side.
Edit: Do I have that backwards?
CHR(13) + CHR(10)
? -
@unknownuser said:
Works for me on a Mac - no errors shown.
Does Safari read from a file, or is it given the HTML otherwise? On a PC, I'd throw an error into the code and MSIE would report the URL.
@unknownuser said:
You gotta double your backslashes if you want backslashes.
Because char substitution goes on during the file writing:
\\x0a
is an escaped backslash followed by the literal "x0a", which is written as a single backslash followed by the literal "x0a" which is the hex literal\x0a
. -
@martinrinehart said:
@dan rathbun said:
.And a good example of why I like Ruby so much:
Js:%(#8000BF)[crlf = String.fromCharCode( 13 ) + String.fromCharCode( 10 );]
Ruby:crlf = 13.chr<<10.chr
I couldn't tell if you were being serious or sarcastic.
Quite serious and honest. (Most people tire of sarcasm very quickly, myself included.)
@martinrinehart said:The Ruby's terse but it wins no prizes for readability. I'd replace either of these with
crlf = ASC(13) + ASC(10)
.
Edit: Do I have that backwards?CHR(13) + CHR(10)
?
Several times now I've seen you make comments that Ruby "wins no prizes for readability" and something about 'Monty' your pet Python. I don't care what Language is better, it doesn't matter. (Ruby was chosen as the scripting language for Sketchup by the powers that be.)
I have NO problems reading Ruby. It makes sense when your brain finally reaches the point of object epiphany. (With Ruby, it didn't happen for me right away, even though I was exposed to semi-OOP languages in the past.) In Ruby the 13 is an object, an instance of class Fixnum. It inherits instance methods from it's class, which usually operate on the object itself, so in Ruby the 'tradition' is to name the method for the class of the output. What could be simpler than that 13.chr will return the 13th character is from the character set currently in use?
The example from Js the reverse. (I purposely avoid any disparaging term here. It's just different is all, not better, not worse.) Instead of being an object method as in Ruby, the Js statement is more a Library function. String being the library, fromCharCode the function, and then the 'term' that you wish it to operate on must be passed as a parameter. (Obviously what I most meant [in my previous post,] is that often there's alot less typing in Ruby, but that's not the best reason why I like Ruby.)
So, the main point I am making, is that Ruby with it's 99% OOP structure, requires a reverse way of thinking while reading and writing the code.
I'm not against Js, I learned it ..oh 12 years ago I guess. And have nothing against Python, I was about to dive in and learn it, when I found Ruby. I still may.. I like the fact of being able to compile Python programs.
Since we are on the subject (going off topic abit,) since SU Ruby plugins can be writen in C and compiled into so or dll, there shouldn't be any reason why you can't write SU plugins in Python and compile them. It would require (not sure what Python calls them,) "wrappers"?
There is a guy in NZ who says he has a Python plugin for Sketchup, he calls it SuPy:
http://groups.google.com/group/sketchupruby/browse_frm/thread/7fdc49a451ae3cae#Anyway.. the point on character substitution is well taken. Another one for the long list WebDialog pitfalls (AAAaaaHHHhaha aaa SPLAT!)
-
"Any customer can have a car painted any color that he wants so long as it is black." H. Ford
There is no doubt that JavaScript is the best language for code running in the browser. Ruby is absolutely the best choice for SketchUp plugins. For similar reasons we've chosen HTML and CSS.
However, if you know a man whose pet Python is named Monty, you may infer something about his personal language choice. (Monty just reminded me that van Rossum works for Google. There's hope.)
Advertisement