forge_doc/preprocessor/
infer_hyperlinks.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
use super::{Preprocessor, PreprocessorId};
use crate::{Comments, Document, ParseItem, ParseSource};
use forge_fmt::solang_ext::SafeUnwrap;
use regex::{Captures, Match, Regex};
use std::{
    borrow::Cow,
    path::{Path, PathBuf},
    sync::LazyLock,
};

/// A regex that matches `{identifier-part}` placeholders
///
/// Overloaded functions are referenced by including the exact function arguments in the `part`
/// section of the placeholder.
static RE_INLINE_LINK: LazyLock<Regex> = LazyLock::new(|| {
    Regex::new(r"(?m)(\{(?P<xref>xref-)?(?P<identifier>[a-zA-Z_][0-9a-zA-Z_]*)(-(?P<part>[a-zA-Z_][0-9a-zA-Z_-]*))?}(\[(?P<link>(.*?))\])?)").unwrap()
});

/// [InferInlineHyperlinks] preprocessor id.
pub const INFER_INLINE_HYPERLINKS_ID: PreprocessorId = PreprocessorId("infer inline hyperlinks");

/// The infer hyperlinks preprocessor tries to map @dev tags to referenced items
/// Traverses the documents and attempts to find referenced items
/// comments for dev comment tags.
///
/// This preprocessor replaces inline links in comments with the links to the referenced items.
#[derive(Debug, Default)]
#[non_exhaustive]
pub struct InferInlineHyperlinks;

impl Preprocessor for InferInlineHyperlinks {
    fn id(&self) -> PreprocessorId {
        INFER_INLINE_HYPERLINKS_ID
    }

    fn preprocess(&self, mut documents: Vec<Document>) -> Result<Vec<Document>, eyre::Error> {
        // traverse all comments and try to match inline links and replace with inline links for
        // markdown
        let mut docs = Vec::with_capacity(documents.len());
        while !documents.is_empty() {
            let mut document = documents.remove(0);
            let target_path = document.relative_output_path().to_path_buf();
            for idx in 0..document.content.len() {
                let (mut comments, item_children_len) = {
                    let item = document.content.get_mut(idx).unwrap();
                    let comments = std::mem::take(&mut item.comments);
                    let children = item.children.len();
                    (comments, children)
                };
                Self::inline_doc_links(&documents, &target_path, &mut comments, &document);
                document.content.get_mut(idx).unwrap().comments = comments;

                // we also need to iterate over all child items
                // This is a bit horrible but we need to traverse all items in all documents
                for child_idx in 0..item_children_len {
                    let mut comments = {
                        let item = document.content.get_mut(idx).unwrap();

                        std::mem::take(&mut item.children[child_idx].comments)
                    };
                    Self::inline_doc_links(&documents, &target_path, &mut comments, &document);
                    document.content.get_mut(idx).unwrap().children[child_idx].comments = comments;
                }
            }

            docs.push(document);
        }

        Ok(docs)
    }
}

impl InferInlineHyperlinks {
    /// Finds the first match for the given link.
    ///
    /// All items get their own section in the markdown file.
    /// This section uses the identifier of the item: `#functionname`
    ///
    /// Note: the target path is the relative path to the markdown file.
    fn find_match<'a>(
        link: &InlineLink<'a>,
        target_path: &Path,
        items: impl Iterator<Item = &'a ParseItem>,
    ) -> Option<InlineLinkTarget<'a>> {
        for item in items {
            match &item.source {
                ParseSource::Contract(contract) => {
                    let name = &contract.name.safe_unwrap().name;
                    if name == link.identifier {
                        if link.part.is_none() {
                            return Some(InlineLinkTarget::borrowed(name, target_path.to_path_buf()))
                        }
                        // try to find the referenced item in the contract's children
                        return Self::find_match(link, target_path, item.children.iter())
                    }
                }
                ParseSource::Function(fun) => {
                    // TODO: handle overloaded functions
                    // functions can be overloaded so we need to keep track of how many matches we
                    // have so we can match the correct one
                    if let Some(id) = &fun.name {
                        // Note: constructors don't have a name
                        if id.name == link.ref_name() {
                            return Some(InlineLinkTarget::borrowed(
                                &id.name,
                                target_path.to_path_buf(),
                            ))
                        }
                    } else if link.ref_name() == "constructor" {
                        return Some(InlineLinkTarget::borrowed(
                            "constructor",
                            target_path.to_path_buf(),
                        ))
                    }
                }
                ParseSource::Variable(_) => {}
                ParseSource::Event(ev) => {
                    let ev_name = &ev.name.safe_unwrap().name;
                    if ev_name == link.ref_name() {
                        return Some(InlineLinkTarget::borrowed(ev_name, target_path.to_path_buf()))
                    }
                }
                ParseSource::Error(err) => {
                    let err_name = &err.name.safe_unwrap().name;
                    if err_name == link.ref_name() {
                        return Some(InlineLinkTarget::borrowed(err_name, target_path.to_path_buf()))
                    }
                }
                ParseSource::Struct(structdef) => {
                    let struct_name = &structdef.name.safe_unwrap().name;
                    if struct_name == link.ref_name() {
                        return Some(InlineLinkTarget::borrowed(
                            struct_name,
                            target_path.to_path_buf(),
                        ))
                    }
                }
                ParseSource::Enum(_) => {}
                ParseSource::Type(_) => {}
            }
        }

        None
    }

    /// Attempts to convert inline links to markdown links.
    fn inline_doc_links(
        documents: &[Document],
        target_path: &Path,
        comments: &mut Comments,
        parent: &Document,
    ) {
        // loop over all comments in the item
        for comment in comments.iter_mut() {
            let val = comment.value.clone();
            // replace all links with inline markdown links
            for link in InlineLink::captures(val.as_str()) {
                let target = if link.is_external() {
                    // find in all documents
                    documents.iter().find_map(|doc| {
                        Self::find_match(
                            &link,
                            doc.relative_output_path(),
                            doc.content.iter_items().flat_map(|item| {
                                Some(item).into_iter().chain(item.children.iter())
                            }),
                        )
                    })
                } else {
                    // find matches in the document
                    Self::find_match(
                        &link,
                        target_path,
                        parent
                            .content
                            .iter_items()
                            .flat_map(|item| Some(item).into_iter().chain(item.children.iter())),
                    )
                };
                if let Some(target) = target {
                    let display_value = link.markdown_link_display_value();
                    let markdown_link = format!("[{display_value}]({target})");
                    // replace the link with the markdown link
                    comment.value =
                        comment.value.as_str().replacen(link.as_str(), markdown_link.as_str(), 1);
                }
            }
        }
    }
}

