@@ -42,6 +42,7 @@ use std::str;
42
42
43
43
use crate :: clean:: RenderedLink ;
44
44
use crate :: doctest;
45
+ use crate :: formats:: cache:: LocalResources ;
45
46
use crate :: html:: escape:: Escape ;
46
47
use crate :: html:: format:: Buffer ;
47
48
use crate :: html:: highlight;
@@ -101,6 +102,8 @@ pub struct Markdown<'a> {
101
102
/// Offset at which we render headings.
102
103
/// E.g. if `heading_offset: HeadingOffset::H2`, then `# something` renders an `<h2>`.
103
104
pub heading_offset : HeadingOffset ,
105
+ pub depth : usize ,
106
+ pub local_resources : Option < & ' a LocalResources > ,
104
107
}
105
108
/// A tuple struct like `Markdown` that renders the markdown with a table of contents.
106
109
pub ( crate ) struct MarkdownWithToc < ' a > {
@@ -109,13 +112,50 @@ pub(crate) struct MarkdownWithToc<'a> {
109
112
pub ( crate ) error_codes : ErrorCodes ,
110
113
pub ( crate ) edition : Edition ,
111
114
pub ( crate ) playground : & ' a Option < Playground > ,
115
+ pub ( crate ) depth : usize ,
116
+ pub ( crate ) local_resources : Option < & ' a LocalResources > ,
112
117
}
113
118
/// A tuple struct like `Markdown` that renders the markdown escaping HTML tags
114
119
/// and includes no paragraph tags.
115
120
pub ( crate ) struct MarkdownItemInfo < ' a > ( pub ( crate ) & ' a str , pub ( crate ) & ' a mut IdMap ) ;
116
121
/// A tuple struct like `Markdown` that renders only the first paragraph.
117
122
pub ( crate ) struct MarkdownSummaryLine < ' a > ( pub & ' a str , pub & ' a [ RenderedLink ] ) ;
118
123
124
+ struct LocalResourcesReplacer < ' b , I > {
125
+ inner : I ,
126
+ local_resources : Option < & ' b LocalResources > ,
127
+ depth : usize ,
128
+ }
129
+
130
+ impl < ' b , I > LocalResourcesReplacer < ' b , I > {
131
+ fn new ( iter : I , local_resources : Option < & ' b LocalResources > , depth : usize ) -> Self {
132
+ Self { inner : iter, local_resources, depth }
133
+ }
134
+ }
135
+
136
+ impl < ' a , ' b , I : Iterator < Item = Event < ' a > > > Iterator for LocalResourcesReplacer < ' b , I > {
137
+ type Item = Event < ' a > ;
138
+
139
+ fn next ( & mut self ) -> Option < Self :: Item > {
140
+ let event = self . inner . next ( ) ?;
141
+ // We only modify
142
+ if let Event :: Start ( Tag :: Image ( type_, ref path, ref title) ) = event &&
143
+ !path. starts_with ( "http://" ) &&
144
+ !path. starts_with ( "https://" ) &&
145
+ let Some ( local_resources) = & self . local_resources &&
146
+ let Some ( correspondance) = local_resources. get_at_depth ( self . depth , & * path)
147
+ {
148
+ Some ( Event :: Start ( Tag :: Image (
149
+ type_,
150
+ CowStr :: Boxed ( correspondance. clone ( ) . into_boxed_str ( ) ) ,
151
+ title. clone ( ) ,
152
+ ) ) )
153
+ } else {
154
+ Some ( event)
155
+ }
156
+ }
157
+ }
158
+
119
159
#[ derive( Copy , Clone , PartialEq , Debug ) ]
120
160
pub enum ErrorCodes {
121
161
Yes ,
@@ -1017,6 +1057,8 @@ impl Markdown<'_> {
1017
1057
edition,
1018
1058
playground,
1019
1059
heading_offset,
1060
+ depth,
1061
+ local_resources,
1020
1062
} = self ;
1021
1063
1022
1064
// This is actually common enough to special-case
@@ -1038,6 +1080,7 @@ impl Markdown<'_> {
1038
1080
let p = HeadingLinks :: new ( p, None , ids, heading_offset) ;
1039
1081
let p = Footnotes :: new ( p) ;
1040
1082
let p = LinkReplacer :: new ( p. map ( |( ev, _) | ev) , links) ;
1083
+ let p = LocalResourcesReplacer :: new ( p, local_resources, depth) ;
1041
1084
let p = TableWrapper :: new ( p) ;
1042
1085
let p = CodeBlocks :: new ( p, codes, edition, playground) ;
1043
1086
html:: push_html ( & mut s, p) ;
@@ -1048,7 +1091,15 @@ impl Markdown<'_> {
1048
1091
1049
1092
impl MarkdownWithToc < ' _ > {
1050
1093
pub ( crate ) fn into_string ( self ) -> String {
1051
- let MarkdownWithToc { content : md, ids, error_codes : codes, edition, playground } = self ;
1094
+ let MarkdownWithToc {
1095
+ content : md,
1096
+ ids,
1097
+ error_codes : codes,
1098
+ edition,
1099
+ playground,
1100
+ depth,
1101
+ local_resources,
1102
+ } = self ;
1052
1103
1053
1104
let p = Parser :: new_ext ( md, main_body_opts ( ) ) . into_offset_iter ( ) ;
1054
1105
@@ -1059,7 +1110,8 @@ impl MarkdownWithToc<'_> {
1059
1110
{
1060
1111
let p = HeadingLinks :: new ( p, Some ( & mut toc) , ids, HeadingOffset :: H1 ) ;
1061
1112
let p = Footnotes :: new ( p) ;
1062
- let p = TableWrapper :: new ( p. map ( |( ev, _) | ev) ) ;
1113
+ let p = LocalResourcesReplacer :: new ( p. map ( |( ev, _) | ev) , local_resources, depth) ;
1114
+ let p = TableWrapper :: new ( p) ;
1063
1115
let p = CodeBlocks :: new ( p, codes, edition, playground) ;
1064
1116
html:: push_html ( & mut s, p) ;
1065
1117
}
0 commit comments