Refactor RenderedLink into its own type
This commit is contained in:
parent
d5495e2155
commit
31a7b6e832
3 changed files with 27 additions and 19 deletions
|
@ -118,7 +118,7 @@ impl Item {
|
||||||
self.attrs.collapsed_doc_value()
|
self.attrs.collapsed_doc_value()
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn links(&self) -> Vec<(String, String)> {
|
pub fn links(&self) -> Vec<RenderedLink> {
|
||||||
self.attrs.links(&self.def_id.krate)
|
self.attrs.links(&self.def_id.krate)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -441,6 +441,13 @@ pub struct ItemLink {
|
||||||
pub(crate) fragment: Option<String>,
|
pub(crate) fragment: Option<String>,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub struct RenderedLink {
|
||||||
|
/// The text the link was original written as
|
||||||
|
pub(crate) original_text: String,
|
||||||
|
/// The URL to put in the `href`
|
||||||
|
pub(crate) href: String,
|
||||||
|
}
|
||||||
|
|
||||||
impl Attributes {
|
impl Attributes {
|
||||||
/// Extracts the content from an attribute `#[doc(cfg(content))]`.
|
/// Extracts the content from an attribute `#[doc(cfg(content))]`.
|
||||||
pub fn extract_cfg(mi: &ast::MetaItem) -> Option<&ast::MetaItem> {
|
pub fn extract_cfg(mi: &ast::MetaItem) -> Option<&ast::MetaItem> {
|
||||||
|
@ -617,7 +624,7 @@ impl Attributes {
|
||||||
/// Gets links as a vector
|
/// Gets links as a vector
|
||||||
///
|
///
|
||||||
/// Cache must be populated before call
|
/// Cache must be populated before call
|
||||||
pub fn links(&self, krate: &CrateNum) -> Vec<(String, String)> {
|
pub fn links(&self, krate: &CrateNum) -> Vec<RenderedLink> {
|
||||||
use crate::html::format::href;
|
use crate::html::format::href;
|
||||||
use crate::html::render::CURRENT_DEPTH;
|
use crate::html::render::CURRENT_DEPTH;
|
||||||
|
|
||||||
|
@ -631,7 +638,7 @@ impl Attributes {
|
||||||
href.push_str("#");
|
href.push_str("#");
|
||||||
href.push_str(fragment);
|
href.push_str(fragment);
|
||||||
}
|
}
|
||||||
Some((s.clone(), href))
|
Some(RenderedLink { original_text: s.clone(), href })
|
||||||
} else {
|
} else {
|
||||||
None
|
None
|
||||||
}
|
}
|
||||||
|
@ -651,16 +658,16 @@ impl Attributes {
|
||||||
};
|
};
|
||||||
// This is a primitive so the url is done "by hand".
|
// This is a primitive so the url is done "by hand".
|
||||||
let tail = fragment.find('#').unwrap_or_else(|| fragment.len());
|
let tail = fragment.find('#').unwrap_or_else(|| fragment.len());
|
||||||
Some((
|
Some(RenderedLink {
|
||||||
s.clone(),
|
original_text: s.clone(),
|
||||||
format!(
|
href: format!(
|
||||||
"{}{}std/primitive.{}.html{}",
|
"{}{}std/primitive.{}.html{}",
|
||||||
url,
|
url,
|
||||||
if !url.ends_with('/') { "/" } else { "" },
|
if !url.ends_with('/') { "/" } else { "" },
|
||||||
&fragment[..tail],
|
&fragment[..tail],
|
||||||
&fragment[tail..]
|
&fragment[tail..]
|
||||||
),
|
),
|
||||||
))
|
})
|
||||||
} else {
|
} else {
|
||||||
panic!("This isn't a primitive?!");
|
panic!("This isn't a primitive?!");
|
||||||
}
|
}
|
||||||
|
|
|
@ -34,6 +34,7 @@ use std::fmt::Write;
|
||||||
use std::ops::Range;
|
use std::ops::Range;
|
||||||
use std::str;
|
use std::str;
|
||||||
|
|
||||||
|
use crate::clean::RenderedLink;
|
||||||
use crate::doctest;
|
use crate::doctest;
|
||||||
use crate::html::highlight;
|
use crate::html::highlight;
|
||||||
use crate::html::toc::TocBuilder;
|
use crate::html::toc::TocBuilder;
|
||||||
|
@ -52,7 +53,7 @@ fn opts() -> Options {
|
||||||
pub struct Markdown<'a>(
|
pub struct Markdown<'a>(
|
||||||
pub &'a str,
|
pub &'a str,
|
||||||
/// A list of link replacements.
|
/// A list of link replacements.
|
||||||
pub &'a [(String, String)],
|
pub &'a [RenderedLink],
|
||||||
/// The current list of used header IDs.
|
/// The current list of used header IDs.
|
||||||
pub &'a mut IdMap,
|
pub &'a mut IdMap,
|
||||||
/// Whether to allow the use of explicit error codes in doctest lang strings.
|
/// Whether to allow the use of explicit error codes in doctest lang strings.
|
||||||
|
@ -78,7 +79,7 @@ pub struct MarkdownHtml<'a>(
|
||||||
pub &'a Option<Playground>,
|
pub &'a Option<Playground>,
|
||||||
);
|
);
|
||||||
/// A tuple struct like `Markdown` that renders only the first paragraph.
|
/// A tuple struct like `Markdown` that renders only the first paragraph.
|
||||||
pub struct MarkdownSummaryLine<'a>(pub &'a str, pub &'a [(String, String)]);
|
pub struct MarkdownSummaryLine<'a>(pub &'a str, pub &'a [RenderedLink]);
|
||||||
|
|
||||||
#[derive(Copy, Clone, PartialEq, Debug)]
|
#[derive(Copy, Clone, PartialEq, Debug)]
|
||||||
pub enum ErrorCodes {
|
pub enum ErrorCodes {
|
||||||
|
@ -339,11 +340,11 @@ impl<'a, I: Iterator<Item = Event<'a>>> Iterator for CodeBlocks<'_, 'a, I> {
|
||||||
/// Make headings links with anchor IDs and build up TOC.
|
/// Make headings links with anchor IDs and build up TOC.
|
||||||
struct LinkReplacer<'a, 'b, I: Iterator<Item = Event<'a>>> {
|
struct LinkReplacer<'a, 'b, I: Iterator<Item = Event<'a>>> {
|
||||||
inner: I,
|
inner: I,
|
||||||
links: &'b [(String, String)],
|
links: &'b [RenderedLink],
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'a, 'b, I: Iterator<Item = Event<'a>>> LinkReplacer<'a, 'b, I> {
|
impl<'a, 'b, I: Iterator<Item = Event<'a>>> LinkReplacer<'a, 'b, I> {
|
||||||
fn new(iter: I, links: &'b [(String, String)]) -> Self {
|
fn new(iter: I, links: &'b [RenderedLink]) -> Self {
|
||||||
LinkReplacer { inner: iter, links }
|
LinkReplacer { inner: iter, links }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -354,8 +355,8 @@ impl<'a, 'b, I: Iterator<Item = Event<'a>>> Iterator for LinkReplacer<'a, 'b, I>
|
||||||
fn next(&mut self) -> Option<Self::Item> {
|
fn next(&mut self) -> Option<Self::Item> {
|
||||||
let event = self.inner.next();
|
let event = self.inner.next();
|
||||||
if let Some(Event::Start(Tag::Link(kind, dest, text))) = event {
|
if let Some(Event::Start(Tag::Link(kind, dest, text))) = event {
|
||||||
if let Some(&(_, ref replace)) = self.links.iter().find(|link| link.0 == *dest) {
|
if let Some(link) = self.links.iter().find(|link| link.original_text == *dest) {
|
||||||
Some(Event::Start(Tag::Link(kind, replace.to_owned().into(), text)))
|
Some(Event::Start(Tag::Link(kind, link.href.clone().into(), text)))
|
||||||
} else {
|
} else {
|
||||||
Some(Event::Start(Tag::Link(kind, dest, text)))
|
Some(Event::Start(Tag::Link(kind, dest, text)))
|
||||||
}
|
}
|
||||||
|
@ -855,8 +856,8 @@ impl Markdown<'_> {
|
||||||
return String::new();
|
return String::new();
|
||||||
}
|
}
|
||||||
let replacer = |_: &str, s: &str| {
|
let replacer = |_: &str, s: &str| {
|
||||||
if let Some(&(_, ref replace)) = links.iter().find(|link| &*link.0 == s) {
|
if let Some(link) = links.iter().find(|link| &*link.original_text == s) {
|
||||||
Some((replace.clone(), s.to_owned()))
|
Some((link.original_text.clone(), link.href.clone()))
|
||||||
} else {
|
} else {
|
||||||
None
|
None
|
||||||
}
|
}
|
||||||
|
@ -933,8 +934,8 @@ impl MarkdownSummaryLine<'_> {
|
||||||
}
|
}
|
||||||
|
|
||||||
let replacer = |_: &str, s: &str| {
|
let replacer = |_: &str, s: &str| {
|
||||||
if let Some(&(_, ref replace)) = links.iter().find(|link| &*link.0 == s) {
|
if let Some(rendered_link) = links.iter().find(|link| &*link.original_text == s) {
|
||||||
Some((replace.clone(), s.to_owned()))
|
Some((rendered_link.original_text.clone(), rendered_link.href.clone()))
|
||||||
} else {
|
} else {
|
||||||
None
|
None
|
||||||
}
|
}
|
||||||
|
|
|
@ -63,7 +63,7 @@ use rustc_span::symbol::{sym, Symbol};
|
||||||
use serde::ser::SerializeSeq;
|
use serde::ser::SerializeSeq;
|
||||||
use serde::{Serialize, Serializer};
|
use serde::{Serialize, Serializer};
|
||||||
|
|
||||||
use crate::clean::{self, AttributesExt, Deprecation, GetDefId, SelfTy, TypeKind};
|
use crate::clean::{self, AttributesExt, Deprecation, GetDefId, RenderedLink, SelfTy, TypeKind};
|
||||||
use crate::config::RenderInfo;
|
use crate::config::RenderInfo;
|
||||||
use crate::config::RenderOptions;
|
use crate::config::RenderOptions;
|
||||||
use crate::docfs::{DocFS, PathError};
|
use crate::docfs::{DocFS, PathError};
|
||||||
|
@ -1780,7 +1780,7 @@ fn render_markdown(
|
||||||
w: &mut Buffer,
|
w: &mut Buffer,
|
||||||
cx: &Context,
|
cx: &Context,
|
||||||
md_text: &str,
|
md_text: &str,
|
||||||
links: Vec<(String, String)>,
|
links: Vec<RenderedLink>,
|
||||||
prefix: &str,
|
prefix: &str,
|
||||||
is_hidden: bool,
|
is_hidden: bool,
|
||||||
) {
|
) {
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue