mdbook_github_authors/
github_authors.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
use handlebars::{to_json, Handlebars};
use mdbook::book::{Book, BookItem};
use mdbook::preprocess::{Preprocessor, PreprocessorContext};
use once_cell::sync::Lazy;
use regex::{CaptureMatches, Captures, Regex};
use serde::Serialize;
use serde_json::value::Map;
use std::include_str;

const CONTRIBUTORS_TEMPLATE: &str = include_str!("./template/authors.hbs");

#[derive(Default)]
pub struct GithubAuthorsPreprocessor;

/// A preprocess for expanding "authors" helper.
///
/// {{#author <github-username>}}
impl GithubAuthorsPreprocessor {
    pub(crate) const NAME: &'static str = "github-author";

    pub fn new() -> Self {
        GithubAuthorsPreprocessor
    }
}

impl Preprocessor for GithubAuthorsPreprocessor {
    fn name(&self) -> &str {
        Self::NAME
    }

    #[allow(unused_variables)]
    fn run(&self, ctx: &PreprocessorContext, mut book: Book) -> anyhow::Result<Book> {
        // Gameplan:
        // 1. Find all authors helper using reg-ex in chapter content, using `find_author_links`
        // 2. Sequentially erase all authors helpers from the content
        // 3. Use handlebar template `authors.hbs` and render the found authors
        // 4. Take the rendered html string and add it to the end of the chapter content
        // 5. Figure out if need to make a cli for this and use CmdPreprocessor
        let src_dir = ctx.root.join(&ctx.config.book.src);

        book.for_each_mut(|section: &mut BookItem| {
            if let BookItem::Chapter(ref mut ch) = *section {
                let (mut content, github_authors) = remove_all_links(&ch.content);

                // get contributors html section
                let mut data = Map::new();
                if !github_authors.is_empty() {
                    data.insert("authors".to_string(), to_json(github_authors));
                }
                let mut handlebars = Handlebars::new();

                // register template from a file and assign a name to it
                handlebars
                    .register_template_string("contributors", CONTRIBUTORS_TEMPLATE)
                    .unwrap();

                if !data.is_empty() {
                    let contributors_html = handlebars.render("contributors", &data).unwrap();
                    content.push_str(contributors_html.as_str());
                }

                // mutate chapter content
                ch.content = content;
            }
        });

        Ok(book)
    }
}

fn remove_all_links(s: &str) -> (String, Vec<GithubAuthor>) {
    let mut previous_end_index = 0;
    let mut replaced = String::new();
    let mut github_authors_vec = vec![];

    for link in find_author_links(s) {
        // remove the author link from the chapter content
        replaced.push_str(&s[previous_end_index..link.start_index]);
        replaced.push_str("");
        previous_end_index = link.end_index;

        // store the author usernames to create the contributors section with handlebars
        let these_authors = match link.link_type {
            AuthorLinkType::SingleAuthor(author) => {
                vec![GithubAuthor {
                    username: author.to_string(),
                }]
            }
            AuthorLinkType::MultipleAuthors(author_list) => author_list
                .split(",")
                .map(|username| GithubAuthor {
                    username: username.to_string(),
                })
                .collect(),
        };

        github_authors_vec.extend(these_authors);
    }

    replaced.push_str(&s[previous_end_index..]);
    (replaced, github_authors_vec)
}

#[derive(PartialEq, Debug, Clone, Serialize)]
pub struct GithubAuthor {
    username: String,
}

#[derive(PartialEq, Debug, Clone)]
enum AuthorLinkType<'a> {
    SingleAuthor(&'a str),
    MultipleAuthors(&'a str),
}

#[derive(PartialEq, Debug, Clone)]
struct AuthorLink<'a> {
    start_index: usize,
    end_index: usize,
    link_type: AuthorLinkType<'a>,
    link_text: &'a str,
}

