Refactor RenderedLink into its own type
This commit is contained in:
parent
d5495e2155
commit
31a7b6e832
@ -118,7 +118,7 @@ pub fn collapsed_doc_value(&self) -> Option<String> {
|
||||
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)
|
||||
}
|
||||
|
||||
@ -441,6 +441,13 @@ pub struct ItemLink {
|
||||
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 {
|
||||
/// Extracts the content from an attribute `#[doc(cfg(content))]`.
|
||||
pub fn extract_cfg(mi: &ast::MetaItem) -> Option<&ast::MetaItem> {
|
||||
@ -617,7 +624,7 @@ pub fn collapsed_doc_value(&self) -> Option<String> {
|
||||
/// Gets links as a vector
|
||||
///
|
||||
/// 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::render::CURRENT_DEPTH;
|
||||
|
||||
@ -631,7 +638,7 @@ pub fn links(&self, krate: &CrateNum) -> Vec<(String, String)> {
|
||||
href.push_str("#");
|
||||
href.push_str(fragment);
|
||||
}
|
||||
Some((s.clone(), href))
|
||||
Some(RenderedLink { original_text: s.clone(), href })
|
||||
} else {
|
||||
None
|
||||
}
|
||||
@ -651,16 +658,16 @@ pub fn links(&self, krate: &CrateNum) -> Vec<(String, String)> {
|
||||
};
|
||||
// This is a primitive so the url is done "by hand".
|
||||
let tail = fragment.find('#').unwrap_or_else(|| fragment.len());
|
||||
Some((
|
||||
s.clone(),
|
||||
format!(
|
||||
Some(RenderedLink {
|
||||
original_text: s.clone(),
|
||||
href: format!(
|
||||
"{}{}std/primitive.{}.html{}",
|
||||
url,
|
||||
if !url.ends_with('/') { "/" } else { "" },
|
||||
&fragment[..tail],
|
||||
&fragment[tail..]
|
||||
),
|
||||
))
|
||||
})
|
||||
} else {
|
||||
panic!("This isn't a primitive?!");
|
||||
}
|
||||
|
@ -34,6 +34,7 @@
|
||||
use std::ops::Range;
|
||||
use std::str;
|
||||
|
||||
use crate::clean::RenderedLink;
|
||||
use crate::doctest;
|
||||
use crate::html::highlight;
|
||||
use crate::html::toc::TocBuilder;
|
||||
@ -52,7 +53,7 @@ fn opts() -> Options {
|
||||
pub struct Markdown<'a>(
|
||||
pub &'a str,
|
||||
/// A list of link replacements.
|
||||
pub &'a [(String, String)],
|
||||
pub &'a [RenderedLink],
|
||||
/// The current list of used header IDs.
|
||||
pub &'a mut IdMap,
|
||||
/// 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>,
|
||||
);
|
||||
/// 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)]
|
||||
pub enum ErrorCodes {
|
||||
@ -339,11 +340,11 @@ fn dont_escape(c: u8) -> bool {
|
||||
/// Make headings links with anchor IDs and build up TOC.
|
||||
struct LinkReplacer<'a, 'b, I: Iterator<Item = Event<'a>>> {
|
||||
inner: I,
|
||||
links: &'b [(String, String)],
|
||||
links: &'b [RenderedLink],
|
||||
}
|
||||
|
||||
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 }
|
||||
}
|
||||
}
|
||||
@ -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> {
|
||||
let event = self.inner.next();
|
||||
if let Some(Event::Start(Tag::Link(kind, dest, text))) = event {
|
||||
if let Some(&(_, ref replace)) = self.links.iter().find(|link| link.0 == *dest) {
|
||||
Some(Event::Start(Tag::Link(kind, replace.to_owned().into(), text)))
|
||||
if let Some(link) = self.links.iter().find(|link| link.original_text == *dest) {
|
||||
Some(Event::Start(Tag::Link(kind, link.href.clone().into(), text)))
|
||||
} else {
|
||||
Some(Event::Start(Tag::Link(kind, dest, text)))
|
||||
}
|
||||
@ -855,8 +856,8 @@ pub fn into_string(self) -> String {
|
||||
return String::new();
|
||||
}
|
||||
let replacer = |_: &str, s: &str| {
|
||||
if let Some(&(_, ref replace)) = links.iter().find(|link| &*link.0 == s) {
|
||||
Some((replace.clone(), s.to_owned()))
|
||||
if let Some(link) = links.iter().find(|link| &*link.original_text == s) {
|
||||
Some((link.original_text.clone(), link.href.clone()))
|
||||
} else {
|
||||
None
|
||||
}
|
||||
@ -933,8 +934,8 @@ pub fn into_string(self) -> String {
|
||||
}
|
||||
|
||||
let replacer = |_: &str, s: &str| {
|
||||
if let Some(&(_, ref replace)) = links.iter().find(|link| &*link.0 == s) {
|
||||
Some((replace.clone(), s.to_owned()))
|
||||
if let Some(rendered_link) = links.iter().find(|link| &*link.original_text == s) {
|
||||
Some((rendered_link.original_text.clone(), rendered_link.href.clone()))
|
||||
} else {
|
||||
None
|
||||
}
|
||||
|
@ -63,7 +63,7 @@
|
||||
use serde::ser::SerializeSeq;
|
||||
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::RenderOptions;
|
||||
use crate::docfs::{DocFS, PathError};
|
||||
@ -1780,7 +1780,7 @@ fn render_markdown(
|
||||
w: &mut Buffer,
|
||||
cx: &Context,
|
||||
md_text: &str,
|
||||
links: Vec<(String, String)>,
|
||||
links: Vec<RenderedLink>,
|
||||
prefix: &str,
|
||||
is_hidden: bool,
|
||||
) {
|
||||
|
Loading…
Reference in New Issue
Block a user