struct InlineLinkTarget<'a> {
    section: Cow<'a, str>,
    target_path: PathBuf,
}

impl<'a> InlineLinkTarget<'a> {
    fn borrowed(section: &'a str, target_path: PathBuf) -> Self {
        Self { section: Cow::Borrowed(section), target_path }
    }
}

impl std::fmt::Display for InlineLinkTarget<'_> {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        // NOTE: the url should be absolute for markdown and section names are lowercase
        write!(f, "/{}#{}", self.target_path.display(), self.section.to_lowercase())
    }
}

/// A parsed link to an item.
#[derive(Debug)]
struct InlineLink<'a> {
    outer: Match<'a>,
    identifier: &'a str,
    part: Option<&'a str>,
    link: Option<&'a str>,
}

impl<'a> InlineLink<'a> {
    fn from_capture(cap: Captures<'a>) -> Option<Self> {
        Some(Self {
            outer: cap.get(1)?,
            identifier: cap.name("identifier")?.as_str(),
            part: cap.name("part").map(|m| m.as_str()),
            link: cap.name("link").map(|m| m.as_str()),
        })
    }

    fn captures(s: &'a str) -> impl Iterator<Item = Self> + 'a {
        RE_INLINE_LINK.captures(s).map(Self::from_capture).into_iter().flatten()
    }

    /// Parses the first inline link.
    #[allow(unused)]
    fn capture(s: &'a str) -> Option<Self> {
        let cap = RE_INLINE_LINK.captures(s)?;
        Self::from_capture(cap)
    }

    /// Returns the name of the link
    fn markdown_link_display_value(&self) -> Cow<'_, str> {
        if let Some(link) = self.link {
            Cow::Borrowed(link)
        } else if let Some(part) = self.part {
            Cow::Owned(format!("{}-{}", self.identifier, part))
        } else {
            Cow::Borrowed(self.identifier)
        }
    }

    /// Returns the name of the referenced item.
    fn ref_name(&self) -> &str {
        self.exact_identifier().split('-').next().unwrap()
    }

    fn exact_identifier(&self) -> &str {
        let mut name = self.identifier;
        if let Some(part) = self.part {
            name = part;
        }
        name
    }

    /// Returns the name of the referenced item and its arguments, if any.
    ///
    /// Eg: `safeMint-address-uint256-` returns `("safeMint", ["address", "uint256"])`
    #[allow(unused)]
    fn ref_name_exact(&self) -> (&str, impl Iterator<Item = &str> + '_) {
        let identifier = self.exact_identifier();
        let mut iter = identifier.split('-');
        (iter.next().unwrap(), iter.filter(|s| !s.is_empty()))
    }

    /// Returns the content of the matched link.
    fn as_str(&self) -> &str {
        self.outer.as_str()
    }

    /// Returns true if the link is external.
    fn is_external(&self) -> bool {
        self.part.is_some()
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn parse_inline_links() {
        let s = "    {IERC165-supportsInterface}   ";
        let cap = RE_INLINE_LINK.captures(s).unwrap();

        let identifier = cap.name("identifier").unwrap().as_str();
        assert_eq!(identifier, "IERC165");
        let part = cap.name("part").unwrap().as_str();
        assert_eq!(part, "supportsInterface");

        let s = "    {supportsInterface}   ";
        let cap = RE_INLINE_LINK.captures(s).unwrap();

        let identifier = cap.name("identifier").unwrap().as_str();
        assert_eq!(identifier, "supportsInterface");

        let s = "{xref-ERC721-_safeMint-address-uint256-}";
        let cap = RE_INLINE_LINK.captures(s).unwrap();

        let identifier = cap.name("identifier").unwrap().as_str();
        assert_eq!(identifier, "ERC721");
        let identifier = cap.name("xref").unwrap().as_str();
        assert_eq!(identifier, "xref-");
        let identifier = cap.name("part").unwrap().as_str();
        assert_eq!(identifier, "_safeMint-address-uint256-");

        let link = InlineLink::capture(s).unwrap();
        assert_eq!(link.ref_name(), "_safeMint");
        assert_eq!(link.as_str(), "{xref-ERC721-_safeMint-address-uint256-}");

        let s = "{xref-ERC721-_safeMint-address-uint256-}[`Named link`]";
        let link = InlineLink::capture(s).unwrap();
        assert_eq!(link.link, Some("`Named link`"));
        assert_eq!(link.markdown_link_display_value(), "`Named link`");
    }
}