impl<'a> AuthorLink<'a> {
    fn from_capture(cap: Captures<'a>) -> Option<AuthorLink<'a>> {
        let link_type = match (cap.get(0), cap.get(1), cap.get(2)) {
            (_, Some(typ), Some(author))
                if ((typ.as_str() == "author") && (!author.as_str().trim().is_empty())) =>
            {
                Some(AuthorLinkType::SingleAuthor(author.as_str().trim()))
            }
            (_, Some(typ), Some(authors_list))
                if ((typ.as_str() == "authors") && (!authors_list.as_str().trim().is_empty())) =>
            {
                Some(AuthorLinkType::MultipleAuthors(
                    authors_list.as_str().trim(),
                ))
            }
            _ => None,
        };

        link_type.and_then(|lnk_type| {
            cap.get(0).map(|mat| AuthorLink {
                start_index: mat.start(),
                end_index: mat.end(),
                link_type: lnk_type,
                link_text: mat.as_str(),
            })
        })
    }
}

#[allow(dead_code)]
struct AuthorLinkIter<'a>(CaptureMatches<'a, 'a>);

impl<'a> Iterator for AuthorLinkIter<'a> {
    type Item = AuthorLink<'a>;
    fn next(&mut self) -> Option<AuthorLink<'a>> {
        for cap in &mut self.0 {
            if let Some(inc) = AuthorLink::from_capture(cap) {
                return Some(inc);
            }
        }
        None
    }
}

#[allow(dead_code)]
fn find_author_links(contents: &str) -> AuthorLinkIter<'_> {
    // lazily compute following regex
    // r"\\\{\{#.*\}\}|\{\{#([a-zA-Z0-9]+)\s*([^}]+)\}\}")?;
    static RE: Lazy<Regex> = Lazy::new(|| {
        Regex::new(
            r"(?x)              # insignificant whitespace mode
        \\\{\{\#.*\}\}      # match escaped link
        |                   # or
        \{\{\s*             # link opening parens and whitespace
        \#([a-zA-Z0-9_]+)   # link type
        \s+                 # separating whitespace
        ([^}]+)             # link target path and space separated properties
        \}\}                # link closing parens",
        )
        .unwrap()
    });

    AuthorLinkIter(RE.captures_iter(contents))
}

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

    #[fixture]
    fn simple_book_content() -> String {
        "Some random text with and more text ... {{#author foo}} {{#authors bar,baz  }}".to_string()
    }

    #[rstest]
    fn test_find_links_no_author_links() -> Result<()> {
        let s = "Some random text without link...";
        assert!(find_author_links(s).collect::<Vec<_>>() == vec![]);
        Ok(())
    }

    #[rstest]
    fn test_find_links_partial_link() -> Result<()> {
        let s = "Some random text with {{#playground...";
        assert!(find_author_links(s).collect::<Vec<_>>() == vec![]);
        let s = "Some random text with {{#include...";
        assert!(find_author_links(s).collect::<Vec<_>>() == vec![]);
        let s = "Some random text with \\{{#include...";
        assert!(find_author_links(s).collect::<Vec<_>>() == vec![]);
        Ok(())
    }

    #[rstest]
    fn test_find_links_empty_link() -> Result<()> {
        let s = "Some random text with {{#author  }} and {{#authors   }} {{}} {{#}}...";
        println!("{:?}", find_author_links(s).collect::<Vec<_>>());
        assert!(find_author_links(s).collect::<Vec<_>>() == vec![]);
        Ok(())
    }

    #[rstest]
    fn test_find_links_unknown_link_type() -> Result<()> {
        let s = "Some random text with {{#my_author ar.rs}} and {{#auth}} {{baz}} {{#bar}}...";
        assert!(find_author_links(s).collect::<Vec<_>>() == vec![]);
        Ok(())
    }

    #[rstest]
    fn test_find_links_simple_author_links(simple_book_content: String) -> Result<()> {
        let res = find_author_links(&simple_book_content[..]).collect::<Vec<_>>();
        println!("\nOUTPUT: {res:?}\n");

        assert_eq!(
            res,
            vec![
                AuthorLink {
                    start_index: 40,
                    end_index: 55,
                    link_type: AuthorLinkType::SingleAuthor("foo"),
                    link_text: "{{#author foo}}",
                },
                AuthorLink {
                    start_index: 56,
                    end_index: 78,
                    link_type: AuthorLinkType::MultipleAuthors("bar,baz"),
                    link_text: "{{#authors bar,baz  }}",
                },
            ]
        );
        Ok(())
    }

    #[rstest]
    fn test_remove_all_links(simple_book_content: String) -> Result<()> {
        let (c, authors) = remove_all_links(&simple_book_content[..]);

        assert_eq!(c, "Some random text with and more text ...  ");
        assert_eq!(
            authors,
            vec![
                GithubAuthor {
                    username: "foo".to_string()
                },
                GithubAuthor {
                    username: "bar".to_string()
                },
                GithubAuthor {
                    username: "baz".to_string()
                }
            ]
        );
        Ok(())
    }
}