about summary refs log tree commit diff
path: root/src/librustdoc/markdown.rs
AgeCommit message (Collapse)AuthorLines
2025-06-29Lazy-ify some markdown renderingYotam Ofek-30/+26
2024-10-19rustdoc: Document `markdown` module.Alona Enraght-Moony-1/+11
Rustdoc markdown handling is currently split between: - html::markdown, which contains all the meaty login - markdown, which is only used for when rustdoc renders a standalone markdown file Adds module-level doc-comment to markdown, and rename the function so it's clear that it's doing IO (instead of just rendering to a string).
2024-09-22Reformat using the new identifier sorting from rustfmtMichael Goulet-1/+1
2024-08-20Add Top TOC support to rustdocMichael Howell-0/+1
This commit adds the headers for the top level documentation to rustdoc's existing table of contents, along with associated items. It only show two levels of headers. Going further would require the sidebar to be wider, and that seems unnecessary (the crates that have manually-built TOCs usually don't need deeply nested headers).
2024-06-07Move Markdown-specific doctest code into submoduleNoah Lev-46/+2
2024-06-01Auto merge of #124577 - ↵bors-12/+1
GuillaumeGomez:stabilize-custom_code_classes_in_docs, r=rustdoc Stabilize `custom_code_classes_in_docs` feature Fixes #79483. This feature has been around for quite some time now, I think it's fine to stabilize it now. ## Summary ## What is the feature about? In short, this PR changes two things, both related to codeblocks in doc comments in Rust documentation: * Allow to disable generation of `language-*` CSS classes with the `custom` attribute. * Add your own CSS classes to a code block so that you can use other tools to highlight them. #### The `custom` attribute Let's start with the new `custom` attribute: it will disable the generation of the `language-*` CSS class on the generated HTML code block. For example: ```rust /// ```custom,c /// int main(void) { /// return 0; /// } /// ``` ``` The generated HTML code block will not have `class="language-c"` because the `custom` attribute has been set. The `custom` attribute becomes especially useful with the other thing added by this feature: adding your own CSS classes. #### Adding your own CSS classes The second part of this feature is to allow users to add CSS classes themselves so that they can then add a JS library which will do it (like `highlight.js` or `prism.js`), allowing to support highlighting for other languages than Rust without increasing burden on rustdoc. To disable the automatic `language-*` CSS class generation, you need to use the `custom` attribute as well. This allow users to write the following: ```rust /// Some code block with `{class=language-c}` as the language string. /// /// ```custom,{class=language-c} /// int main(void) { /// return 0; /// } /// ``` fn main() {} ``` This will notably produce the following HTML: ```html <pre class="language-c"> int main(void) { return 0; }</pre> ``` Instead of: ```html <pre class="rust rust-example-rendered"> <span class="ident">int</span> <span class="ident">main</span>(<span class="ident">void</span>) { <span class="kw">return</span> <span class="number">0</span>; } </pre> ``` To be noted, we could have written `{.language-c}` to achieve the same result. `.` and `class=` have the same effect. One last syntax point: content between parens (`(like this)`) is now considered as comment and is not taken into account at all. In addition to this, I added an `unknown` field into `LangString` (the parsed code block "attribute") because of cases like this: ```rust /// ```custom,class:language-c /// main; /// ``` pub fn foo() {} ``` Without this `unknown` field, it would generate in the DOM: `<pre class="language-class:language-c language-c">`, which is quite bad. So instead, it now stores all unknown tags into the `unknown` field and use the first one as "language". So in this case, since there is no unknown tag, it'll simply generate `<pre class="language-c">`. I added tests to cover this. EDIT(camelid): This description is out-of-date. Using `custom,class:language-c` will generate the output `<pre class="language-class:language-c">` as would be expected; it treats `class:language-c` as just the name of a language (similar to the langstring `c` or `js` or what have you) since it does not use the designed class syntax. Finally, I added a parser for the codeblock attributes to make it much easier to maintain. It'll be pretty easy to extend. As to why this syntax for adding attributes was picked: it's [Pandoc's syntax](https://pandoc.org/MANUAL.html#extension-fenced_code_attributes). Even if it seems clunkier in some cases, it's extensible, and most third-party Markdown renderers are smart enough to ignore Pandoc's brace-delimited attributes (from [this comment](https://github.com/rust-lang/rust/pull/110800#issuecomment-1522044456)). r? `@notriddle`
2024-05-04Add `-` (stdin) support to rustdocUrgau-4/+10
2024-05-01Stabilize `custom_code_classes_in_docs` featureGuillaume Gomez-12/+1
2024-03-23`rustdoc --test`: Prevent reaching the maximum size of command-line by using ↵Guillaume Gomez-1/+10
files for arguments if there are too many
2023-11-02Minimize `pub` usage in `source_map.rs`.Nicholas Nethercote-1/+1
Most notably, this commit changes the `pub use crate::*;` in that file to `use crate::*;`. This requires a lot of `use` items in other crates to be adjusted, because everything defined within `rustc_span::*` was also available via `rustc_span::source_map::*`, which is bizarre. The commit also removes `SourceMap::span_to_relative_line_string`, which is unused.
2023-09-17Update src/librustdoc/markdown.rsManish Goregaokar-1/+1
Co-authored-by: Michael Howell <michael@notriddle.com>
2023-09-17Don't emit an error if the `custom_code_classes_in_docs` feature is disabled ↵Guillaume Gomez-1/+13
when its syntax is used.
2023-08-16Use more named format argsGuillaume Gomez-5/+7
2023-08-16Improve code readability by moving fmt args directly into the stringGuillaume Gomez-5/+5
2023-02-06Turn MarkdownWithToc into a struct with named fieldsGuillaume Gomez-1/+8
2022-12-12rustdoc: remove `type="text/css" from stylesheet linksMichael Howell-1/+1
MDN directly recommends this in <https://developer.mozilla.org/en-US/docs/Web/HTML/Element/link>, since "CSS is the only stylesheet language used on the web." Like 07a243b2a46384235d7e2c08688978b7cf018973, but a few places that were missed the first time.
2022-10-19Avoid cloning `RenderOptions`.Nicholas Nethercote-1/+1
By moving `RenderOptions` out of `Option`, because the two structs' uses are almost entirely separate. The only complication is that `unstable_features` is needed in both structs, but it's a tiny `Copy` type so its duplication seems fine.
2022-10-19Clean up rustdoc startup.Nicholas Nethercote-2/+3
rustc's startup has several layers, including: - `interface::run_compiler` passes a closure, `f`, to `run_in_thread_pool_with_globals`, which creates a thread pool, sets up session globals, and passes `f` to `create_compiler_and_run`. - `create_compiler_and_run` creates a `Session`, a `Compiler`, sets the source map, and calls `f`. rustdoc is a bit different. - `main_args` calls `main_options` via `run_in_thread_pool_with_globals`, which (again) creates a thread pool (hardcoded to a single thread!) and sets up session globals. - `main_options` has four different paths. - The second one calls `interface::run_compiler`, which redoes the `run_in_thread_pool_with_globals`! This is bad. - The fourth one calls `interface::create_compiler_and_run`, which is reasonable. - The first and third ones don't do anything of note involving the above functions, except for some symbol interning which requires session globals. In other words, rustdoc calls into `rustc_interface` at three different levels. It's a bit confused, and feels like code where functionality has been added by different people at different times without fully understanding how the globally accessible stuff is set up. This commit tidies things up. It removes the `run_in_thread_pool_with_globals` call in `main_args`, and adjust the four paths in `main_options` as follows. - `markdown::test` calls `test::test_main`, which provides its own parallelism and so doesn't need a thread pool. It had one small use of symbol interning, which required session globals, but the commit removes this. - `doctest::run` already calls `interface::run_compiler`, so it doesn't need further adjustment. - `markdown::render` is simple but needs session globals for interning (which can't easily be removed), so it's now wrapped in `create_session_globals_then`. - The fourth path now uses `interface::run_compiler`, which is equivalent to the old `run_in_thread_pool_with_globals` + `create_compiler_and_run` pairing.
2022-05-21Remove `crate` visibility modifier in libs, testsJacob Pratt-2/+2
2022-04-12Remove a `format` invocationRoc Yu-2/+3
2022-03-10Use implicit capture syntax in format_argsT-O-R-U-S-1/+1
This updates the standard library's documentation to use the new syntax. The documentation is worthwhile to update as it should be more idiomatic (particularly for features like this, which are nice for users to get acquainted with). The general codebase is likely more hassle than benefit to update: it'll hurt git blame, and generally updates can be done by folks updating the code if (and when) that makes things more readable with the new format. A few places in the compiler and library code are updated (mostly just due to already having been done when this commit was first authored).
2021-12-12Rename `TestOptions` to `GlobalTestOptions`Noah Lev-2/+2
It seems to apply to all doctests in the crate.
2021-11-26Remove `--display-doctest-warnings`Joshua Nelson-7/+1
This can be replicated in full with other existing features, there's no need to have a separate option for it. This also fixes a bug where `--test-args=--show-output` had no effect, and updates the documentation.
2021-10-04heading_level: u32 -> heading_offset: HeadingOffsetMukund Lakshman-2/+4
2021-10-04Change `Markdown(...)` to `Markdown { ... }`Mukund Lakshman-1/+10
2021-10-04librustdoc: Use correct heading levels.Mukund Lakshman-1/+1
- Avoid multiple <h1>s on a page. - The <h#> tags should follow a semantic hierarchy. - Cap at h6 (no h7)
2021-09-14Rename --display-warnings to --display-doctest-warningsGuillaume Gomez-2/+2
2021-07-22Move calls to test_main into one functionGuillaume Gomez-8/+5
2021-07-18Add --nocapture option to rustdocGuillaume Gomez-0/+3
2021-07-13Remove renaming of `test` crateJoshua Nelson-2/+2
This is leftover from when `doctest` used to be called `test`. Remove it now, it's unnecessary and makes the code harder to read.
2021-06-21Don't reallocate the crate name when running doctestsJoshua Nelson-1/+2
2020-11-15Make all rustdoc functions and structs crate-privateJoshua Nelson-2/+2
This gives warnings about dead code.
2020-11-07Allow making `RUSTC_BOOTSTRAP` conditional on the crate nameJoshua Nelson-3/+2
The main change is that `UnstableOptions::from_environment` now requires an (optional) crate name. If the crate name is unknown (`None`), then the new feature is not available and you still have to use `RUSTC_BOOTSTRAP=1`. In practice this means the feature is only available for `--crate-name`, not for `#![crate_name]`; I'm interested in supporting the second but I'm not sure how. Other major changes: - Added `Session::is_nightly_build()`, which uses the `crate_name` of the session - Added `nightly_options::match_is_nightly_build`, a convenience method for looking up `--crate-name` from CLI arguments. `Session::is_nightly_build()`should be preferred where possible, since it will take into account `#![crate_name]` (I think). - Added `unstable_features` to `rustdoc::RenderOptions` There is a user-facing change here: things like `RUSTC_BOOTSTRAP=0` no longer active nightly features. In practice this shouldn't be a big deal, since `RUSTC_BOOTSTRAP` is the opposite of stable and everyone uses `RUSTC_BOOTSTRAP=1` anyway. - Add tests Check against `Cheat`, not whether nightly features are allowed. Nightly features are always allowed on the nightly channel. - Only call `is_nightly_build()` once within a function - Use booleans consistently for rustc_incremental Sessions can't be passed through threads, so `read_file` couldn't take a session. To be consistent, also take a boolean in `write_file_header`.
2020-08-27Rename rustdoc/test -> rustdoc/doctestAleksey Kladov-1/+1
This modules contains the implementation of doctests, and not the tests of rustdoc itself. This name is confusing, so let's rename it to doctest for clarity.
2020-07-15rustdoc: Rename internal API fns to `into_string`Lzu Tao-2/+2
to avoid surprising listed in API guidelines.
2020-05-09End unification of exit codes in librustdocGuillaume Gomez-9/+4
2020-05-08Clean up rustdoc source codeGuillaume Gomez-27/+12
2020-04-23Create new rustdoc lint to check for code blocks tagsGuillaume Gomez-1/+1
2020-03-26Create output dir in rustdoc markdown renderTimothée Gerber-1/+6
2020-02-27use char instead of &str for single char patternsMatthias Krüger-1/+1
2020-02-04remove redundant imports (clippy::single_component_path_imports)Matthias Krüger-1/+0
2020-01-10nix syntax::errors & prefer rustc_errors over errorsMazdak Farrokhzad-3/+2
2020-01-02Normalize `syntax::edition` imports.Mazdak Farrokhzad-1/+1
2020-01-02Normalize `syntax::source_map` imports.Mazdak Farrokhzad-1/+1
2019-12-22Format the worldMark Rousskov-17/+22
2019-11-30move UnstableFeatures -> rustc_featureMazdak Farrokhzad-1/+1
2019-09-11Rollup merge of #64072 - limira:patch-1, r=ollie27Mazdak Farrokhzad-1/+1
Replace file_stem by file_name in rustdoc markdown Before this PR, a file name like `some.file.md` will be output to a file named `some.html` with is not correct because the expected output file must be `some.file.html`
2019-09-04address rebase changesDario Gonzalez-1/+2
2019-09-03added feature gate enable-per-target-ignoresDario Gonzalez-1/+1
updated and augmented tests in html/markdown.rs
2019-09-02Replace file_stem by file_name in rustdoc markdownlimira-1/+1