Skip to content

Generate links at compile-time rather than use JS #207

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

Merged
merged 5 commits into from
Feb 17, 2017
Merged

Generate links at compile-time rather than use JS #207

merged 5 commits into from
Feb 17, 2017

Conversation

steveklabnik
Copy link
Member

Fixes #204

/cc @frewsxcv

@steveklabnik
Copy link
Member Author

oh wait i bet i can do this in one pass....


html = regex.replace_all(&html, |caps: &Captures| {
let text = &caps[1];
let mut id = text.to_string();
Copy link
Member Author

Choose a reason for hiding this comment

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

i am almost tempted to pull this shenanigans out into a microcrate so that rustdoc and mdbook can share it

@steveklabnik
Copy link
Member Author

regex doesn't support back-references, which is what's need to do this in one pass. so we can stick with this, or we can add a dependency on https://github.com/google/fancy-regex. I'm tempted to just stick with it.

@steveklabnik
Copy link
Member Author

actually https://docs.rs/regex/0.2.0/regex/struct.RegexSet.html might be able to do it in one pass...

@steveklabnik
Copy link
Member Author

Nevermind, I don't think so. This should be good to go.

Cargo.toml Outdated
@@ -24,6 +24,7 @@ log = "0.3"
env_logger = "0.3"
toml = { version = "0.2", features = ["serde"] }
open = "1.1"
regex = "0.1.80"
Copy link
Contributor

Choose a reason for hiding this comment

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

The last version of regex seems to be 0.2.1 is there a reason for using this specific version?

Copy link
Member Author

Choose a reason for hiding this comment

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

It was the version that was already in cargo.lock; I'd be happy to upgrade it too, but I was trying to not mess with dependencies 😄

Copy link
Contributor

Choose a reason for hiding this comment

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

I think it's ok to go for the latest, it shouldn't cause any problems right?

Copy link
Member Author

@steveklabnik steveklabnik Feb 16, 2017

Choose a reason for hiding this comment

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

the only thing it might cause is two version of regex, given that iirc this is a dependency of something.

I could also just update all deps; https://github.com/azerupi/mdBook/pull/206 is doing some of that

happy to do whatever you want 😄

Copy link
Contributor

Choose a reason for hiding this comment

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

Updating deps is good, but maybe it's better if I first accept #206 then to avoid conflicts

Copy link
Member Author

Choose a reason for hiding this comment

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

Sounds great!

Copy link
Contributor

Choose a reason for hiding this comment

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

#206 is merged, you can go ahead and update the rest :)

let mut id = text.to_string();
let repl_sub = vec!["<em>", "</em>", "<code>", "</code>",
"<strong>", "</strong>",
"&lt;", "&gt;", "&amp;", "&#39;", "&quot;"];
Copy link
Contributor

Choose a reason for hiding this comment

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

I don't think this list is exhaustive and future proof. Maybe it would be better to match against anything that could look like a tag? I am not sure how GitHub does it.

Copy link
Member Author

Choose a reason for hiding this comment

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

i copied this code directly from rustdoc so that it'd have the same behavior; i agree that it's probably not exhaustive.

I am not sure how GitHub does it.

I don't know either :/

Copy link
Contributor

Choose a reason for hiding this comment

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

I guess it's good enough for now :)
We can improve it later if necessary.

This project already had a transitive dependency on regex; let's use it.

This isn't the most efficient solution, but it should be fine. It ends
up doing five full scans of the text. There's probably an easier way but
I'm mostly just trying to get this to work for now.

This also implements the same algorithm that rustdoc does for generating
the name for the link.

Fixes #204
@steveklabnik
Copy link
Member Author

Okay! I have updated all the deps required to have one version of regex; I did not update all of the deps, but might send in a new PR later 😄

@azerupi
Copy link
Contributor

azerupi commented Feb 16, 2017

Great! Once the little lights turn green it will be good to merge 😉

@steveklabnik
Copy link
Member Author

@azerupi travis was taking a while, and in the meantime, I figured out how to make it one pass. Given that we know it always generates valid tags, we can just do this. LMK if you'd prefer the more explicit version with the extra passes.

@azerupi azerupi merged commit 864be6c into rust-lang:master Feb 17, 2017
@azerupi
Copy link
Contributor

azerupi commented Feb 17, 2017

Looks good to me, thanks! :)

@steveklabnik steveklabnik deleted the gh204 branch February 19, 2017 01:19
@steveklabnik
Copy link
Member Author

@azerupi any chance we can get a crates.io release for this? We need it to ship rust-lang/rust#39855

@azerupi
Copy link
Contributor

azerupi commented Feb 19, 2017

@steveklabnik Done :)

@steveklabnik
Copy link
Member Author

❤️

steveklabnik referenced this pull request in steveklabnik/rust Feb 20, 2017
This version of mdbook includes
https://github.com/azerupi/mdBook/pull/207 , which is needed so that
we can start doing linkchecker on the various books.
bors referenced this pull request in rust-lang/rust Feb 20, 2017
Update mdbook version

This version of mdbook includes
https://github.com/azerupi/mdBook/pull/207 , which is needed so that
we can start doing linkchecker on the various books.
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