Skip to content

Rustdoc: generate unique ID attributes for each page #30036

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
Dec 8, 2015

Conversation

mitaa
Copy link
Contributor

@mitaa mitaa commented Nov 24, 2015

This expands the code which generates unique IDs for Markdown headers within a single block to each rendered page.

fixes #25001
fixes #29449

@rust-highfive
Copy link
Contributor

r? @cmr

(rust_highfive has picked a reviewer for you, use r? to override)

@killercup
Copy link
Member

Uhm, I'm fairly certain that you can't use . (period) in HTML IDs since it's the CSS prefix for classes. (#method.foo.example in CSS means "thing with id 'method' and classes 'foo' and 'example').

Why not use - (regular dash) instead?

@apasel422
Copy link
Contributor

Does this also fix #25001?

@mitaa
Copy link
Contributor Author

mitaa commented Nov 24, 2015

@killercup
I don't have a problem with using - instead, though I'm not sure about . since it is already used in other existing ids (i.e. method.foo, assoc_type.Bar)

@apasel422
No, this only fixes doc-block headers, relying on a unique parent id.
Figuring out unique ids for trait impl items requires some more thought.

@eefriedman
Copy link
Contributor

Uhm, I'm fairly certain that you can't use . (period) in HTML IDs since it's the CSS prefix for classes. (#method.foo.example in CSS means "thing with id 'method' and classes 'foo' and 'example').

No, there isn't any such rule. Granted, it's slightly inconvenient to write #method\.foo\.example in CSS.

@killercup
Copy link
Member

The value must be unique amongst all the IDs in the element's home subtree and must contain at least one character. The value must not contain any space characters.

http://www.w3.org/TR/html5/dom.html#the-id-attribute

I stand corrected. It still looks weird to me, though 😄

@mitaa
Copy link
Contributor Author

mitaa commented Nov 27, 2015

cc @alexcrichton
(I see you didn't like this approach in #21967 very much, though I think this is a bit different)

@alexcrichton
Copy link
Member

Thanks for the PR @mitaa! This is looking pretty good to me, although I think it'd be better if the markdown document renderer supported just automatically handling this. It's a little unfortunate that you have to manually scope ids as it's easy to forget.

I thought there was already support for this in the backend, but basically there should be a TLS map which stores all used ids and then every time you try to use one that's already used a number is just prepended to the end. Between pages this set can be reset, but the idea is that then this is always handled without having to manually annotate scopings.

@alexcrichton alexcrichton assigned alexcrichton and unassigned emberian Dec 1, 2015
@mitaa
Copy link
Contributor Author

mitaa commented Dec 1, 2015

@alexcrichton
Yes, I've noticed the machinery which enumerates the header ids.
The TLS map is currently reset between rendering Markdown blocks.

I guess if this is (re)moved the issue would be solved, but I think this also has some disadvantages which I tried to solve with this approch:

  • link stability
    • id="example-13" will break if a header with the same name is removed or added before it on the page
  • intuitivity
    • method.foo.example can be inferred and typed into the url bar without looking at the html source, example-7 not so much

How important are these aspects?

@alexcrichton
Copy link
Member

I think it's ok to not have super-stable links, and I also think that the part about typing in makes more sense for major sections which are likely to have longer ids, consequently unique and easy-to-remember ones.

@mitaa
Copy link
Contributor Author

mitaa commented Dec 2, 2015

Ah, alright, then I'll rewrite this PR to make the existing mechanism page global, (re)using it for all other ids on the page too.

@mitaa mitaa force-pushed the doc_id branch 3 times, most recently from 52eb35f to ef4b4b7 Compare December 2, 2015 23:06
@mitaa mitaa changed the title Rustdoc: scope ids in doc-blocks to a parent-id Rustdoc: generate unique ID attributes for each page Dec 2, 2015
@mitaa
Copy link
Contributor Author

mitaa commented Dec 3, 2015

(updated)

Should this include a test?

edit: I think ID uniqueness should rather be enforced through htmldocck.py by default.

USED_ID_MAP.with(|s| *s.borrow_mut() = init_ids());
}

pub fn with_unique_id<T, F: FnOnce(&str) -> T>(candidate: String, f: F) -> T {
Copy link
Member

Choose a reason for hiding this comment

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

Can this return a unique id instead of taking a closure?

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, I felt ambivalent about this anyway. (The intention was to avoid the extra heap allocation)

Copy link
Member

Choose a reason for hiding this comment

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

Ah yeah don't worry too much about allocations, it happens a lot in rustdoc anyway :)

@alexcrichton
Copy link
Member

Looking good, thanks @mitaa! And yeah could you add a few small tests here and there in src/test/rustdoc?

@mitaa
Copy link
Contributor Author

mitaa commented Dec 5, 2015

(updated)

Does this look ok to you @alexcrichton?

@alexcrichton
Copy link
Member

@bors: r+ fb7008c

Thanks!

@bors
Copy link
Collaborator

bors commented Dec 8, 2015

⌛ Testing commit fb7008c with merge 461c460...

bors added a commit that referenced this pull request Dec 8, 2015
This expands the code which generates unique IDs for Markdown headers within a single block to each rendered page.

fixes #25001
fixes #29449
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
8 participants