Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Client side play urls #10

Merged
merged 20 commits into from
Nov 11, 2017
Merged

Client side play urls #10

merged 20 commits into from
Nov 11, 2017

Conversation

ests
Copy link
Contributor

@ests ests commented Oct 4, 2016

Assembly links to Play Rust out of source files fetched from the server. This is done just after DOM has loaded - every link with special data attribute will have its href replaced.

The other solution was to use various onclick events, but opening windows via javascript will almost always trigger popup blocking.

See #3 .

ests added 18 commits October 3, 2016 23:03
Files inside directory that is prefixed with underscore are not
accessible for the server, and so cannot be fetched via xhr.
For clients that have js disabled?
It seems that sometimes event is not fired if script is loaded in
asynchronous manner, and thus nothing will work.
But browsers will block windows opened this way.
This also triggers popup blocker :(
I think doing this in js is a bad idea, and console shouldn't be used
in "production" (eslint doesn't like it). A bettter solution would be
writing some Ruby script, that checks all source files for any problems
with those special comments. The only drawback is that this *.rb script
would need to be invoked manually.
var RUST_VERSION = "nightly";
var HOME_URL = window.location.protocol + "//" + window.location.host + "/";

document.addEventListener("DOMContentLoaded", function() {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This will fail in IE 8. Alternative solution would be putting whole script inline, inside main HTML file, just before closing </body> tag. Not the nicest solution, but should work everywhere.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Meh. I think we can assume fairly modern browsers.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(Well, I guess we just need to put a call to playUrl() inline, after the body tag? (And export it out of the function, obviously.))

Still, I don't care much.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure, if anyone complains, it'll be easy to change.

}

function addTransformations(text) {
var solutionRegex = /\/\/ START SOLUTION\s*(.*)\s*\/\/ END SOLUTION/g;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This isn't quite how start/end solution works. The idea is something like this:

fn foo() {
    // PROMPT xxx
    // START SOLUTION
    yyy
    // END SOLUTION
}

would get transformed to:

fn foo() {
    xxx
}

Copy link
Contributor Author

@ests ests Oct 5, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

And it should work exactly that way. I can remove (.*) here, but it will work anyway.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, wait. Maybe I am wrong. =) I was assuming that . in JS regexs doesn't match newlines, but maybe it does?

Regardless, this makes me nervous. In particular, you could have multiple sections:

// START SOLUTION
xxx
// END SOLUTION
yyy
// START SOLUTION
zzz
// END SOLUTION

and a regex engine using maximal matching (the typical default) would gobble up xxx, yyy, and zzz. I suppose you could to (.*?), if JS engines support that, and it'd probably work.

Copy link
Contributor Author

@ests ests Oct 5, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

. in JS doesn't match newlines, that's why we need s+. I am testing all regexes here: https://regex101.com/r/1nWjUL/1
I already added some modifications to the regex.

And it reminds me... https://www.xkcd.com/208/

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So the one you had I think didn't work if there was more than one line in the solution. I made a modified version \/\/ START SOLUTION(\s|.)*?\/\/ END SOLUTION that seems to work though:

https://regex101.com/r/1nWjUL/2

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice catch about multiline solutions! I modify this line, but I don't understand why do you need /n? at the end?

@nikomatsakis
Copy link
Collaborator

This looks pretty good! I left a few comments.

}

function addTransformations(text) {
var solutionRegex = /\/\/ START SOLUTION\s+.*\s+\/\/ END SOLUTION/g;
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As long as your whitespace formatting (new lines) is OK, there is (hopefully) nothing to worry about this expression.

As suggested by @nikomatsakis.

Also requiring at least one character to exist between START END solution (even if it's empty line).
@nikomatsakis
Copy link
Collaborator

Argh, this fell off my radar. I admit I don't feel qualified to know if this is really The Right Way to go about things, but this seems like a good approach to me.

@ests did you delete the original repo? I was going to poke about and try it locally, but I can't seem to figure out where to merge from.

@ests
Copy link
Contributor Author

ests commented Dec 27, 2016

@nikomatsakis I was sure this won't be accepted so I was too quick to delete it - sorry. I also don't have it on my laptop. I don't know if this is (was) the right way to go. But now probably the only way to play with it, would be to somehow to generate diff file on github and apply it locally, but I don't know if this is possible (easily).

@nikomatsakis
Copy link
Collaborator

@nikomatsakis
Copy link
Collaborator

Argh, I've let this sit for way too long! I'm inclined to merge it at this point =) Do these kinds of URLs still work?

@nikomatsakis nikomatsakis merged commit 78451e5 into intorust:master Nov 11, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants