Skip to content

Commit

Permalink
Merge pull request #9 from nikarh/code
Browse files Browse the repository at this point in the history
Added support for @code ... @endocde commands
  • Loading branch information
Techie-Pi authored May 28, 2024
2 parents 311cb31 + 6675833 commit cdabd6f
Show file tree
Hide file tree
Showing 3 changed files with 234 additions and 58 deletions.
40 changes: 32 additions & 8 deletions src/generator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,20 +42,22 @@ pub fn rustdoc(input: String) -> Result<String, ParseError> {

str
}
GrammarItem::Text(v) => if group_started {
v.replacen("*", "", 1)
} else {
v
},
GrammarItem::Text(v) => {
if group_started {
v.replacen("*", "", 1)
} else {
v
}
}
// See <https://stackoverflow.com/a/40354789>
GrammarItem::GroupStart => {
group_started = true;
String::from("# ")
},
}
GrammarItem::GroupEnd => {
group_started = false;
continue
},
continue;
}
};
}

Expand Down Expand Up @@ -171,6 +173,12 @@ fn generate_notation(
"par" => String::from("# "),
"details" | "pre" | "post" => String::from("\n\n"),
"brief" | "short" => String::new(),
"code" => {
let lang = params.first().map(|p| p.as_str()).unwrap_or_default();
let lang = lang.strip_prefix('.').unwrap_or(lang);
format!("```{lang}")
}
"endcode" => String::from("```"),
_ => String::new(),
},
(new_param, new_return, new_throw),
Expand Down Expand Up @@ -359,6 +367,22 @@ mod test {
);
}

#[test]
fn code() {
test_rustdoc!(
"@code\nfn main() {\n test( [1] ); // @code @throw\n@endcode",
"```\nfn main() {\n test( [1] ); // @code @throw\n```"
);
}

#[test]
fn code_with_lang() {
test_rustdoc!(
"@code{.rs}\nfn main() {\n test( [1] ); // @code @throw\n@endcode",
"```rs\nfn main() {\n test( [1] ); // @code @throw\n```"
);
}

#[test]
fn can_parse_example() {
let example = include_str!("../tests/assets/example-bindgen.rs");
Expand Down
40 changes: 24 additions & 16 deletions src/lexer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,22 @@ pub(crate) enum LexItem {
At(String),
Paren(char),
Word(String),
Space,
Whitespace(char),
NewLine,
}

impl LexItem {
pub(crate) fn push_to(&self, acc: &mut String) {
match self {
LexItem::At(s) => acc.push_str(s),
LexItem::Paren(c) => acc.push(*c),
LexItem::Word(s) => acc.push_str(s),
LexItem::Whitespace(c) => acc.push(*c),
LexItem::NewLine => acc.push('\n'),
}
}
}

pub(crate) fn lex(input: String) -> Vec<LexItem> {
let mut result = vec![];

Expand Down Expand Up @@ -34,12 +46,8 @@ pub(crate) fn lex(input: String) -> Vec<LexItem> {
'{' | '}' => {
result.push(LexItem::Paren(c));
}
' ' => {
if let Some(v) = result.last_mut() {
if !matches!(v, LexItem::Space) {
result.push(LexItem::Space);
}
}
' ' | '\t' => {
result.push(LexItem::Whitespace(c));
}
'\n' => {
result.push(LexItem::NewLine);
Expand Down Expand Up @@ -72,9 +80,9 @@ mod test {
vec![
LexItem::At("@".into()),
LexItem::Word("name".into()),
LexItem::Space,
LexItem::Whitespace(' '),
LexItem::Word("Memory".into()),
LexItem::Space,
LexItem::Whitespace(' '),
LexItem::Word("Management".into())
]
);
Expand All @@ -85,9 +93,9 @@ mod test {
vec![
LexItem::At("\\".into()),
LexItem::Word("name".into()),
LexItem::Space,
LexItem::Whitespace(' '),
LexItem::Word("Memory".into()),
LexItem::Space,
LexItem::Whitespace(' '),
LexItem::Word("Management".into())
]
);
Expand All @@ -98,9 +106,9 @@ mod test {
vec![
LexItem::At("\\\\".into()),
LexItem::Word("name".into()),
LexItem::Space,
LexItem::Whitespace(' '),
LexItem::Word("Memory".into()),
LexItem::Space,
LexItem::Whitespace(' '),
LexItem::Word("Management".into())
]
);
Expand All @@ -116,12 +124,12 @@ mod test {
LexItem::Paren('{'),
LexItem::NewLine,
LexItem::Word("*".into()),
LexItem::Space,
LexItem::Whitespace(' '),
LexItem::At("@".into()),
LexItem::Word("name".into()),
LexItem::Space,
LexItem::Whitespace(' '),
LexItem::Word("Memory".into()),
LexItem::Space,
LexItem::Whitespace(' '),
LexItem::Word("Management".into()),
LexItem::NewLine,
LexItem::At("@".into()),
Expand Down
Loading

0 comments on commit cdabd6f

Please sign in to comment.